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

View File

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

View File

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

View File

@@ -55,7 +55,7 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
"service" => "custom-monitoring", "service" => "custom-monitoring",
"image" => "monitoring:latest", "image" => "monitoring:latest",
"registry" => { "server" => "other.registry", "username" => "user", "password" => "pw" }, "registry" => { "server" => "other.registry", "username" => "user", "password" => "pw" },
"roles" => [ "web" ], "role" => "web",
"port" => "4321:4321", "port" => "4321:4321",
"labels" => { "labels" => {
"cache" => "true" "cache" => "true"
@@ -134,7 +134,7 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
exception = assert_raises(Kamal::ConfigurationError) do exception = assert_raises(Kamal::ConfigurationError) do
Kamal::Configuration.new(@deploy) Kamal::Configuration.new(@deploy)
end 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 end
test "all hosts" do test "all hosts" do