]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Trying to get the version of Cobbler
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 27 May 2020 14:36:43 +0000 (16:36 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 27 May 2020 14:36:43 +0000 (16:36 +0200)
lib/cr_vmware_tpl/cobbler.py
lib/cr_vmware_tpl/handler.py

index 307b0083a21876cad45bc3c7e6cc1a8e1a948656..f32d7b02f6d7e93ca1f21ec2495a141f26bd4940 100644 (file)
@@ -11,23 +11,27 @@ from __future__ import absolute_import, print_function
 # Standard module
 import logging
 import re
+import datetime
+import pipes
 
 # Third party modules
 import paramiko
 
 # Own modules
 
-from fb_tools.common import pp, to_str
+from fb_tools.common import pp, to_str, is_sequence
 
 from fb_tools.errors import HandlerError, ExpectedHandlerError
 
+from fb_tools.handling_obj import CompletedProcess
+
 from fb_tools.handler import BaseHandler
 
 from .config import CrTplConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '0.1.1'
+__version__ = '0.2.1'
 
 LOG = logging.getLogger(__name__)
 
@@ -76,6 +80,83 @@ class Cobbler(BaseHandler):
         if initialized:
             self.initialized = True
 
+    # -------------------------------------------------------------------------
+    def exec_cobbler(self, cmd, no_simulate=False):
+
+        simulate = self.simulate
+        if not no_simulate:
+            simulate = False
+
+        cmds = []
+        if simulate:
+            cmds.append('echo')
+        if self.ssh_user != 'root':
+            cmds.append('sudo')
+        cmds.append('cobbler')
+        if cmd is not None:
+            if is_sequence(cmd):
+                cmds += cmd
+            else:
+                c = to_str(cmd)
+                if not isinstance(c, str):
+                    msg = _(
+                        "Command {c!r} is neither an Array nor a String, "
+                        "but a {t!r} instead.").format(
+                        c=cmd, t=cmd.__class__.__name__)
+                    raise TypeError(msg)
+                cmds.append(c)
+
+        ssh = None
+
+        try:
+
+            if self.verbose > 2:
+                LOG.debug(_("Initializing {} ...").format('paramiko SSHClient'))
+            ssh = paramiko.SSHClient()
+            if self.verbose > 2:
+                LOG.debug(_("Loading SSH system host keys."))
+            ssh.load_system_host_keys()
+            if self.verbose > 2:
+                LOG.debug(_("Setting SSH missing host key policy to {}.").format('AutoAddPolicy'))
+            ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
+
+            start_dt = datetime.datetime.now()
+
+            if self.verbose > 1:
+                LOG.debug(_("Connecting to {h!r}, port {p} as {u!r} per SSH ...").format(
+                    h=self.host, p=self.ssh_port, u=self.ssh_user))
+            ssh.connect(
+                self.host, port=self.ssh_port, timeout=self.ssh_timeout,
+                username=self.ssh_user, key_filename=self.private_ssh_key)
+
+            cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmds))
+            if self.verbose > 1:
+                LOG.debug(_("Executing: {}").format(cmd_str))
+
+            stdin, stdout, stderr = ssh.exec_command(cmd_str, timeout=self.ssh_timeout)
+            end_dt = datetime.datetime.now()
+            retcode = stdout.channel.recv_exit_status()
+
+            output = to_str(stdout.read()).strip()
+            err = to_str(stderr.read()).strip()
+
+            proc = CompletedProcess(cmds, retcode, output, err, start_dt=start_dt, end_dt=end_dt)
+
+            if self.verbose > 2:
+                LOG.debug(_("Completed SSH process:") + "\n{}".format(proc))
+            return proc
+
+    # -------------------------------------------------------------------------
+    def get_cobbler_version(self):
+        """Trying to evaluate the version of Cobbler on the cobbler host."""
+
+        proc = self.exec_cobbler('version', no_simulate=True)
+
+        cobbler_version = proc.stdout
+        LOG.info(_("Version of {} is:").format("Cobbler") + "\n{}".format(cobbler_version))
+
+        return cobbler_version
+
 
 # =============================================================================
 if __name__ == "__main__":
index 51861b79bd71f3d5304faa9e58fa6eaba69e436f..98a106c588be80ba805057f687940f0f0eda0927 100644 (file)
@@ -40,7 +40,7 @@ from .cobbler import CobblerError, Cobbler
 
 from .xlate import XLATOR
 
-__version__ = '1.4.2'
+__version__ = '1.4.3'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -124,6 +124,7 @@ class CrTplHandler(BaseHandler):
             appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
             config=config, simulate=self.simulate, force=self.force,
             terminal_has_colors=self.terminal_has_colors, initialized=False)
+        self.cobbler.initialized = True
 
         if initialized:
             self.initialized = True
@@ -171,6 +172,7 @@ class CrTplHandler(BaseHandler):
     def run(self):
 
         LOG.debug(_("Starting handling ..."))
+        self.cobbler.get_cobbler_version()
 
         return 0