Add exec and bash commands to accessories

This commit is contained in:
David Heinemeier Hansson
2023-01-23 12:45:20 +01:00
parent 747e0fd4c2
commit ddf52da132
2 changed files with 46 additions and 0 deletions

View File

@@ -59,6 +59,25 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
end
end
desc "exec [NAME] [CMD]", "Execute a custom command on accessory host"
option :run, type: :boolean, default: false, desc: "Start a new container to run the command rather than reusing existing"
def exec(name, cmd)
accessory = MRSK.accessory(name)
runner = options[:run] ? :run_exec : :exec
on(accessory.host) { |host| puts_by_host host, capture_with_info(*accessory.send(runner, cmd)) }
end
desc "bash [NAME]", "Start a bash session on primary host (or specific host set by --hosts)"
def bash(name)
accessory = MRSK.accessory(name)
run_locally do
info "Launching bash session on #{accessory.host}"
exec accessory.bash(host: accessory.host)
end
end
desc "logs [NAME]", "Show log lines from accessory on host"
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"

View File

@@ -50,6 +50,29 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
if Pathname.new(local).exist?
[ :mkdir, "-p", Pathname.new(remote).dirname.to_s ]
else
def exec(*command, interactive: false)
docker :exec,
("-it" if interactive),
*env_args,
*volume_args,
service_name,
*command
end
def run_exec(*command, interactive: false)
docker :run,
("-it" if interactive),
"--rm",
*env_args,
*volume_args,
image,
*command
end
def bash(host:)
exec_over_ssh "bash", host: host
end
raise "Missing file: #{local}"
end
end
@@ -67,6 +90,10 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
end
private
def exec_over_ssh(*command, host:)
run_over_ssh run_exec(*command, interactive: true).join(" "), host: host
end
def service_filter
[ "--filter", "label=service=#{service_name}" ]
end