From 5e5eeea0d8aad0ca7dd02d8755c4465b3f536e84 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 29 May 2020 13:17:50 +0200 Subject: [PATCH] Seperating code into an extra method --- lib/cr_vmware_tpl/cobbler.py | 46 +++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index 4d960b5..fd9abfe 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -35,7 +35,7 @@ from .config import CrTplConfiguration from .xlate import XLATOR -__version__ = '0.3.9' +__version__ = '0.3.10' LOG = logging.getLogger(__name__) @@ -305,24 +305,28 @@ class Cobbler(BaseHandler): return profile_list # ------------------------------------------------------------------------- - def ensure_profile_ks(self): + def ensure_remote_file(self, local_file, remote_file): - remote_ks = self.config.cobbler_profile_ks - local_ks = self.base_dir / 'kickstart' / (self.config.cobbler_profile + '.ks') + self.check_remote_directory(remote_file.parent) - if not local_ks.exists() or not local_ks.is_file(): - msg = _("Kickstart script {!r} either not exists or is not a regular file.").format( - str(local_ks)) + if self.verbose > 1: + msg = _("Checking remote file {rfile!r} based on local {lfile!r} ...").format( + rfile=str(remote_file), lfile=str(local_file)) + LOG.debug(msg) + + if not local_file.exists() or not local_file.is_file(): + msg = _("Local file {!r} either not exists or is not a regular file.").format( + str(local_file)) raise ExpectedCobblerError(msg) - local_ks_content = local_ks.read_bytes() - digest = hashlib.sha256(local_ks_content).hexdigest() + local_file_content = local_file.read_bytes() + digest = hashlib.sha256(local_file_content).hexdigest() if self.verbose > 1: LOG.debug(_('{typ} sum of {ks!r} is: {dig}').format( - typ='SHA256', ks=str(local_ks), dig=digest)) + typ='SHA256', ks=str(local_file), dig=digest)) cmd = textwrap.dedent("""\ - if [ -f {ks!r} ] ; then - digest=$(sha256sum {ks!r} | awk '{{print $1}}') + if [ -f {rfile!r} ] ; then + digest=$(sha256sum {rfile!r} | awk '{{print $1}}') echo "Digest: ${{digest}}" if [ "${{digest}}" != {dig!r} ] ; then echo "SHA256 sum does not match." >&2 @@ -332,18 +336,26 @@ class Cobbler(BaseHandler): else exit 3 fi - """).format(ks=str(remote_ks), dig=digest) + """).format(rfile=str(remote_file), dig=digest) proc = self.exec_ssh(cmd) if proc.returncode == 0: - LOG.debug(_("Remote Kickstart script {!r} has the correct content.").format( - str(remote_ks))) + LOG.debug(_("Remote file {!r} has the correct content.").format( + str(remote_file))) return - msg = _("Kickstart script {!r} has to be copied.").format(str(local_ks)) + msg = _("File {!r} has to be copied.").format(str(local_file)) LOG.warn(msg) - self.scp_to(local_ks, remote_ks) + self.scp_to(local_file, remote_file) + + # ------------------------------------------------------------------------- + def ensure_profile_ks(self): + + remote_ks = self.config.cobbler_profile_ks + local_ks = self.base_dir / 'kickstart' / (self.config.cobbler_profile + '.ks') + + self.ensure_remote_file(local_ks, remote_ks) # ------------------------------------------------------------------------- def ensure_profile(self): -- 2.39.5