-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.py
More file actions
369 lines (294 loc) · 10.3 KB
/
Copy pathbuild.py
File metadata and controls
369 lines (294 loc) · 10.3 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import logging
import re
import shutil
import socket
import subprocess
import sys
import tempfile
from pathlib import Path
import click
import yaml
from . import qemu
from .utils import waitfor, WaitTimeout
from .vms import VM
logger = logging.getLogger(__name__)
BUILD_STEPS = {}
VAGRANT_PUBKEY_URL = (
'https://raw.githubusercontent.com'
'/hashicorp/vagrant/master/keys/vagrant.pub'
)
VAGRANT_PUBKEY = (
'ssh-rsa '
'AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrt'
'vp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oX'
'evct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2'
'PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWK'
'n5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZW'
'FYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4'
'LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCz'
'RdK8jlqm8tehUc9c9WhQ== '
'vagrant insecure public key'
)
class Console:
def __init__(self, path):
logger.debug('Waiting for %s to show up ...', path)
waitfor(path.exists)
logger.debug('Connecting ...')
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
waitfor(lambda: self.sock.connect(str(path)) or True)
self.sock.settimeout(1)
logger.debug('Connection successful')
def recv(self):
chunk = self.sock.recv(10000)
logger.debug('Received %r', chunk)
return chunk
def wait_for_pattern(
self, pattern, limit=1000, timeout=300, verbose=False
):
buffer = b''
def look_for_pattern():
nonlocal buffer
while not re.search(pattern, buffer, re.DOTALL):
try:
chunk = self.recv()
if verbose:
sys.stdout.buffer.write(chunk)
sys.stdout.buffer.flush()
buffer += chunk
buffer = buffer[-limit:]
except OSError:
return
return True
try:
waitfor(look_for_pattern, timeout=timeout)
except (OSError, WaitTimeout):
logger.warning('Timeout waiting for %r; got %r', pattern, buffer)
raise
return buffer
def send(self, message):
logger.debug('Sending %r', message)
self.sock.sendall(message)
def wait_for_poweroff(self, vm, verbose=False):
def forward_console_until_poweroff():
while True:
try:
chunk = self.recv()
except OSError:
break
if verbose:
sys.stdout.buffer.write(chunk)
sys.stdout.buffer.flush()
if not chunk:
break
return not vm.qmp_path.exists()
waitfor(forward_console_until_poweroff, timeout=300)
def build_step(func):
BUILD_STEPS[func.__name__] = func
return func
def interpolate(string):
return string.format(
arch=qemu.arch,
vagrant_pubkey=VAGRANT_PUBKEY,
vagrant_pubkey_url=VAGRANT_PUBKEY_URL,
)
def attach_to_vm(builder, filename, type):
if type == 'disk':
builder.vm.attach_disk(filename)
elif type == 'cdrom':
builder.vm.attach_cdrom(filename)
else:
raise RuntimeError('Unknown attachment type')
@build_step
def create_disk_image(builder, size, attach=None, filename='disk.qcow2'):
path = builder.vm.path / filename
assert not path.exists()
subprocess.check_call(['qemu-img', 'create', '-f', 'qcow2', path, size])
if attach:
attach_to_vm(builder, filename=filename, **attach)
@build_step
def download(builder, url, resize=None, attach=None, filename=None):
url = interpolate(url)
if filename is None:
filename = url.split('/')[-1]
path = builder.vm.path / filename
assert not path.exists()
cache_path = builder.db.cache.get(url)
shutil.copy(cache_path, path)
if resize:
subprocess.check_call(['qemu-img', 'resize', path, resize])
if attach:
attach_to_vm(builder, filename=filename, **attach)
@build_step
def cloud_init_iso(builder, cloud_config, filename, attach=None):
cloud_config = interpolate(cloud_config)
with tempfile.TemporaryDirectory() as tmp:
user_data_path = Path(tmp) / 'user-data'
with user_data_path.open('w') as f:
f.write('#cloud-config\n')
f.write(cloud_config)
meta_data_path = Path(tmp) / 'meta-data'
meta_data_path.touch()
subprocess.check_call([
qemu.genisoimage_cmd,
'-quiet',
'-volid', 'cidata', '-joliet', '-rock',
'-output', builder.vm.path / filename,
user_data_path,
meta_data_path,
])
if attach:
attach_to_vm(builder, filename=filename, **attach)
@build_step
def run_console(builder, steps):
with builder.vm.run(statusline=False):
builder.console = Console(builder.vm.serial_path)
for step in steps:
builder.console_step(step)
builder.console.wait_for_poweroff(builder.vm, builder.verbose)
@build_step
def run(builder, steps, wait_for_ssh=300):
with builder.vm.run():
builder.vm.wait_for_ssh(wait_for_ssh)
for step in steps:
builder.ssh_step(step)
@build_step
def detach(builder, filename):
resources = builder.vm.config['resources']
for item in list(resources):
if item.get('filename') == filename:
resources.remove(item)
builder.vm.config.save()
class ImageTestError(RuntimeError):
pass
class Builder:
def __init__(self, db, recipe, verbose):
self.db = db
self.recipe = recipe
self.verbose = verbose
def wait(self, pattern, **kwargs):
logger.debug('Waiting for pattern: %r', pattern)
output = self.console.wait_for_pattern(
pattern, verbose=self.verbose, **kwargs
)
logger.info('Received: %r', output)
return output
def send(self, message):
logger.info('Sending: %r', message)
self.console.send(message)
def build_step(self, step):
if 'if_arch' in step:
if qemu.arch != step['if_arch']:
logger.info('Skipping step: %r', step['uses'])
return
func = BUILD_STEPS[step['uses']]
func(self, **step['with'])
def console_step(self, step):
uses = step.get('uses')
name = step.get('name') or f'uses: {uses}'
if 'if_arch' in step:
if qemu.arch != step['if_arch']:
logger.info('Skipping step: %r', name)
return
logger.info('Step: %r', name)
if uses == 'ssh_poweroff':
# XXX ubuntu cloud images on x86_64 don't accept acpi poweroff,
# so we hack it by doing poweroff via ssh
try:
self.vm.ssh('poweroff')
except subprocess.CalledProcessError:
pass
return
if uses is not None:
func = BUILD_STEPS[step['uses']]
func(self, **step.get('with', {}))
return
if 'send' in step:
self.send(interpolate(step['send']).encode('utf8'))
if 'wait' in step:
kwargs = {}
if 'timeout' in step:
kwargs['timeout'] = step['timeout']
self.wait(step['wait'].encode('utf8'), **kwargs)
def ssh_step(self, step):
run = step.get('run')
name = step.get('name') or f'run: {run}'
if 'if_arch' in step:
if qemu.arch != step['if_arch']:
logger.info('Skipping step: %r', name)
return
logger.info('Step: %r', name)
try:
self.vm.ssh(run)
except subprocess.CalledProcessError:
if not step.get('continue_on_error'):
raise
def build(self):
name = '_build'
self.db.get_vm(name).destroy()
if 'from' in self.recipe:
image = self.db.get_image(self.recipe['from'])
logger.info('Using %s as base image', image)
else:
image = None
self.vm = VM.create(
db=self.db,
name=name,
image=image,
memory=str(self.recipe['memory']),
)
for step in self.recipe['steps']:
self.build_step(step)
logger.info('Build finished.')
self.image = self.vm.commit()
return self.image
def test(self):
for test in self.recipe.get('tests', []):
test_name = test.get('name')
logger.info('Running test: %r', test_name)
test_vm_name = '_test'
self.db.get_vm(test_vm_name).destroy()
test_vm = VM.create(
db=self.db,
name=test_vm_name,
image=self.image,
memory=str(self.recipe['memory']),
)
with test_vm.run(wait_for_ssh=60):
try:
out = test_vm.ssh(test['run'], capture=True)
except Exception:
logger.exception('Test %r error.', test_name)
raise ImageTestError
logger.debug('Output: %r', out)
expect = test['expect'].encode('utf8')
if re.match(expect, out):
logger.info('Test %r OK', test_name)
else:
logger.error(
'Test %r failed. Expected: %r; output: %r',
test_name, expect, out
)
raise ImageTestError
def build(db, recipe_path, verbose=False):
with recipe_path.open() as f:
recipe = yaml.load(f, yaml.Loader)
builder = Builder(db, recipe, verbose)
image = builder.build()
builder.test()
return image
@click.command
@click.argument(
'recipe', type=click.Path(exists=True, dir_okay=False, path_type=Path)
)
@click.option('--tag', multiple=True)
@click.option('-v', '--verbose', is_flag=True)
def cli(recipe, tag, verbose):
from minivirt.cli import db
try:
image = build(db, recipe, verbose)
except ImageTestError:
raise click.ClickException('Build test failed')
for tag_name in tag:
image.tag(interpolate(tag_name))
size = image.get_size()
print(image.short_name, size) # noqa: T201