Allow exec to run in its own container

This commit is contained in:
David Heinemeier Hansson
2023-01-15 13:51:08 +01:00
parent 89161b66a1
commit 8e58a9385a
3 changed files with 22 additions and 3 deletions

View File

@@ -41,11 +41,14 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
desc "exec [CMD]", "Execute a custom task on servers passed in as CMD='bin/rake some:task'"
option :once, type: :boolean, default: false
option :run, type: :boolean, default: false
def exec(cmd)
runner = options[:run] ? :run_exec : :exec
if options[:once]
on(MRSK.config.primary_host) { puts capture(*MRSK.app.exec(cmd), verbosity: Logger::INFO) }
on(MRSK.config.primary_host) { puts capture(*MRSK.app.send(runner, cmd), verbosity: Logger::INFO) }
else
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.exec(cmd), verbosity: Logger::INFO) + "\n\n" }
on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.send(runner, cmd), verbosity: Logger::INFO) + "\n\n" }
end
end

View File

@@ -40,6 +40,16 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
*command
end
def run_exec(*command)
docker :run,
"-it",
"--rm",
*rails_master_key_arg,
*config.env_args,
config.absolute_image,
*command
end
def console(host: config.primary_host)
"ssh -t #{config.ssh_user}@#{host} '#{exec("bin/rails", "c", interactive: true).join(" ")}'"
end

View File

@@ -15,4 +15,10 @@ class AppCommandTest < ActiveSupport::TestCase
assert_equal \
[:docker, :run, "-d", "--restart unless-stopped", "--name", "app-123", "-e", "RAILS_MASTER_KEY=456", "--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:123"], @app.run
end
test "run with" do
assert_equal \
[ :docker, :run, "-it", "--rm", "-e", "RAILS_MASTER_KEY=456", "dhh/app:123", "bin/rails", "db:setup" ],
@app.run_exec("bin/rails", "db:setup")
end
end