Quellcode durchsuchen

Fix windows userscript

Gabriel-Adrian Samfira vor 6 Jahren
Ursprung
Commit
0404634581
3 geänderte Dateien mit 22 neuen und 19 gelöschten Zeilen
  1. 3 7
      coriolis/osmorphing/base.py
  2. 7 8
      coriolis/osmorphing/windows.py
  3. 12 4
      coriolis/utils.py

+ 3 - 7
coriolis/osmorphing/base.py

@@ -7,10 +7,10 @@ import os
 import re
 import uuid
 
-from coriolis import exception
 from oslo_log import log as logging
 from six import with_metaclass
 
+from coriolis import exception
 from coriolis import utils
 
 
@@ -111,10 +111,8 @@ class BaseLinuxOSMorphingTools(BaseOSMorphingTools):
                 script_path,
                 user_script)
         except Exception as err:
-            LOG.exception(err)
             raise exception.CoriolisException(
-                "Failed to copy user script to target system."
-                " Error was: %s" % err)
+                "Failed to copy user script to target system.") from err
 
         try:
             utils.exec_ssh_cmd(
@@ -127,10 +125,8 @@ class BaseLinuxOSMorphingTools(BaseOSMorphingTools):
                 'sudo "%s" "%s"' % (script_path, self._os_root_dir),
                 get_pty=True)
         except Exception as err:
-            LOG.exception(err)
             raise exception.CoriolisException(
-                "Failed to run user script."
-                " Error was: %s" % err)
+                "Failed to run user script.") from err
 
     def pre_packages_install(self, package_names):
         self._copy_resolv_conf()

+ 7 - 8
coriolis/osmorphing/windows.py

@@ -233,19 +233,18 @@ class BaseWindowsMorphingTools(base.BaseOSMorphingTools):
                 script_path,
                 user_script)
         except Exception as err:
-            LOG.exception(err)
             raise exception.CoriolisException(
-                "Failed to copy user script to target system."
-                " Error was: %s" % err)
+                "Failed to copy user script to target system.") from err
 
-        cmd = '& "%(script)s" "%(os_root_dir)s"' % {
+        cmd = ('$ErrorActionPreference = "Stop"; powershell.exe '
+               '-NonInteractive -ExecutionPolicy RemoteSigned '
+               '-File "%(script)s" "%(os_root_dir)s"') % {
             "script": script_path,
             "os_root_dir": self._os_root_dir,
         }
         try:
-            self._conn.exec_ps_command(cmd)
+            out = self._conn.exec_ps_command(cmd)
+            LOG.debug("User script output: %s" % out)
         except Exception as err:
-            LOG.exception(err)
             raise exception.CoriolisException(
-                "Failed to run user script."
-                " Error was: %s" % err)
+                "Failed to run user script.") from err

+ 12 - 4
coriolis/utils.py

@@ -204,20 +204,28 @@ def write_ssh_file(ssh, remote_path, content):
     fd.close()
 
 
-@retry_on_error()
-def write_winrm_file(conn, remote_path, content):
+def write_winrm_file(conn, remote_path, content, overwrite=True):
     """This is a poor man's scp command that transfers small
     files, in chunks, over WinRM.
     """
-    conn.exec_ps_command("rm -Force -ErrorAction SilentlyContinue %s" % remote_path)
+    if conn.test_path(remote_path):
+        if overwrite:
+            conn.exec_ps_command(
+                'Remove-Item -Force "%s"' % remote_path)
+        else:
+            raise exception.CoriolisException(
+                "File %s already exists" % remote_path)
     idx = 0
     while True:
         data = content[idx:idx+2048]
         if not data:
             break
+
+        if type(data) is str:
+            data = data.encode()
         asb64 = base64.b64encode(data).decode()
         cmd = ("$ErrorActionPreference = 'Stop';"
-               "$x = [System.IO.FileStream]::new('%s', "
+               "$x = [System.IO.FileStream]::new(\"%s\", "
                "[System.IO.FileMode]::Append); $bytes = "
                "[Convert]::FromBase64String('%s'); $x.Write($bytes, "
                "0, $bytes.Length); $x.Close()") % (