Add a singular role

This commit is contained in:
Krzysztof Duda
2025-05-12 10:55:15 +02:00
parent 4187ee2397
commit 78c9d610cf
4 changed files with 11 additions and 6 deletions

View File

@@ -201,7 +201,9 @@ class Kamal::Configuration::Accessory
end
def hosts_from_roles
if accessory_config.key?("roles")
if accessory_config.key?("role")
config.role(accessory_config["role"])&.hosts
elsif accessory_config.key?("roles")
accessory_config["roles"].flat_map { |role| config.role(role)&.hosts }
end
end
@@ -231,6 +233,8 @@ class Kamal::Configuration::Accessory
def ensure_valid_roles
if accessory_config["roles"] && (missing_roles = accessory_config["roles"] - config.roles.map(&:name)).any?
raise Kamal::ConfigurationError, "accessories/#{name}: unknown roles #{missing_roles.join(", ")}"
elsif accessory_config["role"] && !config.role(accessory_config["role"])
raise Kamal::ConfigurationError, "accessories/#{name}: unknown role #{accessory_config["role"]}"
end
end
end

View File

@@ -46,11 +46,12 @@ accessories:
# Accessory hosts
#
# Specify one of `host`, `hosts`, `roles`, or `tags`:
# Specify one of `host`, `hosts`, `role`, `roles`, `tag` or `tags`:
host: mysql-db1
hosts:
- mysql-db1
- mysql-db2
role: mysql
roles:
- mysql
tag: writer

View File

@@ -2,8 +2,8 @@ class Kamal::Configuration::Validator::Accessory < Kamal::Configuration::Validat
def validate!
super
if (config.keys & [ "host", "hosts", "roles", "tag", "tags" ]).size != 1
error "specify one of `host`, `hosts`, `roles`, `tag` or `tags`"
if (config.keys & [ "host", "hosts", "role", "roles", "tag", "tags" ]).size != 1
error "specify one of `host`, `hosts`, `role`, `roles`, `tag` or `tags`"
end
validate_docker_options!(config["options"])

View File

@@ -55,7 +55,7 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
"service" => "custom-monitoring",
"image" => "monitoring:latest",
"registry" => { "server" => "other.registry", "username" => "user", "password" => "pw" },
"roles" => [ "web" ],
"role" => "web",
"port" => "4321:4321",
"labels" => {
"cache" => "true"
@@ -134,7 +134,7 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
exception = assert_raises(Kamal::ConfigurationError) do
Kamal::Configuration.new(@deploy)
end
assert_equal "accessories/mysql: specify one of `host`, `hosts`, `roles`, `tag` or `tags`", exception.message
assert_equal "accessories/mysql: specify one of `host`, `hosts`, `role`, `roles`, `tag` or `tags`", exception.message
end
test "all hosts" do