Add a require_destination setting

If you always want to use a destination, and have a base deploy.yml file
that doesn't specify any hosts, then if you forget to specific the
destination you will get a cryptic error.

Add a "require_destination" setting you can use to avoid this.
This commit is contained in:
Donal McBreen
2023-09-11 15:16:28 +01:00
parent 9d35793287
commit 26dcd75423
4 changed files with 38 additions and 3 deletions

View File

@@ -9,8 +9,7 @@ class Kamal::Configuration
delegate :service, :image, :servers, :env, :labels, :registry, :stop_wait_time, :hooks_path, to: :raw_config, allow_nil: true delegate :service, :image, :servers, :env, :labels, :registry, :stop_wait_time, :hooks_path, to: :raw_config, allow_nil: true
delegate :argumentize, :optionize, to: Kamal::Utils delegate :argumentize, :optionize, to: Kamal::Utils
attr_accessor :destination attr_reader :destination, :raw_config
attr_accessor :raw_config
class << self class << self
def create_from(config_file:, destination: nil, version: nil) def create_from(config_file:, destination: nil, version: nil)
@@ -120,6 +119,10 @@ class Kamal::Configuration
"#{service}-#{version}" "#{service}-#{version}"
end end
def require_destination?
raw_config.require_destination
end
def volume_args def volume_args
if raw_config.volumes.present? if raw_config.volumes.present?
@@ -161,7 +164,7 @@ class Kamal::Configuration
end end
def valid? def valid?
ensure_required_keys_present && ensure_valid_kamal_version ensure_destination_if_required && ensure_required_keys_present && ensure_valid_kamal_version
end end
@@ -243,6 +246,14 @@ class Kamal::Configuration
true true
end end
def ensure_destination_if_required
if require_destination? && destination.nil?
raise ArgumentError, "You must specify a destination"
end
true
end
def role_names def role_names
raw_config.servers.is_a?(Array) ? [ "web" ] : raw_config.servers.keys.sort raw_config.servers.is_a?(Array) ? [ "web" ] : raw_config.servers.keys.sort

View File

@@ -210,6 +210,18 @@ class ConfigurationTest < ActiveSupport::TestCase
end end
end end
test "destination required" do
dest_config_file = Pathname.new(File.expand_path("fixtures/deploy_for_required_dest.yml", __dir__))
assert_raises(ArgumentError) do
config = Kamal::Configuration.create_from config_file: dest_config_file
end
assert_nothing_raised do
config = Kamal::Configuration.create_from config_file: dest_config_file, destination: "world"
end
end
test "to_h" do test "to_h" do
expected_config = \ expected_config = \
{ :roles=>["web"], { :roles=>["web"],

View File

@@ -0,0 +1,5 @@
servers:
- 1.1.1.1
- 1.1.1.2
env:
REDIS_URL: redis://x/y

View File

@@ -0,0 +1,7 @@
service: app
image: dhh/app
registry:
server: registry.digitalocean.com
username: <%= "my-user" %>
password: <%= "my-password" %>
require_destination: true