From 631afaed50229606543df47f51360e2971d2f405 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 28 May 2020 17:31:12 +0200 Subject: [PATCH] Implementing SCP --- lib/cr_vmware_tpl/cobbler.py | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index afc9232..2713faa 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -173,6 +173,45 @@ class Cobbler(BaseHandler): LOG.debug(_("Completed SSH process:") + "\n{}".format(proc)) return proc + # ------------------------------------------------------------------------- + def scp_to(self, local_file, remote_file): + + ssh = None + proc = 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()) + + 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) + + sftp = ssh.open_sftp() + + if self.verbose > 1: + LOG.debug(_("SCP of {local!r} to {host}@{remote} ...").format( + local=str(local_file), host=self.host, remote=str(remote_file))) + sftp.put(str(local_file), str(remote_file)) + + finally: + sftp = None + if ssh: + if self.verbose > 2: + LOG.debug(_("Closing SSH connection.")) + ssh.close() + # ------------------------------------------------------------------------- def get_cobbler_version(self): """Trying to evaluate the version of Cobbler on the cobbler host.""" @@ -254,6 +293,8 @@ class Cobbler(BaseHandler): msg = _("Kickstart script {!r} has to be copied.").format(str(local_ks)) LOG.warn(msg) + self.scp_to(local_ks, remote_ks) + # ------------------------------------------------------------------------- def ensure_profile(self): """Ensure the existence and the correctnes of the given profile.""" -- 2.39.5