diff --git a/README.md b/README.md index 23fc8ad6..5da301cc 100644 --- a/README.md +++ b/README.md @@ -677,6 +677,15 @@ That'll post a line like follows to a preconfigured chatbot in Basecamp: [My App] [dhh] Rolled back to version d264c4e92470ad1bd18590f04466787262f605de ``` +In addition to the formatted message, MRSK sets a number of environment variables with the components of the broadcast. +You can use these (rather than the command argument) if you want more control over how the message is formatted. +MRSK currently sets: + +- `MRSK_PERFORMER` - the user performing the command +- `MRSK_DESTINATION` - the destination +- `MRSK_ROLE` - the specific role being targetted, if any +- `MRSK_EVENT` - text of the action (e.g. "Deployed app@150b24f") + ### Custom healthcheck MRSK defaults to checking the health of your application again `/up` on port 3000 up to 7 times. You can tailor the behaviour with the `healthcheck` setting: diff --git a/lib/mrsk/cli/base.rb b/lib/mrsk/cli/base.rb index f772b821..22c4a846 100644 --- a/lib/mrsk/cli/base.rb +++ b/lib/mrsk/cli/base.rb @@ -73,7 +73,9 @@ module Mrsk::Cli end def audit_broadcast(line) - run_locally { execute *MRSK.auditor.broadcast(line), verbosity: :debug } + if broadcast = MRSK.auditor.broadcast(line) + system(MRSK.auditor.broadcast_environment(line), broadcast) + end end def with_lock diff --git a/lib/mrsk/commands/auditor.rb b/lib/mrsk/commands/auditor.rb index 6915a521..deafa95f 100644 --- a/lib/mrsk/commands/auditor.rb +++ b/lib/mrsk/commands/auditor.rb @@ -22,6 +22,15 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base end end + def broadcast_environment(line) + { + "MRSK_PERFORMER" => performer, + "MRSK_ROLE" => role, + "MRSK_DESTINATION" => config.destination, + "MRSK_EVENT" => line + } + end + def reveal [ :tail, "-n", 50, audit_log_file ] end @@ -36,7 +45,7 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base end def tagged_broadcast_line(line) - tagged_line performer_tag, role_tag, line + tagged_line performer_tag, role_tag, destination_tag, line end def tagged_line(*tags_and_line) @@ -47,11 +56,19 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base "[#{Time.now.to_fs(:db)}]" end + def performer + `whoami`.strip + end + def performer_tag - "[#{`whoami`.strip}]" + "[#{performer}]" end def role_tag "[#{role}]" if role end + + def destination_tag + "[#{config.destination}]" if config.destination + end end diff --git a/test/commands/auditor_test.rb b/test/commands/auditor_test.rb index 55c3a32e..6068acef 100644 --- a/test/commands/auditor_test.rb +++ b/test/commands/auditor_test.rb @@ -31,9 +31,26 @@ class CommandsAuditorTest < ActiveSupport::TestCase end test "broadcast" do - assert_match \ - /bin\/audit_broadcast '\[.*\] app removed container'/, - new_command.broadcast("app removed container").join(" ") + Mrsk::Commands::Auditor.any_instance.stubs(:performer).returns("bob") + @role = "web" + @destination = "staging" + + assert_equal \ + ["bin/audit_broadcast", "'[bob] [web] [staging] app removed container'"], + new_command.broadcast("app removed container") + end + + test "broadcast environment" do + Mrsk::Commands::Auditor.any_instance.stubs(:performer).returns("bob") + @role = "web" + @destination = "staging" + + env = new_command.broadcast_environment("app removed container") + + assert_equal "bob", env["MRSK_PERFORMER"] + assert_equal "web", env["MRSK_ROLE"] + assert_equal "staging", env["MRSK_DESTINATION"] + assert_equal "app removed container", env["MRSK_EVENT"] end private