From 4f7ebd73a35640962e50113fdc79f2c724dbfbfc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Oct 2024 16:30:32 -0700 Subject: [PATCH 1/4] Specifics#accessory_hosts was being filtered out by role host check --- lib/kamal/commander/specifics.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/kamal/commander/specifics.rb b/lib/kamal/commander/specifics.rb index 190d2b69..238cc012 100644 --- a/lib/kamal/commander/specifics.rb +++ b/lib/kamal/commander/specifics.rb @@ -43,7 +43,12 @@ class Kamal::Commander::Specifics end def specified_hosts - (specific_hosts || config.all_hosts) \ - .select { |host| (specific_roles || config.roles).flat_map(&:hosts).include?(host) } + specified_hosts = specific_hosts || config.all_hosts + + if (specific_role_hosts = specific_roles&.flat_map(&:hosts)).present? + specified_hosts.select { |host| specific_role_hosts.include?(host) } + else + specified_hosts + end end end From 7be2e7e0bafd5cdf48508afdf33babbf73e5b1ea Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Oct 2024 17:03:30 -0700 Subject: [PATCH 2/4] Test accessory_hosts with roles and without filtering --- test/commander_test.rb | 21 ++++++++++++++ test/fixtures/deploy_with_accessories.yml | 2 +- .../fixtures/deploy_with_single_accessory.yml | 29 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/deploy_with_single_accessory.yml diff --git a/test/commander_test.rb b/test/commander_test.rb index 54031e80..b6b61e16 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -150,6 +150,27 @@ class CommanderTest < ActiveSupport::TestCase assert_equal [ "1.1.1.2" ], @kamal.proxy_hosts end + test "accessory hosts without filtering" do + configure_with(:deploy_with_single_accessory) + assert_equal [ "1.1.1.5" ], @kamal.accessory_hosts + + configure_with(:deploy_with_accessories) + assert_equal [ "1.1.1.5", "1.1.1.1", "1.1.1.2" ], @kamal.accessory_hosts + end + + test "accessory hosts with role filtering" do + configure_with(:deploy_with_single_accessory) + @kamal.specific_roles = [ "web" ] + assert_equal [ ], @kamal.accessory_hosts + + configure_with(:deploy_with_accessories) + @kamal.specific_roles = [ "web" ] + assert_equal [ "1.1.1.1", "1.1.1.2" ], @kamal.accessory_hosts + + @kamal.specific_roles = [ "workers" ] + assert_equal [ ], @kamal.accessory_hosts + end + private def configure_with(variant) @kamal = Kamal::Commander.new.tap do |kamal| diff --git a/test/fixtures/deploy_with_accessories.yml b/test/fixtures/deploy_with_accessories.yml index 29f502ec..e6c6825f 100644 --- a/test/fixtures/deploy_with_accessories.yml +++ b/test/fixtures/deploy_with_accessories.yml @@ -16,7 +16,7 @@ builder: accessories: mysql: image: mysql:5.7 - host: 1.1.1.3 + host: 1.1.1.5 port: 3306 env: clear: diff --git a/test/fixtures/deploy_with_single_accessory.yml b/test/fixtures/deploy_with_single_accessory.yml new file mode 100644 index 00000000..4af4a9e6 --- /dev/null +++ b/test/fixtures/deploy_with_single_accessory.yml @@ -0,0 +1,29 @@ +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 +builder: + arch: amd64 + +accessories: + mysql: + image: mysql:5.7 + host: 1.1.1.5 + 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 From 82a436fa02cbc5b2c76fadc1886f7bfc135fdbb2 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Oct 2024 17:07:51 -0700 Subject: [PATCH 3/4] Rubocop --- test/commander_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/commander_test.rb b/test/commander_test.rb index b6b61e16..fc047d4f 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -161,14 +161,14 @@ class CommanderTest < ActiveSupport::TestCase test "accessory hosts with role filtering" do configure_with(:deploy_with_single_accessory) @kamal.specific_roles = [ "web" ] - assert_equal [ ], @kamal.accessory_hosts + assert_equal [], @kamal.accessory_hosts configure_with(:deploy_with_accessories) @kamal.specific_roles = [ "web" ] assert_equal [ "1.1.1.1", "1.1.1.2" ], @kamal.accessory_hosts @kamal.specific_roles = [ "workers" ] - assert_equal [ ], @kamal.accessory_hosts + assert_equal [], @kamal.accessory_hosts end private From e5ca53db6ee84fd136ab983c8d3d88b69e246aa6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Oct 2024 17:34:13 -0700 Subject: [PATCH 4/4] Use new deploy config so as not to update all other tests --- test/commander_test.rb | 4 +- test/fixtures/deploy_with_accessories.yml | 2 +- ...with_accessories_on_independent_server.yml | 38 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/deploy_with_accessories_on_independent_server.yml diff --git a/test/commander_test.rb b/test/commander_test.rb index fc047d4f..4f0a829e 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -154,7 +154,7 @@ class CommanderTest < ActiveSupport::TestCase configure_with(:deploy_with_single_accessory) assert_equal [ "1.1.1.5" ], @kamal.accessory_hosts - configure_with(:deploy_with_accessories) + configure_with(:deploy_with_accessories_on_independent_server) assert_equal [ "1.1.1.5", "1.1.1.1", "1.1.1.2" ], @kamal.accessory_hosts end @@ -163,7 +163,7 @@ class CommanderTest < ActiveSupport::TestCase @kamal.specific_roles = [ "web" ] assert_equal [], @kamal.accessory_hosts - configure_with(:deploy_with_accessories) + configure_with(:deploy_with_accessories_on_independent_server) @kamal.specific_roles = [ "web" ] assert_equal [ "1.1.1.1", "1.1.1.2" ], @kamal.accessory_hosts diff --git a/test/fixtures/deploy_with_accessories.yml b/test/fixtures/deploy_with_accessories.yml index e6c6825f..29f502ec 100644 --- a/test/fixtures/deploy_with_accessories.yml +++ b/test/fixtures/deploy_with_accessories.yml @@ -16,7 +16,7 @@ builder: accessories: mysql: image: mysql:5.7 - host: 1.1.1.5 + host: 1.1.1.3 port: 3306 env: clear: diff --git a/test/fixtures/deploy_with_accessories_on_independent_server.yml b/test/fixtures/deploy_with_accessories_on_independent_server.yml new file mode 100644 index 00000000..e6c6825f --- /dev/null +++ b/test/fixtures/deploy_with_accessories_on_independent_server.yml @@ -0,0 +1,38 @@ +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 +builder: + arch: amd64 + +accessories: + mysql: + image: mysql:5.7 + host: 1.1.1.5 + 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