From 26dcd7542354989c09817b4744bdcc5e2e02f5b8 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Mon, 11 Sep 2023 15:16:28 +0100 Subject: [PATCH] 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. --- lib/kamal/configuration.rb | 17 ++++++++++++++--- test/configuration_test.rb | 12 ++++++++++++ .../fixtures/deploy_for_required_dest.world.yml | 5 +++++ test/fixtures/deploy_for_required_dest.yml | 7 +++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/deploy_for_required_dest.world.yml create mode 100644 test/fixtures/deploy_for_required_dest.yml diff --git a/lib/kamal/configuration.rb b/lib/kamal/configuration.rb index af6e46c5..a77b10ef 100644 --- a/lib/kamal/configuration.rb +++ b/lib/kamal/configuration.rb @@ -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 :argumentize, :optionize, to: Kamal::Utils - attr_accessor :destination - attr_accessor :raw_config + attr_reader :destination, :raw_config class << self def create_from(config_file:, destination: nil, version: nil) @@ -120,6 +119,10 @@ class Kamal::Configuration "#{service}-#{version}" end + def require_destination? + raw_config.require_destination + end + def volume_args if raw_config.volumes.present? @@ -161,7 +164,7 @@ class Kamal::Configuration end def valid? - ensure_required_keys_present && ensure_valid_kamal_version + ensure_destination_if_required && ensure_required_keys_present && ensure_valid_kamal_version end @@ -243,6 +246,14 @@ class Kamal::Configuration true end + def ensure_destination_if_required + if require_destination? && destination.nil? + raise ArgumentError, "You must specify a destination" + end + + true + end + def role_names raw_config.servers.is_a?(Array) ? [ "web" ] : raw_config.servers.keys.sort diff --git a/test/configuration_test.rb b/test/configuration_test.rb index f21fdc8d..ca8e7f85 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -210,6 +210,18 @@ class ConfigurationTest < ActiveSupport::TestCase 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 expected_config = \ { :roles=>["web"], diff --git a/test/fixtures/deploy_for_required_dest.world.yml b/test/fixtures/deploy_for_required_dest.world.yml new file mode 100644 index 00000000..764dd236 --- /dev/null +++ b/test/fixtures/deploy_for_required_dest.world.yml @@ -0,0 +1,5 @@ +servers: + - 1.1.1.1 + - 1.1.1.2 +env: + REDIS_URL: redis://x/y diff --git a/test/fixtures/deploy_for_required_dest.yml b/test/fixtures/deploy_for_required_dest.yml new file mode 100644 index 00000000..28438363 --- /dev/null +++ b/test/fixtures/deploy_for_required_dest.yml @@ -0,0 +1,7 @@ +service: app +image: dhh/app +registry: + server: registry.digitalocean.com + username: <%= "my-user" %> + password: <%= "my-password" %> +require_destination: true