From 83fd2a051d495716db30c2515f16fb14b5a45976 Mon Sep 17 00:00:00 2001 From: Matteo Giaccone Date: Thu, 21 Nov 2024 11:06:36 +0100 Subject: [PATCH] Add support for exec output in accessories When running accessory exec now you get the output from the hosts. Also you can pass commands with arguments and it will work e.g.: cat yourfilename --- lib/kamal/cli/accessory.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/kamal/cli/accessory.rb b/lib/kamal/cli/accessory.rb index 6c51c774d..d707820aa 100644 --- a/lib/kamal/cli/accessory.rb +++ b/lib/kamal/cli/accessory.rb @@ -112,14 +112,15 @@ def details(name) end end - desc "exec [NAME] [CMD]", "Execute a custom command on servers (use --help to show options)" + desc "exec [NAME] [CMD...]", "Execute a custom command on servers within the accessory container (use --help to show options)" 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) + cmd = Kamal::Utils.join_commands(cmd) with_accessory(name) do |accessory, hosts| case when options[:interactive] && options[:reuse] - say "Launching interactive command with via SSH from existing container...", :magenta + say "Launching interactive command via SSH from existing container...", :magenta run_locally { exec accessory.execute_in_existing_container_over_ssh(cmd) } when options[:interactive] @@ -128,16 +129,16 @@ def exec(name, cmd) when options[:reuse] say "Launching command from existing container...", :magenta - on(hosts) do + on(hosts) do |host| execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug - capture_with_info(*accessory.execute_in_existing_container(cmd)) + puts_by_host host, capture_with_info(*accessory.execute_in_existing_container(cmd)) end else say "Launching command from new container...", :magenta - on(hosts) do + on(hosts) do |host| execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug - capture_with_info(*accessory.execute_in_new_container(cmd)) + puts_by_host host, capture_with_info(*accessory.execute_in_new_container(cmd)) end end end