Adds clean service shutdown on Windows

This commit is contained in:
Alessandro Pilotti
2013-03-30 18:41:11 +02:00
parent 581928a5ee
commit 77aa4922c4
3 changed files with 24 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ class InitManager(object):
if self._is_already_configured(osutils):
LOG.info('Host already configured, skipping configuration')
osutils.terminate()
return
plugins = plugins_factory.PluginFactory().load_plugins()
@@ -68,3 +69,5 @@ class InitManager(object):
osutils.reboot()
except Exception, ex:
LOG.error('reboot failed with error \'%s\'' % ex)
osutils.terminate()

View File

@@ -66,3 +66,6 @@ class BaseOSUtils(object):
def wait_for_boot_completion(self):
pass
def terminate(self):
pass

View File

@@ -62,6 +62,7 @@ class WindowsUtils(base.BaseOSUtils):
ERROR_INVALID_MEMBER = 1388
_config_key = 'SOFTWARE\\Cloudbase Solutions\\Cloudbase-Init\\'
_service_name = 'cloudbase-init'
def _enable_shutdown_privilege(self):
process = win32process.GetCurrentProcess()
@@ -299,3 +300,20 @@ class WindowsUtils(base.BaseOSUtils):
'skipping sysprep completion check.')
else:
raise ex
def _stop_service(self, service_name):
LOG.debug('Stopping service %s' % service_name)
conn = wmi.WMI(moniker='//./root/cimv2')
service = conn.Win32_Service(Name=service_name)[0]
(ret_val,) = service.StopService()
if ret_val != 0:
raise Exception('Stopping service %(service_name)s failed with '
'return value: %(ret_val)d' % locals())
def terminate(self):
# Wait for the service to start. Polling the service "Started" property
# is not enough
time.sleep(3)
self._stop_service(self._service_name)