mirror of
https://github.com/gryf/boxpy.git
synced 2025-12-21 06:37:58 +01:00
Rebuild now pass the config to the create function.
This will avoid overwriting all of the configuration during executing vmcreate function, which initially doesn't know anything about existing data - it only knows about passed through command line parameters. Now it should be fixed.
This commit is contained in:
36
box.py
36
box.py
@@ -241,13 +241,16 @@ class Config:
|
|||||||
if not vm_info.get(attr):
|
if not vm_info.get(attr):
|
||||||
continue
|
continue
|
||||||
setattr(self, attr, vm_info[attr])
|
setattr(self, attr, vm_info[attr])
|
||||||
# initialize default from yaml file(s) first
|
|
||||||
|
# next, grab the cloud config file
|
||||||
|
self.user_data = args.cloud_config or vm_info.get('user_data')
|
||||||
|
|
||||||
|
# combine it with the defaults, set attributes by boxpy_data
|
||||||
|
# definition, if found
|
||||||
self._combine_cc(vbox)
|
self._combine_cc(vbox)
|
||||||
|
|
||||||
# than override all of the attrs with provided args from commandline.
|
# than, override all of the attributes with provided arguments from
|
||||||
# If the value of rhe
|
# the command line
|
||||||
# in case we have vbox object provided.
|
|
||||||
# this means that we need to read params stored on the VM attributes.
|
|
||||||
vm_info = vbox.get_vm_info() if vbox else {}
|
vm_info = vbox.get_vm_info() if vbox else {}
|
||||||
for attr in self.ATTRS:
|
for attr in self.ATTRS:
|
||||||
val = getattr(args, attr, None) or vm_info.get(attr)
|
val = getattr(args, attr, None) or vm_info.get(attr)
|
||||||
@@ -255,12 +258,20 @@ class Config:
|
|||||||
continue
|
continue
|
||||||
setattr(self, attr, str(val))
|
setattr(self, attr, str(val))
|
||||||
|
|
||||||
|
# finally, figure out host name
|
||||||
self.hostname = self.hostname or self._normalize_name()
|
self.hostname = self.hostname or self._normalize_name()
|
||||||
|
|
||||||
def get_cloud_config_tpl(self):
|
def get_cloud_config_tpl(self):
|
||||||
conf = "#cloud-config\n" + yaml.safe_dump(self._conf)
|
conf = "#cloud-config\n" + yaml.safe_dump(self._conf)
|
||||||
return string.Template(conf)
|
return string.Template(conf)
|
||||||
|
|
||||||
|
def _set_defaults(self):
|
||||||
|
conf = yaml.safe_load(USER_DATA)
|
||||||
|
|
||||||
|
# update attributes with default values
|
||||||
|
for key, val in conf['boxpy_data'].items():
|
||||||
|
setattr(self, key, str(val))
|
||||||
|
|
||||||
def _normalize_name(self):
|
def _normalize_name(self):
|
||||||
name = self.name.replace(' ', '-')
|
name = self.name.replace(' ', '-')
|
||||||
name = name.encode('ascii', errors='ignore')
|
name = name.encode('ascii', errors='ignore')
|
||||||
@@ -270,14 +281,6 @@ class Config:
|
|||||||
def _combine_cc(self, vbox):
|
def _combine_cc(self, vbox):
|
||||||
conf = yaml.safe_load(USER_DATA)
|
conf = yaml.safe_load(USER_DATA)
|
||||||
|
|
||||||
if vbox and not self.user_data:
|
|
||||||
# in case of not provided (new) custom cloud config, and vbox
|
|
||||||
# object is present, read information out of potentially stored
|
|
||||||
# file in VM attributes.
|
|
||||||
vm_info = vbox.get_vm_info()
|
|
||||||
if os.path.exists(vm_info.get('user_data')):
|
|
||||||
self.user_data = vm_info['user_data']
|
|
||||||
|
|
||||||
# read user custom cloud config (if present) and update config dict
|
# read user custom cloud config (if present) and update config dict
|
||||||
if self.user_data and os.path.exists(self.user_data):
|
if self.user_data and os.path.exists(self.user_data):
|
||||||
with open(self.user_data) as fobj:
|
with open(self.user_data) as fobj:
|
||||||
@@ -648,8 +651,9 @@ class IsoImage:
|
|||||||
'drive')
|
'drive')
|
||||||
|
|
||||||
|
|
||||||
def vmcreate(args):
|
def vmcreate(args, conf=None):
|
||||||
conf = Config(args)
|
if not conf:
|
||||||
|
conf = Config(args)
|
||||||
vbox = VBoxManage(conf.name)
|
vbox = VBoxManage(conf.name)
|
||||||
if not vbox.create(conf.cpus, conf.memory, conf.port):
|
if not vbox.create(conf.cpus, conf.memory, conf.port):
|
||||||
return 10
|
return 10
|
||||||
@@ -740,7 +744,7 @@ def vmrebuild(args):
|
|||||||
conf.disk_size = vbox.get_media_size(disk_path)
|
conf.disk_size = vbox.get_media_size(disk_path)
|
||||||
|
|
||||||
vmdestroy(args)
|
vmdestroy(args)
|
||||||
vmcreate(args)
|
vmcreate(args, conf)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user