-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
138 lines (113 loc) · 4.51 KB
/
lambda_function.py
File metadata and controls
138 lines (113 loc) · 4.51 KB
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
import os, requests, time, json, smtplib, subprocess
API = "https://api.digitalocean.com/v2/droplets"
HEADERS = {
"Authorization": "Bearer %s" %os.environ['TOKEN'],
"Content-Type": "application/json"
}
DROPLET_NAME = "ol-tester"
TEST_SCRIPT = "test.sh"
TEST_OUTPUT = ""
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
def post(args):
r = requests.post(API, data=args, headers=HEADERS)
return r.json()
def get(args):
r = requests.get(API, data=args, headers=HEADERS)
return r.json()
def start():
r = requests.get("https://api.digitalocean.com/v2/account/keys", headers=HEADERS)
keys = map(lambda row: row['id'], r.json()['ssh_keys'])
args = {
"name":DROPLET_NAME,
"region":"nyc2",
"size":"512mb",
"image":"ubuntu-14-04-x64",
"ssh_keys":keys
}
r = requests.post(API, data=json.dumps(args), headers=HEADERS)
return r.json()
def lookup(droplet_id):
r = requests.get('%s/%s' % (API, droplet_id), headers=HEADERS)
return r.json()['droplet']
def kill():
global TEST_OUTPUT
args = {}
droplets = get(args)['droplets']
for d in droplets:
if d['name'] == DROPLET_NAME:
TEST_OUTPUT += 'Deleting %s (%d)\n' % (d['name'], d['id'])
TEST_OUTPUT += '%s\n' % requests.delete('%s/%s' % (API, d['id']), headers=HEADERS)
def test():
global TEST_OUTPUT
# cleanup just in case
kill()
# create new droplet and wait for it
droplet = start()['droplet']
while True:
droplet = lookup(droplet['id'])
# status
s = droplet['status']
try:
assert(s in ['active', 'new'])
except:
TEST_OUTPUT += 'Droplet %s (%d) status not active or new. Giving up.'
return False
# addr
ip = None
for addr in droplet["networks"]["v4"]:
if addr["type"] == "public":
ip = addr["ip_address"]
TEST_OUTPUT += 'STATUS: %s, IP: %s\n' % (str(s), str(ip))
if s == 'active' and ip != None:
break
time.sleep(3)
time.sleep(30) # give SSH some time
script_path = os.path.join(SCRIPT_DIR, TEST_SCRIPT)
scp = ['/usr/bin/scp', '-o', '"StrictHostKeyChecking no"', script_path, 'root@%s:/tmp' % ip]
TEST_OUTPUT += 'RUN %s\n' % ' '.join(scp)
try:
TEST_OUTPUT += subprocess.check_output(scp)
except subprocess.CalledProcessError as e:
TEST_OUTPUT += str(e.output)
TEST_OUTPUT += 'SCP with code %s failed. Giving up.\n' % str(e.returncode)
return False
cmds = 'bash /tmp/%s' % TEST_SCRIPT
ssh = ['/usr/bin/echo', cmds, '|', '/usr/bin/ssh', '-o', '"StrictHostKeyChecking no"', 'root@%s' % ip]
TEST_OUTPUT += 'RUN %s\n' % ' '.join(ssh)
try:
TEST_OUTPUT += subprocess.check_output(ssh)
except subprocess.CalledProcessError as e:
TEST_OUTPUT += str(e.output)
TEST_OUTPUT += 'SCP with code %s failed. Giving up.\n' % str(e.returncode)
return False
# make sure we cleanup everything!
kill()
return True
def scold(commit):
user = os.environ['EMAIL']
pw = os.environ['PW']
FROM = user
#TO = [commit['committer']['email'], '[email protected]', '[email protected]']
SUBJECT = 'OpenLambda Broken Commit %s' % commit['id']
TEXT = """Your latest commit (sha: %s, message: '%s') failed the automated tests. Please push (or roll back to) a working commit as soon as possible.\n\n\
If you are unable to fix this or think there's an issue with the tests, please contact Ed Oakes ([email protected]) or Tyler Harter ([email protected]) so we can address the issue.\n\n\
Here is the output of the tests for reference:\n\n%s\n\n\
<----------------- DO NOT REPLY TO THIS EMAIL ADDRESS ----------------->""" % (commit['id'], commit['message'], TEST_OUTPUT)
message = """From: %s\nTo: %s\nSubject:%s\n\n%s
""" % (FROM, ', '.join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(user, pw)
server.sendmail(FROM, TO, message)
server.close()
return 'tests failed and successfully sent the email'
except Exception as e:
return 'tests failed but couldn\'t send the email: %s' % e
# aws entry
def lambda_handler(event, context):
if not test():
return scold(event['head_commit'])
return 'Tests passed'