From 32b01287497ac5645de02d206cf55a671becb984 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Sat, 21 Nov 2015 23:04:28 +0400 Subject: [PATCH] initial commit --- .env.example | 7 +++++++ README.md | 49 ++++++++++++++++++++++++++++++++++++++++++++ config/schedule.rb | 17 +++++++++++++++ fucking_coffee.rb | 17 +++++++++++++++ hangover.rb | 45 ++++++++++++++++++++++++++++++++++++++++ kumar_asshole.rb | 30 +++++++++++++++++++++++++++ smack_my_bitch_up.rb | 43 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 208 insertions(+) create mode 100644 .env.example create mode 100644 README.md create mode 100644 config/schedule.rb create mode 100755 fucking_coffee.rb create mode 100755 hangover.rb create mode 100755 kumar_asshole.rb create mode 100755 smack_my_bitch_up.rb diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3be3ba0 --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +# Set these environment variables or edit and copy this file into `.env` +# See: https://github.com/bkeepers/dotenv + +TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +TWILIO_AUTH_TOKEN=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy +GMAIL_USERNAME=admin@example.org +GMAIL_PASSWORD=password diff --git a/README.md b/README.md new file mode 100644 index 0000000..67d03cf --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# DevOps Scripts + +Based on a _[true +story](https://www.jitbit.com/alexblog/249-now-thats-what-i-call-a-hacker/)_: + +> xxx: OK, so, our build engineer has left for another company. The dude was literally living inside the terminal. You know, that type of a guy who loves Vim, creates diagrams in Dot and writes wiki-posts in Markdown... If something - anything - requires more than 90 seconds of his time, he writes a script to automate that. + +> xxx: So we're sitting here, looking through his, uhm, "legacy" + +> xxx: You're gonna love this + +> xxx: `smack-my-bitch-up.sh` - sends a text message "late at work" to his wife (apparently). Automatically picks reasons from an array of strings, randomly. Runs inside a cron-job. The job fires if there are active SSH-sessions on the server after 9pm with his login. + +> xxx: `kumar-asshole.sh` - scans the inbox for emails from "Kumar" (a DBA at our clients). Looks for keywords like "help", "trouble", "sorry" etc. If keywords are found - the script SSHes into the clients server and rolls back the staging database to the latest backup. Then sends a reply "no worries mate, be careful next time". + +> xxx: `hangover.sh` - another cron-job that is set to specific dates. Sends automated emails like "not feeling well/gonna work from home" etc. Adds a random "reason" from another predefined array of strings. Fires if there are no interactive sessions on the server at 8:45am. + +> xxx: (and the oscar goes to) `fucking-coffee.sh` - this one waits exactly 17 seconds (!), then opens an SSH session to our coffee-machine (we had no frikin idea the coffee machine is on the network, runs linux and has SSHD up and running) and sends some weird gibberish to it. Looks binary. Turns out this thing starts brewing a mid-sized half-caf latte and waits another 24 (!) seconds before pouring it into a cup. The timing is exactly how long it takes to walk to the machine from the dudes desk. + +> xxx: holy sh*t I'm keeping those + +Scripts are written in Ruby. +Pull requests with other implementations (Python, Perl, Shell, etc) are welcome. + +### Usage + +Install required gems: `gem install dotenv twilio gmail whenever` +Set environment variables. See `.env.example` + +Example cron: + +```sh +# Runs `smack_my_bitch_up` daily at 9:20 am. +20 21 * * * /bin/bash -l -c 'ruby smack_my_bitch_up.rb' + +# Runs `kumar_asshole` every 10 minutes. +45 8 * * * /bin/bash -l -c 'ruby hangover.rb' + +# Runs `hangover` daily at 8:45 am. +0,10,20,30,40,50 * * * * /bin/bash -l -c 'ruby kumar_asshole.rb' + +# Runs `fucking_coffee` hourly from 9am to 6pm. +9,10,11,12,13,14,15,16,17,18 * * * * /bin/bash -l -c 'ruby fucking_coffee.rb' +``` + +Check `config/schedule.rb`. + +--- +Code is released under WTFPL. diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 0000000..b5c65c9 --- /dev/null +++ b/config/schedule.rb @@ -0,0 +1,17 @@ +# http://github.com/javan/whenever + +every :day, at: '9:20 pm' do + command 'ruby smack_my_bitch_up.rb' +end + +every 10.minutes do + command 'ruby kumar_asshole.rb' +end + +every :day, at: '8:45 am' do + command 'ruby hangover.rb' +end + +every :hour, at: 9..18 do + command 'ruby fucking_coffee.rb' +end diff --git a/fucking_coffee.rb b/fucking_coffee.rb new file mode 100755 index 0000000..52319b6 --- /dev/null +++ b/fucking_coffee.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby + +# Skip on weekends +exit if Time.now.saturday? || Time.now.sunday? + +require 'net/telnet' + +coffee_machine_ip = '10.10.42.42' +password = '1234' +password_prompt = 'Password: ' + +con = Net::Telnet.new('Host' => coffee_machine_ip) +con.cmd('String' => password, 'Match' => /#{password_prompt}/) +con.cmd('sys brew') +sleep 64 +con.cmd('sys pour') +con.close diff --git a/hangover.rb b/hangover.rb new file mode 100755 index 0000000..3d23564 --- /dev/null +++ b/hangover.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby + +# Skip on weekends +exit if Time.now.saturday? || Time.now.sunday? + +log_file_name = File.dirname(__FILE__) + '/logs/hangover.txt' + +# Be sure that logs dir always exists +Dir.mkdir('logs') unless File.exists?(log_file_name) + +LOG_FILE = File.open(log_file_name, 'a+') + +# Exit early if sessions with my_username are found +exit unless `who`[/my_username/].nil? + +require 'dotenv' +require 'twilio-ruby' + +Dotenv.load + +TWILIO_ACCOUNT_SID = ENV['TWILIO_ACCOUNT_SID'] +TWILIO_AUTH_TOKEN = ENV['TWILIO_AUTH_TOKEN'] + +@twilio = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN + +# Phone numbers +my_number = '+xxx' +number_of_boss = '+xxx' + +excuses = [ + 'Locked out', + 'Pipes broke', + 'Food poisoning', + 'Not feeling well' +] + +# Send a text message +@twilio.messages.create( + from: my_number, to: number_of_boss, + body: 'Gonna work from home. ' + excuses.sample +) + +# Log this +LOG_FILE.puts("Message sent at: #{Time.now}") +LOG_FILE.close diff --git a/kumar_asshole.rb b/kumar_asshole.rb new file mode 100755 index 0000000..015892f --- /dev/null +++ b/kumar_asshole.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +require 'dotenv' +require 'gmail' + +Dotenv.load + +GMAIL_USERNAME = ENV['GMAIL_USERNAME'] +GMAIL_PASSWORD = ENV['GMAIL_PASSWORD'] + +gmail = Gmail.connect(username, password) + +KEYWORDS_REGEX = /sorry|help|wrong/i + +gmail.inbox.find(:unread, from: 'kumar.a@example.com').each do |email| + if email.body[KEYWORDS_REGEX] + # Restore DB and send a reply + email.label('Database fixes') + reply = reply_to(email.subject) + gmail.deliver(reply) + end +end + +def reply_to(subject) + gmail.compose do + to "email@example.com" + subject "RE: #{subject}" + body "No problem. I've fixed it. \n\n Please be careful next time." + end +end diff --git a/smack_my_bitch_up.rb b/smack_my_bitch_up.rb new file mode 100755 index 0000000..f59332c --- /dev/null +++ b/smack_my_bitch_up.rb @@ -0,0 +1,43 @@ +#!/usr/bin/env ruby + +# Skip on weekends +exit if Time.now.saturday? || Time.now.sunday? + +log_file_name = File.dirname(__FILE__) + '/logs/smack_my_bitch_up.txt' + +# Be sure that logs dir always exists +Dir.mkdir('logs') unless File.exists?(log_file_name) + +LOG_FILE = File.open(log_file_name, 'a+') + +# Exit early if no sessions with my_username are found +exit if `who`[/my_username/].nil? + +require 'dotenv' +require 'twilio-ruby' + +Dotenv.load + +TWILIO_ACCOUNT_SID = ENV['TWILIO_ACCOUNT_SID'] +TWILIO_AUTH_TOKEN = ENV['TWILIO_AUTH_TOKEN'] + +@twilio = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN + +# Phone numbers +my_number = '+xxx' +her_number = '+xxx' + +reasons = [ + 'Working hard', + 'Gotta ship this feature', + 'Someone fucked the system again' +] + +# Send a text message +@twilio.messages.create( + from: my_number, to: her_number, body: 'Late at work. ' + reasons.sample +) + +# Log this +LOG_FILE.puts("Message sent at: #{Time.now}") +LOG_FILE.close