]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Implementing SCP
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 28 May 2020 15:31:12 +0000 (17:31 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 28 May 2020 15:31:12 +0000 (17:31 +0200)
lib/cr_vmware_tpl/cobbler.py

index afc9232d5ae99ff21581dbc19f59e924938beb7d..2713faa9ec5048fd5300fa172b1c2b9be27fba66 100644 (file)
@@ -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."""