From 2f912367ac643c37a0db9ac21b239c1580f55f1f Mon Sep 17 00:00:00 2001 From: Maciej Litwiniuk Date: Tue, 26 Dec 2023 17:24:47 +0100 Subject: [PATCH 1/2] Allow custom user and port for builder host When ssh options are set, they overwrite username and password passed as ssh builder uri. Passing part of uri for ssh-kit is fine, as it then properly extracts username and password and forwards it as host.ssh_options (in which case it's no longer empty) --- lib/kamal/cli/build.rb | 7 ++- lib/kamal/sshkit_with_ext.rb | 3 +- test/cli/build_test.rb | 8 ++++ ...y_with_remote_builder_and_custom_ports.yml | 45 +++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/deploy_with_remote_builder_and_custom_ports.yml diff --git a/lib/kamal/cli/build.rb b/lib/kamal/cli/build.rb index 8eae4dd70..558afe6c5 100644 --- a/lib/kamal/cli/build.rb +++ b/lib/kamal/cli/build.rb @@ -114,8 +114,11 @@ def verify_local_dependencies def connect_to_remote_host(remote_host) remote_uri = URI.parse(remote_host) if remote_uri.scheme == "ssh" - options = { user: remote_uri.user, port: remote_uri.port }.compact - on(remote_uri.host, options) do + host = SSHKit::Host.new( + hostname: remote_uri.host, + ssh_options: { user: remote_uri.user, port: remote_uri.port }.compact + ) + on(host, options) do execute "true" end end diff --git a/lib/kamal/sshkit_with_ext.rb b/lib/kamal/sshkit_with_ext.rb index e0c62c3ad..f0cdf8c82 100644 --- a/lib/kamal/sshkit_with_ext.rb +++ b/lib/kamal/sshkit_with_ext.rb @@ -80,7 +80,8 @@ class << self module LimitConcurrentStartsInstance private def with_ssh(&block) - host.ssh_options = self.class.config.ssh_options.merge(host.ssh_options || {}) + host.ssh_options = (host.ssh_options || {}).merge({ port: host.port, user: host.user }.compact) + host.ssh_options = self.class.config.ssh_options.merge(host.ssh_options) self.class.pool.with( method(:start_with_concurrency_limit), String(host.hostname), diff --git a/test/cli/build_test.rb b/test/cli/build_test.rb index 5c3e3680e..46770cf40 100644 --- a/test/cli/build_test.rb +++ b/test/cli/build_test.rb @@ -79,6 +79,14 @@ class CliBuildTest < CliTestCase end end + test "create remote with custom ports" do + run_command("create", fixture: :with_remote_builder_and_custom_ports).tap do |output| + assert_match "Running /usr/bin/env true on 1.1.1.5", output + assert_match "docker context create kamal-app-native-remote-amd64 --description 'kamal-app-native-remote amd64 native host' --docker 'host=ssh://app@1.1.1.5:2122'", output + assert_match "docker buildx create --name kamal-app-native-remote kamal-app-native-remote-amd64 --platform linux/amd64", output + end + end + test "create with error" do stub_setup SSHKit::Backend::Abstract.any_instance.stubs(:execute) diff --git a/test/fixtures/deploy_with_remote_builder_and_custom_ports.yml b/test/fixtures/deploy_with_remote_builder_and_custom_ports.yml new file mode 100644 index 000000000..d1e81836b --- /dev/null +++ b/test/fixtures/deploy_with_remote_builder_and_custom_ports.yml @@ -0,0 +1,45 @@ +service: app +image: dhh/app +servers: + web: + - "1.1.1.1" + - "1.1.1.2" + workers: + - "1.1.1.3" + - "1.1.1.4" +registry: + username: user + password: pw + +accessories: + mysql: + image: mysql:5.7 + host: 1.1.1.3 + port: 3306 + env: + clear: + MYSQL_ROOT_HOST: '%' + secret: + - MYSQL_ROOT_PASSWORD + files: + - test/fixtures/files/my.cnf:/etc/mysql/my.cnf + directories: + - data:/var/lib/mysql + redis: + image: redis:latest + roles: + - web + port: 6379 + directories: + - data:/data + +readiness_delay: 0 + +ssh: + user: root + port: 22 + +builder: + remote: + arch: amd64 + host: ssh://app@1.1.1.5:2122 From 1e296c41402a7fe34ca223448c1204a480bb8720 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Tue, 21 May 2024 11:38:30 +0100 Subject: [PATCH 2/2] Update sshkit_with_ext.rb --- lib/kamal/sshkit_with_ext.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/kamal/sshkit_with_ext.rb b/lib/kamal/sshkit_with_ext.rb index f0cdf8c82..e0c62c3ad 100644 --- a/lib/kamal/sshkit_with_ext.rb +++ b/lib/kamal/sshkit_with_ext.rb @@ -80,8 +80,7 @@ class << self module LimitConcurrentStartsInstance private def with_ssh(&block) - host.ssh_options = (host.ssh_options || {}).merge({ port: host.port, user: host.user }.compact) - host.ssh_options = self.class.config.ssh_options.merge(host.ssh_options) + host.ssh_options = self.class.config.ssh_options.merge(host.ssh_options || {}) self.class.pool.with( method(:start_with_concurrency_limit), String(host.hostname),