Ensure that the restart policy is unless-stopped

No other restart policy makes sense to don't let it be changed.

Fixes: https://github.com/basecamp/kamal/issues/749
This commit is contained in:
Donal McBreen
2025-04-17 13:31:18 +01:00
parent 2a8d561094
commit 91f01ece1b
5 changed files with 25 additions and 0 deletions

View File

@@ -168,4 +168,10 @@ class Kamal::Configuration::Validator
unknown_keys.reject! { |key| extension?(key) } if allow_extensions? unknown_keys.reject! { |key| extension?(key) } if allow_extensions?
unknown_keys_error unknown_keys if unknown_keys.present? unknown_keys_error unknown_keys if unknown_keys.present?
end end
def validate_docker_options!(options)
if options
error "Cannot set restart policy in docker options, unless-stopped is required" if options["restart"]
end
end
end end

View File

@@ -5,5 +5,7 @@ class Kamal::Configuration::Validator::Accessory < Kamal::Configuration::Validat
if (config.keys & [ "host", "hosts", "roles" ]).size != 1 if (config.keys & [ "host", "hosts", "roles" ]).size != 1
error "specify one of `host`, `hosts` or `roles`" error "specify one of `host`, `hosts` or `roles`"
end end
validate_docker_options!(config["options"])
end end
end end

View File

@@ -6,6 +6,7 @@ class Kamal::Configuration::Validator::Role < Kamal::Configuration::Validator
validate_servers!(config) validate_servers!(config)
else else
super super
validate_docker_options!(config["options"])
end end
end end
end end

View File

@@ -187,4 +187,12 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
assert @config.accessory(:monitoring).running_proxy? assert @config.accessory(:monitoring).running_proxy?
assert_equal [ "monitoring.example.com" ], @config.accessory(:monitoring).proxy.hosts assert_equal [ "monitoring.example.com" ], @config.accessory(:monitoring).proxy.hosts
end end
test "can't set restart in options" do
@deploy[:accessories]["mysql"]["options"] = { "restart" => "always" }
assert_raises Kamal::ConfigurationError, "servers/workers: Cannot set restart policy in docker options, unless-stopped is required" do
Kamal::Configuration.new(@deploy)
end
end
end end

View File

@@ -258,6 +258,14 @@ class ConfigurationRoleTest < ActiveSupport::TestCase
assert_equal "18s", config_with_roles.role(:workers).proxy.deploy_options[:"target-timeout"] assert_equal "18s", config_with_roles.role(:workers).proxy.deploy_options[:"target-timeout"]
end end
test "can't set restart in options" do
@deploy_with_roles[:servers]["workers"]["options"] = { "restart" => "always" }
assert_raises Kamal::ConfigurationError, "servers/workers: Cannot set restart policy in docker options, unless-stopped is required" do
Kamal::Configuration.new(@deploy_with_roles)
end
end
private private
def config def config
Kamal::Configuration.new(@deploy) Kamal::Configuration.new(@deploy)