Pin accessories to tags

This commit is contained in:
David Heinemeier Hansson
2025-05-09 18:14:47 +02:00
parent 2d43f788c4
commit 83a5636e27
4 changed files with 19 additions and 7 deletions

View File

@@ -32,7 +32,7 @@ class Kamal::Configuration::Accessory
end end
def hosts def hosts
hosts_from_host || hosts_from_hosts || hosts_from_roles hosts_from_host || hosts_from_hosts || hosts_from_roles || hosts_from_tags
end end
def port def port
@@ -206,6 +206,12 @@ class Kamal::Configuration::Accessory
end end
end end
def hosts_from_tags
if accessory_config.key?("tags")
accessory_config["tags"].flat_map { |tag| config.tag(tag)&.hosts }
end
end
def network def network
accessory_config["network"] || DEFAULT_NETWORK accessory_config["network"] || DEFAULT_NETWORK
end end

View File

@@ -46,13 +46,15 @@ accessories:
# Accessory hosts # Accessory hosts
# #
# Specify one of `host`, `hosts`, or `roles`: # Specify one of `host`, `hosts`, `roles`, or `tags`:
host: mysql-db1 host: mysql-db1
hosts: hosts:
- mysql-db1 - mysql-db1
- mysql-db2 - mysql-db2
roles: roles:
- mysql - mysql
tags:
- writer
# Custom command # Custom command
# #

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" ]).size != 1 if (config.keys & [ "host", "hosts", "roles", "tags" ]).size != 1
error "specify one of `host`, `hosts` or `roles`" error "specify one of `host`, `hosts`, `roles` or `tags`"
end end
validate_docker_options!(config["options"]) validate_docker_options!(config["options"])

View File

@@ -7,7 +7,7 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
image: "dhh/app", image: "dhh/app",
registry: { "username" => "dhh", "password" => "secret" }, registry: { "username" => "dhh", "password" => "secret" },
servers: { servers: {
"web" => [ "1.1.1.1", "1.1.1.2" ], "web" => [ { "1.1.1.1" => "writer" }, { "1.1.1.2" => "reader" } ],
"workers" => [ "1.1.1.3", "1.1.1.4" ] "workers" => [ "1.1.1.3", "1.1.1.4" ]
}, },
builder: { "arch" => "amd64" }, builder: { "arch" => "amd64" },
@@ -70,6 +70,10 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
"proxy" => { "proxy" => {
"host" => "monitoring.example.com" "host" => "monitoring.example.com"
} }
},
"proxy" => {
"image" => "proxy:latest",
"tags" => [ "writer" ]
} }
} }
} }
@@ -117,14 +121,14 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
end end
end end
test "setting host, hosts and roles" do test "setting host, hosts, roles and tags" do
@deploy[:accessories]["mysql"]["hosts"] = [ "mysql-db1" ] @deploy[:accessories]["mysql"]["hosts"] = [ "mysql-db1" ]
@deploy[:accessories]["mysql"]["roles"] = [ "db" ] @deploy[:accessories]["mysql"]["roles"] = [ "db" ]
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` or `roles`", exception.message assert_equal "accessories/mysql: specify one of `host`, `hosts`, `roles` or `tags`", exception.message
end end
test "all hosts" do test "all hosts" do