def _applyConfiguration(self, configuration): """Configure the proxy server. :param configuration: The configuration object obtained from `_getConfiguration`. """ service = service_monitor.getServiceByName(self.service_name) if configuration.is_rack and not configuration.is_region: if not configuration.enabled: # Proxy should be off and remain off. service.off("not enabled") return service_monitor.ensureService(self.service_name) # Proxy enabled; configure it. d = deferToThread(self._configure, configuration) # Ensure that the service is on. service.on() if snap.running_in_snap(): # XXX: andreserl 2016-05-09 bug=1687620. When running in a # snap, supervisord tracks services. It does not support # reloading. Instead, we need to restart the service. d.addCallback( callOut, service_monitor.restartService, self.service_name ) else: d.addCallback( callOut, service_monitor.reloadService, self.service_name ) return d else: # Proxy is managed by the region. service.any("managed by the region")
def test_sets_dns_rack_service_to_any_when_is_region(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, "debug") self.useFixture(MAASRootFixture()) rpc_service, _ = yield prepareRegion(self, is_region=True) service, dns = self.make_RackDNS_ExternalService(rpc_service, reactor) self.patch_autospec(dns, "_configure") # No-op configuration. # There is no most recently applied configuration. self.assertThat(dns._configuration, Is(None)) with TwistedLoggerFixture() as logger: yield service.startService() self.addCleanup((yield service.stopService)) yield service._orig_tryUpdate() # Ensure that the service was set to any. service = service_monitor.getServiceByName("dns_rack") self.assertEqual( (SERVICE_STATE.ANY, "managed by the region"), service.getExpectedState(), ) # The most recently applied configuration is set, though it was not # actually "applied" because this host was configured as a region+rack # controller, and the rack should not attempt to manage the DNS server # on a region+rack. self.assertThat( dns._configuration, IsInstance(external._DNSConfiguration) ) # The configuration was not applied. self.assertThat(dns._configure, MockNotCalled()) # Nothing was logged; there's no need for lots of chatter. self.assertThat(logger.output, Equals(""))
def _buildServices(self, services): """Build the list of services to be sent over RPC.""" msg_services = list(self.ALWAYS_RUNNING_SERVICES) for name, state in services.items(): service = service_monitor.getServiceByName(name) status, status_info = yield state.getStatusInfo(service) msg_services.append({ "name": name, "status": status, "status_info": status_info }) return msg_services
def _applyConfiguration(self, configuration): """Configure the external server. :param configuration: The configuration object. """ service = service_monitor.getServiceByName(self.service_name) if configuration.is_rack and not configuration.is_region: # Service is managed by the rack controller, so the service # should be on. service.on() return maybeDeferred(self._configure, configuration) else: # Service should not be managed by the rack controller. The region # controller manages this service. service.any("managed by the region")
def _applyConfiguration(self, configuration): """Configure the proxy server. :param configuration: The configuration object obtained from `_getConfiguration`. """ service = service_monitor.getServiceByName(self.service_name) if configuration.is_rack and not configuration.is_region: service.on() d = deferToThread(self._configure, configuration) d.addCallback(callOut, service_monitor.restartService, self.service_name) return d else: # Proxy is managed by the region. service.any("managed by the region")
def configure( server, failover_peers, shared_networks, hosts, interfaces, global_dhcp_snippets=None, ): """Configure the DHCPv6/DHCPv4 server, and restart it as appropriate. This method is not safe to call concurrently. The clusterserver ensures that this method is not called concurrently. :param server: A `DHCPServer` instance. :param failover_peers: List of dicts with failover parameters for each subnet where HA is enabled. :param shared_networks: List of dicts with shared network parameters that contain a list of subnets when the DHCP should server shared. If no shared network are defined, the DHCP server will be stopped. :param hosts: List of dicts with host parameters that contain a list of hosts the DHCP should statically. :param interfaces: List of interfaces that DHCP should use. :param global_dhcp_snippets: List of all global DHCP snippets """ stopping = len(shared_networks) == 0 if global_dhcp_snippets is None: global_dhcp_snippets = [] if stopping: log.debug( "Deleting configuration and stopping the {name} service.", name=server.descriptive_name, ) # Remove the config so that the even an administrator cannot turn it on # accidently when it should be off. yield deferToThread(_delete_config, server) # Ensure that the service is off and is staying off. service = service_monitor.getServiceByName(server.dhcp_service) service.off() yield _catch_service_error(server, "stop", service_monitor.ensureService, server.dhcp_service) _current_server_state[server.dhcp_service] = None else: # Get the new state for the DHCP server. new_state = DHCPState( server.omapi_key, failover_peers, shared_networks, hosts, interfaces, global_dhcp_snippets, ) # Always write the config, that way its always up-to-date. Even if # we are not going to restart the services. This makes sure that even # the comments in the file are updated. log.debug( "Writing updated DHCP configuration for {name} service.", name=server.descriptive_name, ) yield deferToThread(_write_config, server, new_state) # Service should always be on if shared_networks exists. service = service_monitor.getServiceByName(server.dhcp_service) service.on() # Perform the required action based on the state change. current_state = _current_server_state.get(server.dhcp_service, None) if current_state is None: log.debug( "Unknown previous state; restarting {name} service.", name=server.descriptive_name, ) yield _catch_service_error( server, "restart", service_monitor.restartService, server.dhcp_service, ) elif new_state.requires_restart(current_state): log.debug( "Restarting {name} service; configuration change requires " "full restart.", name=server.descriptive_name, ) yield _catch_service_error( server, "restart", service_monitor.restartService, server.dhcp_service, ) else: # No restart required update the host mappings if needed. remove, add, modify = new_state.host_diff(current_state) if len(remove) + len(add) + len(modify) == 0: # Nothing has changed, do nothing but make sure its running. log.debug( "Doing nothing; {name} service configuration has not " "changed.", name=server.descriptive_name, ) yield _catch_service_error( server, "start", service_monitor.ensureService, server.dhcp_service, ) else: log.debug( "Ensuring {name} service is running before updating " "using the OMAPI.", name=server.descriptive_name, ) # Check the state of the service. Only if the services was on # should the host maps be updated over the OMAPI. before_state = yield service_monitor.getServiceState( server.dhcp_service, now=True) yield _catch_service_error( server, "start", service_monitor.ensureService, server.dhcp_service, ) if before_state.active_state == SERVICE_STATE.ON: # Was already running, so update host maps over OMAPI # instead of performing a full restart. log.debug( "Writing to OMAPI for {name} service:\n" "\tremove: {remove()}\n" "\tadd: {add()}\n" "\tmodify: {modify()}\n", name=server.descriptive_name, remove=_debug_hostmap_msg_remove(remove), add=_debug_hostmap_msg(add), modify=_debug_hostmap_msg(modify), ) try: yield deferToThread(_update_hosts, server, remove, add, modify) except Exception: # Error updating the host maps over the OMAPI. # Restart the DHCP service so that the host maps # are in-sync with what MAAS expects. maaslog.warning( "Failed to update all host maps. Restarting %s " "service to ensure host maps are in-sync." % (server.descriptive_name)) yield _catch_service_error( server, "restart", service_monitor.restartService, server.dhcp_service, ) else: log.debug( "Usage of OMAPI skipped; {name} service was started " "with new configuration.", name=server.descriptive_name, ) # Update the current state to the new state. _current_server_state[server.dhcp_service] = new_state