Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allow_empty_roles to control aborting on roles with no hosts. #582

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/kamal/cli/templates/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ registry:
# Caution: there's no support for role renaming yet, so be careful to cleanup
# the previous role on the deployed hosts.
# 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
23 changes: 17 additions & 6 deletions lib/kamal/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ def primary_web_role
raw_config.primary_web_role || "web"
end

def allow_empty_roles?
raw_config.allow_empty_roles
end


def valid?
ensure_destination_if_required && ensure_required_keys_present && ensure_valid_kamal_version
end
Expand Down Expand Up @@ -257,12 +262,6 @@ 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

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)
raise ArgumentError, "The primary_web_role #{primary_web_role} isn't defined"
end
Expand All @@ -271,6 +270,18 @@ def ensure_required_keys_present
raise ArgumentError, "Role #{primary_web_role} needs to have traefik enabled"
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
end

Expand Down
10 changes: 10 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ class ConfigurationTest < ActiveSupport::TestCase
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
assert_equal ["--volume", "/local/path:/container/path"], @config.volume_args
end
Expand Down
Loading