Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable yaml aliases #578

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/kamal/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/cli/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions test/fixtures/deploy_with_aliases.yml
Original file line number Diff line number Diff line change
@@ -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
Loading