From 263b4a4fb8ecf10834fc12ccdddf2c267270eabe Mon Sep 17 00:00:00 2001 From: Matthew Kent Date: Sat, 11 Nov 2023 13:03:23 -0800 Subject: [PATCH 1/2] Enable aliases for more exotic templating situations. This is super useful for DRY when configuring a number of roles and you hit the limits of what's reasonable with ERB. --- lib/kamal/configuration.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/kamal/configuration.rb b/lib/kamal/configuration.rb index fc881c069..87c68dc5a 100644 --- a/lib/kamal/configuration.rb +++ b/lib/kamal/configuration.rb @@ -25,7 +25,9 @@ def load_config_files(*files) def load_config_file(file) if file.exist? - YAML.load(ERB.new(IO.read(file)).result).symbolize_keys + # Newer Psych doesn't load aliases by default + load_method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load + YAML.send(load_method, ERB.new(IO.read(file)).result).symbolize_keys else raise "Configuration file not found in #{file}" end From ed58ce6e610d5ef2ae57d5fe77717f2f55ad8e4d Mon Sep 17 00:00:00 2001 From: Matthew Kent Date: Sat, 11 Nov 2023 13:24:12 -0800 Subject: [PATCH 2/2] Add test coverage with aliases. --- test/cli/main_test.rb | 13 ++++++++++ test/fixtures/deploy_with_aliases.yml | 36 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 test/fixtures/deploy_with_aliases.yml diff --git a/test/cli/main_test.rb b/test/cli/main_test.rb index a19ff90e7..91f748709 100644 --- a/test/cli/main_test.rb +++ b/test/cli/main_test.rb @@ -296,6 +296,19 @@ class CliMainTest < CliTestCase end end + test "config with aliases" do + run_command("config", config_file: "deploy_with_aliases").tap do |output| + config = YAML.load(output) + + assert_equal ["web", "web_tokyo", "workers", "workers_tokyo"], config[:roles] + assert_equal ["1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4"], config[:hosts] + assert_equal "999", config[:version] + assert_equal "registry.digitalocean.com/dhh/app", config[:repository] + assert_equal "registry.digitalocean.com/dhh/app:999", config[:absolute_image] + assert_equal "app-999", config[:service_with_version] + end + end + test "init" do Pathname.any_instance.expects(:exist?).returns(false).times(3) Pathname.any_instance.stubs(:mkpath) diff --git a/test/fixtures/deploy_with_aliases.yml b/test/fixtures/deploy_with_aliases.yml new file mode 100644 index 000000000..90c18b57c --- /dev/null +++ b/test/fixtures/deploy_with_aliases.yml @@ -0,0 +1,36 @@ +# helper aliases +chicago_hosts: &chicago_hosts + hosts: + - 1.1.1.1 + - 1.1.1.2 +tokyo_hosts: &tokyo_hosts + hosts: + - 1.1.1.3 + - 1.1.1.4 +web_common: &web_common + env: + ROLE: "web" + traefik: true + +# actual config +service: app +image: dhh/app +servers: + web: + <<: *chicago_hosts + <<: *web_common + web_tokyo: + <<: *tokyo_hosts + <<: *web_common + workers: + cmd: bin/jobs + <<: *chicago_hosts + workers_tokyo: + cmd: bin/jobs + <<: *tokyo_hosts +env: + REDIS_URL: redis://x/y +registry: + server: registry.digitalocean.com + username: user + password: pw