From 19c241be2c53490080253a850b2de38b5c52ce73 Mon Sep 17 00:00:00 2001 From: Ilarion Ishkulov Date: Sat, 28 Apr 2018 21:55:09 +0300 Subject: [PATCH] Add libvirt support for vagrant plugin --- bootstrapvz/plugins/vagrant/README.rst | 8 +++++- bootstrapvz/plugins/vagrant/__init__.py | 5 ++++ .../plugins/vagrant/manifest-schema.yml | 8 ++++-- bootstrapvz/plugins/vagrant/tasks.py | 26 ++++++++++++++----- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/bootstrapvz/plugins/vagrant/README.rst b/bootstrapvz/plugins/vagrant/README.rst index a40be4a..e8a3eba 100644 --- a/bootstrapvz/plugins/vagrant/README.rst +++ b/bootstrapvz/plugins/vagrant/README.rst @@ -9,4 +9,10 @@ of the virtual machine. This plugin creates a vagrant box that is ready to be shared or deployed. At the moment it is only compatible with the VirtualBox -provider and doesn't requires any additional settings. +and Libvirt providers. + +Settings +~~~~~~~~ + +- ``provider``: Specifies the provider of a resulting vagrant box. + ``optional`` Valid values: ``virtualbox, libvirt`` Default: ``libvirt`` \ No newline at end of file diff --git a/bootstrapvz/plugins/vagrant/__init__.py b/bootstrapvz/plugins/vagrant/__init__.py index ae71103..33ecd11 100644 --- a/bootstrapvz/plugins/vagrant/__init__.py +++ b/bootstrapvz/plugins/vagrant/__init__.py @@ -6,6 +6,11 @@ from bootstrapvz.common.tools import rel_path def validate_manifest(data, validator, error): validator(data, rel_path(__file__, 'manifest-schema.yml')) + vagrant_provider = data['plugins']['vagrant'].get('provider', 'virtualbox') + if vagrant_provider == 'virtualbox' and data['volume']['backing'] != 'vmdk': + error('Virtualbox vagrant boxes support vmdk images only', ['plugins', 'vagrant', 'provider']) + if vagrant_provider == 'libvirt' and data['volume']['backing'] != 'qcow2': + error('Libvirt vagrant boxes support qcow2 images only', ['plugins', 'vagrant', 'provider']) def resolve_tasks(taskset, manifest): diff --git a/bootstrapvz/plugins/vagrant/manifest-schema.yml b/bootstrapvz/plugins/vagrant/manifest-schema.yml index 08c6544..bfaf12f 100644 --- a/bootstrapvz/plugins/vagrant/manifest-schema.yml +++ b/bootstrapvz/plugins/vagrant/manifest-schema.yml @@ -8,7 +8,7 @@ properties: properties: name: type: string - enum: [virtualbox] + enum: [virtualbox, kvm] system: required: [hostname] volume: @@ -16,11 +16,15 @@ properties: properties: backing: type: string - enum: [vmdk] + enum: [vmdk, qcow2] required: [backing] plugins: type: object properties: vagrant: type: object + properties: + provider: + type: string + enum: [virtualbox, libvirt] additionalProperties: false diff --git a/bootstrapvz/plugins/vagrant/tasks.py b/bootstrapvz/plugins/vagrant/tasks.py index 8538d93..bbbd3ce 100644 --- a/bootstrapvz/plugins/vagrant/tasks.py +++ b/bootstrapvz/plugins/vagrant/tasks.py @@ -4,6 +4,7 @@ from bootstrapvz.common.tasks import workspace from bootstrapvz.common.tools import rel_path import os import shutil +import json assets = rel_path(__file__, 'assets') @@ -128,18 +129,29 @@ class PackageBox(Task): from bootstrapvz.common.tools import sed_i sed_i(vagrantfile, '\\[MAC_ADDRESS\\]', mac_address) - metadata_source = os.path.join(assets, 'metadata.json') - metadata = os.path.join(info._vagrant['folder'], 'metadata.json') - shutil.copy(metadata_source, metadata) + vagrant_provider = info.manifest.plugins['vagrant'].get('provider', 'virtualbox') + metadata = {'provider': vagrant_provider} + + if vagrant_provider == 'libvirt': + metadata['format'] = info.manifest.volume['backing'] + virtual_size = info.volume.size.bytes.get_qty_in('G') + metadata['virtual_size'] = virtual_size + + metadata_file = os.path.join(info._vagrant['folder'], 'metadata.json') + with open(metadata_file, 'w') as f: + json.dump(metadata, f) from bootstrapvz.common.tools import log_check_call - disk_name = 'box-disk1.' + info.volume.extension + if vagrant_provider == 'libvirt': + disk_name = 'box.img' + else: + disk_name = 'box-disk1.' + info.volume.extension + ovf_path = os.path.join(info._vagrant['folder'], 'box.ovf') + cls.write_ovf(info, ovf_path, mac_address, disk_name) + disk_link = os.path.join(info._vagrant['folder'], disk_name) log_check_call(['ln', '-s', info.volume.image_path, disk_link]) - ovf_path = os.path.join(info._vagrant['folder'], 'box.ovf') - cls.write_ovf(info, ovf_path, mac_address, disk_name) - box_files = os.listdir(info._vagrant['folder']) log_check_call(['tar', '--create', '--gzip', '--dereference', '--file', info._vagrant['box_path'],