From 6a3b0249fe8c07516992e850f43000a9a745cfdf Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Mon, 11 Sep 2023 17:02:32 +0100 Subject: [PATCH] Connect to remote host before creating builder Connecting to the remote host will make any SSH configuration issues obvious and add the host to known hosts if that is how SSHKit is configured. --- lib/kamal/cli/build.rb | 14 +++++++ test/cli/build_test.rb | 12 +++++- test/fixtures/deploy_with_remote_builder.yml | 41 ++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/deploy_with_remote_builder.yml diff --git a/lib/kamal/cli/build.rb b/lib/kamal/cli/build.rb index 9a23117d..379fd12a 100644 --- a/lib/kamal/cli/build.rb +++ b/lib/kamal/cli/build.rb @@ -1,3 +1,5 @@ +require "uri" + class Kamal::Cli::Build < Kamal::Cli::Base class BuildError < StandardError; end @@ -55,6 +57,10 @@ class Kamal::Cli::Build < Kamal::Cli::Base desc "create", "Create a build setup" def create mutating do + if (remote_host = KAMAL.config.builder.remote_host) + connect_to_remote_host(remote_host) + end + run_locally do begin debug "Using builder: #{KAMAL.builder.name}" @@ -103,4 +109,12 @@ class Kamal::Cli::Build < Kamal::Cli::Base end end end + + def connect_to_remote_host(remote_host) + remote_uri = URI.parse(remote_host) + options = { user: remote_uri.user, port: remote_uri.port }.compact + on(remote_uri.host, options) do + execute "true" + end + end end diff --git a/test/cli/build_test.rb b/test/cli/build_test.rb index 35e8b761..c64a4917 100644 --- a/test/cli/build_test.rb +++ b/test/cli/build_test.rb @@ -66,6 +66,14 @@ class CliBuildTest < CliTestCase end end + test "create remote" do + run_command("create", fixture: :with_remote_builder).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'", 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) @@ -95,8 +103,8 @@ class CliBuildTest < CliTestCase end private - def run_command(*command) - stdouted { Kamal::Cli::Build.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) } + def run_command(*command, fixture: :with_accessories) + stdouted { Kamal::Cli::Build.start([*command, "-c", "test/fixtures/deploy_#{fixture}.yml"]) } end def stub_dependency_checks diff --git a/test/fixtures/deploy_with_remote_builder.yml b/test/fixtures/deploy_with_remote_builder.yml new file mode 100644 index 00000000..44103499 --- /dev/null +++ b/test/fixtures/deploy_with_remote_builder.yml @@ -0,0 +1,41 @@ +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 + +builder: + remote: + arch: amd64 + host: ssh://app@1.1.1.5