-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror.py
199 lines (176 loc) · 5.6 KB
/
mirror.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
import argparse
from jinja2 import Environment, FileSystemLoader
import json
from multiprocessing import Pool
import os
import pwd
import shlex
import subprocess
import sys
LOGDIR = "/tmp/mirror-logs"
MIRROR_TEMPLATES_TMPDIR = "/tmp/mirror-templates"
class Mirror:
"""
A mirror running on a fort server. Consists of:
* a (textual) identifier, unique in the mirroring system
* zero or more templates, rendered before mirroring
* parameters to populate the templates (and the apache vhost template)
* a series of mirroring steps, each of which is several commands
run under a particular user (typically root or fort)
"""
DEFAULTS = {}
def __init__(self, identifier, steps, templates=None, **params):
self.identifier = identifier
self.steps = steps
self.templates = templates
if self.templates is None:
self.templates = []
self.params = dict(self.DEFAULTS)
self.params.update(params)
self.templates.append(
self.params["vhost-template"]
)
self.env = Environment(
loader=FileSystemLoader('./templates'),
autoescape=False,
)
self.template_dir = os.path.join(
MIRROR_TEMPLATES_TMPDIR,
self.identifier,
)
os.makedirs(self.template_dir, exist_ok=True)
def mirror(self, configure_apache=False):
try:
self.render_templates()
if configure_apache:
# copy in apache2 vhost config and enable that site
with open(
self.rendered_template_filename(self.params['vhost-template']),
'r'
) as inf, open(
os.path.join(
'/etc/apache2/sites-available',
'%s.conf' % self.identifier,
)
) as outf:
inf.write(outf.read())
subprocess.call(
"a2ensite %s" % self.identifier
)
# FIXME: add DNS entry
# need to su before we do the mirroring, because we want
# that run as another user
pwdent = pwd.getpwnam(self.params['user'])
uid = pwdent.pw_uid
gid = pwdent.pw_gid
os.seteuid(uid)
os.setegid(gid)
# and change directory -- typically to that user's home
os.chdir(self.params['cwd'])
for step in self.steps:
args = shlex.split(step)
p = subprocess.Popen(args)
finally:
for t in self.templates:
try:
os.unlink(
self.rendered_template_filename(t)
)
except:
pass
def rendered_template_filename(self, t):
return os.path.join(self.template_dir, t)
def render_templates(self):
for t in self.templates:
self.render_template(
"%s.template" % t,
self.rendered_template_filename(t),
)
def render_template(self, template, out):
template = self.env.get_template(template)
with open(out, 'w') as fout:
fout.write(template.render(self.params))
@classmethod
def from_config(kls, config):
kls.DEFAULTS = config['defaults']
for mirror in config['mirrors']:
yield kls(
mirror['id'],
mirror['steps'],
mirror.get('templates'),
**mirror['params'],
)
def do_mirror(mirror):
# Note that this is run in the sub-Process
# So it's safe to throw away stdout and stderr and never
# restore them.
sys.stdout = open(
os.path.join(LOGDIR, "%s.out" % mirror.identifier),
'w',
)
sys.stderr = open(
os.path.join(LOGDIR, "%s.err" % mirror.identifier),
'w',
)
return mirror.mirror()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
"Mirror useful parts of the internet, for fortly purposes.",
)
parser.add_argument(
'--serial',
dest='serial',
default=False,
action='store_true',
help='run in serial rather than parallel',
)
parser.add_argument(
'--logdir',
dest='logdir',
default=LOGDIR,
action='store',
help='set logging directory',
)
parser.add_argument(
'--tmpdir',
dest='tmpdir',
default=MIRROR_TEMPLATES_TMPDIR,
action='store',
help='set temp directory',
)
parser.add_argument(
'configfile',
metavar='configfile',
type=str,
nargs=1,
help='Config file for mirroring',
)
parser.add_argument(
'kv',
metavar='kv',
type=str,
nargs='*',
help='Key-value pairs to add to defaults',
)
args = parser.parse_args()
defaults = dict(
(
kv.split('=', 1)
for kv in args.kv
)
)
LOGDIR = args.logdir
MIRROR_TEMPLATES_TMPDIR = args.tmpdir
with open(args.configfile[0], 'r') as cin:
config = json.load(cin)
config['defaults'].update(defaults)
mirrors = list(Mirror.from_config(config))
if args.serial:
for mirror in mirrors:
do_mirror(mirror)
else:
# FIXME: doesn't work because jinja2 isn't pickleable,
# because pickling is a bad idea but apparently we still
# use it. Sigh.
with Pool(len(mirrors)) as p:
p.map(do_mirror, mirrors)