Created
August 19, 2018 10:25
-
-
Save anthonygrees/a5720006e45dda1d3afe084245709a83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'pry' | |
require 'mixlib/shellout' | |
require 'aws-sdk-ec2' | |
configure do | |
enable :logging, :dump_errors, :raise_errors | |
end | |
get '/' do | |
'Hello World! This project takes notifications from Chef Automate about failed | |
Chef Client and Inspec runs, formats them, and sends the result to a HipChat | |
room. See <a href="https://github.com/kevinreedy/automate-hipchat-notifier"> | |
https://github.com/kevinreedy/automate-hipchat-notifier</a> for more details!' | |
end | |
post '/' do | |
message = JSON.parse(request.body.read) | |
# Write message to file for now for troubleshooting | |
File.write('./data/output.txt', JSON.pretty_generate(message)) | |
# A1 webhook test | |
if message['username'] == 'Chef_Automate' && message['attachments'] | |
puts 'Received webhook test from Chef Automate 1.x:' | |
puts JSON.pretty_generate(message) | |
halt 200 | |
end | |
# A2 webhook test | |
if message['text'] == 'TEST: Successful validation completed by Automate' | |
puts 'Received webhook test from Chef Automate 2.x:' | |
puts JSON.pretty_generate(message) | |
halt 200 | |
end | |
if message['type'] == 'node_failure' | |
puts 'Received node failure:' | |
puts JSON.pretty_generate(message) | |
halt 200 | |
end | |
if message['type'] == 'compliance_failure' | |
puts 'Received compliance failure' | |
# instance_id = 'i-0b52aaaf613a0122b' | |
null_route_sg = 'sg-e5f11195' | |
instance_id = message['node_name'] | |
puts "Quarantining #{message['node_name']}" | |
cmd = Mixlib::ShellOut.new("/root/.local/bin/aws ec2 modify-instance-attribute --instance-id #{instance_id} --groups \"#{null_route_sg}\" ") | |
cmd.run_command | |
puts "Result: #{cmd.stdout}" | |
halt 200 | |
end | |
# Didn't find a message we care about, so 404 | |
halt 404 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment