Created
February 8, 2018 08:17
-
-
Save vadv/b64c8fca951fd9c85787d1ac41b79856 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 "chef/handler" | |
require "etc" | |
require "date" | |
class TelegramHandler < Chef::Handler | |
BOT_API_KEY = 'BOTAPI:KEY' | |
CHAT_ID = -id | |
PROXY_ADDR = 'http://chef/telegram/' | |
def send_report | |
return if Chef::Config[:why_run] | |
return if node[:fqdn] =~ /kitchen/ | |
# success? ? run_status.updated_resources : run_status.formatted_exception | |
# не отсылаем не информативные сообщения | |
return if empty_message | |
uri = URI("#{PROXY_ADDR}/bot#{BOT_API_KEY}/sendMessage") | |
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json') | |
req.body = {'chat_id' => CHAT_ID, 'text' => text}.to_json | |
http = Net::HTTP.new(uri.host, uri.port) | |
response = http.request(req) | |
puts(response.body) if response.code.to_i > 300 | |
end | |
def report | |
begin | |
send_report | |
rescue Exception => e | |
puts("Send telegram message error: #{e}\n#{e.backtrace.join("\n")}") | |
end | |
end | |
end | |
handler = TelegramHandler.new() | |
Chef::Config.send("report_handlers") << handler | |
Chef::Config.send("exception_handlers") << handler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment