]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Adding lib/cr_vmware_tpl/cobbler.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 27 May 2020 11:59:14 +0000 (13:59 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 27 May 2020 11:59:14 +0000 (13:59 +0200)
lib/cr_vmware_tpl/cobbler.py [new file with mode: 0644]
lib/cr_vmware_tpl/config.py
lib/cr_vmware_tpl/handler.py

diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py
new file mode 100644 (file)
index 0000000..b7fe275
--- /dev/null
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+@author: Frank Brehm
+@contact: frank.brehm@pixelpark.com
+@copyright: © 2020 by Frank Brehm, Berlin
+@summary: A handler module for executing cobbler actions
+"""
+from __future__ import absolute_import, print_function
+
+# Standard module
+import logging
+import re
+
+# Third party modules
+import paramiko
+
+# Own modules
+
+from fb_tools.common import pp, to_str
+
+from fb_tools.errors import HandlerError, ExpectedHandlerError
+
+from fb_tools.handler import BaseHandler
+
+from .config import CrTplConfiguration
+
+from .xlate import XLATOR
+
+__version__ = '0.0.1'
+
+LOG = logging.getLogger(__name__)
+
+_ = XLATOR.gettext
+ngettext = XLATOR.ngettext
+
+
+# =============================================================================
+class CobblerError(HandlerError):
+    """Exception class for unexpected exceptions."""
+    pass
+
+# =============================================================================
+class Cobbler(BaseHandler):
+    """
+    A handler class for executing cobbler actions.
+    """
+
+    # -------------------------------------------------------------------------
+    def __init__(
+        self, appname=None, verbose=0, version=__version__, base_dir=None,
+            config=None, terminal_has_colors=False, simulate=None, force=None, initialized=False):
+
+        self.host = CrTplConfiguration.default_cobbler_host
+        self.ssh_port = CrTplConfiguration.default_cobbler_ssh_port
+        self.ssh_user = CrTplConfiguration.default_cobbler_ssh_user
+        self.private_ssh_key = None
+        self.ssh = None
+        self.ssh_timeout = CrTplConfiguration.default_cobbler_ssh_timeout
+
+        super(Cobbler, self).__init__(
+            appname=appname, verbose=verbose, version=version, base_dir=base_dir,
+            terminal_has_colors=terminal_has_colors, simulate=simulate,
+            force=force, initialized=False,
+        )
+
+        self.private_ssh_key = str(self.base_dir.joinpath('keys', CrTplConfiguration.ssh_privkey))
+
+        if config:
+            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
+
+        if initialized:
+            self.initialized = True
+
+
+# =============================================================================
+if __name__ == "__main__":
+
+    pass
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list
index fbd1792a612c17d79f4bd2da382fec046f34b1bf..c1e60a02b81cfe97074e5fcc4a2fcdbb46d53350 100644 (file)
@@ -19,7 +19,7 @@ from fb_tools.config import ConfigError, BaseConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '1.3.1'
+__version__ = '1.4.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -66,6 +66,13 @@ class CrTplConfiguration(BaseConfiguration):
     max_max_wait_for_finish_install = 24 * 60 * 60
     limit_max_nr_templates_stay = 100
 
+    default_cobbler_host = 'cobbler.pixelpark.com'
+    default_cobbler_ssh_port = 22
+    default_cobbler_ssh_user = 'root'
+    default_cobbler_ssh_timeout = 30
+
+    ssh_privkey = 'id_rsa_cr_vmw_tpl'
+
     mac_address_template = "00:16:3e:53:{:02x}:{:02x}"
 
     # -------------------------------------------------------------------------
@@ -97,6 +104,13 @@ class CrTplConfiguration(BaseConfiguration):
         self.vmware_cfg_version = self.default_vmware_cfg_version
         self.os_version = self.default_os_version
 
+        self.private_ssh_key = None
+
+        self.cobbler_host = self.default_cobbler_host
+        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.excluded_datastores = []
 
         super(CrTplConfiguration, self).__init__(
@@ -104,6 +118,8 @@ class CrTplConfiguration(BaseConfiguration):
             encoding=encoding, config_dir=config_dir, config_file=config_file, initialized=False,
         )
 
+        self.private_ssh_key = str(self.base_dir.joinpath('keys', self.ssh_privkey))
+
         if initialized:
             self.initialized = True
 
@@ -190,6 +206,9 @@ class CrTplConfiguration(BaseConfiguration):
         if section_name.lower() == 'timeouts':
             self._eval_config_timeouts(config, section_name)
             return
+        if section_name.lower() == 'cobbler':
+            self._eval_config_cobbler(config, section_name)
+            return
 
         if self.verbose > 1:
             LOG.debug(_("Unhandled configuration section {!r}.").format(section_name))
@@ -363,6 +382,28 @@ class CrTplConfiguration(BaseConfiguration):
                     max_val=self.max_max_wait_for_finish_install,
                     default_val=self.default_max_wait_for_finish_install)
 
+    # -------------------------------------------------------------------------
+    def _eval_config_cobbler(self, config, section_name):
+
+        if self.verbose > 1:
+            LOG.debug(_("Checking config section {!r} ...").format(section_name))
+
+        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)
+
+        for (key, value) in config.items(section_name):
+            if key.lower() == 'host' and value.strip() != '':
+                self.cobbler_host = value.strip().lower()
+                continue
+            if re_port_key.match(key) and value.strip() != '':
+                self.cobbler_ssh_port = int(value)
+                continue
+            if re_user_key.match(key):
+                self.cobbler_ssh_user = value.strip().lower()
+                continue
+            if re_timeout_key.match(key):
+                self.cobbler_ssh_timeout = int(value)
 
 # =============================================================================
 if __name__ == "__main__":
index fbaac0b55b4aee4a635806d141b77f12f5838d31..d02f337bfdcf46f5591a715c3560e1e86db76d58 100644 (file)
@@ -3,7 +3,7 @@
 """
 @author: Frank Brehm
 @contact: frank.brehm@pixelpark.com
-@copyright: © 2018 by Frank Brehm, Berlin
+@copyright: © 2020 by Frank Brehm, Berlin
 @summary: A handler module for creating the VMWare template
 """
 from __future__ import absolute_import, print_function