From 88b5e52b9ff7468a2c4c4e6b1d58b046d9d4ce29 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 1 Feb 2023 13:28:29 +0100 Subject: [PATCH] Exec over ssh with accessory --- lib/mrsk/cli/accessory.rb | 20 ++++++++++++++++---- lib/mrsk/commands/accessory.rb | 8 ++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/mrsk/cli/accessory.rb b/lib/mrsk/cli/accessory.rb index 144dc9c7..fd709d1e 100644 --- a/lib/mrsk/cli/accessory.rb +++ b/lib/mrsk/cli/accessory.rb @@ -83,11 +83,23 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base 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" + option :method, aliases: "-m", default: "exec", desc: "Execution method: [exec] perform inside container / [run] perform in new container / [ssh] perform over ssh" def exec(name, cmd) - with_accessory(name) do |accessory| - runner = options[:run] ? :run_exec : :exec - on(accessory.host) { |host| puts_by_host host, capture_with_info(*accessory.send(runner, 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 #{accessory.host}" + exec accessory.exec_over_ssh(cmd, host: accessory.host) + end + else + on(accessory.host) { puts capture_with_info(*accessory.send(runner, cmd) } end end diff --git a/lib/mrsk/commands/accessory.rb b/lib/mrsk/commands/accessory.rb index 0f8b0e9c..a6a12158 100644 --- a/lib/mrsk/commands/accessory.rb +++ b/lib/mrsk/commands/accessory.rb @@ -63,6 +63,10 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base *command end + def exec_over_ssh(*command, host:) + run_over_ssh run_exec(*command, interactive: true).join(" "), host: host + end + def bash(host:) exec_over_ssh "bash", host: host end @@ -94,10 +98,6 @@ 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