Compare commits

..

10 Commits

Author SHA1 Message Date
Donal McBreen
70096160c9 Replace .env* with .kamal/env*
By default look for the env file in .kamal/env to avoid clashes with
other tools using .env.

For now we'll still load .env and issue a deprecation warning, but in
future we'll stop reading those.
2024-07-31 10:18:59 +01:00
Donal McBreen
ec4aa45852 Bump version for 1.8.1 2024-07-29 09:09:57 +01:00
Donal McBreen
5e11a64181 Merge pull request #891 from basecamp/single-pull
Pull once from hosts that warm registry mirrors
2024-07-22 08:18:48 +01:00
Jeremy Daer
57d9ce177a Pull once from hosts that warm registry mirrors 2024-07-18 09:14:22 -07:00
Donal McBreen
8a98949634 Merge pull request #886 from guoard/patch-2
Remove `--update` flag from `apk add` command
2024-07-16 15:46:37 +01:00
Donal McBreen
0eb9f48082 Merge pull request #887 from basecamp/fix-tests-with-git-config
Fix the tests when you have a git config email set
2024-07-16 13:08:18 +01:00
Donal McBreen
9db6fc0704 Fix the tests when you have a git config email set
The ran ok on CI where we fall back to `whoami`, but failed locally
where there was a git email set.
2024-07-16 12:09:05 +01:00
Donal McBreen
27fede3caa Merge pull request #884 from basecamp/x-config
Add support for configuration extensions
2024-07-16 11:38:28 +01:00
Donal McBreen
29c723f7ec Add support for configuration extensions
Allow blocks prefixed with `x-` in the configuration as a place to
declare reusable blocks with YAML anchors and aliases.

Borrowed from the Docker Compose configuration file format -
https://github.com/compose-spec/compose-spec/blob/main/spec.md#extension

Thanks to @ruyrocha for the suggestion.
2024-07-15 20:47:55 +01:00
Ali Afsharzadeh
2755582c47 Remove --update flag from apk add command 2024-07-15 22:15:25 +03:30
18 changed files with 129 additions and 36 deletions

View File

@@ -14,7 +14,7 @@ COPY Gemfile Gemfile.lock kamal.gemspec ./
COPY lib/kamal/version.rb /kamal/lib/kamal/version.rb COPY lib/kamal/version.rb /kamal/lib/kamal/version.rb
# Install system dependencies # Install system dependencies
RUN apk add --no-cache --update build-base git docker openrc openssh-client-default \ RUN apk add --no-cache build-base git docker openrc openssh-client-default \
&& rc-update add docker boot \ && rc-update add docker boot \
&& gem install bundler --version=2.4.3 \ && gem install bundler --version=2.4.3 \
&& bundle install && bundle install

View File

@@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
kamal (1.8.0) kamal (1.8.1)
activesupport (>= 7.0) activesupport (>= 7.0)
base64 (~> 0.2) base64 (~> 0.2)
bcrypt_pbkdf (~> 1.0) bcrypt_pbkdf (~> 1.0)

View File

@@ -37,11 +37,24 @@ module Kamal::Cli
def load_env def load_env
if destination = options[:destination] if destination = options[:destination]
Dotenv.load(".env.#{destination}", ".env") if File.exist?(".kamal/env.#{destination}") || File.exist?(".kamal/env")
Dotenv.load(".kamal/env.#{destination}", ".kamal/env")
else else
loading_files = [ (".env" if File.exist?(".env")), (".env.#{destination}" if File.exist?(".env.#{destination}")) ].compact
if loading_files.any?
warn "Loading #{loading_files.join(" and ")} from the project root, use .kamal/env* instead"
Dotenv.load(".env.#{destination}", ".env")
end
end
else
if File.exist?(".kamal/env")
Dotenv.load(".kamal/env")
elsif File.exist?(".env")
warn "Loading .env from the project root is deprecated, use .kamal/env instead"
Dotenv.load(".env") Dotenv.load(".env")
end end
end end
end
def reset_env def reset_env
replace_env @original_env replace_env @original_env

View File

@@ -140,7 +140,7 @@ class Kamal::Cli::Build < Kamal::Cli::Base
mirror_hosts = Concurrent::Hash.new mirror_hosts = Concurrent::Hash.new
on(KAMAL.hosts) do |host| on(KAMAL.hosts) do |host|
first_mirror = capture_with_info(*KAMAL.builder.first_mirror).strip.presence first_mirror = capture_with_info(*KAMAL.builder.first_mirror).strip.presence
mirror_hosts[first_mirror] ||= host if first_mirror mirror_hosts[first_mirror] ||= host.to_s if first_mirror
rescue SSHKit::Command::Failed => e rescue SSHKit::Command::Failed => e
raise unless e.message =~ /error calling index: reflect: slice index out of range/ raise unless e.message =~ /error calling index: reflect: slice index out of range/
end end

View File

@@ -182,6 +182,15 @@ class Kamal::Cli::Main < Kamal::Cli::Base
desc "envify", "Create .env by evaluating .env.erb (or .env.staging.erb -> .env.staging when using -d staging)" desc "envify", "Create .env by evaluating .env.erb (or .env.staging.erb -> .env.staging when using -d staging)"
option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip .env file push" option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip .env file push"
def envify def envify
if destination = options[:destination]
env_template_path = ".kamal/env.#{destination}.erb"
env_path = ".kamal/env.#{destination}"
else
env_template_path = ".kamal/env.erb"
env_path = ".kamal/env"
end
unless Pathname.new(File.expand_path(env_template_path)).exist?
if destination = options[:destination] if destination = options[:destination]
env_template_path = ".env.#{destination}.erb" env_template_path = ".env.#{destination}.erb"
env_path = ".env.#{destination}" env_path = ".env.#{destination}"
@@ -190,6 +199,11 @@ class Kamal::Cli::Main < Kamal::Cli::Base
env_path = ".env" env_path = ".env"
end end
if Pathname.new(File.expand_path(env_template_path)).exist?
warn "Loading #{env_template_path} from the project root is deprecated, use .kamal/env[.<DESTINATION>].erb instead"
end
end
if Pathname.new(File.expand_path(env_template_path)).exist? if Pathname.new(File.expand_path(env_template_path)).exist?
# Ensure existing env doesn't pollute template evaluation # Ensure existing env doesn't pollute template evaluation
content = with_original_env { ERB.new(File.read(env_template_path), trim_mode: "-").result } content = with_original_env { ERB.new(File.read(env_template_path), trim_mode: "-").result }

View File

@@ -47,7 +47,7 @@ class Kamal::Configuration
@destination = destination @destination = destination
@declared_version = version @declared_version = version
validate! raw_config, example: validation_yml.symbolize_keys, context: "" validate! raw_config, example: validation_yml.symbolize_keys, context: "", with: Kamal::Configuration::Validator::Configuration
# Eager load config to validate it, these are first as they have dependencies later on # Eager load config to validate it, these are first as they have dependencies later on
@servers = Servers.new(config: self) @servers = Servers.new(config: self)

View File

@@ -2,13 +2,24 @@
# #
# Configuration is read from the `config/deploy.yml` # Configuration is read from the `config/deploy.yml`
# #
# Destinations
#
# When running commands, you can specify a destination with the `-d` flag, # When running commands, you can specify a destination with the `-d` flag,
# e.g. `kamal deploy -d staging` # e.g. `kamal deploy -d staging`
# #
# In this case the configuration will also be read from `config/deploy.staging.yml` # In this case the configuration will also be read from `config/deploy.staging.yml`
# and merged with the base configuration. # and merged with the base configuration.
# Extensions
# #
# The available configuration options are explained below. # Kamal will not accept unrecognized keys in the configuration file.
#
# However, you might want to declare a configuration block using YAML anchors
# and aliases to avoid repetition.
#
# You can use prefix a configuration section with `x-` to indicate that it is an
# extension. Kamal will ignore the extension and not raise an error.
# The service name # The service name
# This is a required value. It is used as the container name prefix. # This is a required value. It is used as the container name prefix.

View File

@@ -15,11 +15,10 @@ class Kamal::Configuration::Validator
def validate_against_example!(validation_config, example) def validate_against_example!(validation_config, example)
validate_type! validation_config, Hash validate_type! validation_config, Hash
if (unknown_keys = validation_config.keys - example.keys).any? check_unknown_keys! validation_config, example
unknown_keys_error unknown_keys
end
validation_config.each do |key, value| validation_config.each do |key, value|
next if extension?(key)
with_context(key) do with_context(key) do
example_value = example[key] example_value = example[key]
@@ -137,4 +136,18 @@ class Kamal::Configuration::Validator
ensure ensure
@context = old_context @context = old_context
end end
def allow_extensions?
false
end
def extension?(key)
key.to_s.start_with?("x-")
end
def check_unknown_keys!(config, example)
unknown_keys = config.keys - example.keys
unknown_keys.reject! { |key| extension?(key) } if allow_extensions?
unknown_keys_error unknown_keys if unknown_keys.present?
end
end end

View File

@@ -0,0 +1,6 @@
class Kamal::Configuration::Validator::Configuration < Kamal::Configuration::Validator
private
def allow_extensions?
true
end
end

View File

@@ -1,3 +1,3 @@
module Kamal module Kamal
VERSION = "1.8.0" VERSION = "1.8.1"
end end

View File

@@ -185,7 +185,7 @@ class CliBuildTest < CliTestCase
run_command("pull").tap do |output| run_command("pull").tap do |output|
assert_match /Pulling image on 1\.1\.1\.\d to seed the mirror\.\.\./, output assert_match /Pulling image on 1\.1\.1\.\d to seed the mirror\.\.\./, output
assert_match "Pulling image on remaining hosts...", output assert_match "Pulling image on remaining hosts...", output
assert_match /docker pull dhh\/app:999/, output assert_equal 4, output.scan(/docker pull dhh\/app:999/).size, output
assert_match "docker inspect -f '{{ .Config.Labels.service }}' dhh/app:999 | grep -x app || (echo \"Image dhh/app:999 is missing the 'service' label\" && exit 1)", output assert_match "docker inspect -f '{{ .Config.Labels.service }}' dhh/app:999 | grep -x app || (echo \"Image dhh/app:999 is missing the 'service' label\" && exit 1)", output
end end
end end
@@ -199,7 +199,7 @@ class CliBuildTest < CliTestCase
run_command("pull").tap do |output| run_command("pull").tap do |output|
assert_match /Pulling image on 1\.1\.1\.\d, 1\.1\.1\.\d to seed the mirrors\.\.\./, output assert_match /Pulling image on 1\.1\.1\.\d, 1\.1\.1\.\d to seed the mirrors\.\.\./, output
assert_match "Pulling image on remaining hosts...", output assert_match "Pulling image on remaining hosts...", output
assert_match /docker pull dhh\/app:999/, output assert_equal 4, output.scan(/docker pull dhh\/app:999/).size, output
assert_match "docker inspect -f '{{ .Config.Labels.service }}' dhh/app:999 | grep -x app || (echo \"Image dhh/app:999 is missing the 'service' label\" && exit 1)", output assert_match "docker inspect -f '{{ .Config.Labels.service }}' dhh/app:999 | grep -x app || (echo \"Image dhh/app:999 is missing the 'service' label\" && exit 1)", output
end end
end end

View File

@@ -42,12 +42,13 @@ class CliTestCase < ActiveSupport::TestCase
end end
def assert_hook_ran(hook, output, version:, service_version:, hosts:, command:, subcommand: nil, runtime: false) def assert_hook_ran(hook, output, version:, service_version:, hosts:, command:, subcommand: nil, runtime: false)
performer = Kamal::Git.email.presence || `whoami`.chomp whoami = `whoami`.chomp
performer = Kamal::Git.email.presence || whoami
service = service_version.split("@").first service = service_version.split("@").first
assert_match "Running the #{hook} hook...\n", output assert_match "Running the #{hook} hook...\n", output
expected = %r{Running\s/usr/bin/env\s\.kamal/hooks/#{hook}\sas\s#{performer}@localhost\n\s expected = %r{Running\s/usr/bin/env\s\.kamal/hooks/#{hook}\sas\s#{whoami}@localhost\n\s
DEBUG\s\[[0-9a-f]*\]\sCommand:\s\(\sexport\s DEBUG\s\[[0-9a-f]*\]\sCommand:\s\(\sexport\s
KAMAL_RECORDED_AT=\"\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ\"\s KAMAL_RECORDED_AT=\"\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ\"\s
KAMAL_PERFORMER=\"#{performer}\"\s KAMAL_PERFORMER=\"#{performer}\"\s

View File

@@ -447,9 +447,9 @@ class CliMainTest < CliTestCase
end end
test "envify" do test "envify" do
with_test_dotenv(".env.erb": "HELLO=<%= 'world' %>") do with_test_env_files("env.erb": "HELLO=<%= 'world' %>") do
run_command("envify") run_command("envify")
assert_equal("HELLO=world", File.read(".env")) assert_equal("HELLO=world", File.read(".kamal/env"))
end end
end end
@@ -461,32 +461,32 @@ class CliMainTest < CliTestCase
<% end -%> <% end -%>
EOF EOF
with_test_dotenv(".env.erb": file) do with_test_env_files("env.erb": file) do
run_command("envify") run_command("envify")
assert_equal("HELLO=world\nKEY=value\n", File.read(".env")) assert_equal("HELLO=world\nKEY=value\n", File.read(".kamal/env"))
end end
end end
test "envify with destination" do test "envify with destination" do
with_test_dotenv(".env.world.erb": "HELLO=<%= 'world' %>") do with_test_env_files("env.world.erb": "HELLO=<%= 'world' %>") do
run_command("envify", "-d", "world", config_file: "deploy_for_dest") run_command("envify", "-d", "world", config_file: "deploy_for_dest")
assert_equal "HELLO=world", File.read(".env.world") assert_equal "HELLO=world", File.read(".kamal/env.world")
end end
end end
test "envify with skip_push" do test "envify with skip_push" do
Pathname.any_instance.expects(:exist?).returns(true).times(1) Pathname.any_instance.expects(:exist?).returns(true).times(2)
File.expects(:read).with(".env.erb").returns("HELLO=<%= 'world' %>") File.expects(:read).with(".kamal/env.erb").returns("HELLO=<%= 'world' %>")
File.expects(:write).with(".env", "HELLO=world", perm: 0600) File.expects(:write).with(".kamal/env", "HELLO=world", perm: 0600)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:env:push").never Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:env:push").never
run_command("envify", "--skip-push") run_command("envify", "--skip-push")
end end
test "envify with clean env" do test "envify with clean env" do
with_test_dotenv(".env": "HELLO=already", ".env.erb": "HELLO=<%= ENV.fetch 'HELLO', 'never' %>") do with_test_env_files("env": "HELLO=already", "env.erb": "HELLO=<%= ENV.fetch 'HELLO', 'never' %>") do
run_command("envify", "--skip-push") run_command("envify", "--skip-push")
assert_equal "HELLO=never", File.read(".env") assert_equal "HELLO=never", File.read(".kamal/env")
end end
end end
@@ -542,16 +542,19 @@ class CliMainTest < CliTestCase
stdouted { Kamal::Cli::Main.start([ *command, "-c", "test/fixtures/#{config_file}.yml" ]) } stdouted { Kamal::Cli::Main.start([ *command, "-c", "test/fixtures/#{config_file}.yml" ]) }
end end
def with_test_dotenv(**files) def with_test_env_files(**files)
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
fixtures_dup = File.join(dir, "test") fixtures_dup = File.join(dir, "test")
FileUtils.mkdir_p(fixtures_dup) FileUtils.mkdir_p(fixtures_dup)
FileUtils.cp_r("test/fixtures/", fixtures_dup) FileUtils.cp_r("test/fixtures/", fixtures_dup)
Dir.chdir(dir) do Dir.chdir(dir) do
FileUtils.mkdir_p(".kamal")
Dir.chdir(".kamal") do
files.each do |filename, contents| files.each do |filename, contents|
File.binwrite(filename.to_s, contents) File.binwrite(filename.to_s, contents)
end end
end
yield yield
end end
end end

View File

@@ -344,4 +344,12 @@ class ConfigurationTest < ActiveSupport::TestCase
assert_raises(Kamal::ConfigurationError) { Kamal::Configuration.new(@deploy_with_roles.merge(retain_containers: 0)) } assert_raises(Kamal::ConfigurationError) { Kamal::Configuration.new(@deploy_with_roles.merge(retain_containers: 0)) }
end end
test "extensions" do
dest_config_file = Pathname.new(File.expand_path("fixtures/deploy_with_extensions.yml", __dir__))
config = Kamal::Configuration.create_from config_file: dest_config_file
assert_equal config.role(:web_tokyo).running_traefik?, true
assert_equal config.role(:web_chicago).running_traefik?, true
end
end end

View File

@@ -0,0 +1,24 @@
x-web: &web
traefik: true
service: app
image: dhh/app
servers:
web_chicago:
<<: *web
hosts:
- 1.1.1.1
- 1.1.1.2
web_tokyo:
<<: *web
hosts:
- 1.1.1.3
- 1.1.1.4
env:
REDIS_URL: redis://x/y
registry:
server: registry.digitalocean.com
username: user
password: pw
primary_role: web_tokyo

View File

@@ -97,7 +97,7 @@ class MainTest < IntegrationTest
private private
def assert_local_env_file(contents) def assert_local_env_file(contents)
assert_equal contents, deployer_exec("cat .env", capture: true) assert_equal contents, deployer_exec("cat .kamal/env", capture: true)
end end
def assert_envs(version:) def assert_envs(version:)
@@ -127,7 +127,7 @@ class MainTest < IntegrationTest
end end
def remove_local_env_file def remove_local_env_file
deployer_exec("rm .env") deployer_exec("rm .kamal/env")
end end
def assert_remote_env_file(contents, vm:) def assert_remote_env_file(contents, vm:)