Merge pull request #582 from basecamp/allow-empty-roles

Add allow_empty_roles to control aborting on roles with no hosts.
This commit is contained in:
Donal McBreen
2023-11-13 09:30:01 +00:00
committed by GitHub
3 changed files with 32 additions and 6 deletions

View File

@@ -91,3 +91,8 @@ registry:
# Caution: there's no support for role renaming yet, so be careful to cleanup # Caution: there's no support for role renaming yet, so be careful to cleanup
# the previous role on the deployed hosts. # the previous role on the deployed hosts.
# primary_web_role: web # primary_web_role: web
# Controls if we abort when see a role with no hosts. Disabling this may be
# useful for more complex deploy configurations.
#
# allow_empty_roles: false

View File

@@ -212,6 +212,11 @@ class Kamal::Configuration
raw_config.primary_web_role || "web" raw_config.primary_web_role || "web"
end end
def allow_empty_roles?
raw_config.allow_empty_roles
end
def valid? def valid?
ensure_destination_if_required && ensure_required_keys_present && ensure_valid_kamal_version ensure_destination_if_required && ensure_required_keys_present && ensure_valid_kamal_version
end end
@@ -259,12 +264,6 @@ class Kamal::Configuration
raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)" raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)"
end end
roles.each do |role|
if role.hosts.empty?
raise ArgumentError, "No servers specified for the #{role.name} role"
end
end
unless role_names.include?(primary_web_role) unless role_names.include?(primary_web_role)
raise ArgumentError, "The primary_web_role #{primary_web_role} isn't defined" raise ArgumentError, "The primary_web_role #{primary_web_role} isn't defined"
end end
@@ -273,6 +272,18 @@ class Kamal::Configuration
raise ArgumentError, "Role #{primary_web_role} needs to have traefik enabled" raise ArgumentError, "Role #{primary_web_role} needs to have traefik enabled"
end end
if role(primary_web_role).hosts.empty?
raise ArgumentError, "No servers specified for the #{primary_web_role} primary_web_role"
end
unless allow_empty_roles?
roles.each do |role|
if role.hosts.empty?
raise ArgumentError, "No servers specified for the #{role.name} role. You can ignore this with allow_empty_roles: true"
end
end
end
true true
end end

View File

@@ -165,6 +165,16 @@ class ConfigurationTest < ActiveSupport::TestCase
end end
end end
test "allow_empty_roles" do
assert_silent do
Kamal::Configuration.new @deploy.merge(servers: { "web" => %w[ web ], "workers" => { "hosts" => %w[ ] } }, allow_empty_roles: true)
end
assert_raises(ArgumentError) do
Kamal::Configuration.new @deploy.merge(servers: { "web" => %w[], "workers" => { "hosts" => %w[] } }, allow_empty_roles: true)
end
end
test "volume_args" do test "volume_args" do
assert_equal ["--volume", "/local/path:/container/path"], @config.volume_args assert_equal ["--volume", "/local/path:/container/path"], @config.volume_args
end end