From 289ce905a163509bbc3d83823b50bf23576e2f04 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 9 Jun 2020 10:41:52 +0200 Subject: [PATCH] Defining hostname and FQDN for template VM to create. --- lib/cr_vmware_tpl/cobbler.py | 42 +++++++++++++++++++++++++++++++++++- lib/cr_vmware_tpl/handler.py | 35 ++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index 83a5aa0..d244128 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -37,7 +37,7 @@ from .config import CrTplConfiguration from .xlate import XLATOR -__version__ = '0.4.1' +__version__ = '0.4.2' LOG = logging.getLogger(__name__) @@ -272,6 +272,46 @@ class Cobbler(BaseHandler): dsc=dsc, rdir=str(rdir), host=self.host) raise ExpectedCobblerError(msg) + # ------------------------------------------------------------------------- + def ensure_remote_directory(self, rdir, desc=None): + + if self.verbose: + msg = _("Ensuring existence of remote directory {!r} ...").format(str(rdir)) + LOG.debug(msg) + + verb = '' + if self.verbose: + verb = " --verbose" + + cmd = textwrap.dedent("""\ + if [ -d {rdir!r} ] ; then + exit 0 + fi + if [ -e {rdir!r} ] ; then + echo "Path {rdir!r} exists, but is not a directory." >&2 + exit 7 + fi + mkdir --parents{verb} {rdir!r} + """).format(rdir=str(rdir), verb=verb) + + proc = self.exec_ssh(cmd) + if proc.returncode == 0: + if proc.stdout: + LOG.debug(_("Output:") + "\n{}".format(proc.stdout)) + else: + dsc = _('Remote directory') + if desc: + dsc = desc + err = _('No error message') + if proc.stderr: + err = proc.stderr + elif proc.stdout: + err = proc.stdout + msg = _( + "{dsc} {rdir!r} on host {host!r} could not be created: {err}").format( + dsc=dsc, rdir=str(rdir), host=self.host, err=err) + raise ExpectedCobblerError(msg) + # ------------------------------------------------------------------------- def get_distro_list(self): """Trying to get a list of all configured distros.""" diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index fd72caa..42bd3d5 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -40,7 +40,7 @@ from .cobbler import CobblerError, Cobbler from .xlate import XLATOR -__version__ = '1.5.0' +__version__ = '1.5.1' LOG = logging.getLogger(__name__) TZ = pytz.timezone('Europe/Berlin') @@ -127,14 +127,45 @@ class CrTplHandler(BaseHandler): terminal_has_colors=self.terminal_has_colors, initialized=False) self.cobbler.initialized = True + if not self.config.os_id: + msg = _("No ID for Operating system defined, please check the configuration.") + raise HandlerError(msg) + cur_ts = datetime.datetime.now() + cur_ts_str = cur_ts.strftime('%Y-%m-%d-%H-%M-%S') + self.tpl_vm_hostname = self.config.os_id + '-' + cur_ts_str + if initialized: self.initialized = True self.vsphere.initialized = True # ------------------------------------------------------------------------- @property - def tpl_vm_hostname_base(self): + def tpl_vm_fqdn(self): + """The FQDN of the template VM.""" + if not self.tpl_vm_hostname: + return None + if not self.config: + return self.tpl_vm_hostname + if not self.config.tpl_vm_domain: + return self.tpl_vm_hostname + return self.tpl_vm_hostname + '.' + self.config.tpl_vm_domain + + # ------------------------------------------------------------------------- + def as_dict(self, short=True): + """ + Transforms the elements of the object into a dict + + @param short: don't include local properties in resulting dict. + @type short: bool + + @return: structure as dict + @rtype: dict + """ + + res = super(CrTplHandler, self).as_dict(short=short) + res['tpl_vm_fqdn'] = self.tpl_vm_fqdn + return res # ------------------------------------------------------------------------- def __call__(self): -- 2.39.5