Created
May 22, 2018 12:38
-
-
Save aq/4ef24884d467cb0c0dd596025bfed403 to your computer and use it in GitHub Desktop.
Utility to execute queries and put them on gist/slack/...
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
#!/usr/bin/env ruby | |
require 'aws-sdk-resources' | |
require 'thor' | |
require 'fileutils' | |
require 'slack/post' | |
%w(aws_aki aws_sak aws_bucket aws_region postgres_url).each do |env_variable| | |
raise "Missing env variable: #{env_variable}" if ENV[env_variable].nil? | |
end | |
aws_credentials = Aws::Credentials.new ENV['aws_aki'], ENV['aws_sak'] | |
Aws.config.update credentials: aws_credentials, region: ENV['aws_region'] | |
class AwsS3Sender < Thor | |
desc "send FILE_NAME", "send a file to S3 dev bucket" | |
def send file_name | |
path = absolute_path file_name | |
key = "antoine/#{File.basename file_name}" | |
put_object key, File.open(path) | |
end | |
desc "send_sql FILE_NAME", "send an html output of an sql request to S3 dev bucket with html utf8 wrapper" | |
def send_sql(file_name) | |
path = absolute_path file_name | |
key = "antoine/#{file_name}" | |
put_object key, <<-HTML | |
<!DOCTYPE html> | |
<html><head><meta charset="utf-8"></head><body>#{File.read path}</body></html> | |
HTML | |
end | |
desc "exec_sql FILE_NAME", "exec sql on postgres_url" | |
def exec_sql file_name | |
raise "missing #{file_name}" unless File.exists? file_name | |
exec %Q{psql #{ENV['postgres_url']} -f "#{absolute_path file_name}"} | |
end | |
desc "send_sql FILE_NAME", "send an html output of an sql request to S3 dev bucket with html utf8 wrapper" | |
def exec_send_sql file_name | |
raise "missing #{file_name}" unless File.exists? file_name | |
html_file = "#{File.basename file_name, '.sql'}.html" | |
path = absolute_path html_file | |
`psql "#{ENV['postgres_url']}" -f "#{absolute_path file_name}" -H -o "#{path}"` | |
key = "antoine/#{html_file}" | |
put_object key, <<-HTML | |
<!DOCTYPE html> | |
<html><head><meta charset="utf-8"></head><body>#{File.read path}</body></html> | |
HTML | |
end | |
desc "post_sql_to_slack FILE_NAME TEXT [CHANNEL]", "create a gist and post it with its result to slack" | |
def post_sql_to_slack file_name, text, channel='#pro_smart_data' | |
result_url = exec_send_sql file_name | |
gist_url = create_gist text, absolute_path(file_name), result_url | |
post_to_slack text, result_url, gist_url, channel | |
end | |
desc "post_csv_to_slack SCRIPT_FILE_NAME TEXT RESULT_URL [CHANNEL]", "for a csv create a gist and post it with its result to slack" | |
def post_csv_to_slack script_file_name, text, result_url, channel='#pro_smart_data' | |
gist_url = create_gist text, absolute_path(script_file_name), result_url | |
post_to_slack text, result_url, gist_url, channel | |
end | |
private | |
def absolute_path file_name | |
file_name.match(/\//) ? file_name : | |
"#{File.dirname(__FILE__)}/#{file_name}" | |
end | |
# returns the public url on S3 | |
def put_object key, body | |
bucket = Aws::S3::Resource.new(region: ENV['aws_region']).bucket ENV['aws_bucket'] | |
bucket.put_object key: key, body: body, acl: 'public-read' | |
bucket.object(key).public_url.tap{|url|puts url} | |
end | |
def create_gist text, file_path, result_url | |
`gist -p -d "#{text} - result: #{result_url}" #{file_path}`.tap{|url|puts url} | |
end | |
def post_to_slack text, result_url, gist_url, channel | |
Slack::Post.configure( | |
webhook_url: 'https://hooks.slack.com/services/xxxxxx', | |
username: 'AQ' | |
) | |
text += "\n<#{result_url.strip}|Result> - <#{gist_url.strip}|Gist>." | |
Slack::Post.post text, channel, icon_emoji: ':heart:' | |
end | |
end | |
AwsS3Sender.start(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment