]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Start with ensuring profile
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 27 May 2020 16:31:35 +0000 (18:31 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 27 May 2020 16:31:35 +0000 (18:31 +0200)
lib/cr_vmware_tpl/cobbler.py
lib/cr_vmware_tpl/config.py
lib/cr_vmware_tpl/handler.py

index c493edabb4be3cb12510252446eebe9929a9c28f..6cc2b7c071c923097ac0006c898b783ba71eb508 100644 (file)
@@ -13,6 +13,7 @@ import logging
 import re
 import datetime
 import pipes
+import json
 
 # Third party modules
 import paramiko
@@ -31,7 +32,7 @@ from .config import CrTplConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '0.2.4'
+__version__ = '0.3.0'
 
 LOG = logging.getLogger(__name__)
 
@@ -61,6 +62,7 @@ class Cobbler(BaseHandler):
         self.private_ssh_key = None
         self.ssh = None
         self.ssh_timeout = CrTplConfiguration.default_cobbler_ssh_timeout
+        self.root_dir = CrTplConfiguration.default_cobbler_rootdir
 
         super(Cobbler, self).__init__(
             appname=appname, verbose=verbose, version=version, base_dir=base_dir,
@@ -76,6 +78,7 @@ class Cobbler(BaseHandler):
             self.ssh_port = config.cobbler_ssh_port
             self.ssh_user = config.cobbler_ssh_user
             self.ssh_timeout = config.cobbler_ssh_timeout
+            self.root_dir = config.cobbler_rootdir
 
         if initialized:
             self.initialized = True
@@ -181,6 +184,55 @@ class Cobbler(BaseHandler):
             LOG.debug(_("Sorted list of found distros:") + "\n{}".format(pp(distro_list)))
         return distro_list
 
+    # -------------------------------------------------------------------------
+    def get_profile_list(self):
+        """Trying to get a list of all configured cobbler profiles."""
+
+        profile_list = []
+        proc = self.exec_cobbler(('profile', 'list'), no_simulate=True)
+        lines = proc.stdout.splitlines()
+        for line in proc.stdout.splitlines():
+            profile = line.strip()
+            if profile:
+                profile_list.append(profile)
+        profile_list.sort(key=str.lower)
+        if self.verbose > 1:
+            LOG.debug(_("Sorted list of found profiles:") + "\n{}".format(pp(profile_list)))
+        return profile_list
+
+    # -------------------------------------------------------------------------
+    def ensure_profile(self):
+        """Ensure the existence and the correctnes of the given profile."""
+
+        profile = self.config.cobbler_profile
+
+        LOG.info(_("Ensuring profile {!r} ...").format(profile))
+
+        profile_list = self.get_profile_list()
+
+        if profile in profile_list:
+            self.change_profile()
+        else:
+            self.add_profile()
+
+    # -------------------------------------------------------------------------
+    def change_profile(self):
+        """Ensure correctnes of an existing profile."""
+
+        profile = self.config.cobbler_profile
+        distro = self.config.cobbler_distro
+
+        LOG.debug(_("Checking existing profile {!r} ...").format(profile))
+
+    # -------------------------------------------------------------------------
+    def add_profile(self):
+        """Creating a new profile."""
+
+        profile = self.config.cobbler_profile
+        distro = self.config.cobbler_distro
+
+        LOG.info(_("Creating new profile {!r} ...").format(profile))
+
 
 # =============================================================================
 if __name__ == "__main__":
index f58aca7202c397ee987258c08cc164893fabd0bc..153a6dfb21b28fce3dc1c9ffe478cf502b0db49f 100644 (file)
@@ -13,13 +13,14 @@ import logging
 import re
 
 # Third party modules
+from pathlib import Path
 
 # Own modules
 from fb_tools.config import ConfigError, BaseConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '1.4.1'
+__version__ = '1.4.2'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -71,6 +72,8 @@ class CrTplConfiguration(BaseConfiguration):
     default_cobbler_ssh_user = 'root'
     default_cobbler_ssh_timeout = 30
     default_cobbler_distro = 'CentOS-8.1-x86_64'
+    default_cobbler_rootdir = Path('/var/lib/cobbler')
+    default_cobbler_profile = 'vmware-template_centos8'
 
     ssh_privkey = 'id_rsa_cr_vmw_tpl'
 
@@ -112,6 +115,8 @@ class CrTplConfiguration(BaseConfiguration):
         self.cobbler_ssh_port = self.default_cobbler_ssh_port
         self.cobbler_ssh_user = self.default_cobbler_ssh_user
         self.cobbler_ssh_timeout = self.default_cobbler_ssh_timeout
+        self.cobbler_rootdir = self.default_cobbler_rootdir
+        self.cobbler_profile = self.default_cobbler_profile
 
         self.excluded_datastores = []
 
@@ -392,7 +397,8 @@ class CrTplConfiguration(BaseConfiguration):
 
         re_port_key = re.compile(r'^\s*ssh[-_]?port\s*$', re.IGNORECASE)
         re_user_key = re.compile(r'^\s*ssh[-_]?user\s*$', re.IGNORECASE)
-        re_timeout_key = re.compile(r'^\s*ssh-_]?timeout\s*$', re.IGNORECASE)
+        re_timeout_key = re.compile(r'^\s*ssh[-_]?timeout\s*$', re.IGNORECASE)
+        re_rootdir_key = re.compile(r'^\s*root[-_]?dir(?:ectory)?\s*$', re.IGNORECASE)
 
         for (key, value) in config.items(section_name):
             if key.lower() == 'distro' and value.strip() != '':
@@ -409,6 +415,19 @@ class CrTplConfiguration(BaseConfiguration):
                 continue
             if re_timeout_key.match(key):
                 self.cobbler_ssh_timeout = int(value)
+                continue
+            if re_rootdir_key.match(key):
+                dpath = Path(value)
+                if dpath.is_absolute():
+                    self.cobbler_rootdir = Path(value)
+                else:
+                    msg = _("Path for Cobbler root directory {!r} is not absolute.").format(
+                        str(dpath))
+                    LOG.error(msg)
+                continue
+            if key.lower() == 'profile' and value.strip() != '':
+                self.cobbler_profile = value.strip().lower()
+                continue
 
 # =============================================================================
 if __name__ == "__main__":
index 1ac829bd12fe931b2f4dab48200d991e336b5e83..665f5eac004928d0daf76d06756ffc1a1a3b6935 100644 (file)
@@ -40,7 +40,7 @@ from .cobbler import CobblerError, Cobbler
 
 from .xlate import XLATOR
 
-__version__ = '1.4.4'
+__version__ = '1.4.5'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -174,6 +174,7 @@ class CrTplHandler(BaseHandler):
         LOG.debug(_("Starting handling ..."))
         self.cobbler.get_cobbler_version()
         self.check_for_cobbler_distro()
+        self.cobbler.ensure_profile()
 
         return 0