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
|
# Copyright (C) 2018 Lars Wirzenius
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
import subprocess
import cliapp
class Runner:
def __init__(self, reporter, maxbuf=128*1024):
self._reporter = reporter
self._buffers = {
'stdout': b'',
'stderr': b'',
}
self._maxbuf = maxbuf
self._timeout = 1.0
def runcmd(self, *argvs, **kwargs):
for argv in argvs:
logging.debug('Runner.runcmd: argv: %r', argv)
for key in kwargs:
logging.debug('Runner.runcmd: kwargs: %r=%r', key, kwargs[key])
assert all(argv is not None for argv in argvs)
with open('/dev/null', 'rb') as devnull:
exit_code, _, _ = cliapp.runcmd_unchecked(
*argvs,
stdin=devnull,
stdout_callback=self.capture_stdout,
stderr=subprocess.STDOUT,
output_timeout=self._timeout,
timeout_callback=self.flush,
**kwargs
)
self.flush()
logging.debug('Runner.runcmd: finished, exit code: %d', exit_code)
return exit_code
def capture_stdout(self, data):
return self.capture('stdout', data)
def capture(self, stream_name, data):
self._buffers[stream_name] += data
if len(self._buffers[stream_name]) >= self._maxbuf:
self.flush()
return b''
def flush(self):
stdout = self._buffers['stdout']
stderr = self._buffers['stderr']
self._reporter.report(None, stdout, stderr)
self._buffers['stdout'] = b''
self._buffers['stderr'] = b''
class Mounter: # pragma: no cover
def __init__(self, mounts, runner):
self._mounts = mounts
self._runner = runner
def __enter__(self):
self.mount()
return self
def __exit__(self, *args):
self.unmount()
def mount(self):
for dirname, mp in self._mounts:
if not os.path.exists(mp):
os.mkdir(mp)
self._runner.runcmd(['sudo', 'mount', '--bind', dirname, mp])
def unmount(self):
for dirname, mp in reversed(self._mounts):
try:
self._runner.runcmd(['sudo', 'umount', mp])
except BaseException as e:
logging.error(
'Ignoring error while unmounting %s: %s', mp, str(e))
class ActionEnvironment: # pragma: no cover
def __init__(self, systree, workspace, reporter):
super().__init__()
self._systree = systree
self._workspace = workspace
self._reporter = reporter
self._extra_env = {}
def set_extra_env(self, extra_env):
self._extra_env = dict(extra_env)
def get_systree_directory(self):
return self._systree
def get_workspace_directory(self):
return self._workspace
def get_mounts(self):
return []
def report(self, exit_code, msg):
self._reporter.report(exit_code, msg, None)
def runcmd(self, argv):
raise NotImplementedError()
def host_runcmd(self, *argvs, cwd=None):
env = self.get_env_vars()
runner = Runner(self._reporter)
mounts = self.get_mounts()
with Mounter(mounts, runner):
return runner.runcmd(*argvs, cwd=cwd, env=env)
def get_env_vars(self):
return dict(self._extra_env)
class HostEnvironment(ActionEnvironment):
def runcmd(self, argv):
return self.host_runcmd(argv, cwd=self._workspace)
class ChrootEnvironment(ActionEnvironment):
def runcmd(self, argv):
prefix = ['sudo', 'chroot', self._workspace]
return self.host_runcmd(prefix + argv)
class ContainerEnvironment(ActionEnvironment):
def runcmd(self, argv):
bind = '{}:/workspace'.format(self._workspace)
prefix = [
'sudo', 'systemd-nspawn',
'-D', self._systree,
'--bind', bind,
'--chdir', '/workspace',
]
env = self.get_env_vars()
for key in env: # pragma: no cover
var = '{}={}'.format(key, env[key])
prefix.extend(['--setenv', var])
return self.host_runcmd(prefix + argv)
|