-
Notifications
You must be signed in to change notification settings - Fork 4
/
shentry.py
executable file
·337 lines (300 loc) · 10.9 KB
/
shentry.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python
# Copyright (c) 2016, EasyPost <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
# granted, provided that the above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
from __future__ import print_function
try:
from urllib.error import HTTPError, URLError
from urllib.parse import urlparse
from urllib.request import Request, urlopen
except ImportError:
from urllib2 import HTTPError, Request, URLError, urlopen
from urlparse import urlparse
try:
import requests
has_requests = True
except ImportError:
has_requests = False
import datetime
import json
import os
import pwd
import shutil
import signal
import socket
import subprocess
import sys
import tempfile
import time
import uuid
from contextlib import closing
VERSION = "1.0.0"
def read_systemwide_config():
try:
with open("/etc/shentry_dsn", "r") as f:
return f.read().strip()
except Exception:
return None
def _get_proxy_url():
if "SHELL_SENTRY_PROXY" in os.environ:
return os.environ["SHELL_SENTRY_PROXY"]
try:
with open("/etc/shentry_proxy", "r") as f:
return f.read().strip()
except Exception:
pass
return None
def _send_urllib2(uri, headers, data, timeout):
req = Request(uri, data=data, headers=headers)
try:
with closing(urlopen(req, timeout=timeout)) as f:
f.read()
return True
except HTTPError as e:
print("Error {0} sending to Sentry".format(e.code), file=sys.stderr)
print(e.read(), file=sys.stderr)
return False
except URLError as e:
print("Error {0} sending to Sentry".format(e.reason), file=sys.stderr)
return False
def _send_requests(uri, headers, data, timeout):
try:
kwargs = {}
proxy_url = _get_proxy_url()
if proxy_url is not None:
kwargs["proxies"] = {"http": proxy_url, "https": proxy_url}
resp = requests.post(uri, headers=headers, data=data, timeout=timeout, **kwargs)
resp.raise_for_status()
return True
except requests.exceptions.RequestException as e:
print("Error {0!r} sending to Sentry".format(e), file=sys.stderr)
return False
if has_requests:
send_to_sentry = _send_requests
else:
send_to_sentry = _send_urllib2
class SimpleSentryClient(object):
TIMEOUT = 5
SENTRY_VERSION = 5
USER_AGENT = "shentry/{0}".format(VERSION)
def __init__(self, dsn, uri, public, secret, project_id):
self.dsn = dsn
self.uri = uri
self.public = public
self.secret = secret
self.project_id = project_id
@classmethod
def new_from_environment(cls):
dsn = os.environ.pop("SHELL_SENTRY_DSN", "")
if not dsn:
dsn = os.environ.pop("SENTRY_DSN", "")
if not dsn:
dsn = read_systemwide_config()
if not dsn:
return None
else:
try:
dsn_fields = urlparse(dsn)
keys, netloc = dsn_fields.netloc.split("@", 1)
if ":" in keys:
public, private = keys.split(":", 1)
else:
public = keys
private = ""
project_id = dsn_fields.path.lstrip("/")
uri = "{proto}://{netloc}/api/{project_id}/store/".format(
proto=dsn_fields.scheme,
netloc=netloc,
project_id=project_id,
)
return cls(dsn, uri, public, private, project_id)
except Exception as e:
print(
"Error parsing sentry DSN {0}: {1}".format(dsn, e), file=sys.stderr
)
def send_event(
self, message, level, fingerprint, logger="", culprit=None, extra_context={}
):
event_id = uuid.uuid4().hex
now = int(time.time())
uname = os.uname()
event = {
"event_id": event_id,
"timestamp": datetime.datetime.utcnow().isoformat().split(".", 1)[0],
"message": message,
"level": level,
"server_name": socket.gethostname(),
"sdk": {
"name": "shentry",
"version": VERSION,
},
"fingerprint": fingerprint,
"platform": "other",
"device": {"name": uname[0], "version": uname[2], "build": uname[3]},
"extra": {},
}
if logger:
event["logger"] = logger
if culprit is not None:
event["culprit"] = culprit
event["extra"].update(extra_context)
headers = {
"X-Sentry-Auth": (
"Sentry sentry_version={self.SENTRY_VERSION}, "
"sentry_client={self.USER_AGENT}, "
"sentry_timestamp={now}, "
"sentry_key={self.public}, "
"sentry_secret={self.secret}"
).format(now=now, self=self),
"User-Agent": self.USER_AGENT,
"Content-Type": "application/json",
}
if os.environ.get("SHELL_SENTRY_VERBOSE", "0") == "1":
print("Sending to shentry", file=sys.stderr)
print(event, file=sys.stderr)
data = json.dumps(event).encode("utf-8")
return send_to_sentry(
uri=self.uri, headers=headers, data=data, timeout=self.TIMEOUT
)
def get_command(argv):
# get the command
i_am_shell = False
command = argv
if command[0] == "-c":
i_am_shell = True
command = command[1:]
if command[0] == "--":
command = command[1:]
shell = os.environ.get("SHELL", "/bin/sh")
if i_am_shell or "shentry" in shell:
shell = "/bin/sh"
command_ws = " ".join(command)
full_command = [shell, "-c", command_ws]
return full_command, command_ws, shell
def show_usage():
print("Usage: shentry [-c] command [...]", file=sys.stderr)
print("", file=sys.stderr)
print(
"Runs COMMAND, sending the output to Sentry if it exits non-0", file=sys.stderr
)
print(
"Takes sentry DSN from $SHELL_SENTRY_DSN, $SENTRY_DSN, or /etc/shentry_dsn",
file=sys.stderr,
)
def read_snippet(fo, max_length):
fo.seek(0, os.SEEK_END)
length = fo.tell()
rv = []
fo.seek(0, os.SEEK_SET)
read_all = False
if length > max_length:
top = int(max_length / 2) - 8
bottom = max_length - top
top = fo.read(top).decode("utf-8", "ignore")
rv.append(top)
if not top.endswith("\n"):
rv.append("\n")
rv.append("\n[snip]\n")
fo.seek(-1 * bottom, os.SEEK_END)
rv.append(fo.read(bottom).decode("utf-8", "ignore"))
else:
rv.append(fo.read().decode("utf-8", "ignore"))
read_all = True
return "".join(rv), read_all
def main(argv=None):
if argv is None:
argv = sys.argv
if len(argv) < 2:
show_usage()
return 2
extra_context = {
"PATH": os.environ.get("PATH", ""),
"username": pwd.getpwuid(os.getuid()).pw_name,
}
if "TZ" in os.environ:
extra_context["TZ"] = os.environ["TZ"]
client = SimpleSentryClient.new_from_environment()
full_command, command_ws, shell = get_command(argv[1:])
extra_context["command"] = command_ws
extra_context["shell"] = shell
# if we couldn't configure sentry, just pass through
if client is None:
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
os.execv(shell, full_command)
print(
"Unable to execv({0}, {1})".format(shell, repr(full_command)),
file=sys.stderr,
)
return 1
working_dir = None
p = None
def passthrough(signum, frame):
if p is not None:
p.send_signal(signum)
else:
raise ValueError("received signal %d without a child; bailing" % signum)
def reset_signals():
for sig in (signal.SIGTERM, signal.SIGQUIT, signal.SIGINT, signal.SIGPIPE):
signal.signal(sig, signal.SIG_DFL)
for sig in (signal.SIGTERM, signal.SIGQUIT, signal.SIGINT):
signal.signal(sig, passthrough)
try:
working_dir = tempfile.mkdtemp()
with open(os.path.join(working_dir, "stdout"), "w+b") as stdout:
with open(os.path.join(working_dir, "stderr"), "w+b") as stderr:
start_time = time.time()
p = subprocess.Popen(
full_command,
stdout=stdout,
stderr=stderr,
shell=False,
preexec_fn=reset_signals,
)
extra_context["start_time"] = start_time
extra_context["load_average_at_exit"] = " ".join(
map(str, os.getloadavg())
)
extra_context["working_directory"] = os.getcwd()
extra_context["_sent_with"] = send_to_sentry.__name__
if p.wait() != 0:
end_time = time.time()
extra_context["duration"] = end_time - start_time
code = p.returncode
extra_context["returncode"] = code
stderr_head, stderr_is_all = read_snippet(stderr, 700)
message = "Command `{0}` failed with code {1}.\n".format(
command_ws, code
)
if stderr_head:
if stderr_is_all:
message += "\nstderr:\n"
else:
message += "\nExcerpt of stderr:\n"
message += stderr_head
stdout_head, stdout_is_all = read_snippet(
stdout, 200 + (700 - len(stderr_head))
)
if stdout_head:
if stdout_is_all:
message += "\nstdout:\n"
else:
message += "\nExcerpt of stdout:\n"
message += stdout_head
client.send_event(
message=message,
level="error",
fingerprint=[socket.gethostname(), command_ws],
extra_context=extra_context,
)
finally:
if working_dir is not None:
shutil.rmtree(working_dir)
if __name__ == "__main__":
sys.exit(main())