# 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__)
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__":
from .xlate import XLATOR
-__version__ = '1.4.2'
+__version__ = '1.4.3'
LOG = logging.getLogger(__name__)
TZ = pytz.timezone('Europe/Berlin')
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
def run(self):
LOG.debug(_("Starting handling ..."))
+ self.cobbler.get_cobbler_version()
return 0