Skip to content

Commit 54b2c79

Browse files
authored
Merge pull request #1520 from basecamp/inherit-lock
Inherit locks
2 parents d464707 + 04568de commit 54b2c79

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

lib/kamal/cli/base.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,13 @@ def raise_if_locked
133133

134134
def run_hook(hook, **extra_details)
135135
if !options[:skip_hooks] && KAMAL.hook.hook_exists?(hook)
136-
details = { hosts: KAMAL.hosts.join(","), roles: KAMAL.specific_roles&.join(","), command: command, subcommand: subcommand }.compact
136+
details = {
137+
hosts: KAMAL.hosts.join(","),
138+
roles: KAMAL.specific_roles&.join(","),
139+
lock: KAMAL.holding_lock?.to_s,
140+
command: command,
141+
subcommand: subcommand
142+
}.compact
137143

138144
say "Running the #{hook} hook...", :magenta
139145
with_env KAMAL.hook.env(**details, **extra_details) do

lib/kamal/commander.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize
1313

1414
def reset
1515
self.verbosity = :info
16-
self.holding_lock = false
16+
self.holding_lock = ENV["KAMAL_LOCK"] == "true"
1717
self.connected = false
1818
@specifics = @specific_roles = @specific_hosts = nil
1919
@config = @config_kwargs = nil

test/cli/main_test.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CliMainTest < CliTestCase
4343
with_test_secrets("secrets" => "DB_PASSWORD=secret") do
4444
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false, "verbose" => true }
4545

46-
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
46+
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
4747
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
4848
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
4949
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
@@ -53,7 +53,7 @@ class CliMainTest < CliTestCase
5353

5454
run_command("deploy", "--verbose").tap do |output|
5555
assert_hook_ran "pre-connect", output
56-
assert_match /Build and push app image/, output
56+
assert_match /Build and push app image/, output
5757
assert_hook_ran "pre-deploy", output
5858
assert_match /Ensure kamal-proxy is running/, output
5959
assert_match /Detect stale containers/, output
@@ -116,6 +116,32 @@ class CliMainTest < CliTestCase
116116
end
117117
end
118118

119+
test "deploy when inheriting lock" do
120+
Thread.report_on_exception = false
121+
122+
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }
123+
124+
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:build:deliver", [], invoke_options)
125+
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:proxy:boot", [], invoke_options)
126+
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true))
127+
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:boot", [], invoke_options)
128+
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:prune:all", [], invoke_options)
129+
130+
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)
131+
132+
with_kamal_lock_env do
133+
KAMAL.reset
134+
run_command("deploy").tap do |output|
135+
assert_no_match /Acquiring the deploy lock/, output
136+
assert_match /Build and push app image/, output
137+
assert_match /Ensure kamal-proxy is running/, output
138+
assert_match /Detect stale containers/, output
139+
assert_match /Prune old containers and images/, output
140+
assert_no_match /Releasing the deploy lock/, output
141+
end
142+
end
143+
end
144+
119145
test "deploy error when locking" do
120146
Thread.report_on_exception = false
121147

@@ -562,4 +588,11 @@ def with_config_files
562588
def assert_file(file, content)
563589
assert_match content, File.read(file)
564590
end
591+
592+
def with_kamal_lock_env
593+
ENV["KAMAL_LOCK"] = "true"
594+
yield
595+
ensure
596+
ENV.delete("KAMAL_LOCK")
597+
end
565598
end

0 commit comments

Comments
 (0)