From ff636c3df6bc416e6eac8788e18c24340d0023a1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 21 Jan 2023 12:39:28 +0100 Subject: [PATCH 01/23] Fix doc line to match new options --- lib/mrsk/cli/app.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index bef85e81..b34355a8 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -77,7 +77,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.current_container_id) } end - desc "logs", "Show last 100 log lines from app on servers" + desc "logs", "Show lines from app on servers" option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)" option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server" option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)" From 652e17f260ae005a7930293f9deac455aeba78fb Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 21 Jan 2023 12:39:47 +0100 Subject: [PATCH 02/23] Configure Traefik logs and catch all --- lib/mrsk/cli/traefik.rb | 9 +++++++-- lib/mrsk/commands/traefik.rb | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/mrsk/cli/traefik.rb b/lib/mrsk/cli/traefik.rb index 6d85bf3c..79d7eef2 100644 --- a/lib/mrsk/cli/traefik.rb +++ b/lib/mrsk/cli/traefik.rb @@ -27,9 +27,14 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base on(MRSK.traefik_hosts) { |host| puts_by_host host, capture_with_info(*MRSK.traefik.info), type: "Traefik" } end - desc "logs", "Show last 100 log lines from Traefik on servers" + desc "logs", "Show log lines from Traefik on servers" + option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)" + option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server" def logs - on(MRSK.traefik_hosts) { |host| puts_by_host host, capture(*MRSK.traefik.logs), type: "Traefik" } + since = options[:since] + lines = options[:lines] + + on(MRSK.traefik_hosts) { |host| puts_by_host host, capture(*MRSK.traefik.logs(since: since, lines: lines)), type: "Traefik" } end desc "remove", "Remove Traefik container and image from servers" diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index 6e7461bc..006d4bbc 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -23,8 +23,12 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base docker :ps, "--filter", "name=traefik" end - def logs - docker :logs, "traefik", "-n", "100", "-t" + def logs(since: nil, lines: nil) + docker :logs, "traefik", + (" --since #{since}" if since), + (" -n #{lines}" if lines), + "-t", + "2>&1" end def remove_container From 5c75404fe9e82ae8b1927f7a1d25f73c9e8de6b8 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 09:44:09 +0100 Subject: [PATCH 03/23] Add reboot Traefik to apply new start config --- lib/mrsk/cli/traefik.rb | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/mrsk/cli/traefik.rb b/lib/mrsk/cli/traefik.rb index 79d7eef2..002fa19d 100644 --- a/lib/mrsk/cli/traefik.rb +++ b/lib/mrsk/cli/traefik.rb @@ -6,6 +6,13 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base on(MRSK.traefik_hosts) { execute *MRSK.traefik.run, raise_on_non_zero_exit: false } end + desc "reboot", "Reboot Traefik on servers" + def reboot + invoke :stop + invoke :remove_container + invoke :boot + end + desc "start", "Start existing Traefik on servers" def start on(MRSK.traefik_hosts) { execute *MRSK.traefik.start, raise_on_non_zero_exit: false } @@ -40,10 +47,17 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base desc "remove", "Remove Traefik container and image from servers" def remove invoke :stop + invoke :remove_container + invoke :remove_image + end - on(MRSK.traefik_hosts) do - execute *MRSK.traefik.remove_container - execute *MRSK.traefik.remove_image - end + desc "remove_container", "Remove Traefik container from servers" + def remove_container + on(MRSK.traefik_hosts) { execute *MRSK.traefik.remove_container } + end + + desc "remove_container", "Remove Traefik image from servers" + def remove_image + on(MRSK.traefik_hosts) { execute *MRSK.traefik.remove_image } end end From 287798ad574a56c91af0615b6c838813a74b9a1d Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:06:04 +0100 Subject: [PATCH 04/23] Add option for remote building of single-arch --- README.md | 15 +++++ lib/mrsk/commands/builder.rb | 10 ++- lib/mrsk/commands/builder/native/remote.rb | 71 ++++++++++++++++++++++ test/commands/builder_test.rb | 18 +++++- 4 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 lib/mrsk/commands/builder/native/remote.rb diff --git a/README.md b/README.md index e133176e..3643425e 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,21 @@ Note: You must have Docker running on the remote host being used as a builder. With that configuration in place, you can setup the local/remote configuration using `mrsk build create`. If you wish to remove the contexts and buildx instances again, you can run `mrsk build remove`. If you had already built using the standard emulation setup, run `mrsk build remove` before doing `mrsk build remote`. +### Configuring remote builder for single-arch + +If you're developing on ARM64 (like Apple Silicon), want to deploy on AMD64 (x86 64-bit), but don't need to run the image locally (or on other ARM64 hosts), you can configure a remote builder that just targets AMD64. This is a bit faster than building with multi-arch, as there's nothing to build locally. + +```yaml +builder: + remote: + arch: amd64 + host: ssh://root@192.168.0.1 +``` + +Note: You must have Docker running on the remote host being used as a builder. + +With that configuration in place, you can setup the remote configuration using `mrsk build create`. If you wish to remove the contexts and buildx instances again, you can run `mrsk build remove`. If you had already built using the standard emulation setup, run `mrsk build remove` before doing `mrsk build remote`. + ### Configuring native builder when multi-arch isn't needed If you're developing on the same architecture as the one you're deploying on, you can speed up the build a lot by forgoing a multi-arch image. This can be done by configuring the builder like so: diff --git a/lib/mrsk/commands/builder.rb b/lib/mrsk/commands/builder.rb index ce18fc63..e24768d1 100644 --- a/lib/mrsk/commands/builder.rb +++ b/lib/mrsk/commands/builder.rb @@ -2,10 +2,9 @@ require "mrsk/commands/base" class Mrsk::Commands::Builder < Mrsk::Commands::Base delegate :create, :remove, :push, :pull, :info, to: :target - delegate :native?, :multiarch?, :remote?, to: :name def name - target.class.to_s.demodulize.downcase.inquiry + target.class.to_s.remove("Mrsk::Commands::Builder::").underscore end def target @@ -14,6 +13,8 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base native when config.builder && config.builder["local"] && config.builder["remote"] multiarch_remote + when config.builder && config.builder["remote"] + native_remote else multiarch end @@ -23,6 +24,10 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base @native ||= Mrsk::Commands::Builder::Native.new(config) end + def native_remote + @native ||= Mrsk::Commands::Builder::Native::Remote.new(config) + end + def multiarch @multiarch ||= Mrsk::Commands::Builder::Multiarch.new(config) end @@ -33,5 +38,6 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base end require "mrsk/commands/builder/native" +require "mrsk/commands/builder/native/remote" require "mrsk/commands/builder/multiarch" require "mrsk/commands/builder/multiarch/remote" diff --git a/lib/mrsk/commands/builder/native/remote.rb b/lib/mrsk/commands/builder/native/remote.rb new file mode 100644 index 00000000..c7cecf89 --- /dev/null +++ b/lib/mrsk/commands/builder/native/remote.rb @@ -0,0 +1,71 @@ +require "mrsk/commands/builder/native" + +class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native + def create + combine \ + create_context, + create_buildx + end + + def remove + combine \ + remove_context, + remove_buildx + end + + def push + docker :buildx, :build, + "--push", + "--platform", platform, + "-t", config.absolute_image, + *build_args, + *build_secrets, + "." + end + + def info + combine \ + docker(:context, :ls), + docker(:buildx, :ls) + end + + + private + def arch + config.builder["remote"]["arch"] + end + + def host + config.builder["remote"]["host"] + end + + def builder_name + "mrsk-#{config.service}" + end + + def builder_name_with_arch + "#{builder_name}-#{arch}" + end + + def platform + "linux/#{arch}" + end + + def create_context + docker :context, :create, + builder_name_with_arch, "--description", "'#{builder_name} #{arch} native host'", "--docker", "'host=#{host}'" + end + + def remove_context + docker :context, :rm, builder_name_with_arch + end + + def create_buildx + docker :buildx, :create, + "--use", "--name", builder_name, builder_name_with_arch, "--platform", platform + end + + def remove_buildx + docker :buildx, :rm, builder_name + end +end diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index 16c8f1ca..2a453080 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -8,15 +8,27 @@ class CommandsBuilderTest < ActiveSupport::TestCase end test "target multiarch by default" do - assert new_builder_command.multiarch? + builder = new_builder_command + assert_equal "multiarch", builder.name + assert_equal [:docker, :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", "dhh/app:123", "."], builder.push end test "target native when multiarch is off" do - assert new_builder_command(builder: { "multiarch" => false }).native? + builder = new_builder_command(builder: { "multiarch" => false }) + assert_equal "native", builder.name + assert_equal [:docker, :build, "-t", "dhh/app:123", ".", "&&", :docker, :push, "dhh/app:123"], builder.push end test "target multiarch remote when local and remote is set" do - assert new_builder_command(builder: { "local" => { }, "remote" => { } }).remote? + builder = new_builder_command(builder: { "local" => { }, "remote" => { } }) + assert_equal "multiarch/remote", builder.name + assert_equal [:docker, :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", "dhh/app:123", "."], builder.push + end + + test "target native remote when only remote is set" do + builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" } }) + assert_equal "native/remote", builder.name + assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64", "-t", "dhh/app:123", "."], builder.push end test "build args" do From 2ad135c23708945e7bb4332a29028043c0cc463a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:06:20 +0100 Subject: [PATCH 05/23] No builder definition needed for native multiarch --- lib/mrsk/commands/builder/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/commands/builder/base.rb b/lib/mrsk/commands/builder/base.rb index d08e8a87..792ebee1 100644 --- a/lib/mrsk/commands/builder/base.rb +++ b/lib/mrsk/commands/builder/base.rb @@ -17,10 +17,10 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base private def args - config.builder["args"] || {} + (config.builder && config.builder["args"]) || {} end def secrets - config.builder["secrets"] || [] + (config.builder && config.builder["secrets"]) || [] end end From bfec21c00f26b171ff029f22a75b574d06c23318 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:07:07 +0100 Subject: [PATCH 06/23] Recommend fetch for early bail-out --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3643425e..c71bad2c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ servers: - 192.168.0.2 registry: username: registry-user-name - password: <%= ENV["MRSK_REGISTRY_PASSWORD"] %> + password: <%= ENV.fetch("MRSK_REGISTRY_PASSWORD") %> ``` Now you're ready to deploy a multi-arch image to the servers: From a3a7fce1e80956c01d57b957f9ed05c6801cca78 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:08:27 +0100 Subject: [PATCH 07/23] Note that it starts with SSH --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c71bad2c..c33c2f2b 100644 --- a/README.md +++ b/README.md @@ -26,15 +26,16 @@ mrsk deploy This will: -1. Install Docker on any server that might be missing it (using apt-get) -2. Log into the registry both locally and remotely -3. Build the image using the standard Dockerfile in the root of the application. -4. Push the image to the registry. -5. Pull the image from the registry on the servers. -6. Ensure Traefik is running and accepting traffic on port 80. -7. Stop any containers running a previous versions of the app. -8. Start a new container with the version of the app that matches the current git version hash. -9. Prune unused images and stopped containers to ensure servers don't fill up. +1. Connect to the servers over SSH (using root by default, authenticated by your loaded ssh key) +2. Install Docker on any server that might be missing it (using apt-get) +3. Log into the registry both locally and remotely +4. Build the image using the standard Dockerfile in the root of the application. +5. Push the image to the registry. +6. Pull the image from the registry on the servers. +7. Ensure Traefik is running and accepting traffic on port 80. +8. Stop any containers running a previous versions of the app. +9. Start a new container with the version of the app that matches the current git version hash. +10. Prune unused images and stopped containers to ensure servers don't fill up. Voila! All the servers are now serving the app on port 80. If you're just running a single server, you're ready to go. If you're running multiple servers, you need to put a load balancer in front of them. From 1795c7c6a4ce6350af03eea5846683e95dd770c5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:12:46 +0100 Subject: [PATCH 08/23] Doc updates --- README.md | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c33c2f2b..2de6817d 100644 --- a/README.md +++ b/README.md @@ -233,24 +233,25 @@ FROM ruby:$RUBY_VERSION-slim as base ### Remote execution -If you need to execute commands inside the Rails containers, you can use `mrsk app exec`, `mrsk app exec --once`, `mrsk app runner`, and `mrsk app runner --once`. Examples: +If you need to execute commands inside the Rails containers, you can use `mrsk app exec` and `mrsk app runner`. Examples: ```bash # Runs command on all servers mrsk app exec 'ruby -v' -App Host: xxx.xxx.xxx.xxx +App Host: 192.168.0.1 ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux] -App Host: xxx.xxx.xxx.xxx +App Host: 192.168.0.2 ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux] -# Runs command on first server -mrsk app exec --once 'cat .ruby-version' +# Runs command on primary server +mrsk app exec --primary 'cat .ruby-version' +App Host: 192.168.0.1 3.1.3 # Runs Rails command on all servers mrsk app exec 'bin/rails about' -App Host: xxx.xxx.xxx.xxx +App Host: 192.168.0.1 About your application's environment Rails version 7.1.0.alpha Ruby version ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux] @@ -262,7 +263,7 @@ Environment production Database adapter sqlite3 Database schema version 20221231233303 -App Host: xxx.xxx.xxx.xxx +App Host: 192.168.0.2 About your application's environment Rails version 7.1.0.alpha Ruby version ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux] @@ -274,8 +275,8 @@ Environment production Database adapter sqlite3 Database schema version 20221231233303 -# Runs Rails runner on first server -mrsk app runner 'puts Rails.application.config.time_zone' +# Runs Rails runner on primary server +mrsk app runner -p 'puts Rails.application.config.time_zone' UTC ``` @@ -288,19 +289,19 @@ If you need to interact with the production console for the app, you can use `mr You can see the state of your servers by running `mrsk details`. It'll show something like this: ``` -Traefik Host: xxx.xxx.xxx.xxx +Traefik Host: 192.168.0.1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6195b2a28c81 traefik "/entrypoint.sh --pr…" 30 minutes ago Up 19 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp traefik -Traefik Host: 164.92.105.119 +Traefik Host: 192.168.0.2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de14a335d152 traefik "/entrypoint.sh --pr…" 30 minutes ago Up 19 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp traefik -App Host: 164.90.145.60 +App Host: 192.168.0.1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES badb1aa51db3 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123 -App Host: 164.92.105.119 +App Host: 192.168.0.2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d3c91ed1f55 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 13 minutes ago Up 13 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123 ``` @@ -312,12 +313,12 @@ You can also see just info for app containers with `mrsk app details` or just fo If you've discovered a bad deploy, you can quickly rollback by reactivating the old, paused container image. You can see what old containers are available for rollback by running `mrsk app containers`. It'll give you a presentation similar to `mrsk app details`, but include all the old containers as well. Showing something like this: ``` -App Host: 164.92.105.119 +App Host: 192.168.0.1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d3c91ed1f51 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 19 minutes ago Up 19 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123 539f26b28369 registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4 -App Host: 164.90.145.60 +App Host: 192.168.0.2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES badb1aa51db4 registry.digitalocean.com/user/app:6ef8a6a84c525b123c5245345a8483f86d05a123 "/rails/bin/docker-e…" 19 minutes ago Up 19 minutes 3000/tcp chat-6ef8a6a84c525b123c5245345a8483f86d05a123 6f170d1172ae registry.digitalocean.com/user/app:e5d9d7c2b898289dfbc5f7f1334140d984eedae4 "/rails/bin/docker-e…" 31 minutes ago Exited (1) 27 minutes ago chat-e5d9d7c2b898289dfbc5f7f1334140d984eedae4 From 925ac86459b489a3ffa51a61977f9462456a769a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:17:40 +0100 Subject: [PATCH 09/23] No longer need actual class name with more descriptive name --- lib/mrsk/cli/build.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mrsk/cli/build.rb b/lib/mrsk/cli/build.rb index 89730f66..db0c38be 100644 --- a/lib/mrsk/cli/build.rb +++ b/lib/mrsk/cli/build.rb @@ -48,7 +48,7 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base desc "details", "Show the name of the configured builder" def details run_locally do - puts "Builder: #{MRSK.builder.name} (#{MRSK.builder.target.class.name})" + puts "Builder: #{MRSK.builder.name}" puts capture(*MRSK.builder.info) end end From e4dc4c300e5cb2aac4f55af2f441e641abc338b4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:21:50 +0100 Subject: [PATCH 10/23] Log more aggressively for now --- lib/mrsk/commands/traefik.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index 006d4bbc..8ede5c6c 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -8,7 +8,8 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base "-p 80:80", "-v /var/run/docker.sock:/var/run/docker.sock", "traefik", - "--providers.docker" + "--providers.docker", + "--log.level=DEBUG" end def start From e783950825f1a34a01b243ea46c627400f6de3c4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:45:05 +0100 Subject: [PATCH 11/23] Always be verbose about building Serves as progress indicator, step too long without one --- lib/mrsk/cli/build.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/mrsk/cli/build.rb b/lib/mrsk/cli/build.rb index db0c38be..5f063439 100644 --- a/lib/mrsk/cli/build.rb +++ b/lib/mrsk/cli/build.rb @@ -13,13 +13,11 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base run_locally do begin - debug "Using builder: #{MRSK.builder.name}" - info "Building image may take a while (run with --verbose for progress logging)" unless verbose - execute *MRSK.builder.push + MRSK.verbosity(:debug) { execute *MRSK.builder.push } rescue SSHKit::Command::Failed => e error "Missing compatible builder, so creating a new one first" execute *MRSK.builder.create - execute *MRSK.builder.push + MRSK.verbosity(:debug) { execute *MRSK.builder.push } end end end From a16e5ce886d4de492bf9055cd4eb7af931d32f8b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:47:22 +0100 Subject: [PATCH 12/23] Use class specific buildx instances So we don't have to muck with the machine default, and can swap between configurations without tearing down the old builder. --- README.md | 4 ---- lib/mrsk/commands/builder/multiarch.rb | 5 +++-- lib/mrsk/commands/builder/multiarch/remote.rb | 15 +++++++++------ lib/mrsk/commands/builder/native/remote.rb | 6 +++--- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2de6817d..a0162f1f 100644 --- a/README.md +++ b/README.md @@ -163,8 +163,6 @@ builder: Note: You must have Docker running on the remote host being used as a builder. -With that configuration in place, you can setup the local/remote configuration using `mrsk build create`. If you wish to remove the contexts and buildx instances again, you can run `mrsk build remove`. If you had already built using the standard emulation setup, run `mrsk build remove` before doing `mrsk build remote`. - ### Configuring remote builder for single-arch If you're developing on ARM64 (like Apple Silicon), want to deploy on AMD64 (x86 64-bit), but don't need to run the image locally (or on other ARM64 hosts), you can configure a remote builder that just targets AMD64. This is a bit faster than building with multi-arch, as there's nothing to build locally. @@ -178,8 +176,6 @@ builder: Note: You must have Docker running on the remote host being used as a builder. -With that configuration in place, you can setup the remote configuration using `mrsk build create`. If you wish to remove the contexts and buildx instances again, you can run `mrsk build remove`. If you had already built using the standard emulation setup, run `mrsk build remove` before doing `mrsk build remote`. - ### Configuring native builder when multi-arch isn't needed If you're developing on the same architecture as the one you're deploying on, you can speed up the build a lot by forgoing a multi-arch image. This can be done by configuring the builder like so: diff --git a/lib/mrsk/commands/builder/multiarch.rb b/lib/mrsk/commands/builder/multiarch.rb index 82ef4ee5..bc87a86f 100644 --- a/lib/mrsk/commands/builder/multiarch.rb +++ b/lib/mrsk/commands/builder/multiarch.rb @@ -12,7 +12,8 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base def push docker :buildx, :build, "--push", - "--platform linux/amd64,linux/arm64", + "--platform", "linux/amd64,linux/arm64", + "--builder", builder_name, "-t", config.absolute_image, *build_args, *build_secrets, @@ -27,6 +28,6 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base private def builder_name - "mrsk-#{config.service}" + "mrsk-#{config.service}-multiarch" end end diff --git a/lib/mrsk/commands/builder/multiarch/remote.rb b/lib/mrsk/commands/builder/multiarch/remote.rb index 2bbd3c3a..b49de328 100644 --- a/lib/mrsk/commands/builder/multiarch/remote.rb +++ b/lib/mrsk/commands/builder/multiarch/remote.rb @@ -15,8 +15,16 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult end private + def builder_name + super + "-remote" + end + + def builder_name_with_arch(arch) + "#{builder_name}-#{arch}" + end + def create_local_buildx - docker :buildx, :create, "--use", "--name", builder_name, builder_name_with_arch(local["arch"]), "--platform", "linux/#{local["arch"]}" + docker :buildx, :create, "--name", builder_name, builder_name_with_arch(local["arch"]), "--platform", "linux/#{local["arch"]}" end def append_remote_buildx @@ -50,9 +58,4 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult def remote config.builder["remote"] end - - private - def builder_name_with_arch(arch) - "#{builder_name}-#{arch}" - end end diff --git a/lib/mrsk/commands/builder/native/remote.rb b/lib/mrsk/commands/builder/native/remote.rb index c7cecf89..6d2aeeea 100644 --- a/lib/mrsk/commands/builder/native/remote.rb +++ b/lib/mrsk/commands/builder/native/remote.rb @@ -17,6 +17,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native docker :buildx, :build, "--push", "--platform", platform, + "--builder", builder_name, "-t", config.absolute_image, *build_args, *build_secrets, @@ -40,7 +41,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native end def builder_name - "mrsk-#{config.service}" + "mrsk-#{config.service}-native-remote" end def builder_name_with_arch @@ -61,8 +62,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native end def create_buildx - docker :buildx, :create, - "--use", "--name", builder_name, builder_name_with_arch, "--platform", platform + docker :buildx, :create, "--name", builder_name, builder_name_with_arch, "--platform", platform end def remove_buildx From 917d429901642984361233e58fe08beb3bc7caea Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 10:51:18 +0100 Subject: [PATCH 13/23] Simpler --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index a0162f1f..f3b3545b 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,7 @@ registry: Now you're ready to deploy a multi-arch image to the servers: ``` -export MRSK_REGISTRY_PASSWORD=your-real-registry-pw -mrsk deploy +MRSK_REGISTRY_PASSWORD=pw mrsk deploy ``` This will: From 34652ca321b64356ac53c20b7bcfee0bbf6a6c3b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 11:00:18 +0100 Subject: [PATCH 14/23] Always fetch to fail quick --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f3b3545b..2ee78ae0 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ The default registry for Docker is Docker Hub. If you'd like to use a different registry: server: registry.digitalocean.com username: registry-user-name - password: <%= ENV["MRSK_REGISTRY_PASSWORD"] %> + password: <%= ENV.fetch("MRSK_REGISTRY_PASSWORD") %> ``` ### Using a different SSH user than root From 6eb0abbb3074f8559ef17a88a414833f34c21113 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 11:00:24 +0100 Subject: [PATCH 15/23] Explain traefik: true --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2ee78ae0..a43602ab 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,19 @@ servers: cmd: bin/jobs ``` -Traefik will only be installed and run on the servers in the `web` role (and on all servers if no roles are defined). +Traefik will only be installed default and run on the servers in the `web` role (and on all servers if no roles are defined). If you need Traefik on hosts in other roles than `web`, add `traefik: true`, like: + +```yaml +servers: + web: + - 192.168.0.1 + - 192.168.0.2 + web2: + traefik: true + hosts: + - 192.168.0.3 + - 192.168.0.4 +``` ### Adding custom container labels From 598e7ab97fb7c57f0fb934a237ad30719d8934e4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 11:27:31 +0100 Subject: [PATCH 16/23] Add power to follow logs on app and traefik --- lib/mrsk/cli/app.rb | 28 +++++++++++++++++++--------- lib/mrsk/cli/traefik.rb | 20 +++++++++++++++++--- lib/mrsk/commands/app.rb | 10 +++++++++- lib/mrsk/commands/base.rb | 4 ++++ lib/mrsk/commands/traefik.rb | 17 +++++++++++------ 5 files changed, 60 insertions(+), 19 deletions(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index b34355a8..9363afd5 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -81,22 +81,32 @@ class Mrsk::Cli::App < Mrsk::Cli::Base option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)" option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server" option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)" + option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)" def logs # FIXME: Catch when app containers aren't running - since = options[:since] - lines = options[:lines] - grep = options[:grep] + grep = options[:grep] - on(MRSK.hosts) do |host| - begin - puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep)) - rescue SSHKit::Command::Failed - puts_by_host host, "Nothing found" + if options[:follow] + run_locally do + info "Following logs on #{MRSK.primary_host}..." + info MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep) + exec MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep) + end + else + since = options[:since] + lines = options[:lines] + + on(MRSK.hosts) do |host| + begin + puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep)) + rescue SSHKit::Command::Failed + puts_by_host host, "Nothing found" + end end end end - + desc "remove", "Remove app containers and images from servers" option :only, default: "", desc: "Use 'containers' or 'images'" def remove diff --git a/lib/mrsk/cli/traefik.rb b/lib/mrsk/cli/traefik.rb index 002fa19d..975db7d8 100644 --- a/lib/mrsk/cli/traefik.rb +++ b/lib/mrsk/cli/traefik.rb @@ -37,11 +37,25 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base desc "logs", "Show log lines from Traefik on servers" option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)" option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server" + option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)" + option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)" def logs - since = options[:since] - lines = options[:lines] + grep = options[:grep] - on(MRSK.traefik_hosts) { |host| puts_by_host host, capture(*MRSK.traefik.logs(since: since, lines: lines)), type: "Traefik" } + if options[:follow] + run_locally do + info "Following logs on #{MRSK.primary_host}..." + info MRSK.traefik.follow_logs(host: MRSK.primary_host, grep: grep) + exec MRSK.traefik.follow_logs(host: MRSK.primary_host, grep: grep) + end + else + since = options[:since] + lines = options[:lines] + + on(MRSK.traefik_hosts) do |host| + puts_by_host host, capture(*MRSK.traefik.logs(since: since, lines: lines, grep: grep)), type: "Traefik" + end + end end desc "remove", "Remove Traefik container and image from servers" diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index e1faf9dc..245d0e33 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -57,6 +57,14 @@ class Mrsk::Commands::App < Mrsk::Commands::Base *command end + def follow_logs(host:, grep: nil) + run_over_ssh pipe( + current_container_id, + "xargs docker logs -t -n 10 -f 2>&1", + ("grep '#{grep}'" if grep) + ).join(" "), host: host + end + def console(host:) exec_over_ssh "bin/rails", "c", host: host end @@ -79,7 +87,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base private def exec_over_ssh(*command, host:) - "ssh -t #{config.ssh_user}@#{host} '#{run_exec(*command, interactive: true).join(" ")}'" + run_over_ssh run_exec(*command, interactive: true).join(" "), host: host end def service_filter diff --git a/lib/mrsk/commands/base.rb b/lib/mrsk/commands/base.rb index d2a0ae89..e7305701 100644 --- a/lib/mrsk/commands/base.rb +++ b/lib/mrsk/commands/base.rb @@ -23,5 +23,9 @@ module Mrsk::Commands def docker(*args) args.compact.unshift :docker end + + def run_over_ssh(command, host:) + "ssh -t #{config.ssh_user}@#{host} '#{command}'" + end end end diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index 8ede5c6c..a2d395d7 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -24,12 +24,17 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base docker :ps, "--filter", "name=traefik" end - def logs(since: nil, lines: nil) - docker :logs, "traefik", - (" --since #{since}" if since), - (" -n #{lines}" if lines), - "-t", - "2>&1" + def logs(since: nil, lines: nil, grep: nil) + pipe \ + docker(:logs, "traefik", (" --since #{since}" if since), (" -n #{lines}" if lines), "-t", "2>&1"), + ("grep '#{grep}'" if grep) + end + + def follow_logs(host:, grep: nil) + run_over_ssh pipe( + docker(:logs, "traefik", "-t", "-n", "10", "-f", "2>&1"), + ("grep '#{grep}'" if grep) + ).join(" "), host: host end def remove_container From 3a9c8455ecab42c13e4e339656bb494b101e2e95 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 11:27:39 +0100 Subject: [PATCH 17/23] Style / presentatino --- lib/mrsk/cli/app.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index 9363afd5..d71987a1 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -49,16 +49,16 @@ class Mrsk::Cli::App < Mrsk::Cli::Base desc "console", "Start Rails Console on primary host (or specific host set by --hosts)" def console run_locally do - puts "Launching Rails console on #{MRSK.primary_host}..." - exec MRSK.app.console(host: MRSK.primary_host) + info "Launching Rails console on #{MRSK.primary_host}" + exec MRSK.app.console(host: MRSK.primary_host) end end desc "bash", "Start a bash session on primary host (or specific host set by --hosts)" def bash run_locally do - puts "Launching bash session on #{MRSK.primary_host}..." - exec MRSK.app.bash(host: MRSK.primary_host) + info "Launching bash session on #{MRSK.primary_host}" + exec MRSK.app.bash(host: MRSK.primary_host) end end From 48c45a0cf8330d5915275e5212faf5e4c6cbe9e7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 11:31:19 +0100 Subject: [PATCH 18/23] Explain reboot procedure --- lib/mrsk/cli/traefik.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mrsk/cli/traefik.rb b/lib/mrsk/cli/traefik.rb index 975db7d8..e60d548f 100644 --- a/lib/mrsk/cli/traefik.rb +++ b/lib/mrsk/cli/traefik.rb @@ -6,7 +6,7 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base on(MRSK.traefik_hosts) { execute *MRSK.traefik.run, raise_on_non_zero_exit: false } end - desc "reboot", "Reboot Traefik on servers" + desc "reboot", "Reboot Traefik on servers (stop container, remove container, start new container)" def reboot invoke :stop invoke :remove_container From 82067cd077052653d7e5dc0d66c417f0e8be3c50 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 11:32:59 +0100 Subject: [PATCH 19/23] Use similar headline form --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a43602ab..bd9d6e19 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ The default SSH user is root, but you can change it using `ssh_user`: ssh_user: app ``` -### Adding custom env variables +### Using custom env variables You can inject custom env variables into the app containers using `env`: @@ -75,7 +75,7 @@ env: REDIS_URL: redis://redis1:6379/1 ``` -### Adding secret custom env variables +### Using secret custom env variables If you have custom env variables that are secret, you can divide the `env` block into `clear` and `secret`: @@ -96,7 +96,7 @@ If the referenced secret ENVs are missing, the configuration will be halted with Note: Marking an ENV as secret currently only redacts its value in the output for MRSK. The ENV is still injected in the clear into the container at runtime. -### Splitting servers into different roles +### Using different roles for servers If your application uses separate hosts for running jobs or other roles beyond the default web running, you can specify these hosts and their custom entrypoint command like so: @@ -126,7 +126,7 @@ servers: - 192.168.0.4 ``` -### Adding custom container labels +### Using custom container labels You can specialize the default Traefik rules by setting custom labels on the containers that are being started: @@ -156,7 +156,7 @@ servers: my-custom-label: "50" ``` -### Configuring remote builder for native multi-arch +### Using remote builder for native multi-arch If you're developing on ARM64 (like Apple Silicon), but you want to deploy on AMD64 (x86 64-bit), you have to use multi-archecture images. By default, MRSK will setup a local buildx configuration that allows for this through QEMU emulation. This can be slow, especially on the first build. @@ -174,7 +174,7 @@ builder: Note: You must have Docker running on the remote host being used as a builder. -### Configuring remote builder for single-arch +### Using remote builder for single-arch If you're developing on ARM64 (like Apple Silicon), want to deploy on AMD64 (x86 64-bit), but don't need to run the image locally (or on other ARM64 hosts), you can configure a remote builder that just targets AMD64. This is a bit faster than building with multi-arch, as there's nothing to build locally. @@ -187,7 +187,7 @@ builder: Note: You must have Docker running on the remote host being used as a builder. -### Configuring native builder when multi-arch isn't needed +### Using native builder when multi-arch isn't needed If you're developing on the same architecture as the one you're deploying on, you can speed up the build a lot by forgoing a multi-arch image. This can be done by configuring the builder like so: @@ -196,7 +196,7 @@ builder: multiarch: false ``` -### Configuring build secrets for new images +### Using build secrets for new images Some images need a secret passed in during build time, like a GITHUB_TOKEN to give access to private gem repositories. This can be done by having the secret in ENV, then referencing it like so in the configuration: From e9f8eea6c9b1dc4332bfab81670007ac44a28b9e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 11:34:58 +0100 Subject: [PATCH 20/23] Word doesn't add anything --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bd9d6e19..1ee282d5 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ The default SSH user is root, but you can change it using `ssh_user`: ssh_user: app ``` -### Using custom env variables +### Using env variables -You can inject custom env variables into the app containers using `env`: +You can inject env variables into the app containers using `env`: ```yaml env: @@ -75,9 +75,9 @@ env: REDIS_URL: redis://redis1:6379/1 ``` -### Using secret custom env variables +### Using secret env variables -If you have custom env variables that are secret, you can divide the `env` block into `clear` and `secret`: +If you have env variables that are secret, you can divide the `env` block into `clear` and `secret`: ```yaml env: @@ -98,7 +98,7 @@ Note: Marking an ENV as secret currently only redacts its value in the output fo ### Using different roles for servers -If your application uses separate hosts for running jobs or other roles beyond the default web running, you can specify these hosts and their custom entrypoint command like so: +If your application uses separate hosts for running jobs or other roles beyond the default web running, you can specify these hosts and their overwritten entrypoint command like so: ```yaml servers: @@ -126,9 +126,9 @@ servers: - 192.168.0.4 ``` -### Using custom container labels +### Using container labels -You can specialize the default Traefik rules by setting custom labels on the containers that are being started: +You can specialize the default Traefik rules by setting labels on the containers that are being started: ``` labels: @@ -153,7 +153,7 @@ servers: - 192.168.0.4 cmd: bin/jobs labels: - my-custom-label: "50" + my-label: "50" ``` ### Using remote builder for native multi-arch From 2af4885b39808d806e3de9ce693fb1ea4140467b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 11:47:06 +0100 Subject: [PATCH 21/23] Doc tweaks --- README.md | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 1ee282d5..7cf63e55 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Kubernetes is a beast. Running it yourself on your own hardware is not for the f ### Using another registry than Docker Hub -The default registry for Docker is Docker Hub. If you'd like to use a different one, just configure the server, like so: +The default registry is Docker Hub, but you can change it using `registry/server`: ```yaml registry: @@ -98,7 +98,7 @@ Note: Marking an ENV as secret currently only redacts its value in the output fo ### Using different roles for servers -If your application uses separate hosts for running jobs or other roles beyond the default web running, you can specify these hosts and their overwritten entrypoint command like so: +If your application uses separate hosts for running jobs or other roles beyond the default web running, you can specify these hosts in a dedicated role with a new entrypoint command like so: ```yaml servers: @@ -112,7 +112,7 @@ servers: cmd: bin/jobs ``` -Traefik will only be installed default and run on the servers in the `web` role (and on all servers if no roles are defined). If you need Traefik on hosts in other roles than `web`, add `traefik: true`, like: +Note: Traefik will only by default be installed and run on the servers in the `web` role (and on all servers if no roles are defined). If you need Traefik on hosts in other roles than `web`, add `traefik: true`: ```yaml servers: @@ -135,12 +135,12 @@ labels: traefik.http.routers.hey.rule: '''Host(`app.hey.com`)''' ``` -(Note: The extra quotes are needed to ensure the rule is passed in correctly!) +Note: The extra quotes are needed to ensure the rule is passed in correctly! This allows you to run multiple applications on the same server sharing the same Traefik instance and port. See https://doc.traefik.io/traefik/routing/routers/#rule for a full list of available routing rules. -The labels can even be applied on a per-role basis: +The labels can also be applied on a per-role basis: ```yaml servers: @@ -158,21 +158,21 @@ servers: ### Using remote builder for native multi-arch -If you're developing on ARM64 (like Apple Silicon), but you want to deploy on AMD64 (x86 64-bit), you have to use multi-archecture images. By default, MRSK will setup a local buildx configuration that allows for this through QEMU emulation. This can be slow, especially on the first build. +If you're developing on ARM64 (like Apple Silicon), but you want to deploy on AMD64 (x86 64-bit), you can use multi-archecture images. By default, MRSK will setup a local buildx configuration that does this through QEMU emulation. But this can be quite slow, especially on the first build. -If you want to speed up this process by using a remote AMD64 host to natively build the AMD64 part of the image, while natively building the ARM64 part locally, you can do so using builder options like follows: +If you want to speed up this process by using a remote AMD64 host to natively build the AMD64 part of the image, while natively building the ARM64 part locally, you can do so using builder options: ```yaml builder: local: arch: arm64 - host: unix:///Users/dhh/.docker/run/docker.sock + host: unix:///Users/<%= `whoami`.strip %>/.docker/run/docker.sock remote: arch: amd64 host: ssh://root@192.168.0.1 ``` -Note: You must have Docker running on the remote host being used as a builder. +Note: You must have Docker running on the remote host being used as a builder. This instance should only be shared for builds using the same registry and credentials. ### Using remote builder for single-arch @@ -185,20 +185,20 @@ builder: host: ssh://root@192.168.0.1 ``` -Note: You must have Docker running on the remote host being used as a builder. - ### Using native builder when multi-arch isn't needed -If you're developing on the same architecture as the one you're deploying on, you can speed up the build a lot by forgoing a multi-arch image. This can be done by configuring the builder like so: +If you're developing on the same architecture as the one you're deploying on, you can speed up the build by forgoing both multi-arch and remote building: ```yaml builder: multiarch: false ``` +This is also a good option if you're running MRSK from a CI server that shares architecture with the deployment servers. + ### Using build secrets for new images -Some images need a secret passed in during build time, like a GITHUB_TOKEN to give access to private gem repositories. This can be done by having the secret in ENV, then referencing it like so in the configuration: +Some images need a secret passed in during build time, like a GITHUB_TOKEN to give access to private gem repositories. This can be done by having the secret in ENV, then referencing it in the builder configuration: ```yaml builder: @@ -206,13 +206,13 @@ builder: - GITHUB_TOKEN ``` -This build secret can then be used in the Dockerfile: +This build secret can then be referenced in the Dockerfile: ``` -# Install application gems +# Copy Gemfiles COPY Gemfile Gemfile.lock ./ -# Private repositories need an access token during the build +# Install dependencies, including private repositories via access token RUN --mount=type=secret,id=GITHUB_TOKEN \ BUNDLE_GITHUB__COM=x-access-token:$(cat /run/secrets/GITHUB_TOKEN) \ bundle install @@ -220,7 +220,7 @@ RUN --mount=type=secret,id=GITHUB_TOKEN \ ### Configuring build args for new images -Build arguments that aren't secret can be configured like so: +Build arguments that aren't secret can also be configured: ```yaml builder: @@ -238,7 +238,7 @@ FROM ruby:$RUBY_VERSION-slim as base ## Commands -### Remote execution +### Running remote execution and runners If you need to execute commands inside the Rails containers, you can use `mrsk app exec` and `mrsk app runner`. Examples: @@ -282,18 +282,18 @@ Environment production Database adapter sqlite3 Database schema version 20221231233303 -# Runs Rails runner on primary server +# Run Rails runner on primary server mrsk app runner -p 'puts Rails.application.config.time_zone' UTC ``` -### Running a Rails console on the primary host +### Running a Rails console If you need to interact with the production console for the app, you can use `mrsk app console`, which will start a Rails console session on the primary host. You can start the console on a different host using `mrsk app console --host 192.168.0.2`. Be mindful that this is a live wire! Any changes made to the production database will take effect immeditately. -### Inspecting +### Running details to see state of containers -You can see the state of your servers by running `mrsk details`. It'll show something like this: +You can see the state of your servers by running `mrsk details`: ``` Traefik Host: 192.168.0.1 @@ -315,7 +315,7 @@ CONTAINER ID IMAGE You can also see just info for app containers with `mrsk app details` or just for Traefik with `mrsk traefik details`. -### Rollback +### Running rollback to fix a bad deploy If you've discovered a bad deploy, you can quickly rollback by reactivating the old, paused container image. You can see what old containers are available for rollback by running `mrsk app containers`. It'll give you a presentation similar to `mrsk app details`, but include all the old containers as well. Showing something like this: @@ -335,7 +335,7 @@ From the example above, we can see that `e5d9d7c2b898289dfbc5f7f1334140d984eedae Note that by default old containers are pruned after 3 days when you run `mrsk deploy`. -### Removing +### Running removal to clean up servers If you wish to remove the entire application, including Traefik, containers, images, and registry session, you can run `mrsk remove`. This will leave the servers clean. From 936d346ca6998c9f228b240d204172836f78a90b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 15:37:42 +0100 Subject: [PATCH 22/23] Use directory for better organization --- test/{configuration_role_test.rb => configuration/role_test.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/{configuration_role_test.rb => configuration/role_test.rb} (100%) diff --git a/test/configuration_role_test.rb b/test/configuration/role_test.rb similarity index 100% rename from test/configuration_role_test.rb rename to test/configuration/role_test.rb From 22137391561a24946a1c123aa456937ea188b35c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2023 15:43:47 +0100 Subject: [PATCH 23/23] Fix tests --- test/commands/builder_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index 2a453080..14aee153 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -10,7 +10,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase test "target multiarch by default" do builder = new_builder_command assert_equal "multiarch", builder.name - assert_equal [:docker, :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", "dhh/app:123", "."], builder.push + assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch", "-t", "dhh/app:123", "."], builder.push end test "target native when multiarch is off" do @@ -22,13 +22,13 @@ class CommandsBuilderTest < ActiveSupport::TestCase test "target multiarch remote when local and remote is set" do builder = new_builder_command(builder: { "local" => { }, "remote" => { } }) assert_equal "multiarch/remote", builder.name - assert_equal [:docker, :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", "dhh/app:123", "."], builder.push + assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch-remote", "-t", "dhh/app:123", "."], builder.push end test "target native remote when only remote is set" do builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" } }) assert_equal "native/remote", builder.name - assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64", "-t", "dhh/app:123", "."], builder.push + assert_equal [:docker, :buildx, :build, "--push", "--platform", "linux/amd64", "--builder", "mrsk-app-native-remote", "-t", "dhh/app:123", "."], builder.push end test "build args" do @@ -48,7 +48,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase test "multiarch push with build args" do builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } }) - assert_equal [ :docker, :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", "dhh/app:123", "--build-arg", "a=1", "--build-arg", "b=2", "." ], builder.push + assert_equal [ :docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "mrsk-app-multiarch", "-t", "dhh/app:123", "--build-arg", "a=1", "--build-arg", "b=2", "." ], builder.push end test "native push with with build secrets" do