def onConfigUpdate(self, config): """Parses the server list and changes the state accordingly.""" delServers = self.servers.copy() # Shallow copy initList = [] for hostName, hostConfig in config.items(): if hostName in self.servers: # Existing server. merge server = delServers.pop(hostName) server.merge(hostConfig) data = { 'status': (server.enabled and "enabled" or "disabled"), 'host': hostName, 'weight': server.weight } log.info( "Merged {status} server {host}, weight {weight}".format( **data), system=self.lvsservice.name) else: # New server server = Server.buildServer(hostName, hostConfig, self.lvsservice) data = { 'status': (server.enabled and "enabled" or "disabled"), 'host': hostName, 'weight': server.weight } # Initialize with LVS service specific configuration self.lvsservice.initServer(server) self.servers[hostName] = server initList.append(server.initialize(self)) log.info("New {status} server {host}, weight {weight}".format( **data), system=self.lvsservice.name) # Remove old servers for hostName, server in delServers.iteritems(): log.info( "{} Removing server {} (no longer found in new configuration)". format(self, hostName)) server.destroy() del self.servers[hostName] # Calculate up status for previously existing, modified servers self.refreshModifiedServers() # Wait for all new servers to finish initializing self.serverInitDeferredList = defer.DeferredList(initList).addCallback( self._serverInitDone) # Update metrics self._updateServerMetrics() self._updatePooledDownMetrics()
def onConfigUpdate(self, config): """Parses the server list and changes the state accordingly.""" delServers = self.servers.copy() # Shallow copy initList = [] for hostName, hostConfig in config.items(): if hostName in self.servers: # Existing server. merge server = delServers.pop(hostName) server.merge(hostConfig) data = {'status': (server.enabled and "enabled" or "disabled"), 'host': hostName, 'weight': server.weight} log.info( "Merged {status} server {host}, weight {weight}".format(**data), system=self.lvsservice.name ) else: # New server server = Server.buildServer(hostName, hostConfig, self.lvsservice) data = {'status': (server.enabled and "enabled" or "disabled"), 'host': hostName, 'weight': server.weight} # Initialize with LVS service specific configuration self.lvsservice.initServer(server) self.servers[hostName] = server initList.append(server.initialize(self)) log.info( "New {status} server {host}, weight {weight}".format(**data), system=self.lvsservice.name ) # Remove old servers for hostName, server in delServers.iteritems(): log.info("{} Removing server {} (no longer found in new configuration)".format(self, hostName)) server.destroy() del self.servers[hostName] # Calculate up status for previously existing, modified servers self.refreshModifiedServers() # Wait for all new servers to finish initializing self.serverInitDeferredList = defer.DeferredList(initList).addCallback(self._serverInitDone)
def onConfigUpdate(self, config): """Parses the server list and changes the state accordingly.""" delServers = self.servers.copy() # Shallow copy initList = [] # Let's keep pybal logging not too chatty by summarizing # the number of added servers on new configurations (pybal start-up) new_config = not self.servers if new_config: lvl = logging.DEBUG else: lvl = logging.INFO for hostName, hostConfig in config.items(): if hostName in self.servers: # Existing server. merge server = delServers.pop(hostName) server.merge(hostConfig) data = {'status': (server.enabled and "enabled" or "disabled"), 'host': hostName, 'weight': server.weight} log.info( "Merged {status} server {host}, weight {weight}".format(**data), system=self.lvsservice.name ) else: # New server server = pybal.server.Server.buildServer(hostName, hostConfig, self.lvsservice) data = {'status': (server.enabled and "enabled" or "disabled"), 'host': hostName, 'weight': server.weight} # Initialize with LVS service specific configuration self.lvsservice.initServer(server) self.servers[hostName] = server initList.append(server.initialize(self)) util._log( "New {status} server {host}, weight {weight}".format(**data), lvl, system=self.lvsservice.name ) if new_config: enabled_servers = len([server for server in self.servers.itervalues() if server.enabled is True]) disabled_servers = len(self.servers) - enabled_servers util._log("Added {total} server(s): {enabled} enabled server(s) and {disabled} disabled server(s)".format( total=len(self.servers), enabled=enabled_servers, disabled=disabled_servers), logging.INFO, system=self.lvsservice.name ) # Remove old servers for hostName, server in delServers.iteritems(): log.info("{} Removing server {} (no longer found in new configuration)".format(self, hostName), system=self.lvsservice.name) server.destroy() del self.servers[hostName] # Calculate up status for previously existing, modified servers self.refreshModifiedServers() # Wait for all new servers to finish initializing self.serverInitDeferredList = defer.DeferredList(initList).addCallback(self._serverInitDone) # Update metrics self._updateServerMetrics() self._updatePooledDownMetrics()
def main(): from ConfigParser import SafeConfigParser # Read the configuration file configFile = '/etc/pybal/pybal.conf' config = SafeConfigParser({'port':'0'}) config.read(configFile) services, cliconfig = {}, {} # Parse the command line parseCommandLine(cliconfig) try: # Install signal handlers installSignalHandlers() for section in config.sections(): cfgtuple = {} if section != 'global': ips = config.get(section, 'ip').split(',') num = 0 for ip in ips: cfgtuple[num] = ( config.get(section, 'protocol'), ip, config.getint(section, 'port'), config.get(section, 'scheduler')) num += 1 # Read the custom configuration options of the LVS section configdict = util.ConfigDict(config.items(section)) # Override with command line options configdict.update(cliconfig) if section != 'global': num = 0 for ip in ips: servicename = section if num: servicename += '_%u' % num services[servicename] = ipvs.LVSService(servicename, cfgtuple[num], configuration=configdict) crd = Coordinator(services[servicename], configUrl=config.get(section, 'config')) log.info("Created LVS service '{}'".format(servicename)) instrumentation.PoolsRoot.addPool(crd.lvsservice.name, crd) num += 1 # Set up BGP try: configdict = util.ConfigDict(config.items('global')) except Exception: configdict = util.ConfigDict() configdict.update(cliconfig) # Set the logging level if configdict.get('debug', False): util.PyBalLogObserver.level = logging.DEBUG else: util.PyBalLogObserver.level = logging.INFO bgpannouncement = BGPFailover(configdict) # Run the web server for instrumentation if configdict.getboolean('instrumentation', False): from twisted.web.server import Site port = configdict.getint('instrumentation_port', 9090) factory = Site(instrumentation.ServerRoot()) reactor.listenTCP(port, factory) reactor.run() finally: for service in services: Loopback.delIP(services[service].ip) log.info("Exiting...")
def onConfigUpdate(self, config): """ Takes a dictionary of server hostnames to configuration dicts as the complete set of new servers, and updates the state of the coordinator accordingly. """ delServers = self.servers.copy() # Shallow copy initList = [] # Let's keep pybal logging not too chatty by summarizing # the number of added servers on new configurations (pybal start-up) new_config = not self.servers if new_config: lvl = logging.DEBUG else: lvl = logging.INFO for hostName, hostConfig in config.items(): if hostName in self.servers: # Existing server. merge server = delServers.pop(hostName) server.merge(hostConfig) # Calculate up status for the previously existing server self.refreshPreexistingServer(server) data = {'status': (server.enabled and "enabled" or "disabled"), 'host': hostName, 'weight': server.weight} log.info( "Merged {status} server {host}, weight {weight}".format(**data), system=self.lvsservice.name ) else: # New server server = pybal.server.Server.buildServer(hostName, hostConfig, self.lvsservice) data = {'status': (server.enabled and "enabled" or "disabled"), 'host': hostName, 'weight': server.weight} # Initialize with LVS service specific configuration self.lvsservice.initServer(server) self.servers[hostName] = server initList.append(server.initialize(self)) util._log( "New {status} server {host}, weight {weight}".format(**data), lvl, system=self.lvsservice.name ) if new_config: enabled_servers = sum(1 for server in self.servers.itervalues() if server.enabled) disabled_servers = len(self.servers) - enabled_servers util._log("Added {total} server(s): {enabled} enabled server(s) and {disabled} disabled server(s)".format( total=len(self.servers), enabled=enabled_servers, disabled=disabled_servers), logging.INFO, system=self.lvsservice.name ) # Remove old servers for hostName, server in delServers.iteritems(): log.info("{} Removing server {} (no longer found in new configuration)".format(self, hostName), system=self.lvsservice.name) server.destroy() del self.servers[hostName] # Wait for all new servers to finish initializing self.serverInitDeferredList = defer.DeferredList(initList).addCallback(self._serverInitDone) # Update metrics self._updateServerMetrics() self._updatePooledDownMetrics() return self.serverInitDeferredList
def main(): from ConfigParser import SafeConfigParser # Read the configuration file configFile = '/etc/pybal/pybal.conf' config = SafeConfigParser() config.read(configFile) services, cliconfig = {}, {} # Parse the command line parseCommandLine(cliconfig) try: # Install signal handlers installSignalHandlers() for section in config.sections(): if section != 'global': cfgtuple = (config.get(section, 'protocol'), config.get(section, 'ip'), config.getint(section, 'port'), config.get(section, 'scheduler')) # Read the custom configuration options of the LVS section configdict = util.ConfigDict(config.items(section)) # Override with command line options configdict.update(cliconfig) if section != 'global': services[section] = ipvs.LVSService(section, cfgtuple, configuration=configdict) crd = Coordinator(services[section], configUrl=config.get(section, 'config')) log.info("Created LVS service '{}'".format(section)) instrumentation.PoolsRoot.addPool(crd.lvsservice.name, crd) # Set up BGP try: configdict = util.ConfigDict(config.items('global')) except Exception: configdict = util.ConfigDict() configdict.update(cliconfig) # Set the logging level if configdict.get('debug', False): util.PyBalLogObserver.level = logging.DEBUG else: util.PyBalLogObserver.level = logging.INFO bgpannouncement = BGPFailover(configdict) # Run the web server for instrumentation if configdict.getboolean('instrumentation', False): from twisted.web.server import Site port = configdict.getint('instrumentation_port', 9090) factory = Site(instrumentation.ServerRoot()) reactor.listenTCP(port, factory) reactor.run() finally: log.info("Exiting...")