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)
|
||||
|
||||
from common.tasks import volume
|
||||
taskset.update([tasks.CreateVagrantBoxDir,
|
||||
taskset.update([tasks.CheckBoxPath,
|
||||
tasks.CreateVagrantBoxDir,
|
||||
tasks.AddPackages,
|
||||
tasks.CreateVagrantUser,
|
||||
tasks.PasswordlessSudo,
|
||||
|
|
|
@ -8,15 +8,32 @@ import shutil
|
|||
assets = os.path.normpath(os.path.join(os.path.dirname(__file__), 'assets'))
|
||||
|
||||
|
||||
class CreateVagrantBoxDir(Task):
|
||||
description = 'Creating directory for the vagrant box'
|
||||
class CheckBoxPath(Task):
|
||||
description = 'Checking if the vagrant box file already exists'
|
||||
phase = phases.preparation
|
||||
predecessors = [workspace.CreateWorkspace]
|
||||
|
||||
@classmethod
|
||||
def run(cls, info):
|
||||
info.vagrant_folder = os.path.join(info.workspace, 'vagrant')
|
||||
os.mkdir(info.vagrant_folder)
|
||||
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)
|
||||
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):
|
||||
|
@ -93,12 +110,8 @@ class PackageBox(Task):
|
|||
|
||||
@classmethod
|
||||
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 = os.path.join(info.vagrant_folder, 'Vagrantfile')
|
||||
vagrantfile = os.path.join(info.vagrant['folder'], 'Vagrantfile')
|
||||
shutil.copy(vagrantfile_source, vagrantfile)
|
||||
|
||||
import random
|
||||
|
@ -107,29 +120,29 @@ class PackageBox(Task):
|
|||
sed_i(vagrantfile, '\\[MAC_ADDRESS\\]', mac_address)
|
||||
|
||||
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)
|
||||
|
||||
from common.tools import log_check_call
|
||||
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])
|
||||
|
||||
ovf_path = os.path.join(info.vagrant_folder, 'box.ovf')
|
||||
cls.write_ovf(info, ovf_path, box_name, mac_address, disk_name)
|
||||
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)
|
||||
box_files = os.listdir(info.vagrant['folder'])
|
||||
log_check_call(['tar', '--create', '--gzip', '--dereference',
|
||||
'--file', box_path,
|
||||
'--directory', info.vagrant_folder]
|
||||
'--file', info.vagrant['box_path'],
|
||||
'--directory', info.vagrant['folder']]
|
||||
+ box_files
|
||||
)
|
||||
import logging
|
||||
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
|
||||
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',
|
||||
'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',
|
||||
|
@ -168,16 +181,16 @@ class PackageBox(Task):
|
|||
attr(disk, 'ovf:uuid', volume_uuid)
|
||||
|
||||
[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/'
|
||||
'vssd:VirtualSystemIdentifier', namespaces)
|
||||
sysid.text = box_name
|
||||
sysid.text = info.vagrant['box_name']
|
||||
|
||||
[machine] = system.findall('./vbox:Machine', namespaces)
|
||||
import uuid
|
||||
attr(machine, 'ovf:uuid', uuid.uuid4())
|
||||
attr(machine, 'ovf:name', box_name)
|
||||
attr(machine, 'ovf:name', info.vagrant['box_name'])
|
||||
from datetime import datetime
|
||||
attr(machine, 'ovf:lastStateChange', datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ'))
|
||||
[nic] = machine.findall('./ovf:Hardware/ovf:Network/ovf:Adapter', namespaces)
|
||||
|
@ -198,5 +211,5 @@ class RemoveVagrantBoxDir(Task):
|
|||
|
||||
@classmethod
|
||||
def run(cls, info):
|
||||
shutil.rmtree(info.vagrant_folder)
|
||||
del info.vagrant_folder
|
||||
shutil.rmtree(info.vagrant['folder'])
|
||||
del info.vagrant['folder']
|
||||
|
|
Loading…
Add table
Reference in a new issue