mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 07:26:29 +00:00
Check if vagrant box exists before bootstrapping
This commit is contained in:
parent
e20c7f9391
commit
9ee318e3d9
2 changed files with 39 additions and 25 deletions
|
@ -14,7 +14,8 @@ def resolve_tasks(taskset, manifest):
|
||||||
taskset.discard(loopback.MoveImage)
|
taskset.discard(loopback.MoveImage)
|
||||||
|
|
||||||
from common.tasks import volume
|
from common.tasks import volume
|
||||||
taskset.update([tasks.CreateVagrantBoxDir,
|
taskset.update([tasks.CheckBoxPath,
|
||||||
|
tasks.CreateVagrantBoxDir,
|
||||||
tasks.AddPackages,
|
tasks.AddPackages,
|
||||||
tasks.CreateVagrantUser,
|
tasks.CreateVagrantUser,
|
||||||
tasks.PasswordlessSudo,
|
tasks.PasswordlessSudo,
|
||||||
|
|
|
@ -8,15 +8,32 @@ import shutil
|
||||||
assets = os.path.normpath(os.path.join(os.path.dirname(__file__), 'assets'))
|
assets = os.path.normpath(os.path.join(os.path.dirname(__file__), 'assets'))
|
||||||
|
|
||||||
|
|
||||||
class CreateVagrantBoxDir(Task):
|
class CheckBoxPath(Task):
|
||||||
description = 'Creating directory for the vagrant box'
|
description = 'Checking if the vagrant box file already exists'
|
||||||
phase = phases.preparation
|
phase = phases.preparation
|
||||||
predecessors = [workspace.CreateWorkspace]
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def run(cls, info):
|
def run(cls, info):
|
||||||
info.vagrant_folder = os.path.join(info.workspace, 'vagrant')
|
box_basename = info.manifest.image['name'].format(**info.manifest_vars)
|
||||||
os.mkdir(info.vagrant_folder)
|
box_name = '{name}.box'.format(name=box_basename)
|
||||||
|
box_path = os.path.join(info.manifest.bootstrapper['workspace'], box_name)
|
||||||
|
if os.path.exists(box_path):
|
||||||
|
from common.exceptions import TaskError
|
||||||
|
msg = 'The vagrant box `{name}\' already exists at `{path}\''.format(name=box_name, path=box_path)
|
||||||
|
raise TaskError(msg)
|
||||||
|
info.vagrant = {'box_name': box_name,
|
||||||
|
'box_path': box_path, }
|
||||||
|
|
||||||
|
|
||||||
|
class CreateVagrantBoxDir(Task):
|
||||||
|
description = 'Creating directory for the vagrant box'
|
||||||
|
phase = phases.preparation
|
||||||
|
predecessors = [workspace.CreateWorkspace, CheckBoxPath]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def run(cls, info):
|
||||||
|
info.vagrant['folder'] = os.path.join(info.workspace, 'vagrant')
|
||||||
|
os.mkdir(info.vagrant['folder'])
|
||||||
|
|
||||||
|
|
||||||
class AddPackages(Task):
|
class AddPackages(Task):
|
||||||
|
@ -93,12 +110,8 @@ class PackageBox(Task):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def run(cls, info):
|
def run(cls, info):
|
||||||
box_basename = info.manifest.image['name'].format(**info.manifest_vars)
|
|
||||||
box_name = '{name}.box'.format(name=box_basename)
|
|
||||||
box_path = os.path.join(info.manifest.bootstrapper['workspace'], box_name)
|
|
||||||
|
|
||||||
vagrantfile_source = os.path.join(assets, 'Vagrantfile')
|
vagrantfile_source = os.path.join(assets, 'Vagrantfile')
|
||||||
vagrantfile = os.path.join(info.vagrant_folder, 'Vagrantfile')
|
vagrantfile = os.path.join(info.vagrant['folder'], 'Vagrantfile')
|
||||||
shutil.copy(vagrantfile_source, vagrantfile)
|
shutil.copy(vagrantfile_source, vagrantfile)
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
@ -107,29 +120,29 @@ class PackageBox(Task):
|
||||||
sed_i(vagrantfile, '\\[MAC_ADDRESS\\]', mac_address)
|
sed_i(vagrantfile, '\\[MAC_ADDRESS\\]', mac_address)
|
||||||
|
|
||||||
metadata_source = os.path.join(assets, 'metadata.json')
|
metadata_source = os.path.join(assets, 'metadata.json')
|
||||||
metadata = os.path.join(info.vagrant_folder, 'metadata.json')
|
metadata = os.path.join(info.vagrant['folder'], 'metadata.json')
|
||||||
shutil.copy(metadata_source, metadata)
|
shutil.copy(metadata_source, metadata)
|
||||||
|
|
||||||
from common.tools import log_check_call
|
from common.tools import log_check_call
|
||||||
disk_name = 'box-disk1.{ext}'.format(ext=info.volume.extension)
|
disk_name = 'box-disk1.{ext}'.format(ext=info.volume.extension)
|
||||||
disk_link = os.path.join(info.vagrant_folder, disk_name)
|
disk_link = os.path.join(info.vagrant['folder'], disk_name)
|
||||||
log_check_call(['ln', '-s', info.volume.image_path, disk_link])
|
log_check_call(['ln', '-s', info.volume.image_path, disk_link])
|
||||||
|
|
||||||
ovf_path = os.path.join(info.vagrant_folder, 'box.ovf')
|
ovf_path = os.path.join(info.vagrant['folder'], 'box.ovf')
|
||||||
cls.write_ovf(info, ovf_path, box_name, mac_address, disk_name)
|
cls.write_ovf(info, ovf_path, mac_address, disk_name)
|
||||||
|
|
||||||
box_files = os.listdir(info.vagrant_folder)
|
box_files = os.listdir(info.vagrant['folder'])
|
||||||
log_check_call(['tar', '--create', '--gzip', '--dereference',
|
log_check_call(['tar', '--create', '--gzip', '--dereference',
|
||||||
'--file', box_path,
|
'--file', info.vagrant['box_path'],
|
||||||
'--directory', info.vagrant_folder]
|
'--directory', info.vagrant['folder']]
|
||||||
+ box_files
|
+ box_files
|
||||||
)
|
)
|
||||||
import logging
|
import logging
|
||||||
logging.getLogger(__name__).info('The vagrant box has been placed at {box_path}'
|
logging.getLogger(__name__).info('The vagrant box has been placed at {box_path}'
|
||||||
.format(box_path=box_path))
|
.format(box_path=info.vagrant['box_path']))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def write_ovf(cls, info, destination, box_name, mac_address, disk_name):
|
def write_ovf(cls, info, destination, mac_address, disk_name):
|
||||||
namespaces = {'ovf': 'http://schemas.dmtf.org/ovf/envelope/1',
|
namespaces = {'ovf': 'http://schemas.dmtf.org/ovf/envelope/1',
|
||||||
'rasd': 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData',
|
'rasd': 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData',
|
||||||
'vssd': 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData',
|
'vssd': 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData',
|
||||||
|
@ -168,16 +181,16 @@ class PackageBox(Task):
|
||||||
attr(disk, 'ovf:uuid', volume_uuid)
|
attr(disk, 'ovf:uuid', volume_uuid)
|
||||||
|
|
||||||
[system] = root.findall('./ovf:VirtualSystem', namespaces)
|
[system] = root.findall('./ovf:VirtualSystem', namespaces)
|
||||||
attr(system, 'ovf:id', box_name)
|
attr(system, 'ovf:id', info.vagrant['box_name'])
|
||||||
|
|
||||||
[sysid] = system.findall('./ovf:VirtualHardwareSection/ovf:System/'
|
[sysid] = system.findall('./ovf:VirtualHardwareSection/ovf:System/'
|
||||||
'vssd:VirtualSystemIdentifier', namespaces)
|
'vssd:VirtualSystemIdentifier', namespaces)
|
||||||
sysid.text = box_name
|
sysid.text = info.vagrant['box_name']
|
||||||
|
|
||||||
[machine] = system.findall('./vbox:Machine', namespaces)
|
[machine] = system.findall('./vbox:Machine', namespaces)
|
||||||
import uuid
|
import uuid
|
||||||
attr(machine, 'ovf:uuid', uuid.uuid4())
|
attr(machine, 'ovf:uuid', uuid.uuid4())
|
||||||
attr(machine, 'ovf:name', box_name)
|
attr(machine, 'ovf:name', info.vagrant['box_name'])
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
attr(machine, 'ovf:lastStateChange', datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ'))
|
attr(machine, 'ovf:lastStateChange', datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ'))
|
||||||
[nic] = machine.findall('./ovf:Hardware/ovf:Network/ovf:Adapter', namespaces)
|
[nic] = machine.findall('./ovf:Hardware/ovf:Network/ovf:Adapter', namespaces)
|
||||||
|
@ -198,5 +211,5 @@ class RemoveVagrantBoxDir(Task):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def run(cls, info):
|
def run(cls, info):
|
||||||
shutil.rmtree(info.vagrant_folder)
|
shutil.rmtree(info.vagrant['folder'])
|
||||||
del info.vagrant_folder
|
del info.vagrant['folder']
|
||||||
|
|
Loading…
Add table
Reference in a new issue