Skip to content

Commit

Permalink
Add a require_destination setting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
djmb committed Sep 11, 2023
1 parent 9d35793 commit 013a095
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/kamal/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -120,6 +119,10 @@ def service_with_version
"#{service}-#{version}"
end

def require_destination?
raw_config.require_destination
end


def volume_args
if raw_config.volumes.present?
Expand Down Expand Up @@ -161,7 +164,7 @@ def minimum_version
end

def valid?
ensure_required_keys_present && ensure_valid_kamal_version
ensure_destination_if_required && ensure_required_keys_present && ensure_valid_kamal_version
end


Expand Down Expand Up @@ -243,6 +246,14 @@ def ensure_valid_kamal_version
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
Expand Down
12 changes: 12 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 013a095

Please sign in to comment.