-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalert_on_change.py
165 lines (153 loc) · 9.07 KB
/
alert_on_change.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""ShutIt module. See http://shutit.tk
"""
from shutit_module import ShutItModule
class alert_on_change(ShutItModule):
def build(self, shutit):
# Some useful API calls for reference. See shutit's docs for more info and options:
#
# ISSUING BASH COMMANDS
# shutit.send(send,expect=<default>) - Send a command, wait for expect (string or compiled regexp)
# to be seen before continuing. By default this is managed
# by ShutIt with shell prompts.
# shutit.multisend(send,send_dict) - Send a command, dict contains {expect1:response1,expect2:response2,...}
# shutit.send_and_get_output(send) - Returns the output of the sent command
# shutit.send_and_match_output(send, matches)
# - Returns True if any lines in output match any of
# the regexp strings in the matches list
# shutit.send_until(send,regexps) - Send command over and over until one of the regexps seen in the output.
# shutit.run_script(script) - Run the passed-in string as a script
# shutit.install(package) - Install a package
# shutit.remove(package) - Remove a package
# shutit.login(user='root', command='su -')
# - Log user in with given command, and set up prompt and expects.
# Use this if your env (or more specifically, prompt) changes at all,
# eg reboot, bash, ssh
# shutit.logout(command='exit') - Clean up from a login.
#
# COMMAND HELPER FUNCTIONS
# shutit.add_to_bashrc(line) - Add a line to bashrc
# shutit.get_url(fname, locations) - Get a file via url from locations specified in a list
# shutit.get_ip_address() - Returns the ip address of the target
# shutit.command_available(command) - Returns true if the command is available to run
#
# LOGGING AND DEBUG
# shutit.log(msg,add_final_message=False) -
# Send a message to the log. add_final_message adds message to
# output at end of build
# shutit.pause_point(msg='') - Give control of the terminal to the user
# shutit.step_through(msg='') - Give control to the user and allow them to step through commands
#
# SENDING FILES/TEXT
# shutit.send_file(path, contents) - Send file to path on target with given contents as a string
# shutit.send_host_file(path, hostfilepath)
# - Send file from host machine to path on the target
# shutit.send_host_dir(path, hostfilepath)
# - Send directory and contents to path on the target
# shutit.insert_text(text, fname, pattern)
# - Insert text into file fname after the first occurrence of
# regexp pattern.
# shutit.delete_text(text, fname, pattern)
# - Delete text from file fname after the first occurrence of
# regexp pattern.
# shutit.replace_text(text, fname, pattern)
# - Replace text from file fname after the first occurrence of
# regexp pattern.
# ENVIRONMENT QUERYING
# shutit.host_file_exists(filename, directory=False)
# - Returns True if file exists on host
# shutit.file_exists(filename, directory=False)
# - Returns True if file exists on target
# shutit.user_exists(user) - Returns True if the user exists on the target
# shutit.package_installed(package) - Returns True if the package exists on the target
# shutit.set_password(password, user='')
# - Set password for a given user on target
#
# USER INTERACTION
# shutit.get_input(msg,default,valid[],boolean?,ispass?)
# - Get input from user and return output
# shutit.fail(msg) - Fail the program and exit with status 1
#
shutit.install('curl git dwdiff html2text python-psycopg2 sudo cron expect wget python-dev python-pip docker.io coreutils')
shutit.send('pip install simplejson')
shutit.send('pip install mailgun')
shutit.send('pip install web.py')
shutit.send('groupadd -g 1000 alertonchange')
shutit.send('useradd -g alertonchange -d /home/alertonchange -s /bin/bash -m alertonchange')
shutit.send('adduser alertonchange sudo')
shutit.send('echo "%sudo ALL=(ALL:ALL) ALL" > /etc/sudoers.d/sudo')
shutit.send('chmod 0440 /etc/sudoers.d/sudo')
shutit.login('postgres')
shutit.send('git config --global user.email ' + shutit.cfg[self.module_id]['git_email'])
shutit.send('git config --global user.name ' + shutit.cfg[self.module_id]['git_name'])
shutit.send('git clone -b ' + shutit.cfg[self.module_id]['git_branch'] + ' https://github.com/ianmiell/alert-on-change.git')
shutit.send('cd alert-on-change')
shutit.send('echo create database alert_on_change | psql postgres')
shutit.send('psql alert_on_change < context/SCHEMA.sql')
shutit.send('psql alert_on_change < context/DATA.sql')
shutit.send('''echo "alter user postgres password 'password'" | psql postgres''')
shutit.send('createuser -s alertonchange')
shutit.send('''echo "alter user alertonchange with password 'postgres'" | psql postgres''')
shutit.logout()
shutit.login('alertonchange')
shutit.send('git config --global user.email ' + shutit.cfg[self.module_id]['git_email'])
shutit.send('git config --global user.name ' + shutit.cfg[self.module_id]['git_name'])
shutit.send('git clone -b ' + shutit.cfg[self.module_id]['git_branch'] + ' https://github.com/ianmiell/alert-on-change.git')
shutit.send('''sed -i 's/MAILGUNAPIUSER/''' + shutit.cfg[self.module_id]['mailgunapiuser'] + '''/g' /home/alertonchange/alert-on-change/context/db.py''')
shutit.send('''sed -i 's/MAILGUNADDRESS/''' + shutit.cfg[self.module_id]['mailgunaddress'] + '''/g' /home/alertonchange/alert-on-change/context/db.py''')
shutit.send('''sed -i 's/MAILGUNAPIUSER/''' + shutit.cfg[self.module_id]['mailgunapiuser'] + """/g' /home/alertonchange/alert-on-change/context/forms/hn_insert.py""")
shutit.send('''sed -i 's/MAILGUNADDRESS/''' + shutit.cfg[self.module_id]['mailgunaddress'] + '''/g' /home/alertonchange/alert-on-change/context/forms/hn_insert.py''')
if not shutit.cfg[self.module_id]['testing']:
shutit.send("""echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * python /home/alertonchange/alert-on-change/context/db.py" | crontab -u alertonchange -""")
else:
shutit.send("""echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * python /home/alertonchange/alert-on-change/context/db.py --test" | crontab -u alertonchange -""")
shutit.logout()
if not shutit.cfg[self.module_id]['testing']:
shutit.login('postgres')
shutit.send('cd alert-on-change')
shutit.send(r"""echo "4,9,14,19,24,29,34,39,44,49,54,59 * * * * cd alert-on-change && pg_dump alert_on_change -a > context/DATA.sql && pg_dump alert_on_change -s > context/SCHEMA.sql && git commit -am 'latest backup' && /tmp/push.exp" | crontab -u postgres -""")
shutit.send_file('/tmp/push.exp',r'''#!/usr/bin/env expect
set timeout 100
spawn bash
send "git push origin ''' + shutit.cfg[self.module_id]['git_branch'] + r'''\n"
expect -re {sername}
send "''' + shutit.cfg[self.module_id]['git_username'] + r'''\n"
expect -re {assword}
send "''' + shutit.cfg[self.module_id]['git_password'] + r'''\n"
expect -re {postgres}''')
shutit.send('chmod +x /tmp/push.exp')
shutit.logout()
return True
def get_config(self, shutit):
# CONFIGURATION
# shutit.get_config(module_id,option,default=None,boolean=False)
# - Get configuration value, boolean indicates whether the item is
# a boolean type, eg get the config with:
# shutit.get_config(self.module_id, 'myconfig', default='a value')
# and reference in your code with:
# shutit.cfg[self.module_id]['myconfig']
shutit.get_config(self.module_id, 'git_name')
shutit.get_config(self.module_id, 'git_email')
shutit.get_config(self.module_id, 'git_username')
shutit.get_config(self.module_id, 'git_password')
shutit.get_config(self.module_id, 'git_branch', default='master')
shutit.get_config(self.module_id, 'testing', boolean=True, default=False)
shutit.get_config(self.module_id, 'mailgunapiuser')
shutit.get_config(self.module_id, 'mailgunaddress')
return True
def test(self, shutit):
# For test cycle part of the ShutIt build.
shutit.send('python /home/alertonchange/alert-on-change/context/db.py --test')
return True
def finalize(self, shutit):
# Any cleanup required at the end.
return True
def is_installed(self, shutit):
return False
def module():
return alert_on_change(
'shutit.alert_on_change.alert_on_change.alert_on_change', 1801706206.00,
description='',
maintainer='',
delivery_methods=['docker'],
depends=['shutit.tk.postgres.postgres']
)