1
0
mirror of https://github.com/gryf/boxpy.git synced 2025-12-19 21:47:59 +01:00

Rearranged a bit Config class.

This commit is contained in:
2021-05-06 20:41:17 +02:00
parent b557a6353d
commit 6d0fa3445f

31
box.py
View File

@@ -226,14 +226,22 @@ class Config:
self.hostname = None self.hostname = None
self.key = None self.key = None
self.memory = None self.memory = None
self.name = None self.name = args.name # this one is not stored anywhere
self.port = None self.port = None # at least is not even tried to be retrieved
self.version = None self.version = None
self._conf = {}
# first, grab the cloud config file # set defaults stored in hard coded yaml
self.user_data = args.cloud_config self._set_defaults()
# initialize default from yaml file(s) first # look at VM metadata, and gather known attributes, and update it
# accordingly
vm_info = vbox.get_vm_info() if vbox else {}
for attr in self.ATTRS:
if not vm_info.get(attr):
continue
setattr(self, attr, vm_info[attr])
# initialize default from yaml file(s) first
self._combine_cc(vbox) self._combine_cc(vbox)
# than override all of the attrs with provided args from commandline. # than override all of the attrs with provided args from commandline.
@@ -249,6 +257,10 @@ class Config:
self.hostname = self.hostname or self._normalize_name() self.hostname = self.hostname or self._normalize_name()
def get_cloud_config_tpl(self):
conf = "#cloud-config\n" + yaml.safe_dump(self._conf)
return string.Template(conf)
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')
@@ -256,7 +268,6 @@ class Config:
return ''.join(x for x in name if x.isalnum() or x == '-') return ''.join(x for x in name if x.isalnum() or x == '-')
def _combine_cc(self, vbox): def _combine_cc(self, vbox):
# that's default config
conf = yaml.safe_load(USER_DATA) conf = yaml.safe_load(USER_DATA)
if vbox and not self.user_data: if vbox and not self.user_data:
@@ -273,21 +284,19 @@ class Config:
custom_conf = yaml.safe_load(fobj) custom_conf = yaml.safe_load(fobj)
conf = self._update(conf, custom_conf) conf = self._update(conf, custom_conf)
# set the attributes. # update the attributes with data from read user cloud config
for key, val in conf.get('boxpy_data', {}).items(): for key, val in conf.get('boxpy_data', {}).items():
if not val: if not val:
continue continue
setattr(self, key, str(val)) setattr(self, key, str(val))
# remove boxpy_data since it will be not needed on the guest side
if conf.get('boxpy_data'): if conf.get('boxpy_data'):
if conf['boxpy_data'].get('advanced'): if conf['boxpy_data'].get('advanced'):
self.advanced = conf['boxpy_data']['advanced'] self.advanced = conf['boxpy_data']['advanced']
del conf['boxpy_data'] del conf['boxpy_data']
self._conf = "#cloud-config\n" + yaml.safe_dump(conf) self._conf = conf
def get_cloud_config_tpl(self):
return string.Template(self._conf)
def _update(self, source, update): def _update(self, source, update):
for key, val in update.items(): for key, val in update.items():