Merge branch 'main' into cleanup-excessive-containers-running
* main: Pull the primary host from the role Minimise holding the deploy lock
This commit is contained in:
@@ -8,25 +8,28 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
|
||||
cli = self
|
||||
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Tagging #{MRSK.config.absolute_image} as the latest image"), verbosity: :debug
|
||||
execute *MRSK.app.tag_current_as_latest
|
||||
end
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role).record("Booted app version #{version}"), verbosity: :debug
|
||||
|
||||
begin
|
||||
if capture_with_info(*MRSK.app(role: role).container_id_for_version(version)).present?
|
||||
tmp_version = "#{version}_#{SecureRandom.hex(8)}"
|
||||
info "Renaming container #{version} to #{tmp_version} as already deployed on #{host}"
|
||||
execute *MRSK.auditor(role: role).record("Renaming container #{version} to #{tmp_version}"), verbosity: :debug
|
||||
execute *MRSK.app(role: role).rename_container(version: version, new_version: tmp_version)
|
||||
end
|
||||
|
||||
old_version = capture_with_info(*MRSK.app(role: role).current_running_version, raise_on_non_zero_exit: false).strip
|
||||
execute *MRSK.app(role: role).run
|
||||
sleep MRSK.config.readiness_delay
|
||||
execute *MRSK.app(role: role).stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
|
||||
if capture_with_info(*MRSK.app(role: role).container_id_for_version(version), raise_on_non_zero_exit: false).present?
|
||||
tmp_version = "#{version}_#{SecureRandom.hex(8)}"
|
||||
info "Renaming container #{version} to #{tmp_version} as already deployed on #{host}"
|
||||
execute *MRSK.auditor(role: role).record("Renaming container #{version} to #{tmp_version}"), verbosity: :debug
|
||||
execute *MRSK.app(role: role).rename_container(version: version, new_version: tmp_version)
|
||||
end
|
||||
|
||||
old_version = capture_with_info(*MRSK.app(role: role).current_running_version, raise_on_non_zero_exit: false).strip
|
||||
execute *MRSK.app(role: role).run
|
||||
sleep MRSK.config.readiness_delay
|
||||
execute *MRSK.app(role: role).stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -77,22 +77,32 @@ module Mrsk::Cli
|
||||
end
|
||||
|
||||
def with_lock
|
||||
acquire_lock
|
||||
if MRSK.holding_lock?
|
||||
yield
|
||||
else
|
||||
acquire_lock
|
||||
|
||||
yield
|
||||
begin
|
||||
yield
|
||||
rescue
|
||||
if MRSK.hold_lock_on_error?
|
||||
error " \e[31mDeploy lock was not released\e[0m"
|
||||
else
|
||||
release_lock
|
||||
end
|
||||
|
||||
release_lock
|
||||
rescue
|
||||
error " \e[31mDeploy lock was not released\e[0m" if MRSK.lock_count > 0
|
||||
raise
|
||||
raise
|
||||
end
|
||||
|
||||
release_lock
|
||||
end
|
||||
end
|
||||
|
||||
def acquire_lock
|
||||
if MRSK.lock_count == 0
|
||||
say "Acquiring the deploy lock"
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.acquire("Automatic deploy lock", MRSK.config.version) }
|
||||
end
|
||||
MRSK.lock_count += 1
|
||||
say "Acquiring the deploy lock"
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.acquire("Automatic deploy lock", MRSK.config.version) }
|
||||
|
||||
MRSK.holding_lock = true
|
||||
rescue SSHKit::Runner::ExecuteError => e
|
||||
if e.message =~ /cannot create directory/
|
||||
invoke "mrsk:cli:lock:status", []
|
||||
@@ -103,10 +113,19 @@ module Mrsk::Cli
|
||||
end
|
||||
|
||||
def release_lock
|
||||
MRSK.lock_count -= 1
|
||||
if MRSK.lock_count == 0
|
||||
say "Releasing the deploy lock"
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.release }
|
||||
say "Releasing the deploy lock"
|
||||
on(MRSK.primary_host) { execute *MRSK.lock.release }
|
||||
|
||||
MRSK.holding_lock = false
|
||||
end
|
||||
|
||||
def hold_lock_on_error
|
||||
if MRSK.hold_lock_on_error?
|
||||
yield
|
||||
else
|
||||
MRSK.hold_lock_on_error = true
|
||||
yield
|
||||
MRSK.hold_lock_on_error = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -40,7 +40,9 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
|
||||
say "Detect stale containers...", :magenta
|
||||
invoke "mrsk:cli:app:stale_containers", [], invoke_options
|
||||
|
||||
invoke "mrsk:cli:app:boot", [], invoke_options
|
||||
hold_lock_on_error do
|
||||
invoke "mrsk:cli:app:boot", [], invoke_options
|
||||
end
|
||||
|
||||
say "Prune old containers and images...", :magenta
|
||||
invoke "mrsk:cli:prune:all", [], invoke_options
|
||||
@@ -71,7 +73,9 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
|
||||
say "Detect stale containers...", :magenta
|
||||
invoke "mrsk:cli:app:stale_containers", [], invoke_options
|
||||
|
||||
invoke "mrsk:cli:app:boot", [], invoke_options
|
||||
hold_lock_on_error do
|
||||
invoke "mrsk:cli:app:boot", [], invoke_options
|
||||
end
|
||||
end
|
||||
|
||||
audit_broadcast "Redeployed #{service_version} in #{runtime.round} seconds" unless options[:skip_broadcast]
|
||||
@@ -81,34 +85,41 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
|
||||
desc "rollback [VERSION]", "Rollback app to VERSION"
|
||||
def rollback(version)
|
||||
with_lock do
|
||||
MRSK.config.version = version
|
||||
invoke_options = deploy_options
|
||||
|
||||
if container_available?(version)
|
||||
say "Start version #{version}, then wait #{MRSK.config.readiness_delay}s for app to boot before stopping the old version...", :magenta
|
||||
|
||||
cli = self
|
||||
hold_lock_on_error do
|
||||
MRSK.config.version = version
|
||||
old_version = nil
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
if container_available?(version)
|
||||
say "Start version #{version}, then wait #{MRSK.config.readiness_delay}s for app to boot before stopping the old version...", :magenta
|
||||
|
||||
roles.each do |role|
|
||||
app = MRSK.app(role: role)
|
||||
old_version = capture_with_info(*app.current_running_version).strip.presence
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Tagging #{MRSK.config.absolute_image} as the latest image"), verbosity: :debug
|
||||
execute *MRSK.app.tag_current_as_latest
|
||||
end
|
||||
|
||||
execute *app.start
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
if old_version
|
||||
sleep MRSK.config.readiness_delay
|
||||
roles.each do |role|
|
||||
app = MRSK.app(role: role)
|
||||
old_version = capture_with_info(*app.current_running_version).strip.presence
|
||||
|
||||
execute *app.stop(version: old_version), raise_on_non_zero_exit: false
|
||||
execute *app.start
|
||||
|
||||
if old_version
|
||||
sleep MRSK.config.readiness_delay
|
||||
|
||||
execute *app.stop(version: old_version), raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
audit_broadcast "Rolled back #{service_version(Mrsk::Utils.abbreviate_version(old_version))} to #{service_version}" unless options[:skip_broadcast]
|
||||
else
|
||||
say "The app version '#{version}' is not available as a container (use 'mrsk app containers' for available versions)", :red
|
||||
audit_broadcast "Rolled back #{service_version(Mrsk::Utils.abbreviate_version(old_version))} to #{service_version}" unless options[:skip_broadcast]
|
||||
else
|
||||
say "The app version '#{version}' is not available as a container (use 'mrsk app containers' for available versions)", :red
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user