From 25e52d6c933982a2d11832c6ea1d78ce632b3a67 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:07:20 +0100 Subject: [PATCH 01/36] Fix escaping --- lib/mrsk/commands/concerns/repository.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mrsk/commands/concerns/repository.rb b/lib/mrsk/commands/concerns/repository.rb index 8df4171b..a0e350c0 100644 --- a/lib/mrsk/commands/concerns/repository.rb +++ b/lib/mrsk/commands/concerns/repository.rb @@ -8,7 +8,7 @@ module Mrsk::Commands::Concerns # FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail! pipe \ docker(:ps, "--filter", "label=service=#{config.service}", "--format", '"{{.Names}}"'), - "sed 's/-/\n/g'", + %(sed 's/-/\\n/g'), "tail -n 1" end From 67f9ffe9617fce3c9c6fa7695817c8295b5379d7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:07:37 +0100 Subject: [PATCH 02/36] xargs when piping --- lib/mrsk/commands/app.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index 2ea27aa7..ae8a1fc9 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -87,7 +87,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base def remove_container(version:) pipe \ container_id_for(container_name: service_with_version(version)), - docker(:container, :rm) + [ "xargs", docker(:container, :rm) ].flatten end def remove_containers From 15a213eec6f4c558602e6b16553c13ef220f67be Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:07:52 +0100 Subject: [PATCH 03/36] Escape pipe and test for xargs --- test/cli/app_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 2b77f447..1e97f0b5 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -15,8 +15,8 @@ class CliAppTest < CliTestCase run_command("boot").tap do |output| assert_match /Rebooting container with same version already deployed/, output # Can't start what's already running - assert_match /docker ps -q --filter label=service=app | xargs docker stop/, output # Stop what's running - assert_match /docker container ls -a -f name=app-999 -q | docker container rm/, output # Remove old container + assert_match /docker ps -q --filter label=service=app \| xargs docker stop/, output # Stop what's running + assert_match /docker container ls -a -f name=app-999 -q \| xargs docker container rm/, output # Remove old container assert_match /docker run/, output # Start new container end ensure From 22bbedf2985f98eb14bdcca9ae9c2384fc24269f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:08:00 +0100 Subject: [PATCH 04/36] Show current running version --- lib/mrsk/cli/app.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index 36816178..ad02a4e0 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -165,6 +165,11 @@ class Mrsk::Cli::App < Mrsk::Cli::Base on(MRSK.hosts) { execute *MRSK.app.remove_images } end + desc "current_version", "Shows the version currently running" + def current_version + on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.current_running_version).strip } + end + private def using_version(new_version) if new_version From 74c493def43dff647cca1223ca10f261120615a0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:31:11 +0100 Subject: [PATCH 05/36] Don't actually need reboot now that boot can do that --- lib/mrsk/cli/app.rb | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index ad02a4e0..cf2d74e3 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -1,11 +1,15 @@ require "mrsk/cli/base" class Mrsk::Cli::App < Mrsk::Cli::Base - desc "boot", "Boot app on servers (or start them if they've already been booted)" + desc "boot", "Boot app on servers (or reboot app if already running)" def boot cli = self + say "Ensure no other version of the app is running...", :magenta + stop + using_version(options[:version] || most_recent_version_available) do |version| + say "Start container with version #{version} (or reboot if already running)...", :magenta MRSK.config.roles.each do |role| on(role.hosts) do |host| begin @@ -14,9 +18,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base if e.message =~ /already in use/ error "Rebooting container with same version already deployed on #{host}" - cli.stop cli.remove_container version - execute *MRSK.app.run(role: role.name) else raise @@ -27,15 +29,6 @@ class Mrsk::Cli::App < Mrsk::Cli::Base end end - desc "reboot", "Reboot app on host (stop container, remove container, start new container with latest image)" - def reboot - old_version = current_running_version - - stop - remove_container old_version - boot - end - desc "start", "Start existing app on servers (use --version= to designate specific version)" def start on(MRSK.hosts) { execute *MRSK.app.start, raise_on_non_zero_exit: false } @@ -187,12 +180,14 @@ class Mrsk::Cli::App < Mrsk::Cli::Base def most_recent_version_available(host: MRSK.primary_host) version = nil + say "Retrieve most recent version available as an image...", :magenta on(host) { version = capture_with_info(*MRSK.app.most_recent_version_from_available_images).strip } version.presence end def current_running_version(host: MRSK.primary_host) version = nil + say "Retrieve current running version...", :magenta on(host) { version = capture_with_info(*MRSK.app.current_running_version).strip } version.presence end From 839a0df40e0024d14ecf4be4df1b822802212e33 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:31:56 +0100 Subject: [PATCH 06/36] Boot now does its own stopping --- lib/mrsk/cli/main.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index 3f2275ab..85e5f5f8 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -25,7 +25,6 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base invoke "mrsk:cli:registry:login" invoke "mrsk:cli:build:deliver" invoke "mrsk:cli:traefik:boot" - invoke "mrsk:cli:app:stop" invoke "mrsk:cli:app:boot" invoke "mrsk:cli:prune:all" end @@ -35,7 +34,6 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base def redeploy print_runtime do invoke "mrsk:cli:build:deliver" - invoke "mrsk:cli:app:stop" invoke "mrsk:cli:app:boot" end end From 78f0be9c7606878d5b84ea6cbb46f3607e92a877 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:33:49 +0100 Subject: [PATCH 07/36] Only multi-stage actions should talk --- lib/mrsk/cli/app.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index cf2d74e3..eaadec9f 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -8,8 +8,10 @@ class Mrsk::Cli::App < Mrsk::Cli::Base say "Ensure no other version of the app is running...", :magenta stop + say "Get most recent version available as an image...", :magenta using_version(options[:version] || most_recent_version_available) do |version| say "Start container with version #{version} (or reboot if already running)...", :magenta + MRSK.config.roles.each do |role| on(role.hosts) do |host| begin @@ -180,14 +182,12 @@ class Mrsk::Cli::App < Mrsk::Cli::Base def most_recent_version_available(host: MRSK.primary_host) version = nil - say "Retrieve most recent version available as an image...", :magenta on(host) { version = capture_with_info(*MRSK.app.most_recent_version_from_available_images).strip } version.presence end def current_running_version(host: MRSK.primary_host) version = nil - say "Retrieve current running version...", :magenta on(host) { version = capture_with_info(*MRSK.app.current_running_version).strip } version.presence end From 13d4eb4017aa55b89cd76f1425c578691e0fc563 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:41:30 +0100 Subject: [PATCH 08/36] Narrate multi-stage actions --- lib/mrsk/cli/main.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index 85e5f5f8..0ec92b7b 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -21,11 +21,21 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base desc "deploy", "Deploy the app to servers" def deploy print_runtime do + say "Ensure Docker is installed...", :magenta invoke "mrsk:cli:server:bootstrap" + + say "Log into image registry...", :magenta invoke "mrsk:cli:registry:login" + + say "Build and push app image...", :magenta invoke "mrsk:cli:build:deliver" + + say "Ensure Traefik is running...", :magenta invoke "mrsk:cli:traefik:boot" + invoke "mrsk:cli:app:boot" + + say "Prune old containers and images...", :magenta invoke "mrsk:cli:prune:all" end end @@ -33,7 +43,9 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base desc "redeploy", "Deploy new version of the app to servers (without bootstrapping servers, starting Traefik, pruning, and registry login)" def redeploy print_runtime do + say "Build and push app image...", :magenta invoke "mrsk:cli:build:deliver" + invoke "mrsk:cli:app:boot" end end From 687b8c9def57ea4b53eb8dff466e9ff1561ca1db Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 14:41:39 +0100 Subject: [PATCH 09/36] Rely on shared --version --- lib/mrsk/cli/main.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index 0ec92b7b..a9575d4e 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -50,11 +50,11 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base end end - desc "rollback [VERSION]", "Rollback the app to VERSION (that must already be on servers)" - def rollback(version) + desc "rollback", "Rollback the app to --version=X (that must already be on servers)" + def rollback on(MRSK.hosts) do execute *MRSK.app.stop, raise_on_non_zero_exit: false - execute *MRSK.app.start(version: version) + execute *MRSK.app.start end end From 76217842354e812f7b9c565d853704e79f0f11a9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 15:05:34 +0100 Subject: [PATCH 10/36] Bring back regular version with narration --- lib/mrsk/cli/main.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index a9575d4e..ca28ab72 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -50,8 +50,13 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base end end - desc "rollback", "Rollback the app to --version=X (that must already be on servers)" - def rollback + desc "rollback [VERSION]", "Rollback the app to VERSION" + def rollback(version) + MRSK.version = version + + cli = self + + cli.say "Stop current version, then start version #{version}...", :magenta on(MRSK.hosts) do execute *MRSK.app.stop, raise_on_non_zero_exit: false execute *MRSK.app.start From 17e75ec2c93190c3a1f0dd5fa0502ae132558d7d Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 15:06:43 +0100 Subject: [PATCH 11/36] No more reboot --- test/cli/app_test.rb | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 1e97f0b5..3d064662 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -23,21 +23,6 @@ class CliAppTest < CliTestCase Thread.report_on_exception = true end - test "reboot to default version" do - run_command("reboot").tap do |output| - assert_match /docker ps --filter label=service=app/, output # Find current container - assert_match /docker stop/, output # Stop old container - assert_match /docker container rm/, output # Remove old container - assert_match /docker run -d --restart unless-stopped .* dhh\/app:999/, output # Start new container - end - end - - test "reboot to specific version" do - run_command("reboot", "--version", "456").tap do |output| - assert_match /docker run -d --restart unless-stopped .* dhh\/app:456/, output - end - end - test "remove_container" do run_command("remove_container", "1234567").tap do |output| assert_match /docker container ls -a -f name=app-1234567 -q | docker container rm/, output From 37e216f2b7743ae98c2eee0cce661457b6b5a8f5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 15:08:44 +0100 Subject: [PATCH 12/36] Add some more tests --- test/cli/app_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 3d064662..4cddd018 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -23,6 +23,24 @@ class CliAppTest < CliTestCase Thread.report_on_exception = true end + test "start" do + run_command("start").tap do |output| + assert_match /docker start app-999/, output + end + end + + test "stop" do + run_command("stop").tap do |output| + assert_match /docker ps -q --filter label=service=app \| xargs docker stop/, output + end + end + + test "details" do + run_command("details").tap do |output| + assert_match /docker ps --filter label=service=app/, output + end + end + test "remove_container" do run_command("remove_container", "1234567").tap do |output| assert_match /docker container ls -a -f name=app-1234567 -q | docker container rm/, output From 3d71ecdf80c10cfec39cd47cc7df4f5ac0c8bf44 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 15:16:30 +0100 Subject: [PATCH 13/36] Only say if you're going to do it --- 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 eaadec9f..66500aa8 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -8,7 +8,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base say "Ensure no other version of the app is running...", :magenta stop - say "Get most recent version available as an image...", :magenta + say "Get most recent version available as an image...", :magenta unless options[:version] using_version(options[:version] || most_recent_version_available) do |version| say "Start container with version #{version} (or reboot if already running)...", :magenta From a3d998508bfe675b7bca14dcf2dee8abff1aed1d Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 15:16:40 +0100 Subject: [PATCH 14/36] Proper versioning for console and bash --- lib/mrsk/cli/app.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index 66500aa8..dd0644dc 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -69,23 +69,19 @@ class Mrsk::Cli::App < Mrsk::Cli::Base desc "console", "Start Rails Console on primary host (or specific host set by --hosts)" def console - using_version(options[:version] || most_recent_version_available) do - run_locally do - if version - info "Launching Rails console on #{MRSK.primary_host} [Version: #{version}]" - exec MRSK.app.console(host: MRSK.primary_host) - else - error "No image available for #{MRSK.config.repository}" - end - end + say "Get most recent version available as an image...", :magenta unless options[:version] + using_version(options[:version] || most_recent_version_available) do |version| + say "Launching Rails console of version #{version} on #{MRSK.primary_host}...", :magenta + run_locally { 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 - info "Launching bash session on #{MRSK.primary_host}" - exec MRSK.app.bash(host: MRSK.primary_host) + say "Get most recent version available as an image...", :magenta unless options[:version] + using_version(options[:version] || most_recent_version_available) do |version| + say "Launching bash session of version #{version} on #{MRSK.primary_host}...", :magenta + run_locally { exec MRSK.app.bash(host: MRSK.primary_host) } end end From 3c1053feddb0b08e9062c594fc778606380a82df Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 16:07:25 +0100 Subject: [PATCH 15/36] Clarify exec modes and drop tailored versions --- README.md | 22 ++++++++++--- lib/mrsk/cli/app.rb | 65 +++++++++++++++++---------------------- lib/mrsk/commands/app.rb | 22 ++++++------- test/commands/app_test.rb | 4 +-- 4 files changed, 56 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 62941f54..e34db4ff 100644 --- a/README.md +++ b/README.md @@ -302,9 +302,9 @@ Now run `mrsk accessory start mysql` to start the MySQL server on 1.1.1.3. See ` ## Commands -### Running remote execution and runners +### Running commands on servers -If you need to execute commands inside the Rails containers, you can use `mrsk app exec` and `mrsk app runner`. Examples: +You can execute one-off commands on the servers: ```bash # Runs command on all servers @@ -347,13 +347,25 @@ Database adapter sqlite3 Database schema version 20221231233303 # Run Rails runner on primary server -mrsk app runner -p 'puts Rails.application.config.time_zone' +mrsk app exec -p 'bin/rails runner "puts Rails.application.config.time_zone"' UTC ``` -### Running a Rails console +### Running interactive commands over SSH + +You can run interactive commands, like a Rails console or a bash session, on a server (default is primary, use `--hosts` to connect to another): + +```bash +# Starts a bash session in a new container made from the most recent app image +mrsk app exec -i bash + +# Starts a bash session in the currently running container for the app +mrsk app exec -i --reuse bash + +# Starts a Rails console in a new container made from the most recent app image +mrsk app exec -i 'bin/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. ### Running details to see state of containers diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index dd0644dc..abab40a5 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -47,49 +47,40 @@ class Mrsk::Cli::App < Mrsk::Cli::Base end desc "exec [CMD]", "Execute a custom command on servers" - option :method, aliases: "-m", default: "exec", desc: "Execution method: [exec] perform inside app container / [run] perform in new container / [ssh] perform over ssh" + option :interactive, aliases: "-i", type: :boolean, default: false, desc: "Execute command over ssh for an interactive shell (use for console/bash)" + option :reuse, type: :boolean, default: false, desc: "Reuse currently running container instead of starting a new one" def exec(cmd) - runner = \ - case options[:method] - when "exec" then "exec" - when "run" then "run_exec" - when "ssh" then "exec_over_ssh" - else raise "Unknown method: #{options[:method]}" - end.inquiry - - if runner.exec_over_ssh? - run_locally do - info "Launching command on #{MRSK.primary_host}" - exec MRSK.app.exec_over_ssh(cmd, host: MRSK.primary_host) + case + when options[:interactive] && options[:reuse] + say "Get current version of running container...", :magenta unless options[:version] + using_version(options[:version] || current_running_version) do |version| + say "Launching interactive command with version #{version} via SSH from existing container on #{MRSK.primary_host}...", :magenta + run_locally { exec MRSK.app.execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) } end + + when options[:interactive] + say "Get most recent version available as an image...", :magenta unless options[:version] + using_version(options[:version] || most_recent_version_available) do |version| + say "Launching interactive command with version #{version} via SSH from new container on #{MRSK.primary_host}...", :magenta + run_locally { exec MRSK.app.execute_in_new_container_over_ssh(cmd, host: MRSK.primary_host) } + end + + when options[:reuse] + say "Get current version of running container...", :magenta unless options[:version] + using_version(options[:version] || current_running_version) do |version| + say "Launching command with version #{version} from existing container on #{MRSK.primary_host}", :magenta + on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd)) } + end + else - on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.send(runner, cmd)) } + say "Get most recent version available as an image...", :magenta unless options[:version] + using_version(options[:version] || most_recent_version_available) do |version| + say "Launching command with version #{version} from new container on #{MRSK.primary_host}", :magenta + on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.execute_in_new_container(cmd)) } + end end end - desc "console", "Start Rails Console on primary host (or specific host set by --hosts)" - def console - say "Get most recent version available as an image...", :magenta unless options[:version] - using_version(options[:version] || most_recent_version_available) do |version| - say "Launching Rails console of version #{version} on #{MRSK.primary_host}...", :magenta - run_locally { 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 - say "Get most recent version available as an image...", :magenta unless options[:version] - using_version(options[:version] || most_recent_version_available) do |version| - say "Launching bash session of version #{version} on #{MRSK.primary_host}...", :magenta - run_locally { exec MRSK.app.bash(host: MRSK.primary_host) } - end - end - - desc "runner [EXPRESSION]", "Execute Rails runner with given expression" - def runner(expression) - on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'")) } - end - desc "containers", "List all the app containers currently on servers" def containers on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.list_containers) } diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index ae8a1fc9..45012d9f 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -42,14 +42,14 @@ class Mrsk::Commands::App < Mrsk::Commands::Base ("grep '#{grep}'" if grep) end - def exec(*command, interactive: false) + def execute_in_existing_container(*command, interactive: false) docker :exec, ("-it" if interactive), config.service_with_version, *command end - - def run_exec(*command, interactive: false) + + def execute_in_new_container(*command, interactive: false) docker :run, ("-it" if interactive), "--rm", @@ -60,8 +60,12 @@ class Mrsk::Commands::App < Mrsk::Commands::Base *command end - def exec_over_ssh(*command, host:) - run_over_ssh run_exec(*command, interactive: true).join(" "), host: host + def execute_in_existing_container_over_ssh(*command, host:) + run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host + end + + def execute_in_new_container_over_ssh(*command, host:) + run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host end def follow_logs(host:, grep: nil) @@ -72,14 +76,6 @@ class Mrsk::Commands::App < Mrsk::Commands::Base ).join(" "), host: host end - def console(host:) - exec_over_ssh "bin/rails", "c", host: host - end - - def bash(host:) - exec_over_ssh "bash", host: host - end - def list_containers docker :container, :ls, "-a", *service_filter end diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index 51ee7b3a..5c8bcda1 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -26,10 +26,10 @@ class CommandsAppTest < ActiveSupport::TestCase [:docker, :run, "-d", "--restart unless-stopped", "--name", "app-missing", "-e", "RAILS_MASTER_KEY=456", "--volume", "/local/path:/container/path", "--label", "service=app", "--label", "role=web", "--label", "traefik.http.routers.app.rule='PathPrefix(`/`)'", "--label", "traefik.http.services.app.loadbalancer.healthcheck.path=/up", "--label", "traefik.http.services.app.loadbalancer.healthcheck.interval=1s", "--label", "traefik.http.middlewares.app.retry.attempts=3", "--label", "traefik.http.middlewares.app.retry.initialinterval=500ms", "dhh/app:missing"], @app.run end - test "run with" do + test "execute in new container" do assert_equal \ [ :docker, :run, "--rm", "-e", "RAILS_MASTER_KEY=456", "dhh/app:missing", "bin/rails", "db:setup" ], - @app.run_exec("bin/rails", "db:setup") + @app.execute_in_new_container("bin/rails", "db:setup") end test "run without master key" do From d263b0ffa53ac60b0271a0c59f77a84947404d6c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 16:27:10 +0100 Subject: [PATCH 16/36] Extract xargs helper --- lib/mrsk/commands/app.rb | 4 ++-- lib/mrsk/commands/base.rb | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index 45012d9f..7a04c7bd 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -28,7 +28,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base end def stop - pipe current_container_id, "xargs docker stop" + pipe current_container_id, xargs(docker(:stop)) end def info @@ -83,7 +83,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base def remove_container(version:) pipe \ container_id_for(container_name: service_with_version(version)), - [ "xargs", docker(:container, :rm) ].flatten + xargs(docker(:container, :rm)) end def remove_containers diff --git a/lib/mrsk/commands/base.rb b/lib/mrsk/commands/base.rb index 49a8bf81..44c1807f 100644 --- a/lib/mrsk/commands/base.rb +++ b/lib/mrsk/commands/base.rb @@ -28,6 +28,10 @@ module Mrsk::Commands combine *commands, by: "|" end + def xargs(command) + [ :xargs, command ].flatten + end + def docker(*args) args.compact.unshift :docker end From a3fe8856c94e7a659a18e266d606c74b8678050a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 16:27:16 +0100 Subject: [PATCH 17/36] Fix test --- test/cli/app_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 4cddd018..28e7fbb0 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -43,7 +43,7 @@ class CliAppTest < CliTestCase test "remove_container" do run_command("remove_container", "1234567").tap do |output| - assert_match /docker container ls -a -f name=app-1234567 -q | docker container rm/, output + assert_match /docker container ls -a -f name=app-1234567 -q \| xargs docker container rm/, output end end From 8848335fbcb12dea6e426a57d5585a3114e9f5bf Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 16:39:26 +0100 Subject: [PATCH 18/36] Extract executions into separate concern --- lib/mrsk/commands/app.rb | 31 +++-------------------- lib/mrsk/commands/concerns.rb | 5 ++++ lib/mrsk/commands/concerns/executions.rb | 27 ++++++++++++++++++++ lib/mrsk/commands/concerns/repository.rb | 32 +++++++++++------------- test/commands/app_test.rb | 21 ++++++++++++++++ 5 files changed, 71 insertions(+), 45 deletions(-) create mode 100644 lib/mrsk/commands/concerns.rb create mode 100644 lib/mrsk/commands/concerns/executions.rb diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index 7a04c7bd..dea02ec4 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -1,8 +1,9 @@ require "mrsk/commands/base" -require "mrsk/commands/concerns/repository" +require "mrsk/commands/concerns" class Mrsk::Commands::App < Mrsk::Commands::Base - include Mrsk::Commands::Concerns::Repository + include Mrsk::Commands::Concerns::Executions, + Mrsk::Commands::Concerns::Repository def run(role: :web) role = config.role(role) @@ -42,32 +43,6 @@ class Mrsk::Commands::App < Mrsk::Commands::Base ("grep '#{grep}'" if grep) end - def execute_in_existing_container(*command, interactive: false) - docker :exec, - ("-it" if interactive), - config.service_with_version, - *command - end - - def execute_in_new_container(*command, interactive: false) - docker :run, - ("-it" if interactive), - "--rm", - *rails_master_key_arg, - *config.env_args, - *config.volume_args, - config.absolute_image, - *command - end - - def execute_in_existing_container_over_ssh(*command, host:) - run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host - end - - def execute_in_new_container_over_ssh(*command, host:) - run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host - end - def follow_logs(host:, grep: nil) run_over_ssh pipe( current_container_id, diff --git a/lib/mrsk/commands/concerns.rb b/lib/mrsk/commands/concerns.rb new file mode 100644 index 00000000..cd4ea343 --- /dev/null +++ b/lib/mrsk/commands/concerns.rb @@ -0,0 +1,5 @@ +module Mrsk::Commands::Concerns +end + +require "mrsk/commands/concerns/executions" +require "mrsk/commands/concerns/repository" diff --git a/lib/mrsk/commands/concerns/executions.rb b/lib/mrsk/commands/concerns/executions.rb new file mode 100644 index 00000000..dd7be827 --- /dev/null +++ b/lib/mrsk/commands/concerns/executions.rb @@ -0,0 +1,27 @@ +module Mrsk::Commands::Concerns::Executions + def execute_in_existing_container(*command, interactive: false) + docker :exec, + ("-it" if interactive), + config.service_with_version, + *command + end + + def execute_in_new_container(*command, interactive: false) + docker :run, + ("-it" if interactive), + "--rm", + *rails_master_key_arg, + *config.env_args, + *config.volume_args, + config.absolute_image, + *command + end + + def execute_in_existing_container_over_ssh(*command, host:) + run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host + end + + def execute_in_new_container_over_ssh(*command, host:) + run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host + end +end diff --git a/lib/mrsk/commands/concerns/repository.rb b/lib/mrsk/commands/concerns/repository.rb index a0e350c0..4bf093d1 100644 --- a/lib/mrsk/commands/concerns/repository.rb +++ b/lib/mrsk/commands/concerns/repository.rb @@ -1,21 +1,19 @@ -module Mrsk::Commands::Concerns - module Repository - def container_id_for(container_name:) - docker :container, :ls, "-a", "-f", "name=#{container_name}", "-q" - end +module Mrsk::Commands::Concerns::Repository + def container_id_for(container_name:) + docker :container, :ls, "-a", "-f", "name=#{container_name}", "-q" + end - def current_running_version - # FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail! - pipe \ - docker(:ps, "--filter", "label=service=#{config.service}", "--format", '"{{.Names}}"'), - %(sed 's/-/\\n/g'), - "tail -n 1" - end + def current_running_version + # FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail! + pipe \ + docker(:ps, "--filter", "label=service=#{service_name}", "--format", '"{{.Names}}"'), + %(sed 's/-/\\n/g'), + "tail -n 1" + end - def most_recent_version_from_available_images - pipe \ - docker(:image, :ls, "--format", '"{{.Tag}}"', config.repository), - "head -n 1" - end + def most_recent_version_from_available_images + pipe \ + docker(:image, :ls, "--format", '"{{.Tag}}"', config.repository), + "head -n 1" end end diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index 5c8bcda1..bf735bb1 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -1,6 +1,7 @@ require "test_helper" require "mrsk/configuration" require "mrsk/commands/app" +require "minitest/autorun" # using stubs that take args class CommandsAppTest < ActiveSupport::TestCase setup do @@ -32,6 +33,26 @@ class CommandsAppTest < ActiveSupport::TestCase @app.execute_in_new_container("bin/rails", "db:setup") end + test "execute in existing container" do + assert_equal \ + [ :docker, :exec, "app-missing", "bin/rails", "db:setup" ], + @app.execute_in_existing_container("bin/rails", "db:setup") + end + + test "execute in new container over ssh" do + @app.stub(:run_over_ssh, ->(cmd, host:) { cmd }) do + assert_match %r|docker run -it --rm -e RAILS_MASTER_KEY=456 dhh/app:missing bin/rails c|, + @app.execute_in_new_container_over_ssh("bin/rails", "c", host: "app-1") + end + end + + test "execute in existing container over ssh" do + @app.stub(:run_over_ssh, ->(cmd, host:) { cmd }) do + assert_match %r|docker exec -it app-missing bin/rails c|, + @app.execute_in_existing_container_over_ssh("bin/rails", "c", host: "app-1") + end + end + test "run without master key" do ENV["RAILS_MASTER_KEY"] = nil @app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config.tap { |c| c[:skip_master_key] = true }) From 13e22f8a343eab6438cca94a6cf33c49d8f6e02f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 16:45:52 +0100 Subject: [PATCH 19/36] Repository really is app specific, since it relies on versions --- lib/mrsk/commands/app.rb | 21 +++++++++++++++++++-- lib/mrsk/commands/concerns.rb | 1 - lib/mrsk/commands/concerns/repository.rb | 19 ------------------- 3 files changed, 19 insertions(+), 22 deletions(-) delete mode 100644 lib/mrsk/commands/concerns/repository.rb diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index dea02ec4..00f5c5c2 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -2,8 +2,7 @@ require "mrsk/commands/base" require "mrsk/commands/concerns" class Mrsk::Commands::App < Mrsk::Commands::Base - include Mrsk::Commands::Concerns::Executions, - Mrsk::Commands::Concerns::Repository + include Mrsk::Commands::Concerns::Executions def run(role: :web) role = config.role(role) @@ -51,6 +50,24 @@ class Mrsk::Commands::App < Mrsk::Commands::Base ).join(" "), host: host end + def container_id_for(container_name:) + docker :container, :ls, "-a", "-f", "name=#{container_name}", "-q" + end + + def current_running_version + # FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail! + pipe \ + docker(:ps, "--filter", "label=service=#{service_name}", "--format", '"{{.Names}}"'), + %(sed 's/-/\\n/g'), + "tail -n 1" + end + + def most_recent_version_from_available_images + pipe \ + docker(:image, :ls, "--format", '"{{.Tag}}"', config.repository), + "head -n 1" + end + def list_containers docker :container, :ls, "-a", *service_filter end diff --git a/lib/mrsk/commands/concerns.rb b/lib/mrsk/commands/concerns.rb index cd4ea343..789fe922 100644 --- a/lib/mrsk/commands/concerns.rb +++ b/lib/mrsk/commands/concerns.rb @@ -2,4 +2,3 @@ module Mrsk::Commands::Concerns end require "mrsk/commands/concerns/executions" -require "mrsk/commands/concerns/repository" diff --git a/lib/mrsk/commands/concerns/repository.rb b/lib/mrsk/commands/concerns/repository.rb deleted file mode 100644 index 4bf093d1..00000000 --- a/lib/mrsk/commands/concerns/repository.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Mrsk::Commands::Concerns::Repository - def container_id_for(container_name:) - docker :container, :ls, "-a", "-f", "name=#{container_name}", "-q" - end - - def current_running_version - # FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail! - pipe \ - docker(:ps, "--filter", "label=service=#{service_name}", "--format", '"{{.Names}}"'), - %(sed 's/-/\\n/g'), - "tail -n 1" - end - - def most_recent_version_from_available_images - pipe \ - docker(:image, :ls, "--format", '"{{.Tag}}"', config.repository), - "head -n 1" - end -end From 64b91daab182f3ade5ab050aef6158aab8a66344 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 16:55:34 +0100 Subject: [PATCH 20/36] Drop concerns Not enough reuse possible --- lib/mrsk/commands/app.rb | 42 ++++++++++++++++++++---- lib/mrsk/commands/concerns.rb | 4 --- lib/mrsk/commands/concerns/executions.rb | 27 --------------- 3 files changed, 35 insertions(+), 38 deletions(-) delete mode 100644 lib/mrsk/commands/concerns.rb delete mode 100644 lib/mrsk/commands/concerns/executions.rb diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index 00f5c5c2..d3140cb1 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -1,9 +1,6 @@ require "mrsk/commands/base" -require "mrsk/commands/concerns" class Mrsk::Commands::App < Mrsk::Commands::Base - include Mrsk::Commands::Concerns::Executions - def run(role: :web) role = config.role(role) @@ -23,10 +20,6 @@ class Mrsk::Commands::App < Mrsk::Commands::Base docker :start, service_with_version end - def current_container_id - docker :ps, "-q", *service_filter - end - def stop pipe current_container_id, xargs(docker(:stop)) end @@ -35,6 +28,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base docker :ps, *service_filter end + def logs(since: nil, lines: nil, grep: nil) pipe \ current_container_id, @@ -50,6 +44,38 @@ class Mrsk::Commands::App < Mrsk::Commands::Base ).join(" "), host: host end + + def execute_in_existing_container(*command, interactive: false) + docker :exec, + ("-it" if interactive), + config.service_with_version, + *command + end + + def execute_in_new_container(*command, interactive: false) + docker :run, + ("-it" if interactive), + "--rm", + *rails_master_key_arg, + *config.env_args, + *config.volume_args, + config.absolute_image, + *command + end + + def execute_in_existing_container_over_ssh(*command, host:) + run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host + end + + def execute_in_new_container_over_ssh(*command, host:) + run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host + end + + + def current_container_id + docker :ps, "-q", *service_filter + end + def container_id_for(container_name:) docker :container, :ls, "-a", "-f", "name=#{container_name}", "-q" end @@ -68,6 +94,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base "head -n 1" end + def list_containers docker :container, :ls, "-a", *service_filter end @@ -90,6 +117,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base docker :image, :prune, "-a", "-f", *service_filter end + private def service_with_version(version = nil) if version diff --git a/lib/mrsk/commands/concerns.rb b/lib/mrsk/commands/concerns.rb deleted file mode 100644 index 789fe922..00000000 --- a/lib/mrsk/commands/concerns.rb +++ /dev/null @@ -1,4 +0,0 @@ -module Mrsk::Commands::Concerns -end - -require "mrsk/commands/concerns/executions" diff --git a/lib/mrsk/commands/concerns/executions.rb b/lib/mrsk/commands/concerns/executions.rb deleted file mode 100644 index dd7be827..00000000 --- a/lib/mrsk/commands/concerns/executions.rb +++ /dev/null @@ -1,27 +0,0 @@ -module Mrsk::Commands::Concerns::Executions - def execute_in_existing_container(*command, interactive: false) - docker :exec, - ("-it" if interactive), - config.service_with_version, - *command - end - - def execute_in_new_container(*command, interactive: false) - docker :run, - ("-it" if interactive), - "--rm", - *rails_master_key_arg, - *config.env_args, - *config.volume_args, - config.absolute_image, - *command - end - - def execute_in_existing_container_over_ssh(*command, host:) - run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host - end - - def execute_in_new_container_over_ssh(*command, host:) - run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host - end -end From 87e54d41e494f06154995cba21cb54f2cb916446 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:03:26 +0100 Subject: [PATCH 21/36] Need two stubs! --- test/commands/app_test.rb | 1 - test/test_helper.rb | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index bf735bb1..7453ff17 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -1,7 +1,6 @@ require "test_helper" require "mrsk/configuration" require "mrsk/commands/app" -require "minitest/autorun" # using stubs that take args class CommandsAppTest < ActiveSupport::TestCase setup do diff --git a/test/test_helper.rb b/test/test_helper.rb index 3da81a65..732b5af4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,7 +2,8 @@ require "bundler/setup" require "active_support/test_case" require "active_support/testing/autorun" require "debug" -require "mocha/minitest" +require "mocha/minitest" # using #stubs that can alter returns +require "minitest/autorun" # using #stub that take args require "sshkit" ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(STDOUT) if ENV["VERBOSE"] From c453b947e01375c606ec57a87ef381b8cc9bfe0b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:18:42 +0100 Subject: [PATCH 22/36] Add exec tests --- test/cli/app_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 28e7fbb0..b17ba8d4 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -47,6 +47,19 @@ class CliAppTest < CliTestCase end end + test "exec" do + run_command("exec", "ruby -v").tap do |output| + assert_match /ruby -v/, output + end + end + + test "exec with reuse" do + run_command("exec", "--reuse", "ruby -v").tap do |output| + assert_match %r[docker ps --filter label=service=app --format \"{{.Names}}\" | sed 's/-/\\n/g' | tail -n 1], output # Get current version + assert_match %r[docker exec app-999 ruby -v], output # Get current version + end + end + private def run_command(*command) stdouted { Mrsk::Cli::App.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) } From 59199cc69ab2af92a0e8e76b59ce3ecbc36d1113 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:18:47 +0100 Subject: [PATCH 23/36] Fix bug --- lib/mrsk/commands/app.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index d3140cb1..37d4d00d 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -83,7 +83,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base def current_running_version # FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail! pipe \ - docker(:ps, "--filter", "label=service=#{service_name}", "--format", '"{{.Names}}"'), + docker(:ps, "--filter", "label=service=#{config.service}", "--format", '"{{.Names}}"'), %(sed 's/-/\\n/g'), "tail -n 1" end From 5ed3ea9d263d0957527ce36a000ac3d78478339b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:18:58 +0100 Subject: [PATCH 24/36] Grouping by spacing --- test/commands/app_test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index 7453ff17..c888bcff 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -26,6 +26,7 @@ class CommandsAppTest < ActiveSupport::TestCase [:docker, :run, "-d", "--restart unless-stopped", "--name", "app-missing", "-e", "RAILS_MASTER_KEY=456", "--volume", "/local/path:/container/path", "--label", "service=app", "--label", "role=web", "--label", "traefik.http.routers.app.rule='PathPrefix(`/`)'", "--label", "traefik.http.services.app.loadbalancer.healthcheck.path=/up", "--label", "traefik.http.services.app.loadbalancer.healthcheck.interval=1s", "--label", "traefik.http.middlewares.app.retry.attempts=3", "--label", "traefik.http.middlewares.app.retry.initialinterval=500ms", "dhh/app:missing"], @app.run end + test "execute in new container" do assert_equal \ [ :docker, :run, "--rm", "-e", "RAILS_MASTER_KEY=456", "dhh/app:missing", "bin/rails", "db:setup" ], @@ -52,6 +53,7 @@ class CommandsAppTest < ActiveSupport::TestCase end end + test "run without master key" do ENV["RAILS_MASTER_KEY"] = nil @app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config.tap { |c| c[:skip_master_key] = true }) From 5856a77a53fa74c003b7eec014d6fd117956a878 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:19:20 +0100 Subject: [PATCH 25/36] Bring accessory execution in line with app --- lib/mrsk/commands/accessory.rb | 21 ++++++++++-------- test/commands/accessory_test.rb | 39 +++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lib/mrsk/commands/accessory.rb b/lib/mrsk/commands/accessory.rb index a8f8c661..8362cc9f 100644 --- a/lib/mrsk/commands/accessory.rb +++ b/lib/mrsk/commands/accessory.rb @@ -33,6 +33,7 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base docker :ps, *service_filter end + def logs(since: nil, lines: nil, grep: nil) pipe \ docker(:logs, service_name, (" --since #{since}" if since), (" -n #{lines}" if lines), "-t", "2>&1"), @@ -46,14 +47,15 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base ).join(" ") end - def exec(*command, interactive: false) + + def execute_in_existing_container(*command, interactive: false) docker :exec, ("-it" if interactive), service_name, *command end - def run_exec(*command, interactive: false) + def execute_in_new_container(*command, interactive: false) docker :run, ("-it" if interactive), "--rm", @@ -63,17 +65,18 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base *command end + def execute_in_existing_container_over_ssh(*command) + run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host + end + + def execute_in_new_container_over_ssh(*command) + run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host + end + def run_over_ssh(command) super command, host: host end - def exec_over_ssh(*command) - run_over_ssh run_exec(*command, interactive: true).join(" ") - end - - def bash - exec_over_ssh "bash" - end def ensure_local_file_present(local_file) if !local_file.is_a?(StringIO) && !Pathname.new(local_file).exist? diff --git a/test/commands/accessory_test.rb b/test/commands/accessory_test.rb index 5294d48c..0c256172 100644 --- a/test/commands/accessory_test.rb +++ b/test/commands/accessory_test.rb @@ -41,18 +41,20 @@ class CommandsAccessoryTest < ActiveSupport::TestCase @config = Mrsk::Configuration.new(@config) @mysql = Mrsk::Commands::Accessory.new(@config, name: :mysql) @redis = Mrsk::Commands::Accessory.new(@config, name: :redis) + + ENV["MYSQL_ROOT_PASSWORD"] = "secret123" + end + + teardown do + ENV.delete("MYSQL_ROOT_PASSWORD") end test "run" do - ENV["MYSQL_ROOT_PASSWORD"] = "secret123" - assert_equal \ [:docker, :run, "--name", "app-mysql", "-d", "--restart", "unless-stopped", "-p", "3306:3306", "-e", "MYSQL_ROOT_PASSWORD=secret123", "-e", "MYSQL_ROOT_HOST=%", "--label", "service=app-mysql", "mysql:8.0"], @mysql.run assert_equal \ [:docker, :run, "--name", "app-redis", "-d", "--restart", "unless-stopped", "-p", "6379:6379", "-e", "SOMETHING=else", "--volume", "/var/lib/redis:/data", "--label", "service=app-redis", "--label", "cache=true", "redis:latest"], @redis.run - ensure - ENV["MYSQL_ROOT_PASSWORD"] = nil end test "start" do @@ -67,6 +69,35 @@ class CommandsAccessoryTest < ActiveSupport::TestCase assert_equal [:docker, :ps, "--filter", "label=service=app-mysql"], @mysql.info end + + test "execute in new container" do + assert_equal \ + [ :docker, :run, "--rm", "-e", "MYSQL_ROOT_PASSWORD=secret123", "-e", "MYSQL_ROOT_HOST=%", "mysql:8.0", "mysql", "-u", "root" ], + @mysql.execute_in_new_container("mysql", "-u", "root") + end + + test "execute in existing container" do + assert_equal \ + [ :docker, :exec, "app-mysql", "mysql", "-u", "root" ], + @mysql.execute_in_existing_container("mysql", "-u", "root") + end + + test "execute in new container over ssh" do + @mysql.stub(:run_over_ssh, ->(cmd, host:) { cmd }) do + assert_match %r|docker run -it --rm -e MYSQL_ROOT_PASSWORD=secret123 -e MYSQL_ROOT_HOST=% mysql:8.0 mysql -u root|, + @mysql.execute_in_new_container_over_ssh("mysql", "-u", "root") + end + end + + test "execute in existing container over ssh" do + @mysql.stub(:run_over_ssh, ->(cmd, host:) { cmd }) do + assert_match %r|docker exec -it app-mysql mysql -u root|, + @mysql.execute_in_existing_container_over_ssh("mysql", "-u", "root") + end + end + + + test "logs" do assert_equal [:docker, :logs, "app-mysql", "-t", "2>&1"], @mysql.logs assert_equal [:docker, :logs, "app-mysql", " --since 5m", " -n 100", "-t", "2>&1", "|", "grep 'thing'"], @mysql.logs(since: "5m", lines: 100, grep: "thing") From 58417f610fc5c5983e8ad0395d9866124146e423 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:20:14 +0100 Subject: [PATCH 26/36] Dupe comment --- test/cli/app_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index b17ba8d4..734659e4 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -56,7 +56,7 @@ class CliAppTest < CliTestCase test "exec with reuse" do run_command("exec", "--reuse", "ruby -v").tap do |output| assert_match %r[docker ps --filter label=service=app --format \"{{.Names}}\" | sed 's/-/\\n/g' | tail -n 1], output # Get current version - assert_match %r[docker exec app-999 ruby -v], output # Get current version + assert_match %r[docker exec app-999 ruby -v], output end end From 1fb2c71f6544e7227a61bd2a1521b0103dd04e05 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:22:55 +0100 Subject: [PATCH 27/36] Follow same dot style --- lib/mrsk/cli/app.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index abab40a5..7fdad85a 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -68,14 +68,14 @@ class Mrsk::Cli::App < Mrsk::Cli::Base when options[:reuse] say "Get current version of running container...", :magenta unless options[:version] using_version(options[:version] || current_running_version) do |version| - say "Launching command with version #{version} from existing container on #{MRSK.primary_host}", :magenta + say "Launching command with version #{version} from existing container on #{MRSK.primary_host}...", :magenta on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd)) } end else say "Get most recent version available as an image...", :magenta unless options[:version] using_version(options[:version] || most_recent_version_available) do |version| - say "Launching command with version #{version} from new container on #{MRSK.primary_host}", :magenta + say "Launching command with version #{version} from new container on #{MRSK.primary_host}...", :magenta on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.execute_in_new_container(cmd)) } end end From b964e04f935afd8f1cc2e4efe032cb1a915da372 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:24:36 +0100 Subject: [PATCH 28/36] Bring accessory execution in line with app --- lib/mrsk/cli/accessory.rb | 48 ++++++++++++++------------------------ test/cli/accessory_test.rb | 14 +++++++++++ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/mrsk/cli/accessory.rb b/lib/mrsk/cli/accessory.rb index 2263fe48..918c3d0b 100644 --- a/lib/mrsk/cli/accessory.rb +++ b/lib/mrsk/cli/accessory.rb @@ -82,39 +82,27 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base end end - desc "exec [NAME] [CMD]", "Execute a custom command on accessory host" - option :method, aliases: "-m", default: "exec", desc: "Execution method: [exec] perform inside container / [run] perform in new container / [ssh] perform over ssh" + desc "exec [NAME] [CMD]", "Execute a custom command on servers" + option :interactive, aliases: "-i", type: :boolean, default: false, desc: "Execute command over ssh for an interactive shell (use for console/bash)" + option :reuse, type: :boolean, default: false, desc: "Reuse currently running container instead of starting a new one" def exec(name, cmd) - runner = \ - case options[:method] - when "exec" then "exec" - when "run" then "run_exec" - when "ssh_exec" then "exec_over_ssh" - when "ssh_run" then "run_over_ssh" - else raise "Unknown method: #{options[:method]}" - end.inquiry - with_accessory(name) do |accessory| - if runner.exec_over_ssh? || runner.run_over_ssh? - run_locally do - info "Launching command on #{accessory.host}" - exec accessory.send(runner, cmd) - end + case + when options[:interactive] && options[:reuse] + say "Launching interactive command with via SSH from existing container...", :magenta + run_locally { exec accessory.execute_in_existing_container_over_ssh(cmd) } + + when options[:interactive] + say "Launching interactive command via SSH from new container...", :magenta + run_locally { exec accessory.execute_in_new_container_over_ssh(cmd) } + + when options[:reuse] + say "Launching command from existing container...", :magenta + on(accessory.host) { capture_with_info(*accessory.execute_in_existing_container(cmd)) } + else - on(accessory.host) do - info "Launching command on #{accessory.host}" - execute *accessory.send(runner, cmd) - end - end - end - end - - desc "bash [NAME]", "Start a bash session on primary host (or specific host set by --hosts)" - def bash(name) - with_accessory(name) do |accessory| - run_locally do - info "Launching bash session on #{accessory.host}" - exec accessory.bash + say "Launching command from new container...", :magenta + on(accessory.host) { capture_with_info(*accessory.execute_in_new_container(cmd)) } end end end diff --git a/test/cli/accessory_test.rb b/test/cli/accessory_test.rb index f3373f84..dde44a61 100644 --- a/test/cli/accessory_test.rb +++ b/test/cli/accessory_test.rb @@ -17,6 +17,20 @@ class CliAccessoryTest < CliTestCase assert_match "Running docker run --name app-mysql -d --restart unless-stopped -p 3306:3306 -e [REDACTED] -e MYSQL_ROOT_HOST=% --volume $PWD/app-mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf --volume $PWD/app-mysql/data:/var/lib/mysql --label service=app-mysql mysql:5.7 on 1.1.1.3", run_command("boot", "mysql") end + test "exec" do + run_command("exec", "mysql", "mysql -v").tap do |output| + assert_match /Launching command from new container/, output + assert_match /mysql -v/, output + end + end + + test "exec with reuse" do + run_command("exec", "mysql", "--reuse", "mysql -v").tap do |output| + assert_match /Launching command from existing container/, output + assert_match %r[docker exec app-mysql mysql -v], output + end + end + private def run_command(*command) stdouted { Mrsk::Cli::Accessory.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) } From 5c9a602d7688370ae183e9528d0950c316438608 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:46:41 +0100 Subject: [PATCH 29/36] Fixed host --- lib/mrsk/commands/accessory.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/commands/accessory.rb b/lib/mrsk/commands/accessory.rb index 8362cc9f..6cc6bf5c 100644 --- a/lib/mrsk/commands/accessory.rb +++ b/lib/mrsk/commands/accessory.rb @@ -66,11 +66,11 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base end def execute_in_existing_container_over_ssh(*command) - run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host + run_over_ssh execute_in_existing_container(*command, interactive: true).join(" ") end def execute_in_new_container_over_ssh(*command) - run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host + run_over_ssh execute_in_new_container(*command, interactive: true).join(" ") end def run_over_ssh(command) From 2182cfb5c7b21d580efbb287f45c618dea0afffa Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 17:49:47 +0100 Subject: [PATCH 30/36] Bump version for 0.5.0 --- Gemfile.lock | 2 +- lib/mrsk/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6ca852e2..c50e01e4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - mrsk (0.4.0) + mrsk (0.5.0) activesupport (>= 7.0) dotenv (~> 2.8) sshkit (~> 1.21) diff --git a/lib/mrsk/version.rb b/lib/mrsk/version.rb index 96e26016..df8e4919 100644 --- a/lib/mrsk/version.rb +++ b/lib/mrsk/version.rb @@ -1,3 +1,3 @@ module Mrsk - VERSION = "0.4.0" + VERSION = "0.5.0" end From 8a42fd2f30f986ed0f3bc4e6c5819c093c2b2375 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 20:43:22 +0100 Subject: [PATCH 31/36] Fix signature --- test/commands/accessory_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/commands/accessory_test.rb b/test/commands/accessory_test.rb index 0c256172..21886440 100644 --- a/test/commands/accessory_test.rb +++ b/test/commands/accessory_test.rb @@ -83,14 +83,14 @@ class CommandsAccessoryTest < ActiveSupport::TestCase end test "execute in new container over ssh" do - @mysql.stub(:run_over_ssh, ->(cmd, host:) { cmd }) do + @mysql.stub(:run_over_ssh, ->(cmd) { cmd }) do assert_match %r|docker run -it --rm -e MYSQL_ROOT_PASSWORD=secret123 -e MYSQL_ROOT_HOST=% mysql:8.0 mysql -u root|, @mysql.execute_in_new_container_over_ssh("mysql", "-u", "root") end end test "execute in existing container over ssh" do - @mysql.stub(:run_over_ssh, ->(cmd, host:) { cmd }) do + @mysql.stub(:run_over_ssh, ->(cmd) { cmd }) do assert_match %r|docker exec -it app-mysql mysql -u root|, @mysql.execute_in_existing_container_over_ssh("mysql", "-u", "root") end From 497c57e3e51479ba49d63d91532246252a20315b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 20:44:43 +0100 Subject: [PATCH 32/36] Style --- lib/mrsk/cli/registry.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mrsk/cli/registry.rb b/lib/mrsk/cli/registry.rb index f86702ac..939e7c33 100644 --- a/lib/mrsk/cli/registry.rb +++ b/lib/mrsk/cli/registry.rb @@ -3,7 +3,7 @@ require "mrsk/cli/base" class Mrsk::Cli::Registry < Mrsk::Cli::Base desc "login", "Login to the registry locally and remotely" def login - run_locally { execute *MRSK.registry.login } + run_locally { execute *MRSK.registry.login } on(MRSK.hosts) { execute *MRSK.registry.login } rescue ArgumentError => e puts e.message From 3daecf696ab5bd7b8634ee9a23b06bb54dff028c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 20:45:32 +0100 Subject: [PATCH 33/36] Extract proper auditor and audit everything --- lib/mrsk/cli/accessory.rb | 44 +++++++++++++++++++++++++++++------- lib/mrsk/cli/app.rb | 42 ++++++++++++++++++++++++++-------- lib/mrsk/cli/build.rb | 5 +++- lib/mrsk/cli/main.rb | 7 ++++++ lib/mrsk/cli/prune.rb | 10 ++++++-- lib/mrsk/cli/traefik.rb | 20 ++++++++++++---- lib/mrsk/commander.rb | 7 +++++- lib/mrsk/commands/auditor.rb | 31 +++++++++++++++++++++++++ lib/mrsk/commands/base.rb | 4 ++++ 9 files changed, 145 insertions(+), 25 deletions(-) create mode 100644 lib/mrsk/commands/auditor.rb diff --git a/lib/mrsk/cli/accessory.rb b/lib/mrsk/cli/accessory.rb index 918c3d0b..549ba8e0 100644 --- a/lib/mrsk/cli/accessory.rb +++ b/lib/mrsk/cli/accessory.rb @@ -9,7 +9,10 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base with_accessory(name) do |accessory| directories(name) upload(name) - on(accessory.host) { execute *accessory.run } + on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} boot"), verbosity: :debug + execute *accessory.run + end end end end @@ -18,6 +21,8 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base def upload(name) with_accessory(name) do |accessory| on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} upload files"), verbosity: :debug + accessory.files.each do |(local, remote)| accessory.ensure_local_file_present(local) @@ -33,6 +38,8 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base def directories(name) with_accessory(name) do |accessory| on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} create directories"), verbosity: :debug + accessory.directories.keys.each do |host_path| execute *accessory.make_directory(host_path) end @@ -52,14 +59,20 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base desc "start [NAME]", "Start existing accessory on host" def start(name) with_accessory(name) do |accessory| - on(accessory.host) { execute *accessory.start } + on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} start"), verbosity: :debug + execute *accessory.start + end end end desc "stop [NAME]", "Stop accessory on host" def stop(name) with_accessory(name) do |accessory| - on(accessory.host) { execute *accessory.stop, raise_on_non_zero_exit: false } + on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} stop"), verbosity: :debug + execute *accessory.stop, raise_on_non_zero_exit: false + end end end @@ -98,11 +111,17 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base when options[:reuse] say "Launching command from existing container...", :magenta - on(accessory.host) { capture_with_info(*accessory.execute_in_existing_container(cmd)) } + on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} cmd '#{cmd}'"), verbosity: :debug + capture_with_info(*accessory.execute_in_existing_container(cmd)) + end else say "Launching command from new container...", :magenta - on(accessory.host) { capture_with_info(*accessory.execute_in_new_container(cmd)) } + on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} cmd '#{cmd}'"), verbosity: :debug + capture_with_info(*accessory.execute_in_new_container(cmd)) + end end end end @@ -150,21 +169,30 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base desc "remove_container [NAME]", "Remove accessory container from host" def remove_container(name) with_accessory(name) do |accessory| - on(accessory.host) { execute *accessory.remove_container } + on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} remove container"), verbosity: :debug + execute *accessory.remove_container + end end end desc "remove_image [NAME]", "Remove accessory image from host" def remove_image(name) with_accessory(name) do |accessory| - on(accessory.host) { execute *accessory.remove_image } + on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} remove image"), verbosity: :debug + execute *accessory.remove_image + end end end desc "remove_service_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host" def remove_service_directory(name) with_accessory(name) do |accessory| - on(accessory.host) { execute *accessory.remove_service_directory } + on(accessory.host) do + execute *MRSK.auditor.record("accessory #{name} remove service directory"), verbosity: :debug + execute *accessory.remove_service_directory + end end end diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index 7fdad85a..9d3e5b58 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -14,6 +14,8 @@ class Mrsk::Cli::App < Mrsk::Cli::Base MRSK.config.roles.each do |role| on(role.hosts) do |host| + execute *MRSK.auditor.record("app boot version #{version}"), verbosity: :debug + begin execute *MRSK.app.run(role: role.name) rescue SSHKit::Command::Failed => e @@ -33,12 +35,18 @@ class Mrsk::Cli::App < Mrsk::Cli::Base desc "start", "Start existing app on servers (use --version= to designate specific version)" def start - on(MRSK.hosts) { execute *MRSK.app.start, raise_on_non_zero_exit: false } + on(MRSK.hosts) do + execute *MRSK.auditor.record("app start version #{MRSK.version}"), verbosity: :debug + execute *MRSK.app.start, raise_on_non_zero_exit: false + end end desc "stop", "Stop app on servers" def stop - on(MRSK.hosts) { execute *MRSK.app.stop, raise_on_non_zero_exit: false } + on(MRSK.hosts) do + execute *MRSK.auditor.record("app stop"), verbosity: :debug + execute *MRSK.app.stop, raise_on_non_zero_exit: false + end end desc "details", "Display details about app containers" @@ -68,15 +76,22 @@ class Mrsk::Cli::App < Mrsk::Cli::Base when options[:reuse] say "Get current version of running container...", :magenta unless options[:version] using_version(options[:version] || current_running_version) do |version| - say "Launching command with version #{version} from existing container on #{MRSK.primary_host}...", :magenta - on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd)) } + say "Launching command with version #{version} from existing container...", :magenta + + on(MRSK.hosts) do |host| + execute *MRSK.auditor.record("app cmd '#{cmd}' with version #{version}"), verbosity: :debug + puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd)) + end end else say "Get most recent version available as an image...", :magenta unless options[:version] using_version(options[:version] || most_recent_version_available) do |version| - say "Launching command with version #{version} from new container on #{MRSK.primary_host}...", :magenta - on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.execute_in_new_container(cmd)) } + say "Launching command with version #{version} from new container...", :magenta + on(MRSK.hosts) do |host| + execute *MRSK.auditor.record("app cmd '#{cmd}' with version #{version}"), verbosity: :debug + puts_by_host host, capture_with_info(*MRSK.app.execute_in_new_container(cmd)) + end end end end @@ -134,17 +149,26 @@ class Mrsk::Cli::App < Mrsk::Cli::Base desc "remove_container [VERSION]", "Remove app container with given version from servers" def remove_container(version) - on(MRSK.hosts) { execute *MRSK.app.remove_container(version: version) } + on(MRSK.hosts) do + execute *MRSK.auditor.record("app remove container #{version}"), verbosity: :debug + execute *MRSK.app.remove_container(version: version) + end end desc "remove_containers", "Remove all app containers from servers" def remove_containers - on(MRSK.hosts) { execute *MRSK.app.remove_containers } + on(MRSK.hosts) do + execute *MRSK.auditor.record("app remove containers"), verbosity: :debug + execute *MRSK.app.remove_containers + end end desc "remove_images", "Remove all app images from servers" def remove_images - on(MRSK.hosts) { execute *MRSK.app.remove_images } + on(MRSK.hosts) do + execute *MRSK.auditor.record("app remove images"), verbosity: :debug + execute *MRSK.app.remove_images + end end desc "current_version", "Shows the version currently running" diff --git a/lib/mrsk/cli/build.rb b/lib/mrsk/cli/build.rb index 8a8e856d..2873dfed 100644 --- a/lib/mrsk/cli/build.rb +++ b/lib/mrsk/cli/build.rb @@ -30,7 +30,10 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base desc "pull", "Pull app image from the registry onto servers" def pull - on(MRSK.hosts) { execute *MRSK.builder.pull } + on(MRSK.hosts) do + execute *MRSK.auditor.record("build pull image #{MRSK.version}"), verbosity: :debug + execute *MRSK.builder.pull + end end desc "create", "Create a local build setup" diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index ca28ab72..dd570c50 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -70,6 +70,13 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base invoke "mrsk:cli:accessory:details", [ "all" ] end + desc "audit", "Show audit log from servers" + def audit + on(MRSK.hosts) do |host| + puts_by_host host, capture_with_info(*MRSK.auditor.reveal) + end + end + desc "config", "Show combined config" def config run_locally do diff --git a/lib/mrsk/cli/prune.rb b/lib/mrsk/cli/prune.rb index 37bb4f08..32bd7f04 100644 --- a/lib/mrsk/cli/prune.rb +++ b/lib/mrsk/cli/prune.rb @@ -9,11 +9,17 @@ class Mrsk::Cli::Prune < Mrsk::Cli::Base desc "images", "Prune unused images older than 30 days" def images - on(MRSK.hosts) { execute *MRSK.prune.images } + on(MRSK.hosts) do + execute *MRSK.auditor.record("prune images"), verbosity: :debug + execute *MRSK.prune.images + end end desc "containers", "Prune stopped containers for the service older than 3 days" def containers - on(MRSK.hosts) { execute *MRSK.prune.containers } + on(MRSK.hosts) do + execute *MRSK.auditor.record("prune containers"), verbosity: :debug + execute *MRSK.prune.containers + end end end diff --git a/lib/mrsk/cli/traefik.rb b/lib/mrsk/cli/traefik.rb index ca62bbd3..04cb108e 100644 --- a/lib/mrsk/cli/traefik.rb +++ b/lib/mrsk/cli/traefik.rb @@ -15,12 +15,18 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base desc "start", "Start existing Traefik on servers" def start - on(MRSK.traefik_hosts) { execute *MRSK.traefik.start, raise_on_non_zero_exit: false } + on(MRSK.traefik_hosts) do + execute *MRSK.auditor.record("traefik start"), verbosity: :debug + execute *MRSK.traefik.start, raise_on_non_zero_exit: false + end end desc "stop", "Stop Traefik on servers" def stop - on(MRSK.traefik_hosts) { execute *MRSK.traefik.stop, raise_on_non_zero_exit: false } + on(MRSK.traefik_hosts) do + execute *MRSK.auditor.record("traefik stop"), verbosity: :debug + execute *MRSK.traefik.stop, raise_on_non_zero_exit: false + end end desc "restart", "Restart Traefik on servers" @@ -67,11 +73,17 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base desc "remove_container", "Remove Traefik container from servers" def remove_container - on(MRSK.traefik_hosts) { execute *MRSK.traefik.remove_container } + on(MRSK.traefik_hosts) do + execute *MRSK.auditor.record("traefik remove container"), verbosity: :debug + execute *MRSK.traefik.remove_container + end end desc "remove_container", "Remove Traefik image from servers" def remove_image - on(MRSK.traefik_hosts) { execute *MRSK.traefik.remove_image } + on(MRSK.traefik_hosts) do + execute *MRSK.auditor.record("traefik remove image"), verbosity: :debug + execute *MRSK.traefik.remove_image + end end end diff --git a/lib/mrsk/commander.rb b/lib/mrsk/commander.rb index c8d929fd..3dd95941 100644 --- a/lib/mrsk/commander.rb +++ b/lib/mrsk/commander.rb @@ -3,6 +3,7 @@ require "active_support/core_ext/enumerable" require "mrsk/configuration" require "mrsk/commands/accessory" require "mrsk/commands/app" +require "mrsk/commands/auditor" require "mrsk/commands/builder" require "mrsk/commands/prune" require "mrsk/commands/traefik" @@ -77,6 +78,10 @@ class Mrsk::Commander Mrsk::Commands::Accessory.new(config, name: name) end + def auditor + @auditor ||= Mrsk::Commands::Auditor.new(config) + end + def with_verbosity(level) old_level = SSHKit.config.output_verbosity @@ -89,7 +94,7 @@ class Mrsk::Commander # Test-induced damage! def reset @config = @config_file = @destination = @version = nil - @app = @builder = @traefik = @registry = @prune = nil + @app = @builder = @traefik = @registry = @prune = @auditor = nil @verbosity = :info end diff --git a/lib/mrsk/commands/auditor.rb b/lib/mrsk/commands/auditor.rb new file mode 100644 index 00000000..28267463 --- /dev/null +++ b/lib/mrsk/commands/auditor.rb @@ -0,0 +1,31 @@ +require "active_support/core_ext/time/conversions" +require "mrsk/commands/base" + +class Mrsk::Commands::Auditor < Mrsk::Commands::Base + def record(line) + append \ + [ :echo, "'#{tags} #{line}'" ], + audit_log_file + end + + def reveal + [ :tail, "-n", 50, audit_log_file ] + end + + private + def audit_log_file + "mrsk-#{config.service}-audit.log" + end + + def tags + "[#{timestamp}] [#{performer}]" + end + + def performer + `whoami`.strip + end + + def timestamp + Time.now.to_fs(:db) + end +end diff --git a/lib/mrsk/commands/base.rb b/lib/mrsk/commands/base.rb index 44c1807f..9c0bd1c7 100644 --- a/lib/mrsk/commands/base.rb +++ b/lib/mrsk/commands/base.rb @@ -28,6 +28,10 @@ module Mrsk::Commands combine *commands, by: "|" end + def append(*commands) + combine *commands, by: ">>" + end + def xargs(command) [ :xargs, command ].flatten end From 5911914e95b365e10696f9244daa9979528aa17b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 20:48:21 +0100 Subject: [PATCH 34/36] Bump version for 0.5.1 --- Gemfile.lock | 2 +- lib/mrsk/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index c50e01e4..5591da14 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - mrsk (0.5.0) + mrsk (0.5.1) activesupport (>= 7.0) dotenv (~> 2.8) sshkit (~> 1.21) diff --git a/lib/mrsk/version.rb b/lib/mrsk/version.rb index df8e4919..99c8c23e 100644 --- a/lib/mrsk/version.rb +++ b/lib/mrsk/version.rb @@ -1,3 +1,3 @@ module Mrsk - VERSION = "0.5.0" + VERSION = "0.5.1" end From 94b28a1b29aba52822b60a02bd3c56009e6cd8ac Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 3 Feb 2023 20:53:33 +0100 Subject: [PATCH 35/36] Extract method --- lib/mrsk/commands/auditor.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/mrsk/commands/auditor.rb b/lib/mrsk/commands/auditor.rb index 28267463..951abaae 100644 --- a/lib/mrsk/commands/auditor.rb +++ b/lib/mrsk/commands/auditor.rb @@ -4,7 +4,7 @@ require "mrsk/commands/base" class Mrsk::Commands::Auditor < Mrsk::Commands::Base def record(line) append \ - [ :echo, "'#{tags} #{line}'" ], + [ :echo, tagged_line(line) ], audit_log_file end @@ -17,6 +17,10 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base "mrsk-#{config.service}-audit.log" end + def tagged_line(line) + "'#{tags} #{line}'" + end + def tags "[#{timestamp}] [#{performer}]" end From 539752e9bd56d752ad6d01e9044cdc7f47149881 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 3 Feb 2023 22:45:12 +0100 Subject: [PATCH 36/36] Load with Zeitwerk --- Gemfile.lock | 2 ++ bin/mrsk | 2 +- lib/mrsk.rb | 8 ++++++-- lib/mrsk/cli.rb | 4 ---- lib/mrsk/cli/accessory.rb | 2 -- lib/mrsk/cli/app.rb | 10 ++++------ lib/mrsk/cli/build.rb | 4 +--- lib/mrsk/cli/main.rb | 10 ---------- lib/mrsk/cli/prune.rb | 2 -- lib/mrsk/cli/registry.rb | 2 -- lib/mrsk/cli/server.rb | 2 -- lib/mrsk/cli/traefik.rb | 2 -- lib/mrsk/commander.rb | 11 +---------- lib/mrsk/commands/accessory.rb | 4 +--- lib/mrsk/commands/app.rb | 2 -- lib/mrsk/commands/auditor.rb | 1 - lib/mrsk/commands/builder.rb | 7 ------- lib/mrsk/commands/builder/base.rb | 2 -- lib/mrsk/commands/builder/multiarch.rb | 2 -- lib/mrsk/commands/builder/multiarch/remote.rb | 2 -- lib/mrsk/commands/builder/native.rb | 2 -- lib/mrsk/commands/builder/native/remote.rb | 2 -- lib/mrsk/commands/prune.rb | 1 - lib/mrsk/commands/registry.rb | 2 -- lib/mrsk/commands/traefik.rb | 2 -- lib/mrsk/configuration.rb | 4 ---- mrsk.gemspec | 1 + test/cli/cli_test_case.rb | 1 - test/commander_test.rb | 1 - test/commands/accessory_test.rb | 4 +--- test/commands/app_test.rb | 2 -- test/commands/builder_test.rb | 2 -- test/commands/registry_test.rb | 4 +--- test/commands/traefik_test.rb | 2 -- test/configuration/accessory_test.rb | 3 +-- test/configuration/role_test.rb | 9 ++++----- test/configuration_test.rb | 1 - test/test_helper.rb | 1 + 38 files changed, 25 insertions(+), 100 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 5591da14..850fb735 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,7 @@ PATH dotenv (~> 2.8) sshkit (~> 1.21) thor (~> 1.2) + zeitwerk (~> 2.5) GEM remote: https://rubygems.org/ @@ -91,6 +92,7 @@ PLATFORMS arm64-darwin-22 x86_64-darwin-20 x86_64-darwin-21 + x86_64-darwin-22 x86_64-linux DEPENDENCIES diff --git a/bin/mrsk b/bin/mrsk index d156bed1..dac9af62 100755 --- a/bin/mrsk +++ b/bin/mrsk @@ -4,7 +4,7 @@ Thread.report_on_exception = false require "dotenv/load" -require "mrsk/cli" +require "mrsk" begin Mrsk::Cli::Main.start(ARGV) diff --git a/lib/mrsk.rb b/lib/mrsk.rb index 3adf2bef..19b86051 100644 --- a/lib/mrsk.rb +++ b/lib/mrsk.rb @@ -1,5 +1,9 @@ module Mrsk end -require "mrsk/version" -require "mrsk/commander" +require "zeitwerk" + +loader = Zeitwerk::Loader.for_gem +loader.ignore("#{__dir__}/mrsk/sshkit_with_ext.rb") +loader.setup +loader.eager_load # We need all commands loaded. diff --git a/lib/mrsk/cli.rb b/lib/mrsk/cli.rb index 35fc8f44..62323076 100644 --- a/lib/mrsk/cli.rb +++ b/lib/mrsk/cli.rb @@ -1,9 +1,5 @@ -require "mrsk" - module Mrsk::Cli end # SSHKit uses instance eval, so we need a global const for ergonomics MRSK = Mrsk::Commander.new - -require "mrsk/cli/main" diff --git a/lib/mrsk/cli/accessory.rb b/lib/mrsk/cli/accessory.rb index 549ba8e0..5bf9ff9b 100644 --- a/lib/mrsk/cli/accessory.rb +++ b/lib/mrsk/cli/accessory.rb @@ -1,5 +1,3 @@ -require "mrsk/cli/base" - class Mrsk::Cli::Accessory < Mrsk::Cli::Base desc "boot [NAME]", "Boot accessory service on host (use NAME=all to boot all accessories)" def boot(name) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index 9d3e5b58..e4c71493 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -1,5 +1,3 @@ -require "mrsk/cli/base" - class Mrsk::Cli::App < Mrsk::Cli::Base desc "boot", "Boot app on servers (or reboot app if already running)" def boot @@ -40,7 +38,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base execute *MRSK.app.start, raise_on_non_zero_exit: false end end - + desc "stop", "Stop app on servers" def stop on(MRSK.hosts) do @@ -48,12 +46,12 @@ class Mrsk::Cli::App < Mrsk::Cli::Base execute *MRSK.app.stop, raise_on_non_zero_exit: false end end - + desc "details", "Display details about app containers" def details on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.info) } end - + desc "exec [CMD]", "Execute a custom command on servers" option :interactive, aliases: "-i", type: :boolean, default: false, desc: "Execute command over ssh for an interactive shell (use for console/bash)" option :reuse, type: :boolean, default: false, desc: "Reuse currently running container instead of starting a new one" @@ -110,7 +108,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base def current on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.current_container_id) } end - + 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" diff --git a/lib/mrsk/cli/build.rb b/lib/mrsk/cli/build.rb index 2873dfed..f52c8223 100644 --- a/lib/mrsk/cli/build.rb +++ b/lib/mrsk/cli/build.rb @@ -1,5 +1,3 @@ -require "mrsk/cli/base" - class Mrsk::Cli::Build < Mrsk::Cli::Base desc "deliver", "Deliver a newly built app image to servers" def deliver @@ -11,7 +9,7 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base def push cli = self - run_locally do + run_locally do begin MRSK.with_verbosity(:debug) { execute *MRSK.builder.push } rescue SSHKit::Command::Failed => e diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index dd570c50..c1b985c3 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -1,13 +1,3 @@ -require "mrsk/cli/base" - -require "mrsk/cli/accessory" -require "mrsk/cli/app" -require "mrsk/cli/build" -require "mrsk/cli/prune" -require "mrsk/cli/registry" -require "mrsk/cli/server" -require "mrsk/cli/traefik" - class Mrsk::Cli::Main < Mrsk::Cli::Base desc "setup", "Setup all accessories and deploy the app to servers" def setup diff --git a/lib/mrsk/cli/prune.rb b/lib/mrsk/cli/prune.rb index 32bd7f04..91e881ce 100644 --- a/lib/mrsk/cli/prune.rb +++ b/lib/mrsk/cli/prune.rb @@ -1,5 +1,3 @@ -require "mrsk/cli/base" - class Mrsk::Cli::Prune < Mrsk::Cli::Base desc "all", "Prune unused images and stopped containers" def all diff --git a/lib/mrsk/cli/registry.rb b/lib/mrsk/cli/registry.rb index 939e7c33..5342c32f 100644 --- a/lib/mrsk/cli/registry.rb +++ b/lib/mrsk/cli/registry.rb @@ -1,5 +1,3 @@ -require "mrsk/cli/base" - class Mrsk::Cli::Registry < Mrsk::Cli::Base desc "login", "Login to the registry locally and remotely" def login diff --git a/lib/mrsk/cli/server.rb b/lib/mrsk/cli/server.rb index f015d1d0..ce8ab2cd 100644 --- a/lib/mrsk/cli/server.rb +++ b/lib/mrsk/cli/server.rb @@ -1,5 +1,3 @@ -require "mrsk/cli/base" - class Mrsk::Cli::Server < Mrsk::Cli::Base desc "bootstrap", "Ensure Docker is installed on the servers" def bootstrap diff --git a/lib/mrsk/cli/traefik.rb b/lib/mrsk/cli/traefik.rb index 04cb108e..b77d16ce 100644 --- a/lib/mrsk/cli/traefik.rb +++ b/lib/mrsk/cli/traefik.rb @@ -1,5 +1,3 @@ -require "mrsk/cli/base" - class Mrsk::Cli::Traefik < Mrsk::Cli::Base desc "boot", "Boot Traefik on servers" def boot diff --git a/lib/mrsk/commander.rb b/lib/mrsk/commander.rb index 3dd95941..661f8a15 100644 --- a/lib/mrsk/commander.rb +++ b/lib/mrsk/commander.rb @@ -1,14 +1,5 @@ require "active_support/core_ext/enumerable" -require "mrsk/configuration" -require "mrsk/commands/accessory" -require "mrsk/commands/app" -require "mrsk/commands/auditor" -require "mrsk/commands/builder" -require "mrsk/commands/prune" -require "mrsk/commands/traefik" -require "mrsk/commands/registry" - class Mrsk::Commander attr_accessor :config_file, :destination, :verbosity, :version @@ -83,7 +74,7 @@ class Mrsk::Commander end - def with_verbosity(level) + def with_verbosity(level) old_level = SSHKit.config.output_verbosity SSHKit.config.output_verbosity = level yield diff --git a/lib/mrsk/commands/accessory.rb b/lib/mrsk/commands/accessory.rb index 6cc6bf5c..0e9c5c12 100644 --- a/lib/mrsk/commands/accessory.rb +++ b/lib/mrsk/commands/accessory.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/base" - class Mrsk::Commands::Accessory < Mrsk::Commands::Base attr_reader :accessory_config delegate :service_name, :image, :host, :port, :files, :directories, :env_args, :volume_args, :label_args, to: :accessory_config @@ -10,7 +8,7 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base end def run - docker :run, + docker :run, "--name", service_name, "-d", "--restart", "unless-stopped", diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index 37d4d00d..ae4fce97 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/base" - class Mrsk::Commands::App < Mrsk::Commands::Base def run(role: :web) role = config.role(role) diff --git a/lib/mrsk/commands/auditor.rb b/lib/mrsk/commands/auditor.rb index 951abaae..692844c8 100644 --- a/lib/mrsk/commands/auditor.rb +++ b/lib/mrsk/commands/auditor.rb @@ -1,5 +1,4 @@ require "active_support/core_ext/time/conversions" -require "mrsk/commands/base" class Mrsk::Commands::Auditor < Mrsk::Commands::Base def record(line) diff --git a/lib/mrsk/commands/builder.rb b/lib/mrsk/commands/builder.rb index e24768d1..70957afe 100644 --- a/lib/mrsk/commands/builder.rb +++ b/lib/mrsk/commands/builder.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/base" - class Mrsk::Commands::Builder < Mrsk::Commands::Base delegate :create, :remove, :push, :pull, :info, to: :target @@ -36,8 +34,3 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base @multiarch_remote ||= Mrsk::Commands::Builder::Multiarch::Remote.new(config) end 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/base.rb b/lib/mrsk/commands/builder/base.rb index 792ebee1..8e0003c7 100644 --- a/lib/mrsk/commands/builder/base.rb +++ b/lib/mrsk/commands/builder/base.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/base" - class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base delegate :argumentize, to: Mrsk::Utils diff --git a/lib/mrsk/commands/builder/multiarch.rb b/lib/mrsk/commands/builder/multiarch.rb index bc87a86f..9b1fcb19 100644 --- a/lib/mrsk/commands/builder/multiarch.rb +++ b/lib/mrsk/commands/builder/multiarch.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/builder/base" - class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base def create docker :buildx, :create, "--use", "--name", builder_name diff --git a/lib/mrsk/commands/builder/multiarch/remote.rb b/lib/mrsk/commands/builder/multiarch/remote.rb index b49de328..5ca10048 100644 --- a/lib/mrsk/commands/builder/multiarch/remote.rb +++ b/lib/mrsk/commands/builder/multiarch/remote.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/builder/multiarch" - class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Multiarch def create combine \ diff --git a/lib/mrsk/commands/builder/native.rb b/lib/mrsk/commands/builder/native.rb index 310ace07..18140eb3 100644 --- a/lib/mrsk/commands/builder/native.rb +++ b/lib/mrsk/commands/builder/native.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/builder/base" - class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base def create # No-op on native diff --git a/lib/mrsk/commands/builder/native/remote.rb b/lib/mrsk/commands/builder/native/remote.rb index d3089fdb..4a457309 100644 --- a/lib/mrsk/commands/builder/native/remote.rb +++ b/lib/mrsk/commands/builder/native/remote.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/builder/native" - class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native def create chain \ diff --git a/lib/mrsk/commands/prune.rb b/lib/mrsk/commands/prune.rb index 19451b67..dd9b14b8 100644 --- a/lib/mrsk/commands/prune.rb +++ b/lib/mrsk/commands/prune.rb @@ -1,4 +1,3 @@ -require "mrsk/commands/base" require "active_support/duration" require "active_support/core_ext/numeric/time" diff --git a/lib/mrsk/commands/registry.rb b/lib/mrsk/commands/registry.rb index 8d05b0cb..4e9b1ce4 100644 --- a/lib/mrsk/commands/registry.rb +++ b/lib/mrsk/commands/registry.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/base" - class Mrsk::Commands::Registry < Mrsk::Commands::Base delegate :registry, to: :config diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index f8f7aee7..dd0715cf 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -1,5 +1,3 @@ -require "mrsk/commands/base" - class Mrsk::Commands::Traefik < Mrsk::Commands::Base def run docker :run, "--name traefik", diff --git a/lib/mrsk/configuration.rb b/lib/mrsk/configuration.rb index 23723214..6e7dff38 100644 --- a/lib/mrsk/configuration.rb +++ b/lib/mrsk/configuration.rb @@ -3,7 +3,6 @@ require "active_support/core_ext/string/inquiry" require "active_support/core_ext/module/delegation" require "pathname" require "erb" -require "mrsk/utils" class Mrsk::Configuration delegate :service, :image, :servers, :env, :labels, :registry, :builder, to: :raw_config, allow_nil: true @@ -171,6 +170,3 @@ class Mrsk::Configuration raw_config.servers.is_a?(Array) ? [ "web" ] : raw_config.servers.keys.sort end end - -require "mrsk/configuration/role" -require "mrsk/configuration/accessory" diff --git a/mrsk.gemspec b/mrsk.gemspec index 78952cb6..7cbfb888 100644 --- a/mrsk.gemspec +++ b/mrsk.gemspec @@ -16,4 +16,5 @@ Gem::Specification.new do |spec| spec.add_dependency "sshkit", "~> 1.21" spec.add_dependency "thor", "~> 1.2" spec.add_dependency "dotenv", "~> 2.8" + spec.add_dependency "zeitwerk", "~> 2.5" end diff --git a/test/cli/cli_test_case.rb b/test/cli/cli_test_case.rb index c0cecbdd..f7a0ad07 100644 --- a/test/cli/cli_test_case.rb +++ b/test/cli/cli_test_case.rb @@ -1,6 +1,5 @@ require "test_helper" require "active_support/testing/stream" -require "mrsk/cli" class CliTestCase < ActiveSupport::TestCase include ActiveSupport::Testing::Stream diff --git a/test/commander_test.rb b/test/commander_test.rb index 5b3ed297..89ac4627 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -1,5 +1,4 @@ require "test_helper" -require "mrsk/commander" class CommanderTest < ActiveSupport::TestCase setup do diff --git a/test/commands/accessory_test.rb b/test/commands/accessory_test.rb index 21886440..8a68cbb7 100644 --- a/test/commands/accessory_test.rb +++ b/test/commands/accessory_test.rb @@ -1,10 +1,8 @@ require "test_helper" -require "mrsk/configuration" -require "mrsk/commands/accessory" class CommandsAccessoryTest < ActiveSupport::TestCase setup do - @config = { + @config = { service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ], accessories: { diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index c888bcff..2cbc0eec 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -1,6 +1,4 @@ require "test_helper" -require "mrsk/configuration" -require "mrsk/commands/app" class CommandsAppTest < ActiveSupport::TestCase setup do diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index 14aee153..dfd43313 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -1,6 +1,4 @@ require "test_helper" -require "mrsk/configuration" -require "mrsk/commands/builder" class CommandsBuilderTest < ActiveSupport::TestCase setup do diff --git a/test/commands/registry_test.rb b/test/commands/registry_test.rb index 390f2208..15a2c312 100755 --- a/test/commands/registry_test.rb +++ b/test/commands/registry_test.rb @@ -1,10 +1,8 @@ require "test_helper" -require "mrsk/configuration" -require "mrsk/commands/registry" class CommandsRegistryTest < ActiveSupport::TestCase setup do - @config = { service: "app", + @config = { service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret", diff --git a/test/commands/traefik_test.rb b/test/commands/traefik_test.rb index 8f204ea5..b529b3bc 100644 --- a/test/commands/traefik_test.rb +++ b/test/commands/traefik_test.rb @@ -1,6 +1,4 @@ require "test_helper" -require "mrsk/configuration" -require "mrsk/commands/traefik" class CommandsTraefikTest < ActiveSupport::TestCase setup do diff --git a/test/configuration/accessory_test.rb b/test/configuration/accessory_test.rb index cee4511c..7f90e82a 100644 --- a/test/configuration/accessory_test.rb +++ b/test/configuration/accessory_test.rb @@ -1,5 +1,4 @@ require "test_helper" -require "mrsk/configuration" class ConfigurationAccessoryTest < ActiveSupport::TestCase setup do @@ -66,7 +65,7 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase test "missing host" do @deploy[:accessories]["mysql"]["host"] = nil @config = Mrsk::Configuration.new(@deploy) - + assert_raises(ArgumentError) do @config.accessory(:mysql).host end diff --git a/test/configuration/role_test.rb b/test/configuration/role_test.rb index 5c67b798..52970395 100644 --- a/test/configuration/role_test.rb +++ b/test/configuration/role_test.rb @@ -1,5 +1,4 @@ require "test_helper" -require "mrsk/configuration" class ConfigurationRoleTest < ActiveSupport::TestCase setup do @@ -63,7 +62,7 @@ class ConfigurationRoleTest < ActiveSupport::TestCase end test "default traefik label on non-web role" do - config = Mrsk::Configuration.new(@deploy_with_roles.tap { |c| + config = Mrsk::Configuration.new(@deploy_with_roles.tap { |c| c[:servers]["beta"] = { "traefik" => "true", "hosts" => [ "1.1.1.5" ] } }) @@ -97,7 +96,7 @@ class ConfigurationRoleTest < ActiveSupport::TestCase ENV["REDIS_PASSWORD"] = "secret456" ENV["DB_PASSWORD"] = "secret123" - + assert_equal ["-e", "REDIS_PASSWORD=secret456", "-e", "DB_PASSWORD=secret123", "-e", "REDIS_URL=redis://a/b", "-e", "WEB_CONCURRENCY=4"], @config_with_roles.role(:workers).env_args ensure ENV["REDIS_PASSWORD"] = nil @@ -116,7 +115,7 @@ class ConfigurationRoleTest < ActiveSupport::TestCase } ENV["DB_PASSWORD"] = "secret123" - + assert_equal ["-e", "DB_PASSWORD=secret123", "-e", "REDIS_URL=redis://a/b", "-e", "WEB_CONCURRENCY=4"], @config_with_roles.role(:workers).env_args ensure ENV["DB_PASSWORD"] = nil @@ -133,7 +132,7 @@ class ConfigurationRoleTest < ActiveSupport::TestCase } ENV["REDIS_PASSWORD"] = "secret456" - + assert_equal ["-e", "REDIS_PASSWORD=secret456", "-e", "REDIS_URL=redis://a/b", "-e", "WEB_CONCURRENCY=4"], @config_with_roles.role(:workers).env_args ensure ENV["REDIS_PASSWORD"] = nil diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 1cd45fcb..7cbebabd 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -1,5 +1,4 @@ require "test_helper" -require "mrsk/configuration" class ConfigurationTest < ActiveSupport::TestCase setup do diff --git a/test/test_helper.rb b/test/test_helper.rb index 732b5af4..4c381df2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,6 +5,7 @@ require "debug" require "mocha/minitest" # using #stubs that can alter returns require "minitest/autorun" # using #stub that take args require "sshkit" +require "mrsk" ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(STDOUT) if ENV["VERBOSE"]