Skip to content

Commit

Permalink
Merge pull request #508 from leonvogt/ssh-port-option
Browse files Browse the repository at this point in the history
Configurable SSH port
  • Loading branch information
djmb authored Nov 6, 2023
2 parents 8f53104 + 2d86d4f commit da16144
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/kamal/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion lib/kamal/configuration/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/cli/accessory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class CliAccessoryTest < CliTestCase

test "logs with follow" do
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
.with("ssh -t [email protected] 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'")
.with("ssh -t [email protected] -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
Expand Down
6 changes: 3 additions & 3 deletions test/cli/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class CliAppTest < CliTestCase

test "exec interactive" do
SSHKit::Backend::Abstract.any_instance.expects(:exec)
.with("ssh -t [email protected] 'docker run -it --rm --env-file .kamal/env/roles/app-web.env dhh/app:latest ruby -v'")
.with("ssh -t [email protected] -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
Expand All @@ -181,7 +181,7 @@ class CliAppTest < CliTestCase

test "exec interactive with reuse" do
SSHKit::Backend::Abstract.any_instance.expects(:exec)
.with("ssh -t [email protected] 'docker exec -it app-web-999 ruby -v'")
.with("ssh -t [email protected] -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
Expand Down Expand Up @@ -210,7 +210,7 @@ class CliAppTest < CliTestCase

test "logs with follow" do
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
.with("ssh -t [email protected] '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 [email protected] -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
Expand Down
2 changes: 1 addition & 1 deletion test/cli/traefik_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CliTraefikTest < CliTestCase

test "logs with follow" do
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
.with("ssh -t [email protected] 'docker logs traefik --timestamps --tail 10 --follow 2>&1'")
.with("ssh -t [email protected] -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
Expand Down
2 changes: 1 addition & 1 deletion test/commands/accessory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class CommandsAccessoryTest < ActiveSupport::TestCase

test "follow logs" do
assert_equal \
"ssh -t [email protected] 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'",
"ssh -t [email protected] -p 22 'docker logs app-mysql --timestamps --tail 10 --follow 2>&1'",
new_command(:mysql).follow_logs
end

Expand Down
17 changes: 11 additions & 6 deletions test/commands/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,32 +190,37 @@ class CommandsAppTest < ActiveSupport::TestCase
end

test "run over ssh" do
assert_equal "ssh -t [email protected] 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
assert_equal "ssh -t [email protected] -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 [email protected] 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
assert_equal "ssh -t [email protected] -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 [email protected] -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 [email protected] -t [email protected] 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
assert_equal "ssh -J [email protected] -t [email protected] -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" => "[email protected]" }
assert_equal "ssh -J [email protected] -t [email protected] 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
assert_equal "ssh -J [email protected] -t [email protected] -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 [email protected] -t [email protected] 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
assert_equal "ssh -J [email protected] -t [email protected] -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 [email protected] '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 [email protected] -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
end

test "current_running_container_id" do
Expand Down
4 changes: 2 additions & 2 deletions test/commands/traefik_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ class CommandsTraefikTest < ActiveSupport::TestCase

test "traefik follow logs" do
assert_equal \
"ssh -t [email protected] 'docker logs traefik --timestamps --tail 10 --follow 2>&1'",
"ssh -t [email protected] -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 [email protected] 'docker logs traefik --timestamps --tail 10 --follow 2>&1 | grep \"hello!\"'",
"ssh -t [email protected] -p 22 'docker logs traefik --timestamps --tail 10 --follow 2>&1 | grep \"hello!\"'",
new_command.follow_logs(host: @config[:servers].first, grep: 'hello!')
end

Expand Down
3 changes: 3 additions & 0 deletions test/configuration/ssh_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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=>{},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit da16144

Please sign in to comment.