Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed Nov 21, 2015
0 parents commit 32b0128
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=[email protected]
GMAIL_PASSWORD=password
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions fucking_coffee.rb
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions hangover.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions kumar_asshole.rb
Original file line number Diff line number Diff line change
@@ -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: '[email protected]').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 protected]"
subject "RE: #{subject}"
body "No problem. I've fixed it. \n\n Please be careful next time."
end
end
43 changes: 43 additions & 0 deletions smack_my_bitch_up.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 32b0128

Please sign in to comment.