From: Frank Brehm Date: Fri, 29 May 2020 10:26:14 +0000 (+0200) Subject: Better exception handling X-Git-Tag: 2.1.2^2~9^2~31^2~80 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=da8f9eed48856ba2fbd71722a072736ccfd2f58f;p=pixelpark%2Fcreate-vmware-tpl.git Better exception handling --- diff --git a/etc/create-vmware-template.ini.default b/etc/create-vmware-template.ini.default index b56ca18..f08e18b 100644 --- a/etc/create-vmware-template.ini.default +++ b/etc/create-vmware-template.ini.default @@ -16,6 +16,8 @@ [Template] +;vm_id = centos8 + ;vm = template.pixelpark.com ;vmware_cfg_version = vmx-14 @@ -56,4 +58,27 @@ ;max_wait_for_finish_install = 3600 +[Cobbler] + +# distro = CentOS-8.1-x86_64 + +# host = cobbler.pixelpark.com + +# ssh_port = 22 + +# ssh_user = root + +# ssh_timeout = 30 + +# root_dir = /var/lib/cobbler + +# profile = + +# profile_repos = pp-centos8-baseos + +# nameservers = 93.188.109.11, 93.188.109.12, 93.188.109.13 + +# dns_search = pixelpark.net, pixelpark.com, pixelpark.de + + ; vim: filetype=dosini diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index 357b8ec..1130864 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -19,6 +19,7 @@ import textwrap # Third party modules import paramiko +from paramiko.ssh_exception import SSHException # Own modules @@ -34,7 +35,7 @@ from .config import CrTplConfiguration from .xlate import XLATOR -__version__ = '0.3.7' +__version__ = '0.3.8' LOG = logging.getLogger(__name__) @@ -163,6 +164,11 @@ class Cobbler(BaseHandler): proc = CompletedProcess(cmd, retcode, output, err, start_dt=start_dt, end_dt=end_dt) + except SSHException as e: + msg = _("Could not connect via {w} to {user}@{host}: {e}").format( + w='SSH', user=self.ssh_user, host=self.host, e=e) + raise ExpectedCobblerError(msg) + finally: if ssh: if self.verbose > 2: @@ -205,6 +211,11 @@ class Cobbler(BaseHandler): local=str(local_file), host=self.host, remote=str(remote_file))) sftp.put(str(local_file), str(remote_file)) + except SSHException as e: + msg = _("Could not connect via {w} to {user}@{host}: {e}").format( + w='SCP', user=self.ssh_user, host=self.host, e=e) + raise ExpectedCobblerError(msg) + finally: sftp = None if ssh: @@ -218,10 +229,38 @@ class Cobbler(BaseHandler): proc = self.exec_cobbler('version', no_simulate=True) + if proc.returncode != 0: + err = _('No error message') + if proc.stderr: + err = proc.stderr + elif proc.stdout: + err = proc.stdout + msg = _("Could not get version of cobbler: {}").format(err) + raise ExpectedCobblerError(msg) + first_line = proc.stdout.splitlines()[0] cobbler_version = re.sub(r'^\s*cobbler\s+', '', first_line, flags=re.IGNORECASE).strip() LOG.info(_("Version of {} is:").format("Cobbler") + " {!r}".format(cobbler_version)) + rdir = self.config.cobbler_rootdir + if self.verbose > 1: + msg = _("Checking existence of remote directory {!r} ...").format(str(rdir)) + LOG.debug(msg) + + cmd = textwrap.dedent("""\ + if [ -d {rdir!r} ] ; then + exit 0 + fi + exit 7 + """).format(rdir=str(rdir)) + + proc = self.exec_ssh(cmd) + if proc.returncode != 0: + msg = _( + "Cobbler root directory {rdir!r} on host {host!r} does not " + "exists or is not a directory.").format(rdir=str(rdir), host=self.host) + raise ExpectedCobblerError(msg) + return cobbler_version # -------------------------------------------------------------------------