]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Modifying some properties in class Cobbler in module cr_vmware_tpl.cobbler
authorFrank Brehm <frank@brehm-online.com>
Tue, 21 Jun 2022 16:18:17 +0000 (18:18 +0200)
committerFrank Brehm <frank@brehm-online.com>
Tue, 21 Jun 2022 16:18:17 +0000 (18:18 +0200)
lib/cr_vmware_tpl/cobbler.py

index 098e9f316bcad4db71f63b840eef43d695849441..d3d7a163a6631df71345bf5220bfd4e8659b75ce 100644 (file)
@@ -42,7 +42,7 @@ from .config import CrTplConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '0.7.1'
+__version__ = '0.8.1'
 
 LOG = logging.getLogger(__name__)
 
@@ -71,11 +71,11 @@ class Cobbler(BaseHandler):
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
-            config=None, terminal_has_colors=False, simulate=None, force=None, initialized=False):
+            cfg=None, terminal_has_colors=False, simulate=None, force=None, initialized=False):
 
-        if not isinstance(config, CrTplConfiguration):
+        if not isinstance(cfg, CrTplConfiguration):
             msg = _("{w} is not an instance of {c}, but an instance of {i} instead.").format(
-                w='Parameter config', c='CrTplConfiguration', i=config.__class__.__name__)
+                w='Parameter cfg', c='CrTplConfiguration', i=cfg.__class__.__name__)
             raise HandlerError(msg)
 
         self.host = CrTplConfiguration.default_cobbler_host
@@ -86,7 +86,7 @@ class Cobbler(BaseHandler):
         self.ssh = None
         self.ssh_timeout = CrTplConfiguration.default_cobbler_ssh_timeout
         self.root_dir = CrTplConfiguration.default_cobbler_rootdir
-        self.config = config
+        self.cfg = cfg
         self.cobbler_version = None
 
         super(Cobbler, self).__init__(
@@ -97,13 +97,13 @@ class Cobbler(BaseHandler):
 
         self.private_ssh_key = str(self.base_dir.joinpath('keys', CrTplConfiguration.ssh_privkey))
 
-        self.cobbler_bin = config.cobbler_bin
-        self.private_ssh_key = config.private_ssh_key
-        self.host = config.cobbler_host
-        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
+        self.cobbler_bin = cfg.cobbler_bin
+        self.private_ssh_key = cfg.private_ssh_key
+        self.host = cfg.cobbler_host
+        self.ssh_port = cfg.cobbler_ssh_port
+        self.ssh_user = cfg.cobbler_ssh_user
+        self.ssh_timeout = cfg.cobbler_ssh_timeout
+        self.root_dir = cfg.cobbler_rootdir
 
         if initialized:
             self.initialized = True
@@ -260,12 +260,12 @@ class Cobbler(BaseHandler):
             msg = msg.format(ver=cobbler_version, co='Cobbler', valid=format_list(['2.X', '3.X']))
             raise ExpectedCobblerError(msg)
 
-        self.config.cobbler_major_version = self.cobbler_version.major
+        self.cfg.cobbler_major_version = self.cobbler_version.major
 
-        self.check_remote_directory(self.config.cobbler_rootdir, _('Cobbler root directory'))
+        self.check_remote_directory(self.cfg.cobbler_rootdir, _('Cobbler root directory'))
 
         if self.verbose > 3:
-            LOG.debug("Current configuration:\n" + pp(self.config.as_dict()))
+            LOG.debug("Current configuration:\n" + pp(self.cfg.as_dict()))
 
         return cobbler_version
 
@@ -443,7 +443,7 @@ class Cobbler(BaseHandler):
 
         bname = 'auth_keys_pp_betrieb'
         local_file = self.base_dir / 'keys' / bname
-        remote_file = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir / bname
+        remote_file = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir / bname
 
         self.ensure_remote_file(local_file, remote_file)
 
@@ -451,8 +451,8 @@ class Cobbler(BaseHandler):
     def ensure_rsyslog_cfg_files(self):
 
         files_dir = self.base_dir / 'files'
-        docroot = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir
-        remote_dir = docroot / self.config.system_status
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        remote_dir = docroot / self.cfg.system_status
 
         LOG.info(_("Ensuring currentness of rsyslog config files ..."))
         print_section_start(
@@ -469,8 +469,8 @@ class Cobbler(BaseHandler):
     # -------------------------------------------------------------------------
     def ensure_profile_ks(self):
 
-        local_ks = self.base_dir / 'kickstart' / (self.config.cobbler_profile + '.ks')
-        remote_ks = self.config.cobbler_profile_ks
+        local_ks = self.base_dir / 'kickstart' / ('profile.' + self.cfg.cobbler_profile + '.ks')
+        remote_ks = self.cfg.cobbler_profile_ks
         LOG.info(_("Ensuring currentness of profile kickstart script {!r}.").format(
             str(remote_ks)))
 
@@ -481,7 +481,7 @@ class Cobbler(BaseHandler):
         """Ensure the existence and the correctnes of the given profile."""
 
         self.ensure_profile_ks()
-        profile = self.config.cobbler_profile
+        profile = self.cfg.cobbler_profile
 
         LOG.info(_("Ensuring profile {!r} ...").format(profile))
         print_section_start(
@@ -500,12 +500,10 @@ class Cobbler(BaseHandler):
     def change_profile(self):
         """Ensure correctnes of an existing profile."""
 
-        profile = self.config.cobbler_profile
+        profile = self.cfg.cobbler_profile
 
         LOG.debug(_("Checking existing profile {!r} ...").format(profile))
-        remote_file = (
-            self.config.cobbler_rootdir / 'config' / 'profiles.d' / (
-                self.config.cobbler_profile + '.json'))
+        remote_file = self.cfg.cobbler_profile_dir / (self.cfg.cobbler_profile + '.json')
 
         fcontent = self.get_remote_filecontent(remote_file)
         if self.verbose > 2:
@@ -526,8 +524,8 @@ class Cobbler(BaseHandler):
     # -------------------------------------------------------------------------
     def _change_profile(self, js):
 
-        profile = self.config.cobbler_profile
-        distro = self.config.cobbler_distro
+        profile = self.cfg.cobbler_profile
+        distro = self.cfg.cobbler_distro
 
         args = []
 
@@ -539,27 +537,30 @@ class Cobbler(BaseHandler):
             args.append('--enable-menu')
             args.append('1')
 
-        if js['kickstart'] != str(self.config.cobbler_profile_ks):
-            args.append('--kickstart')
-            args.append(str(self.config.cobbler_profile_ks))
+        if js['kickstart'] != str(self.cfg.cobbler_profile_ks):
+            if self.cfg.cobbler_major_version == 3:
+                args.append('--autoinstall')
+            else:
+                args.append('--kickstart')
+            args.append(str(self.cfg.cobbler_profile_ks))
 
-        if js['repos'] != self.config.cobbler_profile_repos:
+        if js['repos'] != self.cfg.cobbler_profile_repos:
             args.append('--repos')
-            args.append(' '.join(self.config.cobbler_profile_repos))
+            args.append(' '.join(self.cfg.cobbler_profile_repos))
 
-        os_id = self.config.os_id
-        comment = "Profile for creating a {} profile.".format(os_id)
+        os_id = self.cfg.os_id
+        comment = "Kickstart file for creating a {} profile.".format(os_id)
         if js['comment'] != comment:
             args.append('--comment')
             args.append(comment)
 
-        if js['name_servers'] != self.config.cobbler_nameservers:
+        if js['name_servers'] != self.cfg.cobbler_nameservers:
             args.append('--name-servers')
-            args.append(' '.join(self.config.cobbler_nameservers))
+            args.append(' '.join(self.cfg.cobbler_nameservers))
 
-        if js['name_servers_search'] != self.config.cobbler_dns_search:
+        if js['name_servers_search'] != self.cfg.cobbler_dns_search:
             args.append('--name-servers-search')
-            args.append(' '.join(self.config.cobbler_dns_search))
+            args.append(' '.join(self.cfg.cobbler_dns_search))
 
         if self.verbose:
             LOG.debug("Args for 'profile edit:\n{}".format(pp(args)))
@@ -586,25 +587,25 @@ class Cobbler(BaseHandler):
     def add_profile(self):
         """Creating a new profile."""
 
-        profile = self.config.cobbler_profile
+        profile = self.cfg.cobbler_profile
 
         LOG.info(_("Creating new profile {!r} ...").format(profile))
 
-        os_id = self.config.os_id
+        os_id = self.cfg.os_id
         comment = "Profile for creating a {} profile.".format(os_id)
 
         args = ['profile', 'add']
         args.append('--name')
-        args.append(self.config.cobbler_profile)
+        args.append(self.cfg.cobbler_profile)
         args.append('--distro')
-        args.append(self.config.cobbler_distro)
+        args.append(self.cfg.cobbler_distro)
         args.append('--enable-menu')
         args.append('1')
         args.append('--kickstart')
-        args.append(str(self.config.cobbler_profile_ks))
-        if self.config.cobbler_profile_repos:
+        args.append(str(self.cfg.cobbler_profile_ks))
+        if self.cfg.cobbler_profile_repos:
             args.append('--repos')
-            args.append(' '.join(self.config.cobbler_profile_repos))
+            args.append(' '.join(self.cfg.cobbler_profile_repos))
         args.append('--comment')
         args.append(comment)
         args.append('--virt-cpus')
@@ -618,9 +619,9 @@ class Cobbler(BaseHandler):
         args.append('--virt-bridge')
         args.append('br0')
         args.append('--name-servers')
-        args.append(' '.join(self.config.cobbler_nameservers))
+        args.append(' '.join(self.cfg.cobbler_nameservers))
         args.append('--name-servers-search')
-        args.append(' '.join(self.config.cobbler_dns_search))
+        args.append(' '.join(self.cfg.cobbler_dns_search))
 
         proc = self.exec_cobbler(args)
 
@@ -637,9 +638,9 @@ class Cobbler(BaseHandler):
     # -------------------------------------------------------------------------
     def ensure_system_ks(self):
 
-        local_ks_base = 'template-' + self.config.os_id + '.ks'
+        local_ks_base = 'template-' + self.cfg.os_id + '.ks'
         local_ks = self.base_dir / 'kickstart' / local_ks_base
-        remote_ks = self.config.system_ks
+        remote_ks = self.cfg.system_ks
         LOG.info(_("Ensuring currentness of system kickstart script {!r}.").format(
             str(remote_ks)))
         print_section_start(
@@ -652,14 +653,14 @@ class Cobbler(BaseHandler):
     def ensure_snippets(self):
 
         local_snippets_dir = self.base_dir / 'snippets'
-        self.ensure_remote_directory(self.config.snippets_dir)
+        self.ensure_remote_directory(self.cfg.snippets_dir)
 
         LOG.info(_("Ensuring currentness of snippets below {!r}.").format(
-            str(self.config.snippets_dir)))
+            str(self.cfg.snippets_dir)))
         print_section_start('ensure_snippets', "Ensuring currentness of snippets", collapsed=True)
 
         for local_snippet in local_snippets_dir.glob('*'):
-            remote_snippet = self.config.snippets_dir / local_snippet.name
+            remote_snippet = self.cfg.snippets_dir / local_snippet.name
             LOG.debug(_("Ensuring {loc!r} => {rem!r}.").format(
                 loc=str(local_snippet), rem=str(remote_snippet)))
             self.ensure_remote_file(local_snippet, remote_snippet, check_parent=False)
@@ -670,8 +671,8 @@ class Cobbler(BaseHandler):
     def ensure_bashrc(self):
 
         files_dir = self.base_dir / 'files'
-        docroot = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir
-        remote_dir = docroot / self.config.system_status
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        remote_dir = docroot / self.cfg.system_status
 
         LOG.info(_("Ensuring currentness of bashrc files."))
         print_section_start(
@@ -689,8 +690,8 @@ class Cobbler(BaseHandler):
     def ensure_vimrc(self):
 
         files_dir = self.base_dir / 'files'
-        docroot = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir
-        remote_dir = docroot / self.config.system_status
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        remote_dir = docroot / self.cfg.system_status
 
         LOG.info(_("Ensuring currentness of vimrc files."))
         print_section_start(
@@ -708,8 +709,8 @@ class Cobbler(BaseHandler):
     def ensure_logrotate_files(self):
 
         files_dir = self.base_dir / 'files'
-        docroot = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir
-        remote_dir = docroot / self.config.system_status
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        remote_dir = docroot / self.cfg.system_status
 
         LOG.info(_("Ensuring currentness of logrotate files."))
         print_section_start(
@@ -728,8 +729,8 @@ class Cobbler(BaseHandler):
     def ensure_create_motd(self):
 
         local_script = self.base_dir / 'bin' / 'create-motd.sh'
-        docroot = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir
-        remote_dir = docroot / self.config.system_status
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        remote_dir = docroot / self.cfg.system_status
         remote_script = remote_dir / local_script.name
 
         LOG.info(_("Ensuring currentness of create-motd.sh."))
@@ -745,20 +746,20 @@ class Cobbler(BaseHandler):
     def add_system(self, name, fqdn, mac_address, comment=None):
         """Creating a new system."""
 
-        profile = self.config.cobbler_profile
-        os_id = self.config.os_id
+        profile = self.cfg.cobbler_profile
+        os_id = self.cfg.os_id
 
         LOG.info(_("Creating new system {!r} ...").format(name))
 
         if not comment:
             comment = "VMWare template for creating a {} system.".format(os_id)
-        status = self.config.system_status
+        status = self.cfg.system_status
 
         ks_meta_list = []
-        ks_meta_list.append("ROOT_PWD_HASH={}".format(self.config.get_root_pwd_hash()))
-        ks_meta_list.append("SWAP_SIZE_MB={}".format(self.config.swap_size_mb))
+        ks_meta_list.append("ROOT_PWD_HASH={}".format(self.cfg.get_root_pwd_hash()))
+        ks_meta_list.append("SWAP_SIZE_MB={}".format(self.cfg.swap_size_mb))
         ks_meta_list.append("SYSTEM_STATUS={}".format(status))
-        ks_meta_list.append("WS_REL_FILESDIR={}".format(self.config.cobbler_ws_rel_filesdir))
+        ks_meta_list.append("WS_REL_FILESDIR={}".format(self.cfg.cobbler_ws_rel_filesdir))
 
         ks_meta = None
         if ks_meta_list:
@@ -778,7 +779,7 @@ class Cobbler(BaseHandler):
             args.append('--ksmeta')
             args.append(ks_meta)
         args.append('--kickstart')
-        args.append(str(self.config.system_ks))
+        args.append(str(self.cfg.system_ks))
         args.append('--power-type')
         args.append('apc')
         args.append('--hostname')
@@ -858,8 +859,8 @@ class Cobbler(BaseHandler):
 
         local_keys_dir = self.base_dir / 'keys'
         auth_keys_file = local_keys_dir / "auth_keys_pp_betrieb"
-        docroot = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir
-        remote_dir = docroot / self.config.system_status / 'keys'
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        remote_dir = docroot / self.cfg.system_status / 'keys'
         remote_file = remote_dir / "auth_keys_pp_betrieb"
 
         LOG.info(_("Ensuring currentness of authorized_keys file of root {!r}.").format(
@@ -874,8 +875,8 @@ class Cobbler(BaseHandler):
     def ensure_repo_files(self):
 
         files_dir = self.base_dir / 'files'
-        docroot = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir
-        remote_dir = docroot / self.config.system_status / 'repos'
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        remote_dir = docroot / self.cfg.system_status / 'repos'
 
         LOG.info(_("Ensuring currentness of repo files below {!r}.").format(str(files_dir)))
         print_section_start(
@@ -901,8 +902,8 @@ class Cobbler(BaseHandler):
     def ensure_postfix_files(self):
 
         files_dir = self.base_dir / 'files' / 'postfix'
-        docroot = self.config.cobbler_ws_docroot / self.config.cobbler_ws_rel_filesdir
-        remote_dir = docroot / self.config.system_status / 'postfix'
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        remote_dir = docroot / self.cfg.system_status / 'postfix'
 
         LOG.info(_("Ensuring currentness of postfix files below {!r}.").format(str(files_dir)))
         print_section_start(