Skip to content

Commit 32b0128

Browse files
committed
initial commit
0 parents  commit 32b0128

7 files changed

+208
-0
lines changed

.env.example

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Set these environment variables or edit and copy this file into `.env`
2+
# See: https://github.com/bkeepers/dotenv
3+
4+
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
5+
TWILIO_AUTH_TOKEN=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
6+
GMAIL_USERNAME=[email protected]
7+
GMAIL_PASSWORD=password

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# DevOps Scripts
2+
3+
Based on a _[true
4+
story](https://www.jitbit.com/alexblog/249-now-thats-what-i-call-a-hacker/)_:
5+
6+
> 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.
7+
8+
> xxx: So we're sitting here, looking through his, uhm, "legacy"
9+
10+
> xxx: You're gonna love this
11+
12+
> 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.
13+
14+
> 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".
15+
16+
> 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.
17+
18+
> 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.
19+
20+
> xxx: holy sh*t I'm keeping those
21+
22+
Scripts are written in Ruby.
23+
Pull requests with other implementations (Python, Perl, Shell, etc) are welcome.
24+
25+
### Usage
26+
27+
Install required gems: `gem install dotenv twilio gmail whenever`
28+
Set environment variables. See `.env.example`
29+
30+
Example cron:
31+
32+
```sh
33+
# Runs `smack_my_bitch_up` daily at 9:20 am.
34+
20 21 * * * /bin/bash -l -c 'ruby smack_my_bitch_up.rb'
35+
36+
# Runs `kumar_asshole` every 10 minutes.
37+
45 8 * * * /bin/bash -l -c 'ruby hangover.rb'
38+
39+
# Runs `hangover` daily at 8:45 am.
40+
0,10,20,30,40,50 * * * * /bin/bash -l -c 'ruby kumar_asshole.rb'
41+
42+
# Runs `fucking_coffee` hourly from 9am to 6pm.
43+
9,10,11,12,13,14,15,16,17,18 * * * * /bin/bash -l -c 'ruby fucking_coffee.rb'
44+
```
45+
46+
Check `config/schedule.rb`.
47+
48+
---
49+
Code is released under WTFPL.

config/schedule.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# http://github.com/javan/whenever
2+
3+
every :day, at: '9:20 pm' do
4+
command 'ruby smack_my_bitch_up.rb'
5+
end
6+
7+
every 10.minutes do
8+
command 'ruby kumar_asshole.rb'
9+
end
10+
11+
every :day, at: '8:45 am' do
12+
command 'ruby hangover.rb'
13+
end
14+
15+
every :hour, at: 9..18 do
16+
command 'ruby fucking_coffee.rb'
17+
end

fucking_coffee.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env ruby
2+
3+
# Skip on weekends
4+
exit if Time.now.saturday? || Time.now.sunday?
5+
6+
require 'net/telnet'
7+
8+
coffee_machine_ip = '10.10.42.42'
9+
password = '1234'
10+
password_prompt = 'Password: '
11+
12+
con = Net::Telnet.new('Host' => coffee_machine_ip)
13+
con.cmd('String' => password, 'Match' => /#{password_prompt}/)
14+
con.cmd('sys brew')
15+
sleep 64
16+
con.cmd('sys pour')
17+
con.close

hangover.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env ruby
2+
3+
# Skip on weekends
4+
exit if Time.now.saturday? || Time.now.sunday?
5+
6+
log_file_name = File.dirname(__FILE__) + '/logs/hangover.txt'
7+
8+
# Be sure that logs dir always exists
9+
Dir.mkdir('logs') unless File.exists?(log_file_name)
10+
11+
LOG_FILE = File.open(log_file_name, 'a+')
12+
13+
# Exit early if sessions with my_username are found
14+
exit unless `who`[/my_username/].nil?
15+
16+
require 'dotenv'
17+
require 'twilio-ruby'
18+
19+
Dotenv.load
20+
21+
TWILIO_ACCOUNT_SID = ENV['TWILIO_ACCOUNT_SID']
22+
TWILIO_AUTH_TOKEN = ENV['TWILIO_AUTH_TOKEN']
23+
24+
@twilio = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN
25+
26+
# Phone numbers
27+
my_number = '+xxx'
28+
number_of_boss = '+xxx'
29+
30+
excuses = [
31+
'Locked out',
32+
'Pipes broke',
33+
'Food poisoning',
34+
'Not feeling well'
35+
]
36+
37+
# Send a text message
38+
@twilio.messages.create(
39+
from: my_number, to: number_of_boss,
40+
body: 'Gonna work from home. ' + excuses.sample
41+
)
42+
43+
# Log this
44+
LOG_FILE.puts("Message sent at: #{Time.now}")
45+
LOG_FILE.close

kumar_asshole.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'dotenv'
4+
require 'gmail'
5+
6+
Dotenv.load
7+
8+
GMAIL_USERNAME = ENV['GMAIL_USERNAME']
9+
GMAIL_PASSWORD = ENV['GMAIL_PASSWORD']
10+
11+
gmail = Gmail.connect(username, password)
12+
13+
KEYWORDS_REGEX = /sorry|help|wrong/i
14+
15+
gmail.inbox.find(:unread, from: '[email protected]').each do |email|
16+
if email.body[KEYWORDS_REGEX]
17+
# Restore DB and send a reply
18+
email.label('Database fixes')
19+
reply = reply_to(email.subject)
20+
gmail.deliver(reply)
21+
end
22+
end
23+
24+
def reply_to(subject)
25+
gmail.compose do
26+
27+
subject "RE: #{subject}"
28+
body "No problem. I've fixed it. \n\n Please be careful next time."
29+
end
30+
end

smack_my_bitch_up.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env ruby
2+
3+
# Skip on weekends
4+
exit if Time.now.saturday? || Time.now.sunday?
5+
6+
log_file_name = File.dirname(__FILE__) + '/logs/smack_my_bitch_up.txt'
7+
8+
# Be sure that logs dir always exists
9+
Dir.mkdir('logs') unless File.exists?(log_file_name)
10+
11+
LOG_FILE = File.open(log_file_name, 'a+')
12+
13+
# Exit early if no sessions with my_username are found
14+
exit if `who`[/my_username/].nil?
15+
16+
require 'dotenv'
17+
require 'twilio-ruby'
18+
19+
Dotenv.load
20+
21+
TWILIO_ACCOUNT_SID = ENV['TWILIO_ACCOUNT_SID']
22+
TWILIO_AUTH_TOKEN = ENV['TWILIO_AUTH_TOKEN']
23+
24+
@twilio = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN
25+
26+
# Phone numbers
27+
my_number = '+xxx'
28+
her_number = '+xxx'
29+
30+
reasons = [
31+
'Working hard',
32+
'Gotta ship this feature',
33+
'Someone fucked the system again'
34+
]
35+
36+
# Send a text message
37+
@twilio.messages.create(
38+
from: my_number, to: her_number, body: 'Late at work. ' + reasons.sample
39+
)
40+
41+
# Log this
42+
LOG_FILE.puts("Message sent at: #{Time.now}")
43+
LOG_FILE.close

0 commit comments

Comments
 (0)