Merge pull request #1520 from basecamp/inherit-lock

Inherit locks
This commit is contained in:
Donal McBreen
2025-04-22 09:31:35 +01:00
committed by GitHub
3 changed files with 43 additions and 4 deletions

View File

@@ -133,7 +133,13 @@ module Kamal::Cli
def run_hook(hook, **extra_details) def run_hook(hook, **extra_details)
if !options[:skip_hooks] && KAMAL.hook.hook_exists?(hook) if !options[:skip_hooks] && KAMAL.hook.hook_exists?(hook)
details = { hosts: KAMAL.hosts.join(","), roles: KAMAL.specific_roles&.join(","), command: command, subcommand: subcommand }.compact details = {
hosts: KAMAL.hosts.join(","),
roles: KAMAL.specific_roles&.join(","),
lock: KAMAL.holding_lock?.to_s,
command: command,
subcommand: subcommand
}.compact
say "Running the #{hook} hook...", :magenta say "Running the #{hook} hook...", :magenta
with_env KAMAL.hook.env(**details, **extra_details) do with_env KAMAL.hook.env(**details, **extra_details) do

View File

@@ -13,7 +13,7 @@ class Kamal::Commander
def reset def reset
self.verbosity = :info self.verbosity = :info
self.holding_lock = false self.holding_lock = ENV["KAMAL_LOCK"] == "true"
self.connected = false self.connected = false
@specifics = @specific_roles = @specific_hosts = nil @specifics = @specific_roles = @specific_hosts = nil
@config = @config_kwargs = nil @config = @config_kwargs = nil

View File

@@ -43,7 +43,7 @@ class CliMainTest < CliTestCase
with_test_secrets("secrets" => "DB_PASSWORD=secret") do with_test_secrets("secrets" => "DB_PASSWORD=secret") do
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false, "verbose" => true } invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false, "verbose" => true }
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options) Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options) Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true)) Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options) Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
@@ -53,7 +53,7 @@ class CliMainTest < CliTestCase
run_command("deploy", "--verbose").tap do |output| run_command("deploy", "--verbose").tap do |output|
assert_hook_ran "pre-connect", output assert_hook_ran "pre-connect", output
assert_match /Build and push app image/, output assert_match /Build and push app image/, output
assert_hook_ran "pre-deploy", output assert_hook_ran "pre-deploy", output
assert_match /Ensure kamal-proxy is running/, output assert_match /Ensure kamal-proxy is running/, output
assert_match /Detect stale containers/, output assert_match /Detect stale containers/, output
@@ -116,6 +116,32 @@ class CliMainTest < CliTestCase
end end
end end
test "deploy when inheriting lock" do
Thread.report_on_exception = false
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
with_kamal_lock_env do
KAMAL.reset
run_command("deploy").tap do |output|
assert_no_match /Acquiring the deploy lock/, output
assert_match /Build and push app image/, output
assert_match /Ensure kamal-proxy is running/, output
assert_match /Detect stale containers/, output
assert_match /Prune old containers and images/, output
assert_no_match /Releasing the deploy lock/, output
end
end
end
test "deploy error when locking" do test "deploy error when locking" do
Thread.report_on_exception = false Thread.report_on_exception = false
@@ -562,4 +588,11 @@ class CliMainTest < CliTestCase
def assert_file(file, content) def assert_file(file, content)
assert_match content, File.read(file) assert_match content, File.read(file)
end end
def with_kamal_lock_env
ENV["KAMAL_LOCK"] = "true"
yield
ensure
ENV.delete("KAMAL_LOCK")
end
end end