Added check on reboot
This commit is contained in:
		@@ -28,8 +28,21 @@ LOG = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class InitManager(object):
 | 
			
		||||
    _config_done_key = 'config_done'
 | 
			
		||||
 | 
			
		||||
    def _is_already_configured(self, osutils):
 | 
			
		||||
        return osutils.get_config_value(self._config_done_key) == 1
 | 
			
		||||
 | 
			
		||||
    def _mark_as_configured(self, osutils):
 | 
			
		||||
        osutils.set_config_value(self._config_done_key, 1)
 | 
			
		||||
        
 | 
			
		||||
    def configure_host(self):
 | 
			
		||||
        osutils = OSUtilsFactory().get_os_utils()
 | 
			
		||||
        
 | 
			
		||||
        if self._is_already_configured(osutils):
 | 
			
		||||
            LOG.info('Host already configured, skipping configuration')
 | 
			
		||||
            return
 | 
			
		||||
        
 | 
			
		||||
        plugins = PluginFactory().load_plugins()
 | 
			
		||||
        service = MetadataServiceFactory().get_metadata_service()
 | 
			
		||||
        LOG.info('Metadata service loaded: \'%s\'' %
 | 
			
		||||
@@ -50,6 +63,8 @@ class InitManager(object):
 | 
			
		||||
        finally:
 | 
			
		||||
            service.cleanup()
 | 
			
		||||
 | 
			
		||||
        self._mark_as_configured(osutils)
 | 
			
		||||
            
 | 
			
		||||
        if reboot_required:
 | 
			
		||||
            try:
 | 
			
		||||
                osutils.reboot()
 | 
			
		||||
 
 | 
			
		||||
@@ -55,3 +55,10 @@ class BaseOSUtils(object):
 | 
			
		||||
    def set_static_network_config(self, adapter_name, address, netmask,
 | 
			
		||||
        broadcast, gateway, dnsdomain, dnsnameservers):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def set_config_value(self, name, value):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def get_config_value(self, name):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -58,6 +58,8 @@ class WindowsUtils(BaseOSUtils):
 | 
			
		||||
    ERROR_MEMBER_IN_ALIAS = 1378
 | 
			
		||||
    ERROR_INVALID_MEMBER = 1388
 | 
			
		||||
 | 
			
		||||
    _config_key = 'SOFTWARE\\Cloudbase Solutions\\Cloudbase-Init\\'
 | 
			
		||||
 | 
			
		||||
    def reboot(self):
 | 
			
		||||
        conn = wmi.WMI(moniker='//./root/cimv2')
 | 
			
		||||
        conn.Win32_OperatingSystem()[0].Reboot()
 | 
			
		||||
@@ -244,3 +246,21 @@ class WindowsUtils(BaseOSUtils):
 | 
			
		||||
        reboot_required = reboot_required or ret_val == 1
 | 
			
		||||
 | 
			
		||||
        return reboot_required
 | 
			
		||||
 | 
			
		||||
    def set_config_value(self, name, value):
 | 
			
		||||
        with _winreg.CreateKey(_winreg.HKEY_LOCAL_MACHINE,
 | 
			
		||||
            self._config_key) as key:
 | 
			
		||||
            if type(value) == int:
 | 
			
		||||
                regtype = _winreg.REG_DWORD
 | 
			
		||||
            else:
 | 
			
		||||
                regtype = _winreg.REG_SZ
 | 
			
		||||
            _winreg.SetValueEx(key, name, 0, regtype, value)
 | 
			
		||||
 | 
			
		||||
    def get_config_value(self, name):
 | 
			
		||||
        try:
 | 
			
		||||
            with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
 | 
			
		||||
                self._config_key) as key:
 | 
			
		||||
                (value, regtype) = _winreg.QueryValueEx(key, name)
 | 
			
		||||
                return value
 | 
			
		||||
        except WindowsError:
 | 
			
		||||
            return None
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user