Skip to content

Commit

Permalink
Add SSH port to run_over_ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
leonvogt committed Oct 1, 2023
1 parent b3f85dc commit 9ef795f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 15 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
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

0 comments on commit 9ef795f

Please sign in to comment.