From fb58fc0ba6e6aeb27d3c426a8041cf0d26878bfe Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Thu, 11 Jan 2024 23:01:38 -0700 Subject: [PATCH 1/3] Add in a server exec command for running ad-hoc commands directly on the server --- lib/kamal/cli/app.rb | 2 +- lib/kamal/cli/server.rb | 22 ++++++++++++++++++++++ test/cli/server_test.rb | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/kamal/cli/app.rb b/lib/kamal/cli/app.rb index fae607fc..6dbfba26 100644 --- a/lib/kamal/cli/app.rb +++ b/lib/kamal/cli/app.rb @@ -106,7 +106,7 @@ class Kamal::Cli::App < Kamal::Cli::Base end end - desc "exec [CMD]", "Execute a custom command on servers (use --help to show options)" + desc "exec [CMD]", "Execute a custom command on servers within the app 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(cmd) diff --git a/lib/kamal/cli/server.rb b/lib/kamal/cli/server.rb index 6dcc6b02..58eed4af 100644 --- a/lib/kamal/cli/server.rb +++ b/lib/kamal/cli/server.rb @@ -1,4 +1,26 @@ class Kamal::Cli::Server < Kamal::Cli::Base + desc "exec", "Run a custom command on the server(use --help to show options)" + option :interactive, type: :boolean, aliases: "-i", default: false, desc: "Run the command interactively(use for console/bash)" + def exec(cmd) + hosts = KAMAL.hosts | KAMAL.accessory_hosts + + case + when options[:interactive] + host = hosts.first + + say "Running '#{cmd}' on #{host} interactively...", :magenta + + run_locally { exec KAMAL.server.run_over_ssh(cmd, host: host) } + else + say "Running '#{cmd}' on #{hosts.join(', ')}...", :magenta + + on(hosts) do |host| + execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{host}"), verbosity: :debug + puts_by_host host, capture_with_info(cmd) + end + end + end + desc "bootstrap", "Set up Docker to run Kamal apps" def bootstrap missing = [] diff --git a/test/cli/server_test.rb b/test/cli/server_test.rb index 1c8a2607..bd34c726 100644 --- a/test/cli/server_test.rb +++ b/test/cli/server_test.rb @@ -1,6 +1,20 @@ require_relative "cli_test_case" class CliServerTest < CliTestCase + test "running a command with exec" do + SSHKit::Backend::Abstract.any_instance.stubs(:capture) + .with("date", verbosity: 1) + .returns("Today") + + hosts = "1.1.1.1".."1.1.1.4" + run_command("exec", "date").tap do |output| + hosts.map do |host| + assert_match "Running 'date' on #{hosts.to_a.join(', ')}...", output + assert_match "App Host: #{host}\nToday", output + end + end + end + test "bootstrap already installed" do SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(true).at_least_once SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:mkdir, "-p", ".kamal").returns("").at_least_once From 8a4f7163bb8ac6e29ccc7394138c1d15d4d4d531 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Mon, 20 May 2024 11:15:14 -0700 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Donal McBreen --- lib/kamal/cli/server.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kamal/cli/server.rb b/lib/kamal/cli/server.rb index 58eed4af..c89bee61 100644 --- a/lib/kamal/cli/server.rb +++ b/lib/kamal/cli/server.rb @@ -1,12 +1,12 @@ class Kamal::Cli::Server < Kamal::Cli::Base - desc "exec", "Run a custom command on the server(use --help to show options)" + desc "exec", "Run a custom command on the server (use --help to show options)" option :interactive, type: :boolean, aliases: "-i", default: false, desc: "Run the command interactively(use for console/bash)" def exec(cmd) hosts = KAMAL.hosts | KAMAL.accessory_hosts case when options[:interactive] - host = hosts.first + host = KAMAL.primary_host say "Running '#{cmd}' on #{host} interactively...", :magenta From 060e5d2027509876b8b6e712fc3654bf091f0b3b Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Tue, 21 May 2024 08:22:20 +0100 Subject: [PATCH 3/3] Update lib/kamal/cli/server.rb Co-authored-by: Sijawusz Pur Rahnama --- lib/kamal/cli/server.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kamal/cli/server.rb b/lib/kamal/cli/server.rb index c89bee61..9b5dbfc1 100644 --- a/lib/kamal/cli/server.rb +++ b/lib/kamal/cli/server.rb @@ -1,6 +1,6 @@ class Kamal::Cli::Server < Kamal::Cli::Base desc "exec", "Run a custom command on the server (use --help to show options)" - option :interactive, type: :boolean, aliases: "-i", default: false, desc: "Run the command interactively(use for console/bash)" + option :interactive, type: :boolean, aliases: "-i", default: false, desc: "Run the command interactively (use for console/bash)" def exec(cmd) hosts = KAMAL.hosts | KAMAL.accessory_hosts