Commit 2d812a34 authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

windows: Uninstall the WinEXE service just once

Do not install and uninstall the winexe service in every single
execution of vm.rexec(). Only uninstall the service after executing the
last command which is either the sysprep or the shutdown command.
parent 9439eee2
......@@ -30,6 +30,7 @@ import tempfile
import re
import os
import uuid
import time
# For more info see:
......@@ -339,7 +340,7 @@ class Windows(OSBase):
self.vm.rexec(r'C:\Windows\system32\sysprep\sysprep '
r'/quiet /generalize /oobe /shutdown')
r'/quiet /generalize /oobe /shutdown', uninstall=True)
self.sysprepped = True
@sysprep('Converting the image into a KMS client', enabled=False)
......@@ -422,7 +423,7 @@ class Windows(OSBase):
r'DEL /Q %SCRIPT%"')
stdout, stderr, rc = self.vm.rexec(cmd, False)
stdout, stderr, rc = self.vm.rexec(cmd, fatal=False)
if rc != 0:
FatalError("Shrinking failed. Please make sure the media is "
......@@ -430,7 +431,7 @@ class Windows(OSBase):
"`Defrag.exe /U /X /W'")
for line in stdout.splitlines():
if line.find('shrunk') >= 0:
self.out.output(" %s" % line)
def do_sysprep(self):
"""Prepare system for image creation."""
......@@ -496,6 +497,11 @@ class Windows(OSBase):
booted = True
# Since the password is reset when logging in, sleep a little
# bit before checking the connectivity, to avoid race
# conditions
self.out.output("Checking connectivity to the VM ...", False)
# self.out.success('done')
......@@ -565,7 +571,7 @@ class Windows(OSBase):
def _shutdown(self):
"""Shuts down the windows VM"""
self.vm.rexec(r'shutdown /s /t 5')
self.vm.rexec(r'shutdown /s /t 5', uninstall=True)
def _disable_autologon(self):
"""Disable automatic logon on the windows image"""
......@@ -205,26 +205,51 @@ class VM(object):
return (stdout, stderr, self.process.poll())
def rexec(self, command, fatal=True, debug=False):
"""Remote execute a command on the windows VM"""
def rexec(self, command, **kwargs):
"""Remote execute a command on the windows VM
The following optional flags are allowed:
* fatal: If True, a FatalError is thrown if the command fails
* debug: If True, WinEXE is executed in the highest debug level
* uninstall: If True, the winexesvc.exe service will be uninstalled
after the execution of the command.
fatal = kwargs['fatal'] if 'fatal' in kwargs else True
debug = kwargs['debug'] if 'debug' in kwargs else False
uninstall = kwargs['uninstall'] if 'uninstall' in kwargs else False
user = self.params['admin'].value
winexe = WinEXE(user, 'localhost', password=self.password)
winexe.runas(user, self.password).uninstall().no_pass()
winexe.runas(user, self.password).no_pass()
if debug:
if uninstall:
(stdout, stderr, rc) =
except WinexeTimeout:
FatalError("Command: `%s' timeout out." % command)
if rc != 0 and fatal:
reason = stderr if len(stderr) else stdout
log = tempfile.NamedTemporaryFile(delete=False)
log.file.write("STDOUT:\n%s\n" % stdout)
log.file.write("STDERR:\n%s\n" % stderr)
fname =
# self.out.output("Command: `%s' failed (rc=%d). Reason: %s" %
# (command, rc, reason))
raise FatalError("Command: `%s' failed (rc=%d). Reason: %s" %
(command, rc, reason))
raise FatalError("Command: `%s' failed (rc=%d). See: %s" %
(command, rc, fname))
return (stdout, stderr, rc)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment