Bring accessory execution in line with app

This commit is contained in:
David Heinemeier Hansson
2023-02-03 17:24:36 +01:00
parent 1fb2c71f65
commit b964e04f93
2 changed files with 32 additions and 30 deletions

View File

@@ -82,39 +82,27 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
end end
end end
desc "exec [NAME] [CMD]", "Execute a custom command on accessory host" desc "exec [NAME] [CMD]", "Execute a custom command on servers"
option :method, aliases: "-m", default: "exec", desc: "Execution method: [exec] perform inside 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(name, cmd) 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| with_accessory(name) do |accessory|
if runner.exec_over_ssh? || runner.run_over_ssh? case
run_locally do when options[:interactive] && options[:reuse]
info "Launching command on #{accessory.host}" say "Launching interactive command with via SSH from existing container...", :magenta
exec accessory.send(runner, cmd) run_locally { exec accessory.execute_in_existing_container_over_ssh(cmd) }
end
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 else
on(accessory.host) do say "Launching command from new container...", :magenta
info "Launching command on #{accessory.host}" on(accessory.host) { capture_with_info(*accessory.execute_in_new_container(cmd)) }
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
end end
end end
end end

View File

@@ -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") 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 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 private
def run_command(*command) def run_command(*command)
stdouted { Mrsk::Cli::Accessory.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) } stdouted { Mrsk::Cli::Accessory.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }