From 43672ec9a5b2bb150960fa5b2d9f876baac45738 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Mon, 25 Mar 2024 22:42:22 +0400 Subject: [PATCH 1/3] Added destination to the lock directory --- lib/kamal/commands/lock.rb | 4 +++- test/commands/lock_test.rb | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/kamal/commands/lock.rb b/lib/kamal/commands/lock.rb index 9b535f5f..f16de3ce 100644 --- a/lib/kamal/commands/lock.rb +++ b/lib/kamal/commands/lock.rb @@ -41,7 +41,9 @@ class Kamal::Commands::Lock < Kamal::Commands::Base end def lock_dir - "#{config.run_directory}/lock-#{config.service}" + dir_name = [ config.service, config.destination ].compact.join("-") + + "#{config.run_directory}/lock-#{dir_name}" end def lock_details_file diff --git a/test/commands/lock_test.rb b/test/commands/lock_test.rb index dc8981f9..504cef46 100644 --- a/test/commands/lock_test.rb +++ b/test/commands/lock_test.rb @@ -10,24 +10,24 @@ class CommandsLockTest < ActiveSupport::TestCase test "status" do assert_equal \ - "stat .kamal/lock-app > /dev/null && cat .kamal/lock-app/details | base64 -d", + "stat .kamal/lock-app-production > /dev/null && cat .kamal/lock-app-production/details | base64 -d", new_command.status.join(" ") end test "acquire" do assert_match \ - %r{mkdir \.kamal/lock-app && echo ".*" > \.kamal/lock-app/details}m, + %r{mkdir \.kamal/lock-app-production && echo ".*" > \.kamal/lock-app-production/details}m, new_command.acquire("Hello", "123").join(" ") end test "release" do assert_match \ - "rm .kamal/lock-app/details && rm -r .kamal/lock-app", + "rm .kamal/lock-app-production/details && rm -r .kamal/lock-app-production", new_command.release.join(" ") end private def new_command - Kamal::Commands::Lock.new(Kamal::Configuration.new(@config, version: "123")) + Kamal::Commands::Lock.new(Kamal::Configuration.new(@config, version: "123", destination: "production")) end end From cee449c26911915be4e9d76e67841836abea7971 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Wed, 27 Mar 2024 12:04:39 +0400 Subject: [PATCH 2/3] Put locks in a locks directory. Ensure that locks directory exits on a primary host. --- lib/kamal/cli/base.rb | 8 ++++++++ lib/kamal/commands/lock.rb | 12 ++++++++++-- test/cli/app_test.rb | 2 +- test/cli/cli_test_case.rb | 6 ++++-- test/cli/lock_test.rb | 2 +- test/cli/main_test.rb | 14 ++++++++++---- test/commands/lock_test.rb | 6 +++--- 7 files changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/kamal/cli/base.rb b/lib/kamal/cli/base.rb index 12a72e66..8cf303e5 100644 --- a/lib/kamal/cli/base.rb +++ b/lib/kamal/cli/base.rb @@ -86,6 +86,8 @@ module Kamal::Cli ensure_run_directory + ensure_locks_directory + acquire_lock begin @@ -191,5 +193,11 @@ module Kamal::Cli execute(*KAMAL.server.ensure_run_directory) end end + + def ensure_locks_directory + on(KAMAL.primary_host) do + execute(*KAMAL.lock.ensure_locks_directory) + end + end end end diff --git a/lib/kamal/commands/lock.rb b/lib/kamal/commands/lock.rb index f16de3ce..395b6f3a 100644 --- a/lib/kamal/commands/lock.rb +++ b/lib/kamal/commands/lock.rb @@ -21,6 +21,10 @@ class Kamal::Commands::Lock < Kamal::Commands::Base read_lock_details end + def ensure_locks_directory + [ :mkdir, "-p", locks_dir ] + end + private def write_lock_details(message, version) write \ @@ -40,14 +44,18 @@ class Kamal::Commands::Lock < Kamal::Commands::Base "/dev/null" end + def locks_dir + File.join(config.run_directory, "locks") + end + def lock_dir dir_name = [ config.service, config.destination ].compact.join("-") - "#{config.run_directory}/lock-#{dir_name}" + File.join(locks_dir, dir_name) end def lock_details_file - [ lock_dir, :details ].join("/") + File.join(lock_dir, "details") end def lock_details(message, version) diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 717531f2..416bd895 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -45,7 +45,7 @@ class CliAppTest < CliTestCase end test "boot uses group strategy when specified" do - Kamal::Cli::App.any_instance.stubs(:on).with("1.1.1.1").twice # acquire & release lock + Kamal::Cli::App.any_instance.stubs(:on).with("1.1.1.1").times(3) # ensure locks dir, acquire & release lock Kamal::Cli::App.any_instance.stubs(:on).with([ "1.1.1.1" ]) # tag container # Strategy is used when booting the containers diff --git a/test/cli/cli_test_case.rb b/test/cli/cli_test_case.rb index ebe92293..1e103715 100644 --- a/test/cli/cli_test_case.rb +++ b/test/cli/cli_test_case.rb @@ -31,9 +31,11 @@ class CliTestCase < ActiveSupport::TestCase SSHKit::Backend::Abstract.any_instance.stubs(:execute) .with { |*args| args == [ :mkdir, "-p", ".kamal" ] } SSHKit::Backend::Abstract.any_instance.stubs(:execute) - .with { |arg1, arg2| arg1 == :mkdir && arg2 == ".kamal/lock-app" } + .with { |arg1, arg2, arg3| arg1 == :mkdir && arg2 == "-p" && arg3 == ".kamal/locks" } SSHKit::Backend::Abstract.any_instance.stubs(:execute) - .with { |arg1, arg2| arg1 == :rm && arg2 == ".kamal/lock-app/details" } + .with { |arg1, arg2| arg1 == :mkdir && arg2 == ".kamal/locks/app" } + SSHKit::Backend::Abstract.any_instance.stubs(:execute) + .with { |arg1, arg2| arg1 == :rm && arg2 == ".kamal/locks/app/details" } end def assert_hook_ran(hook, output, version:, service_version:, hosts:, command:, subcommand: nil, runtime: nil) diff --git a/test/cli/lock_test.rb b/test/cli/lock_test.rb index 66482153..f6874d49 100644 --- a/test/cli/lock_test.rb +++ b/test/cli/lock_test.rb @@ -3,7 +3,7 @@ require_relative "cli_test_case" class CliLockTest < CliTestCase test "status" do run_command("status").tap do |output| - assert_match "Running /usr/bin/env stat .kamal/lock-app > /dev/null && cat .kamal/lock-app/details | base64 -d on 1.1.1.1", output + assert_match "Running /usr/bin/env stat .kamal/locks/app > /dev/null && cat .kamal/locks/app/details | base64 -d on 1.1.1.1", output end end diff --git a/test/cli/main_test.rb b/test/cli/main_test.rb index 2c0bd476..6111ade8 100644 --- a/test/cli/main_test.rb +++ b/test/cli/main_test.rb @@ -102,11 +102,14 @@ class CliMainTest < CliTestCase .with { |*args| args == [ :mkdir, "-p", ".kamal" ] } SSHKit::Backend::Abstract.any_instance.stubs(:execute) - .with { |*arg| arg[0..1] == [ :mkdir, ".kamal/lock-app" ] } - .raises(RuntimeError, "mkdir: cannot create directory ‘kamal_lock-app’: File exists") + .with { |*args| args == [ :mkdir, "-p", ".kamal/locks" ] } + + SSHKit::Backend::Abstract.any_instance.stubs(:execute) + .with { |*arg| arg[0..1] == [ :mkdir, ".kamal/locks/app" ] } + .raises(RuntimeError, "mkdir: cannot create directory ‘kamal/locks/app’: File exists") SSHKit::Backend::Abstract.any_instance.expects(:capture_with_debug) - .with(:stat, ".kamal/lock-app", ">", "/dev/null", "&&", :cat, ".kamal/lock-app/details", "|", :base64, "-d") + .with(:stat, ".kamal/locks/app", ">", "/dev/null", "&&", :cat, ".kamal/locks/app/details", "|", :base64, "-d") assert_raises(Kamal::Cli::LockError) do run_command("deploy") @@ -120,7 +123,10 @@ class CliMainTest < CliTestCase .with { |*args| args == [ :mkdir, "-p", ".kamal" ] } SSHKit::Backend::Abstract.any_instance.stubs(:execute) - .with { |*arg| arg[0..1] == [ :mkdir, ".kamal/lock-app" ] } + .with { |*args| args == [ :mkdir, "-p", ".kamal/locks" ] } + + SSHKit::Backend::Abstract.any_instance.stubs(:execute) + .with { |*arg| arg[0..1] == [ :mkdir, ".kamal/locks/app" ] } .raises(SocketError, "getaddrinfo: nodename nor servname provided, or not known") assert_raises(SSHKit::Runner::ExecuteError) do diff --git a/test/commands/lock_test.rb b/test/commands/lock_test.rb index 504cef46..fc2b15ab 100644 --- a/test/commands/lock_test.rb +++ b/test/commands/lock_test.rb @@ -10,19 +10,19 @@ class CommandsLockTest < ActiveSupport::TestCase test "status" do assert_equal \ - "stat .kamal/lock-app-production > /dev/null && cat .kamal/lock-app-production/details | base64 -d", + "stat .kamal/locks/app-production > /dev/null && cat .kamal/locks/app-production/details | base64 -d", new_command.status.join(" ") end test "acquire" do assert_match \ - %r{mkdir \.kamal/lock-app-production && echo ".*" > \.kamal/lock-app-production/details}m, + %r{mkdir \.kamal/locks/app-production && echo ".*" > \.kamal/locks/app-production/details}m, new_command.acquire("Hello", "123").join(" ") end test "release" do assert_match \ - "rm .kamal/lock-app-production/details && rm -r .kamal/lock-app-production", + "rm .kamal/locks/app-production/details && rm -r .kamal/locks/app-production", new_command.release.join(" ") end From 699bcc0d27c7ac538e3a74fe4ad6b5eb958f17a1 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Wed, 27 Mar 2024 20:56:47 +0400 Subject: [PATCH 3/3] Combined two methods and into one --- lib/kamal/cli/base.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/kamal/cli/base.rb b/lib/kamal/cli/base.rb index 8cf303e5..39b546f1 100644 --- a/lib/kamal/cli/base.rb +++ b/lib/kamal/cli/base.rb @@ -84,9 +84,7 @@ module Kamal::Cli run_hook "pre-connect" - ensure_run_directory - - ensure_locks_directory + ensure_run_and_locks_directory acquire_lock @@ -188,13 +186,11 @@ module Kamal::Cli instance_variable_get("@_invocations").first end - def ensure_run_directory + def ensure_run_and_locks_directory on(KAMAL.hosts) do execute(*KAMAL.server.ensure_run_directory) end - end - def ensure_locks_directory on(KAMAL.primary_host) do execute(*KAMAL.lock.ensure_locks_directory) end