From 792aa1dbdfd0e5e5edf6a9c765dcfcee77fc8a12 Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 1 Oct 2023 13:59:48 +0200 Subject: [PATCH 1/2] Add SSH port option --- lib/kamal/configuration/ssh.rb | 6 +++++- test/configuration/ssh_test.rb | 3 +++ test/configuration_test.rb | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/kamal/configuration/ssh.rb b/lib/kamal/configuration/ssh.rb index 57a740b70..b96301027 100644 --- a/lib/kamal/configuration/ssh.rb +++ b/lib/kamal/configuration/ssh.rb @@ -9,6 +9,10 @@ def user config.fetch("user", "root") end + def port + config.fetch("port", 22) + end + def proxy if (proxy = config["proxy"]) Net::SSH::Proxy::Jump.new(proxy.include?("@") ? proxy : "root@#{proxy}") @@ -18,7 +22,7 @@ def proxy end def options - { user: user, proxy: proxy, logger: logger, keepalive: true, keepalive_interval: 30 }.compact + { user: user, port: port, proxy: proxy, logger: logger, keepalive: true, keepalive_interval: 30 }.compact end def to_h diff --git a/test/configuration/ssh_test.rb b/test/configuration/ssh_test.rb index cb3d5f5c9..438a0f453 100644 --- a/test/configuration/ssh_test.rb +++ b/test/configuration/ssh_test.rb @@ -22,6 +22,9 @@ class ConfigurationSshTest < ActiveSupport::TestCase config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "log_level" => "debug" }) }) assert_equal 0, config.ssh.options[:logger].level + + config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "port" => 2222 }) }) + assert_equal 2222, config.ssh.options[:port] end test "ssh options with proxy host" do diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 5b9f4cc9b..e48659ed7 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -227,7 +227,7 @@ class ConfigurationTest < ActiveSupport::TestCase :repository=>"dhh/app", :absolute_image=>"dhh/app:missing", :service_with_version=>"app-missing", - :ssh_options=>{ :user=>"root", log_level: :fatal, keepalive: true, keepalive_interval: 30 }, + :ssh_options=>{ :user=>"root", port: 22, log_level: :fatal, keepalive: true, keepalive_interval: 30 }, :sshkit=>{}, :volume_args=>["--volume", "/local/path:/container/path"], :builder=>{}, From 2d86d4f7cc6f894f3d2e1f42d07543c1c698f32b Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 1 Oct 2023 14:13:26 +0200 Subject: [PATCH 2/2] Add SSH port to `run_over_ssh` --- lib/kamal/commands/base.rb | 2 +- test/cli/accessory_test.rb | 2 +- test/cli/app_test.rb | 6 +++--- test/cli/traefik_test.rb | 2 +- test/commands/accessory_test.rb | 2 +- test/commands/app_test.rb | 17 +++++++++++------ test/commands/traefik_test.rb | 4 ++-- test/integration/main_test.rb | 2 +- 8 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/kamal/commands/base.rb b/lib/kamal/commands/base.rb index 7552a92dc..3ce9b325d 100644 --- a/lib/kamal/commands/base.rb +++ b/lib/kamal/commands/base.rb @@ -18,7 +18,7 @@ def run_over_ssh(*command, host:) elsif config.ssh.proxy && config.ssh.proxy.is_a?(Net::SSH::Proxy::Command) cmd << " -o ProxyCommand='#{config.ssh.proxy.command_line_template}'" end - cmd << " -t #{config.ssh.user}@#{host} '#{command.join(" ")}'" + cmd << " -t #{config.ssh.user}@#{host} -p #{config.ssh.port} '#{command.join(" ")}'" end end diff --git a/test/cli/accessory_test.rb b/test/cli/accessory_test.rb index 5ecf80389..786238083 100644 --- a/test/cli/accessory_test.rb +++ b/test/cli/accessory_test.rb @@ -97,7 +97,7 @@ class CliAccessoryTest < CliTestCase test "logs with follow" do SSHKit::Backend::Abstract.any_instance.stubs(:exec) - .with("ssh -t root@1.1.1.3 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'") + .with("ssh -t root@1.1.1.3 -p 22 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'") assert_match "docker logs app-mysql --timestamps --tail 10 --follow 2>&1", run_command("logs", "mysql", "--follow") end diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index fd1064213..206d85249 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -172,7 +172,7 @@ class CliAppTest < CliTestCase test "exec interactive" do SSHKit::Backend::Abstract.any_instance.expects(:exec) - .with("ssh -t root@1.1.1.1 'docker run -it --rm --env-file .kamal/env/roles/app-web.env dhh/app:latest ruby -v'") + .with("ssh -t root@1.1.1.1 -p 22 'docker run -it --rm --env-file .kamal/env/roles/app-web.env dhh/app:latest ruby -v'") run_command("exec", "-i", "ruby -v").tap do |output| assert_match "Get most recent version available as an image...", output assert_match "Launching interactive command with version latest via SSH from new container on 1.1.1.1...", output @@ -181,7 +181,7 @@ class CliAppTest < CliTestCase test "exec interactive with reuse" do SSHKit::Backend::Abstract.any_instance.expects(:exec) - .with("ssh -t root@1.1.1.1 'docker exec -it app-web-999 ruby -v'") + .with("ssh -t root@1.1.1.1 -p 22 'docker exec -it app-web-999 ruby -v'") run_command("exec", "-i", "--reuse", "ruby -v").tap do |output| assert_match "Get current version of running container...", output assert_match "Running docker ps --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --latest --format \"{{.Names}}\" | while read line; do echo ${line#app-web-}; done on 1.1.1.1", output @@ -210,7 +210,7 @@ class CliAppTest < CliTestCase test "logs with follow" do SSHKit::Backend::Abstract.any_instance.stubs(:exec) - .with("ssh -t root@1.1.1.1 'docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --latest | xargs docker logs --timestamps --tail 10 --follow 2>&1'") + .with("ssh -t root@1.1.1.1 -p 22 'docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --latest | xargs docker logs --timestamps --tail 10 --follow 2>&1'") assert_match "docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --latest | xargs docker logs --timestamps --tail 10 --follow 2>&1", run_command("logs", "--follow") end diff --git a/test/cli/traefik_test.rb b/test/cli/traefik_test.rb index 6c6fbf641..aadbd228f 100644 --- a/test/cli/traefik_test.rb +++ b/test/cli/traefik_test.rb @@ -64,7 +64,7 @@ class CliTraefikTest < CliTestCase test "logs with follow" do SSHKit::Backend::Abstract.any_instance.stubs(:exec) - .with("ssh -t root@1.1.1.1 'docker logs traefik --timestamps --tail 10 --follow 2>&1'") + .with("ssh -t root@1.1.1.1 -p 22 'docker logs traefik --timestamps --tail 10 --follow 2>&1'") assert_match "docker logs traefik --timestamps --tail 10 --follow", run_command("logs", "--follow") end diff --git a/test/commands/accessory_test.rb b/test/commands/accessory_test.rb index 2825f3de0..687c7fe54 100644 --- a/test/commands/accessory_test.rb +++ b/test/commands/accessory_test.rb @@ -128,7 +128,7 @@ class CommandsAccessoryTest < ActiveSupport::TestCase test "follow logs" do assert_equal \ - "ssh -t root@1.1.1.5 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'", + "ssh -t root@1.1.1.5 -p 22 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'", new_command(:mysql).follow_logs end diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index 13abad782..b459d222c 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -190,32 +190,37 @@ class CommandsAppTest < ActiveSupport::TestCase end test "run over ssh" do - assert_equal "ssh -t root@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + assert_equal "ssh -t root@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") end test "run over ssh with custom user" do @config[:ssh] = { "user" => "app" } - assert_equal "ssh -t app@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + assert_equal "ssh -t app@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + end + + test "run over ssh with custom port" do + @config[:ssh] = { "port" => "2222" } + assert_equal "ssh -t root@1.1.1.1 -p 2222 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") end test "run over ssh with proxy" do @config[:ssh] = { "proxy" => "2.2.2.2" } - assert_equal "ssh -J root@2.2.2.2 -t root@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + assert_equal "ssh -J root@2.2.2.2 -t root@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") end test "run over ssh with proxy user" do @config[:ssh] = { "proxy" => "app@2.2.2.2" } - assert_equal "ssh -J app@2.2.2.2 -t root@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + assert_equal "ssh -J app@2.2.2.2 -t root@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") end test "run over ssh with custom user with proxy" do @config[:ssh] = { "user" => "app", "proxy" => "2.2.2.2" } - assert_equal "ssh -J root@2.2.2.2 -t app@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + assert_equal "ssh -J root@2.2.2.2 -t app@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") end test "run over ssh with proxy_command" do @config[:ssh] = { "proxy_command" => "ssh -W %h:%p user@proxy-server" } - assert_equal "ssh -o ProxyCommand='ssh -W %h:%p user@proxy-server' -t root@1.1.1.1 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + assert_equal "ssh -o ProxyCommand='ssh -W %h:%p user@proxy-server' -t root@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") end test "current_running_container_id" do diff --git a/test/commands/traefik_test.rb b/test/commands/traefik_test.rb index 7dfcff97a..2d97fb6cb 100644 --- a/test/commands/traefik_test.rb +++ b/test/commands/traefik_test.rb @@ -167,13 +167,13 @@ class CommandsTraefikTest < ActiveSupport::TestCase test "traefik follow logs" do assert_equal \ - "ssh -t root@1.1.1.1 'docker logs traefik --timestamps --tail 10 --follow 2>&1'", + "ssh -t root@1.1.1.1 -p 22 'docker logs traefik --timestamps --tail 10 --follow 2>&1'", new_command.follow_logs(host: @config[:servers].first) end test "traefik follow logs with grep hello!" do assert_equal \ - "ssh -t root@1.1.1.1 'docker logs traefik --timestamps --tail 10 --follow 2>&1 | grep \"hello!\"'", + "ssh -t root@1.1.1.1 -p 22 'docker logs traefik --timestamps --tail 10 --follow 2>&1 | grep \"hello!\"'", new_command.follow_logs(host: @config[:servers].first, grep: 'hello!') end diff --git a/test/integration/main_test.rb b/test/integration/main_test.rb index 1171c3de0..a0364fb26 100644 --- a/test/integration/main_test.rb +++ b/test/integration/main_test.rb @@ -54,7 +54,7 @@ class MainTest < IntegrationTest assert_equal "registry:4443/app:#{version}", config[:absolute_image] assert_equal "app-#{version}", config[:service_with_version] assert_equal [], config[:volume_args] - assert_equal({ user: "root", keepalive: true, keepalive_interval: 30, log_level: :fatal }, config[:ssh_options]) + assert_equal({ user: "root", port: 22, keepalive: true, keepalive_interval: 30, log_level: :fatal }, config[:ssh_options]) assert_equal({ "multiarch" => false, "args" => { "COMMIT_SHA" => version } }, config[:builder]) assert_equal [ "--log-opt", "max-size=\"10m\"" ], config[:logging] assert_equal({ "path" => "/up", "port" => 3000, "max_attempts" => 7, "exposed_port" => 3999, "cord"=>"/tmp/kamal-cord", "log_lines" => 50, "cmd"=>"wget -qO- http://localhost > /dev/null || exit 1" }, config[:healthcheck])