Commit 3b79c76d authored by Michael Pöhn's avatar Michael Pöhn

refactored dry_run and verbose into params

parent ae807651
......@@ -47,7 +47,7 @@ def vm_size_str_to_bytes(size):
"(Try something like: '100G')".format(ssize))
def init_params(provider, debver, workdir='.'):
def init_params(provider, debver, workdir='.', verbose=False, dry_run=False):
"""creates dict with carefully chosen parameters for buildservers"""
params = {'vm_name': 'fdroid-{}'.format(debver),
......@@ -63,7 +63,9 @@ def init_params(provider, debver, workdir='.'):
'username': 'vagrant',
'password': 'vagrant',
'boxfile': 'fdroid-{debver}-{provider}.box'.format(
debver=debver, provider=provider)}
debver=debver, provider=provider),
'verbose': verbose,
'dry_run': dry_run}
params['size_bytes'] = vm_size_str_to_bytes(params['size'])
params['size_megs'] = math.ceil(params['size_bytes'] / (2.**20))
params['size_gigs'] = math.ceil(params['size_bytes'] / (2.**30))
......@@ -126,7 +128,7 @@ def write_bootstrap_script(path, deb_mirror, deb_distro, username, provider):
stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def exec_vmdebootstrap(params, provider, verbose=False, dry_run=False):
def exec_vmdebootstrap(params, provider):
write_bootstrap_script(params['boostrapscript'],
params['deb_mirror'],
......@@ -149,11 +151,11 @@ def exec_vmdebootstrap(params, provider, verbose=False, dry_run=False):
'--systemd-network',
'--sudo',
'--sparse']
if verbose:
if params['verbose']:
cmd += ['--verbose']
if provider == 'virtualbox':
cmd += ['--debootstrapopts=components=main,contrib']
if dry_run:
if params['dry_run']:
cmd += ['--dry-run']
logging.debug('> {}'.format(' '.join(cmd)))
logging.info('running vmdebootstrap (please be patient, '
......@@ -161,7 +163,7 @@ def exec_vmdebootstrap(params, provider, verbose=False, dry_run=False):
subprocess.call(cmd)
def libvirt_convert_raw_to_qcow2(params, verbose=False, dry_run=False):
def libvirt_convert_raw_to_qcow2(params):
cmd = (shutil.which('qemu-img'),
'convert',
'-f', 'raw',
......@@ -169,7 +171,7 @@ def libvirt_convert_raw_to_qcow2(params, verbose=False, dry_run=False):
params['img_path_raw'],
params['img_path_qcow2'])
logging.debug('> {}'.format(' '.join(cmd)))
if not dry_run:
if not params['dry_run']:
if os.path.isfile(params['img_path_qcow2']):
os.remove(params['img_path_qcow2'])
logging.debug('removed already existing file: {}'.format(
......@@ -180,8 +182,8 @@ def libvirt_convert_raw_to_qcow2(params, verbose=False, dry_run=False):
logging.info('(dry run) Skip converting raw image to qcow2')
def libvirt_write_metadata_json(params, dry_run=False):
if not dry_run:
def libvirt_write_metadata_json(params):
if not params['dry_run']:
with open(params['metadata_json_path'], 'w') as f:
f.write(json.dumps({'provider': 'libvirt',
'format': 'qcow2',
......@@ -192,8 +194,8 @@ def libvirt_write_metadata_json(params, dry_run=False):
.format(params['metadata_json_path']))
def libvirt_write_vagrantfile(params, dry_run=False):
if not dry_run:
def libvirt_write_vagrantfile(params):
if not params['dry_run']:
with open(params['vagrantfile_path'], 'w') as f:
f.write(get_resource_as_string('libvirt.Vagrantfile')
.format(**params))
......@@ -202,8 +204,8 @@ def libvirt_write_vagrantfile(params, dry_run=False):
.format(params['vagrantfile_path']))
def libvirt_package_box(params, dry_run=False):
if not dry_run:
def libvirt_package_box(params):
if not params['dry_run']:
logging.info("building vagrant box file: '{boxfile}' "
"(please be patient, this may take several minutes)"
.format(**params))
......@@ -231,7 +233,7 @@ def get_resource_as_string(resource_name):
return f.read()
def vbox_convert_raw_to_vmdk(params, dry_run=False):
def vbox_convert_raw_to_vmdk(params):
cmd = (shutil.which('VBoxManage'),
'convertfromraw',
params['img_path_raw'],
......@@ -239,7 +241,7 @@ def vbox_convert_raw_to_vmdk(params, dry_run=False):
'--format',
'VMDK')
logging.debug('> {}'.format(' '.join(cmd)))
if not dry_run:
if not params['dry_run']:
if os.path.isfile(params['img_path_vmdk']):
os.remove(params['img_path_vmdk'])
logging.debug('removed already existing file: {}'.format(
......@@ -250,13 +252,13 @@ def vbox_convert_raw_to_vmdk(params, dry_run=False):
logging.info('(dry run) Skip converting raw image to vmdk')
def vbox_write_ovf(params, dry_run=False):
def vbox_write_ovf(params):
if params['deb_distro'] == 'jessie':
ovf_template = get_resource_as_string('jessie.box.ovf')
elif params['deb_distro'] == 'stretch':
ovf_template = get_resource_as_string('stretch.box.ovf')
if not dry_run:
if not params['dry_run']:
with open(params['ovf_path'], 'w') as f:
f.write(ovf_template.format(**params))
else:
......@@ -264,8 +266,8 @@ def vbox_write_ovf(params, dry_run=False):
.format(params['ovf_path']))
def vbox_write_vagrantfile(params, dry_run=False):
if not dry_run:
def vbox_write_vagrantfile(params):
if not params['dry_run']:
with open(params['vagrantfile_path'], 'w') as f:
f.write(get_resource_as_string('virtualbox.Vagrantfile')
.format(**params))
......@@ -274,8 +276,8 @@ def vbox_write_vagrantfile(params, dry_run=False):
.format(params['vagrantfile_path']))
def vbox_package_box(params, dry_run=False):
if not dry_run:
def vbox_package_box(params):
if not params['dry_run']:
logging.info("building vagrant box file: '{boxfile}' "
"(please be patient, this may take several minutes)"
.format(**params))
......@@ -342,24 +344,23 @@ def main(provider='virtualbox', debver='jessie64',
workdir = tmpdir
# main parameters for this image
params = init_params(provider, debver, workdir=workdir)
params = init_params(provider, debver, workdir=workdir,
verbose=verbose, dry_run=dry_run)
logging.debug('image parameters: {}'
.format(json.dumps(params, indent=2)))
exec_vmdebootstrap(params, provider, verbose=verbose, dry_run=dry_run)
exec_vmdebootstrap(params, provider)
if provider == 'libvirt':
libvirt_convert_raw_to_qcow2(params,
verbose=verbose,
dry_run=dry_run)
libvirt_write_metadata_json(params, dry_run=dry_run)
libvirt_write_vagrantfile(params, dry_run=dry_run)
libvirt_package_box(params, dry_run=dry_run)
libvirt_convert_raw_to_qcow2(params)
libvirt_write_metadata_json(params)
libvirt_write_vagrantfile(params)
libvirt_package_box(params)
elif provider == 'virtualbox':
vbox_convert_raw_to_vmdk(params, dry_run=dry_run)
vbox_write_ovf(params, dry_run=dry_run)
vbox_write_vagrantfile(params, dry_run=dry_run)
vbox_package_box(params, dry_run=dry_run)
vbox_convert_raw_to_vmdk(params)
vbox_write_ovf(params)
vbox_write_vagrantfile(params)
vbox_package_box(params)
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment