From 8e9176537ba727896bc429632266df0408c4fefa Mon Sep 17 00:00:00 2001 From: Yoel Cabo Date: Sun, 19 Nov 2023 05:54:54 +0100 Subject: [PATCH] refactor: make config.primary_role a Role instead of a string --- lib/kamal/cli/healthcheck.rb | 2 +- lib/kamal/cli/main.rb | 2 +- lib/kamal/commands/healthcheck.rb | 2 +- lib/kamal/configuration.rb | 14 +++++++++----- lib/kamal/configuration/role.rb | 2 +- test/commander_test.rb | 4 ++-- test/configuration_test.rb | 4 ++-- 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/kamal/cli/healthcheck.rb b/lib/kamal/cli/healthcheck.rb index b3dbf6bd1..54d01c031 100644 --- a/lib/kamal/cli/healthcheck.rb +++ b/lib/kamal/cli/healthcheck.rb @@ -3,7 +3,7 @@ class Kamal::Cli::Healthcheck < Kamal::Cli::Base desc "perform", "Health check current app version" def perform - raise "The primary host is not configured to run Traefik" unless KAMAL.config.role(KAMAL.config.primary_role).running_traefik? + raise "The primary host is not configured to run Traefik" unless KAMAL.config.primary_role.running_traefik? on(KAMAL.primary_host) do begin execute *KAMAL.healthcheck.run diff --git a/lib/kamal/cli/main.rb b/lib/kamal/cli/main.rb index 6adc36feb..c8f0411df 100644 --- a/lib/kamal/cli/main.rb +++ b/lib/kamal/cli/main.rb @@ -38,7 +38,7 @@ def deploy say "Ensure Traefik is running...", :magenta invoke "kamal:cli:traefik:boot", [], invoke_options - if KAMAL.config.role(KAMAL.config.primary_role).running_traefik? + if KAMAL.config.primary_role.running_traefik? say "Ensure app can pass healthcheck...", :magenta invoke "kamal:cli:healthcheck:perform", [], invoke_options end diff --git a/lib/kamal/commands/healthcheck.rb b/lib/kamal/commands/healthcheck.rb index 35bf95621..647a3f170 100644 --- a/lib/kamal/commands/healthcheck.rb +++ b/lib/kamal/commands/healthcheck.rb @@ -1,7 +1,7 @@ class Kamal::Commands::Healthcheck < Kamal::Commands::Base def run - primary = config.role(config.primary_role) + primary = config.primary_role docker :run, "--detach", diff --git a/lib/kamal/configuration.rb b/lib/kamal/configuration.rb index e5ecbb530..426b4b039 100644 --- a/lib/kamal/configuration.rb +++ b/lib/kamal/configuration.rb @@ -92,7 +92,7 @@ def all_hosts end def primary_host - role(primary_role)&.primary_host + primary_role&.primary_host end def traefik_roles @@ -209,6 +209,10 @@ def asset_path end def primary_role + role(primary_role_name) + end + + def primary_role_name raw_config.primary_role || "web" end @@ -264,12 +268,12 @@ def ensure_required_keys_present raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)" end - unless role_names.include?(primary_role) - raise ArgumentError, "The primary_role #{primary_role} isn't defined" + unless role_names.include?(primary_role_name) + raise ArgumentError, "The primary_role #{primary_role_name} isn't defined" end - if role(primary_role).hosts.empty? - raise ArgumentError, "No servers specified for the #{primary_role} primary_role" + if primary_role.hosts.empty? + raise ArgumentError, "No servers specified for the #{primary_role_name} primary_role" end unless allow_empty_roles? diff --git a/lib/kamal/configuration/role.rb b/lib/kamal/configuration/role.rb index da88d406b..1adaf4cf7 100644 --- a/lib/kamal/configuration/role.rb +++ b/lib/kamal/configuration/role.rb @@ -101,7 +101,7 @@ def running_traefik? end def primary? - @config.primary_role == name + @config.primary_role_name == name end diff --git a/test/commander_test.rb b/test/commander_test.rb index 06cc5bbb1..f99f2e0f0 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -74,9 +74,9 @@ class CommanderTest < ActiveSupport::TestCase end test "primary_role" do - assert_equal "web", @kamal.primary_role + assert_equal "web", @kamal.primary_role_name @kamal.specific_roles = "workers" - assert_equal "workers", @kamal.primary_role + assert_equal "workers", @kamal.primary_role_name end test "roles_on" do diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 392b6afb9..191402dca 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -290,14 +290,14 @@ class ConfigurationTest < ActiveSupport::TestCase end test "primary role" do - assert_equal "web", @config.primary_role + assert_equal "web", @config.primary_role_name config = Kamal::Configuration.new(@deploy_with_roles.deep_merge({ servers: { "alternate_web" => { "hosts" => [ "1.1.1.4", "1.1.1.5" ] } }, primary_role: "alternate_web" } )) - assert_equal "alternate_web", config.primary_role + assert_equal "alternate_web", config.primary_role_name assert_equal "1.1.1.4", config.primary_host assert config.role(:alternate_web).primary? assert config.role(:alternate_web).running_traefik?