forked from reddit-archive/reddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap-job
executable file
·81 lines (60 loc) · 1.98 KB
/
wrap-job
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
#!/usr/bin/python
"""
Wrap a command, setuid/setgid, change directory to the reddit
code, and send output to syslog.
The following environment variables may be used to control
the environment the wrapped command runs in:
REDDIT_USER
The user to run the job as. Defaults to "reddit".
REDDIT_GROUP
The group to run the job as. Defaults to $REDDIT_USER.
REDDIT_ROOT
The root directory of the reddit package. It's where the makefile lives.
REDDIT_LOG_FACILITY
The syslog facility to write messages to.
"""
import os
import sys
import grp
import pwd
import syslog
import subprocess
CONSUMER_PREFIX = "reddit-consumer-"
# drop permissions
user = os.environ.get("REDDIT_USER", "reddit")
group = os.environ.get("REDDIT_GROUP", user)
uid = pwd.getpwnam(user).pw_uid
gid = grp.getgrnam(group).gr_gid
os.setgroups([])
os.setgid(gid)
os.setuid(uid)
# change directory to the reddit code root
root = os.environ.get("REDDIT_ROOT", "/opt/reddit/lib/public/r2")
os.chdir(root)
# configure syslog
job_name = os.environ.get("UPSTART_JOB", "-".join(sys.argv[1:]))
if job_name.startswith(CONSUMER_PREFIX):
# consumers are a bit different from crons, while crons want an
# ident of reddit-job-JOBNAME, we want consumers to have an ident
# of CONSUMERNAME_INSTANCE
job_name = (job_name[len(CONSUMER_PREFIX):] +
"_" +
os.environ.get("UPSTART_INSTANCE", ""))
facility = getattr(syslog, "LOG_" + os.environ.get("REDDIT_LOG_FACILITY", "CRON"))
syslog.openlog(ident=job_name, facility=facility)
# run the wrapped command
child = subprocess.Popen(sys.argv[1:],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1)
# write out to syslog
while True:
line = child.stdout.readline()
if not line:
break
line = line.rstrip('\n')
syslog.syslog(syslog.LOG_NOTICE, line)
print line
# our success depends on our child's success
child.wait()
sys.exit(child.returncode)