-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rubify sync check, add logs as attachments (#1867)
- Loading branch information
1 parent
acd23b9
commit 5db55e1
Showing
9 changed files
with
142 additions
and
55 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM fedora:36 | ||
|
||
RUN dnf install -y docker ruby ruby-devel make gcc | ||
|
||
# Install required Ruby packages | ||
RUN gem install docker-api slack-ruby-client |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'docker' | ||
|
||
# Tools to facilitate interacting with Docker | ||
module DockerUtils | ||
# returns the specified container logs as String | ||
def self.get_container_logs(container_name) | ||
container = Docker::Container.get container_name | ||
container.streaming_logs(stdout: true, stderr: true) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'slack-ruby-client' | ||
|
||
# Wrapper Slack client class to handle sending messages and uploading logs. | ||
class SlackClient | ||
@last_thread = nil | ||
@channel = nil | ||
@client = nil | ||
|
||
def initialize(channel, token) | ||
raise "Invalid channel name: #{channel}, must start with \#" unless channel.start_with? '#' | ||
raise 'Missing token' if token.nil? | ||
|
||
Slack.configure do |config| | ||
config.token = token | ||
end | ||
|
||
@channel = channel | ||
@client = Slack::Web::Client.new | ||
end | ||
|
||
# Posts a new message to configured channel. | ||
def post_message(text) | ||
msg = @client.chat_postMessage(channel: @channel, text: text) | ||
@last_thread = msg[:ts] | ||
end | ||
|
||
# Attaches files to the last posted thread. | ||
def attach_files(*files) | ||
files.each do |file| | ||
attach_file file | ||
end | ||
end | ||
|
||
# Attaches a file to the latest posted thread. | ||
def attach_file(file) | ||
raise "No such file #{file}" unless File.exist? file | ||
raise 'Need to create a thread before attaching a file.' if @last_thread.nil? | ||
|
||
@client.files_upload( | ||
channels: @channel, | ||
file: Faraday::UploadIO.new(file, 'text/plain'), | ||
filename: File.basename(file), | ||
initial_comment: 'Attached a file.', | ||
thread_ts: @last_thread | ||
) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'slack_client' | ||
require_relative 'docker_utils' | ||
require 'logger' | ||
require 'fileutils' | ||
|
||
# Retrieves an environmental variable, failing if its not set or empty. | ||
def get_and_assert_env_variable(name) | ||
var = ENV[name] | ||
raise "Please set #{name} environmental variable" if var.nil? || var.empty? | ||
|
||
var | ||
end | ||
|
||
SLACK_TOKEN = get_and_assert_env_variable 'SLACK_API_TOKEN' | ||
CHANNEL = get_and_assert_env_variable 'SLACK_NOTIF_CHANNEL' | ||
SCRIPTS_DIR = get_and_assert_env_variable 'SCRIPTS_DIR' | ||
LOG_DIR = get_and_assert_env_variable 'LOG_DIR' | ||
|
||
hostname = ARGV[0] | ||
raise 'No arguments supplied. Please provide Forest hostname, e.g. forest-mainnet' if ARGV.empty? | ||
|
||
# Current datetime, to append to the log files | ||
DATE = Time.new.strftime '%FT%H:%M:%S' | ||
LOG_HEALTH = "#{LOG_DIR}/#{hostname}_#{DATE}_health" | ||
LOG_FOREST = "#{LOG_DIR}/#{hostname}_#{DATE}_forest" | ||
LOG_SYNC = "#{LOG_DIR}/#{hostname}_#{DATE}_sync" | ||
|
||
# Create log directory | ||
FileUtils.mkdir_p LOG_DIR | ||
|
||
logger = Logger.new(LOG_SYNC) | ||
|
||
# Run the actual health check | ||
logger.info 'Running the health check...' | ||
health_check_passed = system("bash #{SCRIPTS_DIR}/health_check.sh #{hostname} > #{LOG_HEALTH} 2>&1") | ||
logger.info 'Health check finished' | ||
|
||
# Save the log capture from the Forest container | ||
container_logs = DockerUtils.get_container_logs hostname | ||
File.write(LOG_FOREST, container_logs) | ||
|
||
client = SlackClient.new CHANNEL, SLACK_TOKEN | ||
|
||
if health_check_passed | ||
client.post_message "✅ Sync check for #{hostname} passed. 🌲🌳🌲🌳🌲" | ||
else | ||
client.post_message "⛔ Sync check for #{hostname} fiascoed. 🔥🌲🔥 " | ||
end | ||
client.attach_files(LOG_HEALTH, LOG_SYNC, LOG_FOREST) | ||
|
||
logger.info 'Sync check finished' |
This file was deleted.
Oops, something went wrong.