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
|
#!/usr/bin/env python
#
# Interpret.py: this file is part of the GRS suite
# Copyright (C) 2015 Anthony G. Basile
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import re
import signal
import sys
import time
from grs.Constants import CONST
from grs.Daemon import Daemon
from grs.ISOIt import ISOIt
from grs.Log import Log
from grs.Kernel import Kernel
from grs.MountDirectories import MountDirectories
from grs.PivotChroot import PivotChroot
from grs.Populate import Populate
from grs.RunScript import RunScript
from grs.Synchronize import Synchronize
from grs.Seed import Seed
from grs.TarIt import TarIt
class Interpret(Daemon):
""" This is the main daemon class. """
def run(self):
""" This overrides the empty Daemon run method and is started when .start()
is executed. This first daemonizes the process and then runs this method.
Note that the Daemon class does not set up any signal handles and expects
that to be done in the subclass.
"""
# First we set up some inner methods:
def handler(signum, frame):
""" On SIGTERM, propagate the signal to all processes in the cgroup/subcgroup
except yourself. If a process won't terminate nicely, then kill it.
Finally unmount all the mounted filesystems. Hopefully this will work
since there should be no more open files on those filesystems.
"""
mypid = os.getpid()
while True:
with open(os.path.join(self.subcgroupdir, 'tasks'), 'r') as _file:
lines = _file.readlines()
if len(lines) <= 1:
break
for _pid in lines:
pid = int(_pid.strip())
if mypid == pid:
continue
try:
for i in range(10):
os.kill(pid, signal.SIGTERM)
time.sleep(0.2)
while True:
os.kill(pid, signal.SIGKILL)
time.sleep(0.2)
except ProcessLookupError:
pass
try:
_md.umount_all()
except NameError:
pass
sys.exit(signum + 128)
def smartlog(_log, obj, has_obj):
""" This logs whether or not we have a grammatically incorrect
directive, or we are doing a mock run, and returns whether
or not we should execute the directive:
True = skip this directive
False = don't skip it
"""
if (has_obj and not obj) or (not has_obj and obj):
_lo.log('Bad command: %s' % _log)
return True
if self.mock_run:
_lo.log(_log)
return True
return False
def stampit(progress):
""" Create an empty file to mark the progress through the
build script.
"""
open(progress, 'w').close()
# Register the signals to terminate the entire process cgroup
signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGTERM, handler)
# Grab all the GRS namespace variables
repo_uri = CONST.repo_uris[self.run_number]
stage_uri = CONST.stage_uris[self.run_number]
name = CONST.names[self.run_number]
libdir = CONST.libdirs[self.run_number]
logfile = CONST.logfiles[self.run_number]
tmpdir = CONST.tmpdirs[self.run_number]
workdir = CONST.workdirs[self.run_number]
package = CONST.packages[self.run_number]
kernelroot = CONST.kernelroots[self.run_number]
portage_configroot = CONST.portage_configroots[self.run_number]
# Initialize all the classes that will run the directives from
# the build script. Note that we expect these classes to just
# initialize some variables but not do any work in their initializers.
_lo = Log(logfile)
_sy = Synchronize(repo_uri, name, libdir, logfile)
_se = Seed(stage_uri, tmpdir, portage_configroot, package, logfile)
_md = MountDirectories(portage_configroot, package, logfile)
_po = Populate(libdir, workdir, portage_configroot, logfile)
_ru = RunScript(libdir, portage_configroot, logfile)
_pc = PivotChroot(tmpdir, portage_configroot, logfile)
_ke = Kernel(libdir, portage_configroot, kernelroot, package, logfile)
_bi = TarIt(name, portage_configroot, logfile)
_io = ISOIt(name, libdir, tmpdir, portage_configroot, logfile)
# Just in case /var/tmp/grs doesn't already exist.
os.makedirs(tmpdir, mode=0o755, exist_ok=True)
# Rotate any prevously existing logs and make unmount any existing
# bind mounts from a previous run that were not cleaned up.
_lo.rotate_logs()
_md.umount_all()
# Both sync() + seed() do not need build script directives.
# sync() is done unconditionally for an update run.
progress = os.path.join(tmpdir, '.completed_sync')
if not os.path.exists(progress) or self.update_run:
_sy.sync()
stampit(progress)
# seed() is never done for an update run
progress = os.path.join(tmpdir, '.completed_seed')
if not os.path.exists(progress) and not self.update_run:
_se.seed()
stampit(progress)
# Read the build script and execute a line at a time.
build_script = os.path.join(libdir, 'build')
with open(build_script, 'r') as _file:
line_number = 0
medium_type = None
for _line in _file.readlines():
# Increment the line number by one and create the name of the
# file for the progress stamp.
line_number += 1
progress = os.path.join(tmpdir, '.completed_%02d' % line_number)
# Do nothing for lines with initial # or blank lines. Create
# a progress stamp only if we are not doing an update run.
if re.search(r'^(#).*$', _line) or len(_line.strip()) == 0:
if not self.update_run:
stampit(progress)
continue
# For a release run, execute every line of the build script.
# For an update run, exexute only lines with a leading +.
ignore_stamp = False
_match = re.search(r'^(\+)(.*)$', _line)
if _match:
# There is a leading +, so remove it and ignore any progress
# stamp if its an update run.
ignore_stamp = self.update_run
_line = _match.group(2)
else:
# There is no leading +, so skip if this is an update run
if self.update_run:
continue
# Skip a line if the progres stamp exists, or ignore it
# because its an update run.
if os.path.exists(progress) and not ignore_stamp:
continue
# This is pretty simple syntax. The first word on a line
# is a verb. The remaining words are objcts.
sentence = _line.split()
verb = sentence[0]
objs = sentence[1:]
obj = objs[0]
# This long concatenated if is where the semantics of the
# build script are implemented. Note: 'hashit' can only come
# after 'tarit' or 'isoit' so that it knows the medium_name
# to hash, ie whether its a .tar.xz or a .iso
if verb == 'log':
if smartlog(_line, obj, True):
stampit(progress)
continue
if obj == 'stamp':
_lo.log('='*80)
else:
_lo.log(obj)
elif verb == 'mount':
if smartlog(_line, obj, False):
stampit(progress)
continue
_md.mount_all()
elif verb == 'unmount':
if smartlog(_line, obj, False):
stampit(progress)
continue
_md.umount_all()
elif verb == 'populate':
if smartlog(_line, obj, True):
stampit(progress)
continue
_po.populate(cycle=int(obj))
elif verb == 'runscript':
if smartlog(_line, obj, True):
stampit(progress)
continue
_ru.runscript(obj)
elif verb == 'pivot':
if smartlog(_line, obj, True):
stampit(progress)
continue
_pc.pivot(obj, _md)
elif verb == 'kernel':
if smartlog(_line, obj, False):
stampit(progress)
continue
_ke.kernel()
elif verb == 'tarit':
# 'tarit' can either be just a verb,
# or a 'verb obj' pair.
if obj:
smartlog(_line, obj, True)
_bi.tarit(obj)
else:
smartlog(_line, obj, False)
_bi.tarit()
medium_type = 'tarit'
elif verb == 'isoit':
# 'isoit' can either be just a verb,
# or a 'verb obj' pair.
if obj:
smartlog(_line, obj, True)
_io.isoit(obj)
else:
smartlog(_line, obj, False)
_io.isoit()
medium_type = 'isoit'
elif verb == 'hashit':
if smartlog(_line, obj, False):
stampit(progress)
continue
if medium_type == 'tarit':
_bi.hashit()
elif medium_type == 'isoit':
_io.hashit()
else:
raise Exception('Unknown medium to hash.')
else:
_lo.log('Bad command: %s' % _line)
stampit(progress)
# Just in case the build script lacks a final unmount, if we
# are done, then let's make sure we clean up after ourselves.
try:
_md.umount_all()
except NameError:
pass
|