Allow customizing audit broadcast with env
When invoking the audit broadcast command, provide a few environment variables so that people can customize the format of the message if they want. We currently provide `MRSK_PERFORMER`, `MRSK_ROLE`, `MRSK_DESTINATION` and `MRSK_EVENT`. Also adds the destination to the default message, which we continue to send as the first argument as before.
This commit is contained in:
@@ -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
|
[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
|
### 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:
|
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:
|
||||||
|
|||||||
@@ -73,7 +73,9 @@ module Mrsk::Cli
|
|||||||
end
|
end
|
||||||
|
|
||||||
def audit_broadcast(line)
|
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
|
end
|
||||||
|
|
||||||
def with_lock
|
def with_lock
|
||||||
|
|||||||
@@ -22,6 +22,15 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def broadcast_environment(line)
|
||||||
|
{
|
||||||
|
"MRSK_PERFORMER" => performer,
|
||||||
|
"MRSK_ROLE" => role,
|
||||||
|
"MRSK_DESTINATION" => config.destination,
|
||||||
|
"MRSK_EVENT" => line
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def reveal
|
def reveal
|
||||||
[ :tail, "-n", 50, audit_log_file ]
|
[ :tail, "-n", 50, audit_log_file ]
|
||||||
end
|
end
|
||||||
@@ -36,7 +45,7 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def tagged_broadcast_line(line)
|
def tagged_broadcast_line(line)
|
||||||
tagged_line performer_tag, role_tag, line
|
tagged_line performer_tag, role_tag, destination_tag, line
|
||||||
end
|
end
|
||||||
|
|
||||||
def tagged_line(*tags_and_line)
|
def tagged_line(*tags_and_line)
|
||||||
@@ -47,11 +56,19 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
|
|||||||
"[#{Time.now.to_fs(:db)}]"
|
"[#{Time.now.to_fs(:db)}]"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def performer
|
||||||
|
`whoami`.strip
|
||||||
|
end
|
||||||
|
|
||||||
def performer_tag
|
def performer_tag
|
||||||
"[#{`whoami`.strip}]"
|
"[#{performer}]"
|
||||||
end
|
end
|
||||||
|
|
||||||
def role_tag
|
def role_tag
|
||||||
"[#{role}]" if role
|
"[#{role}]" if role
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def destination_tag
|
||||||
|
"[#{config.destination}]" if config.destination
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -31,9 +31,26 @@ class CommandsAuditorTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "broadcast" do
|
test "broadcast" do
|
||||||
assert_match \
|
Mrsk::Commands::Auditor.any_instance.stubs(:performer).returns("bob")
|
||||||
/bin\/audit_broadcast '\[.*\] app removed container'/,
|
@role = "web"
|
||||||
new_command.broadcast("app removed container").join(" ")
|
@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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
Reference in New Issue
Block a user