]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Trying to create profile
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 28 May 2020 14:21:33 +0000 (16:21 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 28 May 2020 14:21:33 +0000 (16:21 +0200)
lib/cr_vmware_tpl/cobbler.py
lib/cr_vmware_tpl/config.py

index 14506fc3dc85619fd490b59e36b497ff01c16f17..3149d47ec6cc9ecd02fe301dcea68a6c5ca5313c 100644 (file)
@@ -32,7 +32,7 @@ from .config import CrTplConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '0.3.2'
+__version__ = '0.3.3'
 
 LOG = logging.getLogger(__name__)
 
@@ -45,6 +45,11 @@ class CobblerError(HandlerError):
     """Exception class for unexpected exceptions."""
     pass
 
+# =============================================================================
+class ExpectedCobblerError(ExpectedHandlerError, CobblerError):
+    """Exception class for predictible exceptions."""
+    pass
+
 # =============================================================================
 class Cobbler(BaseHandler):
     """
@@ -243,6 +248,47 @@ class Cobbler(BaseHandler):
 
         LOG.info(_("Creating new profile {!r} ...").format(profile))
 
+        os_id = self.config.os_id
+        comment = "Profile for creating a {} profile.".format(os_id)
+
+        args = []
+        args.append('--name')
+        args.append(self.config.cobbler_profile)
+        args.append('--distro')
+        args.append(self.config.cobbler_distro)
+        args.append('--enable-menu')
+        args.append('1')
+        args.append('--kickstart')
+        args.append(str(self.config.cobbler_profile_ks))
+        args.append('--kopts')
+        args.append('inst.sshd')
+        if self.config.cobbler_profile_repos:
+            args.append('--repos')
+            args.append(' '.join(self.config.cobbler_profile_repos))
+        args.append('--comment')
+        args.append(comment)
+        args.append('--virt-cpus')
+        args.append('2')
+        args.append('--virt-file-size')
+        args.append('32')
+        args.append('--virt-ram')
+        args.append('4096')
+        args.append('--virt-type')
+        args.append('vmware')
+        args.append('--virt-bridge')
+        args.append('br0')
+        args.append('--name-servers')
+        args.append(' '.join(self.config.cobbler_nameservers))
+        args.append('--name-servers-search')
+        args.append(' '.join(self.config.cobbler_dns_search))
+
+        proc = self.exec_cobbler(args)
+
+        if proc.returncode:
+            msg = _("Error creating a cobbler profile - returncode was {rc}: {err}").format(
+                rc=proc.returncode, err=proc.stderr)
+            raise ExpectedCobblerError(msg)
+
 
 # =============================================================================
 if __name__ == "__main__":
index 153a6dfb21b28fce3dc1c9ffe478cf502b0db49f..9e20716a5d356c2b09d750505a5d3da6a5ad6c75 100644 (file)
@@ -11,6 +11,7 @@ from __future__ import absolute_import
 # Standard module
 import logging
 import re
+import copy
 
 # Third party modules
 from pathlib import Path
@@ -20,7 +21,7 @@ from fb_tools.config import ConfigError, BaseConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '1.4.2'
+__version__ = '1.4.3'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -42,6 +43,8 @@ class CrTplConfiguration(BaseConfiguration):
     and methods to read it from configuration files.
     """
 
+    default_os_id = 'centos8'
+
     default_vsphere_host = 'vcs01.ppbrln.internal'
     default_vsphere_port = 443
     default_vsphere_user = 'root'
@@ -49,7 +52,7 @@ class CrTplConfiguration(BaseConfiguration):
     default_dc = 'vmcc'
     default_folder = 'templates'
     default_template_vm = 'template.pixelpark.com'
-    default_template_name = 'oracle-linux-7-template'
+    default_template_name = default_os_id + '-template'
     default_data_size_gb = 32.0
     default_num_cpus = 2
     default_ram_mb = 4 * 1024
@@ -73,7 +76,20 @@ class CrTplConfiguration(BaseConfiguration):
     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'
+    default_cobbler_profile = 'vmware-template-' + default_os_id
+    default_cobbler_ks_dir = default_cobbler_rootdir / 'kickstarts'
+    default_cobbler_profile_ks = default_cobbler_ks_dir / (default_cobbler_profile + '.ks')
+    default_cobbler_profile_repos = ['pp-centos8-baseos']
+    default_cobbler_nameservers = [
+        '93.188.109.11',
+        '93.188.109.12',
+        '93.188.109.13',
+    ]
+    default_cobbler_dns_search = [
+        'pixelpark.net',
+        'pixelpark.com',
+        'pixelpark.de',
+    ]
 
     ssh_privkey = 'id_rsa_cr_vmw_tpl'
 
@@ -84,6 +100,8 @@ class CrTplConfiguration(BaseConfiguration):
         self, appname=None, verbose=0, version=__version__, base_dir=None,
             encoding=None, config_dir=None, config_file=None, initialized=False):
 
+        self.os_id = self.default_os_id
+
         self.vsphere_host = self.default_vsphere_host
         self.vsphere_port = self.default_vsphere_port
         self.vsphere_user = self.default_vsphere_user
@@ -108,6 +126,9 @@ class CrTplConfiguration(BaseConfiguration):
         self.vmware_cfg_version = self.default_vmware_cfg_version
         self.os_version = self.default_os_version
 
+        self._cobbler_profile_given = False
+        self._template_name_given = False
+
         self.private_ssh_key = None
 
         self.cobbler_distro = self.default_cobbler_distro
@@ -117,6 +138,11 @@ class CrTplConfiguration(BaseConfiguration):
         self.cobbler_ssh_timeout = self.default_cobbler_ssh_timeout
         self.cobbler_rootdir = self.default_cobbler_rootdir
         self.cobbler_profile = self.default_cobbler_profile
+        self.cobbler_ks_dir = self.default_cobbler_ks_dir
+        self.cobbler_profile_ks = self.default_cobbler_profile_ks
+        self.cobbler_profile_repos = copy.copy(self.default_cobbler_profile_repos)
+        self.cobbler_nameservers = copy.copy(self.default_cobbler_nameservers)
+        self.cobbler_dns_search = copy.copy(self.default_cobbler_dns_search)
 
         self.excluded_datastores = []
 
@@ -199,6 +225,15 @@ class CrTplConfiguration(BaseConfiguration):
         if self.max_wait_for_shutdown_vm is None:
             self.max_wait_for_shutdown_vm = self.max_wait_for_general
 
+        if not self._template_name_given:
+            self.template_name = self.os_id + '-template'
+
+        if not self._template_name_given:
+            self.cobbler_profile = 'vmware-template-' + self.os_id
+
+        self.cobbler_ks_dir = self.cobbler_rootdir / 'kickstarts'
+        self.cobbler_profile_ks = self.cobbler_ks_dir / (self.cobbler_profile + '.ks')
+
     # -------------------------------------------------------------------------
     def eval_config_section(self, config, section_name):
 
@@ -278,36 +313,58 @@ class CrTplConfiguration(BaseConfiguration):
         if self.verbose > 1:
             LOG.debug(_("Checking config section {!r} ...").format(section_name))
 
+        re_os_id = re.compile(r'^\s*os[-_]?id\s*$', re.IGNORECASE)
+        re_os_id_subst = re.compile(r'[^a-z0-9_-]+', re.IGNORECASE)
+
         for (key, value) in config.items(section_name):
             if key.lower() == 'vm':
                 self.template_vm = value
                 continue
+            elif re_os_id.match(key):
+                v = value.strip().lower()
+                v = re_os_id_subst.sub('', v)
+                if v:
+                    self.os_id = v
+                continue
             elif key.lower() == 'name':
                 self.template_name = value
+                self._template_name_given = True
+                continue
             elif key.lower() == 'data_size_gb':
                 self.data_size_gb = float(value)
+                continue
             elif key.lower() == 'data_size_mb':
                 self.data_size_gb = float(value) / 1024.0
+                continue
             elif key.lower() == 'data_size_kb':
                 self.data_size_gb = float(value) / 1024.0 / 1024.0
+                continue
             elif key.lower() == 'data_size':
                 self.data_size_gb = float(value) / 1024.0 / 1024.0 / 1024.0
+                continue
             elif key.lower() == 'num_cpus':
                 self.num_cpus = int(value)
+                continue
             elif key.lower() == 'ram_gb':
                 self.ram_mb = int(float(value) * 1024.0)
+                continue
             elif key.lower() == 'ram_mb':
                 self.ram_mb = int(value)
+                continue
             elif key.lower() == 'network':
                 self.network = value.strip()
+                continue
             elif key.lower() == 'mac_address':
                 v = value.strip().lower()
                 if v:
                     self.mac_address = v
+                continue
             elif key.lower() == 'vmware_cfg_version':
                 self.vmware_cfg_version = value.strip()
+                continue
             elif key.lower() == 'os_version':
                 self.os_version = value.strip()
+                continue
 
         return
 
@@ -356,6 +413,7 @@ class CrTplConfiguration(BaseConfiguration):
                     min_val=self.min_max_wait_for_finish_general,
                     max_val=self.max_max_wait_for_finish_general,
                     default_val=self.default_max_wait_for_general)
+                continue
 
         for (key, value) in config.items(section_name):
             if key.lower() == 'max_wait_for_create_vm':
@@ -364,30 +422,35 @@ class CrTplConfiguration(BaseConfiguration):
                     min_val=self.min_max_wait_for_finish_general,
                     max_val=self.max_max_wait_for_finish_general,
                     default_val=self.max_wait_for_general)
+                continue
             elif key.lower() == 'max_wait_for_poweron_vm':
                 self._eval_timeout_value(
                     prop_name='max_wait_for_poweron_vm', value=value,
                     min_val=self.min_max_wait_for_finish_general,
                     max_val=self.max_max_wait_for_finish_general,
                     default_val=self.max_wait_for_general)
+                continue
             elif key.lower() == 'max_wait_for_shutdown_vm':
                 self._eval_timeout_value(
                     prop_name='max_wait_for_shutdown_vm', value=value,
                     min_val=self.min_max_wait_for_finish_general,
                     max_val=self.max_max_wait_for_finish_general,
                     default_val=self.default_max_wait_for_shutdown)
+                continue
             elif key.lower() == 'max_wait_for_purge_vm':
                 self._eval_timeout_value(
                     prop_name='max_wait_for_purge_vm', value=value,
                     min_val=self.min_max_wait_for_finish_general,
                     max_val=self.max_max_wait_for_finish_general,
                     default_val=self.max_wait_for_general)
+                continue
             elif key.lower() == 'max_wait_for_finish_install':
                 self._eval_timeout_value(
                     prop_name='max_wait_for_finish_install', value=value,
                     min_val=self.min_max_wait_for_finish_install,
                     max_val=self.max_max_wait_for_finish_install,
                     default_val=self.default_max_wait_for_finish_install)
+                continue
 
     # -------------------------------------------------------------------------
     def _eval_config_cobbler(self, config, section_name):
@@ -399,6 +462,10 @@ class CrTplConfiguration(BaseConfiguration):
         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_rootdir_key = re.compile(r'^\s*root[-_]?dir(?:ectory)?\s*$', re.IGNORECASE)
+        re_split_values = re.compile(r'[,;\s]+')
+        re_pr_repos_key = re.compile(r'^\s*profile[-_]?repos?\s*$', re.IGNORECASE)
+        re_nameserver_key = re.compile(r'^\s*nameservers?\s*$', re.IGNORECASE)
+        re_dns_search_key = re.compile(r'^\s*dns[-_]?search\s*$', re.IGNORECASE)
 
         for (key, value) in config.items(section_name):
             if key.lower() == 'distro' and value.strip() != '':
@@ -427,7 +494,18 @@ class CrTplConfiguration(BaseConfiguration):
                 continue
             if key.lower() == 'profile' and value.strip() != '':
                 self.cobbler_profile = value.strip().lower()
+                self._cobbler_profile_given = True
                 continue
+            if re_pr_repos_key.match(key) and value.strip() != '':
+                self.cobbler_profile_repos = re_split_values.split(value.strip())
+                continue
+            if re_nameserver_key.match(key) and value.strip() != '':
+                self.cobbler_nameservers = re_split_values.split(value.strip().lower())
+                continue
+            if re_dns_search_key.match(key) and value.strip() != '':
+                self.cobbler_dns_search = re_split_values.split(value.strip().lower())
+                continue
+
 
 # =============================================================================
 if __name__ == "__main__":