def apply_preset(self, preset): conf = Configuration(self._config_format) conf.read(self._config_path) self._before_apply_preset() ver = self._software_version for opt in self._manifest: path = opt.name if not opt.section else '%s/%s' % (opt.section, opt.name) try: value = conf.get(path) except NoPathError: value = '' if opt.name in preset.settings: new_value = preset.settings[opt.name] # Skip unsupported if ver and opt.supported_from and opt.supported_from > ver: self._logger.debug("Skipping option '%s' supported from %s; installed %s" % (opt.name, opt.supported_from, ver)) continue if not opt.default_value: self._logger.debug("Option '%s' has no default value" % opt.name) pass elif new_value == opt.default_value: if value: self._logger.debug("Option '%s' equal to default. Removing." % opt.name) conf.remove(path) self._after_remove_option(opt) continue if self.definitions and new_value in self.definitions: manifest = Configuration('ini') if os.path.exists(self._manifest_path): manifest.read(self._manifest_path) try: if manifest.get('%s/type' % opt.name) == 'boolean': new_value = self.definitions[new_value] except NoPathError, e: pass self._logger.debug("Check that '%s' value changed:'%s'='%s'"%(opt.name, value, new_value)) if new_value == value: self._logger.debug("Skip option '%s'. Not changed" % opt.name) pass else: self._logger.debug("Set option '%s' = '%s'" % (opt.name, new_value)) self._logger.debug('Set path %s = %s', path, new_value) conf.set(path, new_value, force=True) self._after_set_option(opt, new_value) else: if value: self._logger.debug("Removing option '%s'. Not found in preset" % opt.name) conf.remove(path) self._after_remove_option(opt)
class MysqlProxyHandler(ServiceCtlHandler): def __init__(self): self._logger = logging.getLogger(__name__) self.service = initdv2.lookup(BEHAVIOUR) self._service_name = BEHAVIOUR bus.on(init=self.on_init) def accept(self, message, queue, behaviour=None, platform=None, os=None, dist=None): return message.behaviour and is_mysql_role( message.behaviour) and message.name in ( Messages.HOST_UP, Messages.HOST_DOWN, NEW_MASTER_UP, DbMsrMessages.DBMSR_NEW_MASTER_UP) def on_init(self): bus.on(start=self.on_start, before_host_up=self.on_before_host_up, reload=self.on_reload) def on_reload(self): self._reload_backends() def on_start(self): cnf = bus.cnf if cnf.state == config.ScalarizrState.RUNNING: self._reload_backends() def on_before_host_up(self, msg): self._reload_backends() def _reload_backends(self): self._logger.info('Updating mysql-proxy backends list') self.config = Configuration('mysql') if os.path.exists(CONFIG_FILE_PATH): self.config.read(CONFIG_FILE_PATH) self.config.remove('./mysql-proxy/proxy-backend-addresses') self.config.remove( './mysql-proxy/proxy-read-only-backend-addresses') try: self.config.get('./mysql-proxy') except NoPathError: self.config.add('./mysql-proxy') queryenv = bus.queryenv_service roles = queryenv.list_roles() master = None slaves = [] for role in roles: if not is_mysql_role(role.behaviour): continue for host in role.hosts: ip = host.internal_ip or host.external_ip if host.replication_master: master = ip else: slaves.append(ip) if master: self._logger.debug( 'Adding mysql master %s to mysql-proxy defaults file', master) self.config.add('./mysql-proxy/proxy-backend-addresses', '%s:3306' % master) if slaves: self._logger.debug( 'Adding mysql slaves to mysql-proxy defaults file: %s', ', '.join(slaves)) for slave in slaves: self.config.add( './mysql-proxy/proxy-read-only-backend-addresses', '%s:3306' % slave) self.config.set('./mysql-proxy/pid-file', PID_FILE, force=True) self.config.set('./mysql-proxy/daemon', 'true', force=True) self.config.set('./mysql-proxy/log-file', LOG_FILE, force=True) if self.service.version > (0, 8, 0): self.config.set('./mysql-proxy/plugins', 'proxy', force=True) self._logger.debug('Saving new mysql-proxy defaults file') self.config.write(CONFIG_FILE_PATH) os.chmod(CONFIG_FILE_PATH, 0660) self.service.restart() def on_HostUp(self, message): self._reload_backends() on_DbMsr_NewMasterUp = on_Mysql_NewMasterUp = on_HostDown = on_HostUp
def apply_preset(self, preset): conf = Configuration(self._config_format) conf.read(self._config_path) self._before_apply_preset() ver = self._software_version for opt in self._manifest: path = opt.name if not opt.section else '%s/%s' % (opt.section, opt.name) try: value = conf.get(path) except NoPathError: value = '' if opt.name in preset.settings: new_value = preset.settings[opt.name] # Skip unsupported if ver and opt.supported_from and opt.supported_from > ver: self._logger.debug( "Skipping option '%s' supported from %s; installed %s" % (opt.name, opt.supported_from, ver)) continue if not opt.default_value: self._logger.debug("Option '%s' has no default value" % opt.name) pass elif new_value == opt.default_value: if value: self._logger.debug( "Option '%s' equal to default. Removing." % opt.name) conf.remove(path) self._after_remove_option(opt) continue if self.definitions and new_value in self.definitions: manifest = Configuration('ini') if os.path.exists(self._manifest_path): manifest.read(self._manifest_path) try: if manifest.get('%s/type' % opt.name) == 'boolean': new_value = self.definitions[new_value] except NoPathError, e: pass self._logger.debug("Check that '%s' value changed:'%s'='%s'" % (opt.name, value, new_value)) if new_value == value: self._logger.debug("Skip option '%s'. Not changed" % opt.name) pass else: self._logger.debug("Set option '%s' = '%s'" % (opt.name, new_value)) self._logger.debug('Set path %s = %s', path, new_value) conf.set(path, new_value, force=True) self._after_set_option(opt, new_value) else: if value: self._logger.debug( "Removing option '%s'. Not found in preset" % opt.name) conf.remove(path) self._after_remove_option(opt)
class MysqlProxyHandler(ServiceCtlHandler): def __init__(self): self._logger = logging.getLogger(__name__) self.service = initdv2.lookup(BEHAVIOUR) self._service_name = BEHAVIOUR bus.on(init=self.on_init) def accept(self, message, queue, behaviour=None, platform=None, os=None, dist=None): return message.behaviour and is_mysql_role(message.behaviour) and message.name in ( Messages.HOST_UP, Messages.HOST_DOWN, NEW_MASTER_UP, DbMsrMessages.DBMSR_NEW_MASTER_UP ) def on_init(self): bus.on( start=self.on_start, before_host_up=self.on_before_host_up, reload=self.on_reload ) def on_reload(self): self._reload_backends() def on_start(self): cnf = bus.cnf if cnf.state == config.ScalarizrState.RUNNING: self._reload_backends() def on_before_host_up(self, msg): self._reload_backends() def _reload_backends(self): self._logger.info('Updating mysql-proxy backends list') self.config = Configuration('mysql') if os.path.exists(CONFIG_FILE_PATH): self.config.read(CONFIG_FILE_PATH) self.config.remove('./mysql-proxy/proxy-backend-addresses') self.config.remove('./mysql-proxy/proxy-read-only-backend-addresses') try: self.config.get('./mysql-proxy') except NoPathError: self.config.add('./mysql-proxy') queryenv = bus.queryenv_service roles = queryenv.list_roles() master = None slaves = [] for role in roles: if not is_mysql_role(role.behaviour): continue for host in role.hosts: ip = host.internal_ip or host.external_ip if host.replication_master: master = ip else: slaves.append(ip) if master: self._logger.debug('Adding mysql master %s to mysql-proxy defaults file', master) self.config.add('./mysql-proxy/proxy-backend-addresses', '%s:3306' % master) if slaves: self._logger.debug('Adding mysql slaves to mysql-proxy defaults file: %s', ', '.join(slaves)) for slave in slaves: self.config.add('./mysql-proxy/proxy-read-only-backend-addresses', '%s:3306' % slave) self.config.set('./mysql-proxy/pid-file', PID_FILE, force=True) self.config.set('./mysql-proxy/daemon', 'true', force=True) self.config.set('./mysql-proxy/log-file', LOG_FILE, force=True) if self.service.version > (0,8,0): self.config.set('./mysql-proxy/plugins', 'proxy', force=True) self._logger.debug('Saving new mysql-proxy defaults file') self.config.write(CONFIG_FILE_PATH) os.chmod(CONFIG_FILE_PATH, 0660) self.service.restart() def on_HostUp(self, message): self._reload_backends() on_DbMsr_NewMasterUp = on_Mysql_NewMasterUp = on_HostDown = on_HostUp
class MongoDBConfig(BaseConfig): config_type = "mongodb" config_name = os.path.basename(CONFIG_PATH_DEFAULT) @classmethod def find(cls, config_dir=None): # conf_path = UBUNTU_CONFIG_PATH if disttool.is_ubuntu() else CENTOS_CONFIG_PATH return cls(os.path.join(config_dir, cls.config_name) if config_dir else CONFIG_PATH_DEFAULT) def set(self, option, value): if not self.data: self.data = Configuration(self.config_type) if os.path.exists(self.path): self.data.read(self.path) if value: self.data.set(option, value, force=True) else: self.data.remove(option) if self.autosave: self.save_data() self.data = None def set_bool_option(self, option, value): try: assert value in (None, True, False) except AssertionError: raise ValueError("%s must be a boolean (got %s instead)" % (option, type(value))) value_to_set = None if value is None else str(value).lower() self.set(option, value_to_set) def get_bool_option(self, option): value = self.get(option) try: assert not value or value == "true" or value == "false" except AssertionError: raise ValueError("%s must be true or false (got %s instead)" % (option, type(value))) return True if value == "true" else False def _get_logpath(self): return self.get("logpath") def _set_logpath(self, path): self.set("logpath", path) def _get_replSet(self): return self.get("replSet") def _set_replSet(self, name): self.set("replSet", name) def _get_port(self): return self.get_numeric_option("port") def _set_port(self, number): self.set_numeric_option("port", number) def _get_logappend(self): return self.get_bool_option("logappend") def _set_logappend(self, on=True): self.set_bool_option("logappend", on) def _get_dbpath(self): return self.get("dbpath") def _set_dbpath(self, path): self.set_path_type_option("dbpath", path) def _get_nojournal(self): return self.get_bool_option("nojournal") def _set_nojournal(self, on=False): self.set_bool_option("nojournal", on) def _get_nohttpinterface(self): return self.get_bool_option("nohttpinterface") def _set_nohttpinterface(self, on=False): self.set_bool_option("nohttpinterface", on) def _get_rest(self): return self.get_bool_option("rest") def _set_rest(self, on=False): self.set_bool_option("rest", on) def _set_shardsvr(self, value): self.set_bool_option("shardsvr", value) def _get_shardsvr(self): return self.get_bool_option("shardsvr") shardsvr = property(_get_shardsvr, _set_shardsvr) rest = property(_get_rest, _set_rest) nohttpinterface = property(_get_nohttpinterface, _set_nohttpinterface) nojournal = property(_get_nojournal, _set_nojournal) dbpath = property(_get_dbpath, _set_dbpath) logappend = property(_get_logappend, _set_logappend) port = property(_get_port, _set_port) replSet = property(_get_replSet, _set_replSet) logpath = property(_get_logpath, _set_logpath)