Include env validation of new config

So we fail fast when required ENVs are missing!
This commit is contained in:
David Heinemeier Hansson
2023-01-30 13:50:15 +01:00
parent 52d75508ea
commit 7c72dfcb5d
2 changed files with 25 additions and 6 deletions

View File

@@ -39,7 +39,7 @@ class Mrsk::Configuration
def initialize(raw_config, version: "missing", validate: true) def initialize(raw_config, version: "missing", validate: true)
@raw_config = ActiveSupport::InheritableOptions.new(raw_config) @raw_config = ActiveSupport::InheritableOptions.new(raw_config)
@version = version @version = version
ensure_required_keys_present if validate valid? if validate
end end
@@ -120,6 +120,12 @@ class Mrsk::Configuration
end end
end end
def valid?
ensure_required_keys_present && ensure_env_available
end
def to_h def to_h
{ {
roles: role_names, roles: role_names,
@@ -139,6 +145,7 @@ class Mrsk::Configuration
private private
# Will raise ArgumentError if any required config keys are missing
def ensure_required_keys_present def ensure_required_keys_present
%i[ service image registry servers ].each do |key| %i[ service image registry servers ].each do |key|
raise ArgumentError, "Missing required configuration for #{key}" unless raw_config[key].present? raise ArgumentError, "Missing required configuration for #{key}" unless raw_config[key].present?
@@ -151,6 +158,16 @@ class Mrsk::Configuration
if raw_config.registry["password"].blank? if raw_config.registry["password"].blank?
raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)" raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)"
end end
true
end
# Will raise KeyError if any secret ENVs are missing
def ensure_env_available
env_args
roles.each(&:env_args)
true
end end
def role_names def role_names

View File

@@ -126,15 +126,17 @@ class ConfigurationTest < ActiveSupport::TestCase
end end
test "env args with missing secret" do test "env args with missing secret" do
config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!({
env: { "secret" => [ "PASSWORD" ] }
}) })
assert_raises(KeyError) do assert_raises(KeyError) do
assert_equal [ "-e", "PASSWORD=secret123" ], config.env_args config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!({
env: { "secret" => [ "PASSWORD" ] }
}) })
end end
end end
test "valid config" do
assert @config.valid?
end
test "ssh options" do test "ssh options" do
assert_equal "root", @config.ssh_options[:user] assert_equal "root", @config.ssh_options[:user]