From 3b695ae127a256ce2ca558d6f1c322c7a44f5a64 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Tue, 23 May 2023 10:46:17 +0100 Subject: [PATCH] Add service_version and add running hook message --- README.md | 1 + lib/mrsk/cli/base.rb | 13 ++++++------- lib/mrsk/cli/main.rb | 4 ---- lib/mrsk/commands/auditor.rb | 2 +- lib/mrsk/tags.rb | 7 ++++++- lib/mrsk/utils.rb | 9 ++++++++- test/commands/hook_test.rb | 6 ++++-- 7 files changed, 26 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b5e9b6bf..227d0ba2 100644 --- a/README.md +++ b/README.md @@ -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_PERFORMER` - the local user performing the command (from `whoami`) - `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_ROLE` - optional: role targeted, e.g. "web" diff --git a/lib/mrsk/cli/base.rb b/lib/mrsk/cli/base.rb index cfe826a5..038ac3f0 100644 --- a/lib/mrsk/cli/base.rb +++ b/lib/mrsk/cli/base.rb @@ -132,13 +132,12 @@ module Mrsk::Cli end def run_hook(hook, **details) - run_locally do - if MRSK.hook.hook_exists?(hook) - begin - MRSK.with_verbosity(:debug) { execute(*MRSK.hook.run(hook, **details)) } - rescue SSHKit::Command::Failed - raise HookError.new("Hook `#{hook}` failed") - end + if MRSK.hook.hook_exists?(hook) + say "Running the #{hook} hook...", :magenta + run_locally do + MRSK.with_verbosity(:debug) { execute *MRSK.hook.run(hook, **details) } + rescue SSHKit::Command::Failed + raise HookError.new("Hook `#{hook}` failed") end end end diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index 45f996a5..d305ebb1 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -238,8 +238,4 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base def deploy_options { "version" => MRSK.config.version }.merge(options.without("skip_push")) end - - def service_version(version = MRSK.config.abbreviated_version) - [ MRSK.config.service, version ].compact.join("@") - end end diff --git a/lib/mrsk/commands/auditor.rb b/lib/mrsk/commands/auditor.rb index c11f63f8..5089fb4d 100644 --- a/lib/mrsk/commands/auditor.rb +++ b/lib/mrsk/commands/auditor.rb @@ -9,7 +9,7 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base # Runs remotely def record(line, **details) append \ - [ :echo, audit_tags(**details).except(:version).to_s, line ], + [ :echo, audit_tags(**details).except(:version, :service_version).to_s, line ], audit_log_file end diff --git a/lib/mrsk/tags.rb b/lib/mrsk/tags.rb index 2e9e4cf5..c134a8f7 100644 --- a/lib/mrsk/tags.rb +++ b/lib/mrsk/tags.rb @@ -12,7 +12,12 @@ class Mrsk::Tags { recorded_at: Time.now.utc.iso8601, performer: `whoami`.chomp, 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 diff --git a/lib/mrsk/utils.rb b/lib/mrsk/utils.rb index 62dc6d8d..e463f547 100644 --- a/lib/mrsk/utils.rb +++ b/lib/mrsk/utils.rb @@ -84,6 +84,13 @@ module Mrsk::Utils # Abbreviate a git revhash for concise display def abbreviate_version(version) - version[0...7] if version + if version + # Don't abbreviate _uncommitted_ + if version.include?("_") + version + else + version[0...7] + end + end end end diff --git a/test/commands/hook_test.rb b/test/commands/hook_test.rb index f88d87b3..726ef7d8 100644 --- a/test/commands/hook_test.rb +++ b/test/commands/hook_test.rb @@ -21,7 +21,8 @@ class CommandsHookTest < ActiveSupport::TestCase { env: { "MRSK_RECORDED_AT" => @recorded_at, "MRSK_PERFORMER" => @performer, - "MRSK_VERSION" => "123" } } + "MRSK_VERSION" => "123", + "MRSK_SERVICE_VERSION" => "app@123" } } ], new_command.run("foo") end @@ -31,7 +32,8 @@ class CommandsHookTest < ActiveSupport::TestCase { env: { "MRSK_RECORDED_AT" => @recorded_at, "MRSK_PERFORMER" => @performer, - "MRSK_VERSION" => "123" } } + "MRSK_VERSION" => "123", + "MRSK_SERVICE_VERSION" => "app@123" } } ], new_command(hooks_path: "custom/hooks/path").run("foo") end