From 2c7de768618f1e1a4cdc7a1f8e7e415f0200d70c Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 27 May 2020 16:36:43 +0200 Subject: [PATCH] Trying to get the version of Cobbler --- lib/cr_vmware_tpl/cobbler.py | 85 +++++++++++++++++++++++++++++++++++- lib/cr_vmware_tpl/handler.py | 4 +- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index 307b008..f32d7b0 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -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__": diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 51861b7..98a106c 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -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 -- 2.39.5