Add a deploy lock for commands that are unsafe to run concurrently.
The lock is taken by creating a `mrsk_lock` directory on the primary
host. Details of who took the lock are added to a details file in that
directory.
Additional CLI commands have been added to manual release and acquire
the lock and to check its status.
```
Commands:
mrsk lock acquire -m, --message=MESSAGE # Acquire the deploy lock
mrsk lock help [COMMAND] # Describe subcommands or one specific subcommand
mrsk lock release # Release the deploy lock
mrsk lock status # Report lock status
Options:
-v, [--verbose], [--no-verbose] # Detailed logging
-q, [--quiet], [--no-quiet] # Minimal logging
[--version=VERSION] # Run commands against a specific app version
-p, [--primary], [--no-primary] # Run commands only on primary host instead of all
-h, [--hosts=HOSTS] # Run commands on these hosts instead of all (separate by comma)
-r, [--roles=ROLES] # Run commands on these roles instead of all (separate by comma)
-c, [--config-file=CONFIG_FILE] # Path to config file
# Default: config/deploy.yml
-d, [--destination=DESTINATION] # Specify destination to be used for config file (staging -> deploy.staging.yml)
-B, [--skip-broadcast], [--no-skip-broadcast] # Skip audit broadcasts
```
If we add support for running multiple deployments on a single server
we'll need to extend the locking to lock per deployment.
38 lines
973 B
Ruby
38 lines
973 B
Ruby
class Mrsk::Cli::Lock < Mrsk::Cli::Base
|
|
desc "status", "Report lock status"
|
|
def status
|
|
handle_missing_lock do
|
|
on(MRSK.primary_host) { puts capture_with_info(*MRSK.lock.status) }
|
|
end
|
|
end
|
|
|
|
desc "acquire", "Acquire the deploy lock"
|
|
option :message, aliases: "-m", type: :string, desc: "A lock mesasge", required: true
|
|
def acquire
|
|
message = options[:message]
|
|
handle_missing_lock do
|
|
on(MRSK.primary_host) { execute *MRSK.lock.acquire(message, MRSK.config.version) }
|
|
say "Set the deploy lock"
|
|
end
|
|
end
|
|
|
|
desc "release", "Release the deploy lock"
|
|
def release
|
|
handle_missing_lock do
|
|
on(MRSK.primary_host) { execute *MRSK.lock.release }
|
|
say "Removed the deploy lock"
|
|
end
|
|
end
|
|
|
|
private
|
|
def handle_missing_lock
|
|
yield
|
|
rescue SSHKit::Runner::ExecuteError => e
|
|
if e.message =~ /No such file or directory/
|
|
say "There is no deploy lock"
|
|
else
|
|
raise
|
|
end
|
|
end
|
|
end
|