diff --git a/utils/windows_configuration.py b/utils/windows_configuration.py index ae933f1f1e..943749af26 100644 --- a/utils/windows_configuration.py +++ b/utils/windows_configuration.py @@ -17,13 +17,29 @@ log = logging.getLogger(__name__) +config_attributes = ['api_key', 'tags', 'hostname', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_password'] + +def remove_registry_conf(): + try: + with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, + WINDOWS_REG_PATH, 0, _winreg.KEY_WRITE) as reg_key: + for attribute in config_attributes: + try: + _winreg.DeleteValue(reg_key, attribute) + except Exception as e: + log.error("Failed to delete value %s %s", attribute, str(e)) + # it's ok if it's not there. + pass + except Exception: + # also OK if the whole tree isn't there + pass def get_registry_conf(agentConfig): registry_conf = {} try: with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, WINDOWS_REG_PATH) as reg_key: - for attribute in ['api_key', 'tags', 'hostname']: + for attribute in config_attributes: option = _winreg.QueryValueEx(reg_key, attribute)[0] if option != '': registry_conf[attribute] = option @@ -99,5 +115,6 @@ def update_conf_file(registry_conf, config_path): os.rename(temp_config_path, config_path) except OSError as e: log.exception('Unable to save new datadog.conf') + raise else: log.debug('Successfully saved the new datadog.conf') diff --git a/win32/service.py b/win32/service.py index 609af3bf7e..6b18f0f6d3 100644 --- a/win32/service.py +++ b/win32/service.py @@ -30,7 +30,7 @@ from config import get_config, get_config_path, get_confd_path from jmxfetch import JMXFetch from utils.jmx import JMXFiles -from utils.windows_configuration import get_registry_conf, update_conf_file +from utils.windows_configuration import get_registry_conf, update_conf_file, remove_registry_conf log = logging.getLogger('service') @@ -134,7 +134,12 @@ def _update_config_file(self, config): config.update(registry_conf) if registry_conf: log.info('Updating conf file options: %s', registry_conf.keys()) - update_conf_file(registry_conf, get_config_path()) + try: + update_conf_file(registry_conf, get_config_path()) + log.info('update succeeded, deleting old values') + remove_registry_conf() + except Exception: + log.warning('Failed to update config file; registry configuration persisted') def SvcStop(self): # Stop all services.