]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Defining hostname and FQDN for template VM to create.
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 9 Jun 2020 08:41:52 +0000 (10:41 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 9 Jun 2020 08:41:52 +0000 (10:41 +0200)
lib/cr_vmware_tpl/cobbler.py
lib/cr_vmware_tpl/handler.py

index 83a5aa0d7f17d659f0d81605febea9c6112dbc43..d244128f0c107ee45267565a5475541e82da84cb 100644 (file)
@@ -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."""
index fd72caa840c09cd3642a3ef8d897f2dede5b3a26..42bd3d5462f361f6a9e92fdb98e9d063ea178ca2 100644 (file)
@@ -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):