-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragile.py
179 lines (126 loc) · 3.78 KB
/
fragile.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import logging
import os
import random
import string
import sys, getopt
import opbeat
import requests
import time
from flask import Flask
from opbeat.contrib.flask import Opbeat
from opbeat.handlers.logging import OpbeatHandler
from helpers import zero_division_no_error, just_log_stuff, deep_zero_division_error
app = Flask(__name__)
@app.route('/')
def main():
return 'So far so good...'
@app.route('/errors/log')
def key_error():
"""
Creates an error log that goes to the same group
"""
return {}['invalid lines?']
@app.route('/errors/deep')
def deep_error():
"""
Creates an error i
"""
return deep_zero_division_error()
@app.route('/log/info')
def log_info():
"""
Logs a message
"""
just_log_stuff(app)
return 'Did it work?...'
@app.route('/log/error')
def log_error():
"""
Captures an exception and sends a log to opbeat
"""
zero_division_no_error(app)
return 'Did it work this time?...'
@app.route('/errors/group')
def assertion_error():
"""
Creates a new error group every time this end point is hit
"""
import imp
module_name = ''.join(random.sample(string.letters, 10))
m = imp.new_module(module_name)
exec _nope_code in m.__dict__
m.nope()
_nope_code = \
"""
def nope():
raise AssertionError('Nope')
"""
@app.route('/release/')
def release():
os.chdir(os.path.dirname(__file__))
f = os.path.join(os.path.dirname(__file__), 'elephant.txt')
return _release(push=True, f=f)
@app.route('/perf/')
def perf():
_time_it()
return 'slow hand'
@opbeat.trace()
def _time_it():
zzz = random.randint(100, 3000)
time.sleep(zzz / 1000.0) # seconds, man
@app.route('/ship/')
def ship():
os.chdir(os.path.dirname(__file__))
return _release(push=True)
@app.route('/bad-release/')
def bad_release():
os.chdir(os.path.dirname(__file__))
f = os.path.join(os.path.dirname(__file__), 'elephant.txt')
return _release(push=False, f=f)
def _release(push=True, f=None):
if f:
with open(f, "r") as elephant_counter:
next_elephant = str(elephant_counter.read().count('\n') - 4)
with open(f, "a") as elephant_song:
elephant_song.write(next_elephant + ' elephants stood on the web of a spider...\n')
os.popen('git add elephant.txt').read()
os.popen('git commit -m "add another elephant"').read()
else:
os.popen('git add .').read()
os.popen('git commit -m "just commit"').read()
if push:
os.popen('git push').read()
url = _M.intake_base_url + '/api/v1/organizations/' + _M.organization_id + '/apps/' + _M.app_id + '/releases/'
rev = os.popen('git log -n 1 --pretty=format:%H').read()
branch = 'master' # os.popen('git rev-parse --abbrev-ref HEAD').read()
h = {'Authorization': 'Bearer ' + _M.secret_token}
d = {'rev': rev, 'branch': branch, 'status': 'completed'}
r = requests.post(url, data=d, headers=h)
assert r.status_code < 300
return 'SHiP MaSTer ' + url
_M = None
def _instrument(env):
"""
Order of operations in here is relevant.
Don't do this at home. NEVER.
"""
global _M
_M = __import__('sssh.' + env, fromlist=['']) # lol
app.config['OPBEAT'] = {'SERVERS': [_M.intake_base_url]}
return Opbeat(
app,
organization_id=_M.organization_id,
app_id=_M.app_id,
secret_token=_M.secret_token,
logging=True)
def run_flask(env):
opbeat = _instrument(env)
handler = OpbeatHandler(client=opbeat.client)
handler.setLevel(logging.WARN)
app.logger.addHandler(handler)
app.run(threaded=True, port=5000)
if __name__ == '__main__':
args = sys.argv[1:]
opts, _ = getopt.getopt(args, '-e:')
env_str = dict(opts).get('-e', 'local')
run_flask(env_str)