Add service_version and add running hook message

This commit is contained in:
Donal McBreen
2023-05-23 10:46:17 +01:00
parent 258887a451
commit 3b695ae127
7 changed files with 26 additions and 16 deletions

View File

@@ -881,6 +881,7 @@ firing a JSON webhook. These variables include:
- `MRSK_RECORDED_AT` - UTC timestamp in ISO 8601 format, e.g. `2023-04-14T17:07:31Z` - `MRSK_RECORDED_AT` - UTC timestamp in ISO 8601 format, e.g. `2023-04-14T17:07:31Z`
- `MRSK_PERFORMER` - the local user performing the command (from `whoami`) - `MRSK_PERFORMER` - the local user performing the command (from `whoami`)
- `MRSK_MESSAGE` - the full audit message, e.g. "Deployed app@150b24f" - `MRSK_MESSAGE` - the full audit message, e.g. "Deployed app@150b24f"
- `MRSK_SERVICE_VERSION` - an abbreviated version (for use in messages)
- `MRSK_DESTINATION` - optional: destination, e.g. "staging" - `MRSK_DESTINATION` - optional: destination, e.g. "staging"
- `MRSK_ROLE` - optional: role targeted, e.g. "web" - `MRSK_ROLE` - optional: role targeted, e.g. "web"

View File

@@ -132,13 +132,12 @@ module Mrsk::Cli
end end
def run_hook(hook, **details) def run_hook(hook, **details)
run_locally do if MRSK.hook.hook_exists?(hook)
if MRSK.hook.hook_exists?(hook) say "Running the #{hook} hook...", :magenta
begin run_locally do
MRSK.with_verbosity(:debug) { execute(*MRSK.hook.run(hook, **details)) } MRSK.with_verbosity(:debug) { execute *MRSK.hook.run(hook, **details) }
rescue SSHKit::Command::Failed rescue SSHKit::Command::Failed
raise HookError.new("Hook `#{hook}` failed") raise HookError.new("Hook `#{hook}` failed")
end
end end
end end
end end

View File

@@ -238,8 +238,4 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
def deploy_options def deploy_options
{ "version" => MRSK.config.version }.merge(options.without("skip_push")) { "version" => MRSK.config.version }.merge(options.without("skip_push"))
end end
def service_version(version = MRSK.config.abbreviated_version)
[ MRSK.config.service, version ].compact.join("@")
end
end end

View File

@@ -9,7 +9,7 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
# Runs remotely # Runs remotely
def record(line, **details) def record(line, **details)
append \ append \
[ :echo, audit_tags(**details).except(:version).to_s, line ], [ :echo, audit_tags(**details).except(:version, :service_version).to_s, line ],
audit_log_file audit_log_file
end end

View File

@@ -12,7 +12,12 @@ class Mrsk::Tags
{ recorded_at: Time.now.utc.iso8601, { recorded_at: Time.now.utc.iso8601,
performer: `whoami`.chomp, performer: `whoami`.chomp,
destination: config.destination, destination: config.destination,
version: config.version } version: config.version,
service_version: service_version(config) }
end
def service_version(config)
[ config.service, config.abbreviated_version ].compact.join("@")
end end
end end

View File

@@ -84,6 +84,13 @@ module Mrsk::Utils
# Abbreviate a git revhash for concise display # Abbreviate a git revhash for concise display
def abbreviate_version(version) def abbreviate_version(version)
version[0...7] if version if version
# Don't abbreviate <sha>_uncommitted_<etc>
if version.include?("_")
version
else
version[0...7]
end
end
end end
end end

View File

@@ -21,7 +21,8 @@ class CommandsHookTest < ActiveSupport::TestCase
{ env: { { env: {
"MRSK_RECORDED_AT" => @recorded_at, "MRSK_RECORDED_AT" => @recorded_at,
"MRSK_PERFORMER" => @performer, "MRSK_PERFORMER" => @performer,
"MRSK_VERSION" => "123" } } "MRSK_VERSION" => "123",
"MRSK_SERVICE_VERSION" => "app@123" } }
], new_command.run("foo") ], new_command.run("foo")
end end
@@ -31,7 +32,8 @@ class CommandsHookTest < ActiveSupport::TestCase
{ env: { { env: {
"MRSK_RECORDED_AT" => @recorded_at, "MRSK_RECORDED_AT" => @recorded_at,
"MRSK_PERFORMER" => @performer, "MRSK_PERFORMER" => @performer,
"MRSK_VERSION" => "123" } } "MRSK_VERSION" => "123",
"MRSK_SERVICE_VERSION" => "app@123" } }
], new_command(hooks_path: "custom/hooks/path").run("foo") ], new_command(hooks_path: "custom/hooks/path").run("foo")
end end