def process(self): """Process the DNS and/or proxy update.""" def _onFailureRetry(failure, attr): """Retry update on failure. Doesn't mask the failure, the failure is still raised. """ if self.retryOnFailure: setattr(self, attr, True) return failure def _rbacInit(result): """Mark initialization took place.""" if result is not None: # A sync occurred. self.rbacInit = True return result def _rbacFailure(failure, delay): log.err(failure, "Failed syncing resources to RBAC.") if delay: return pause(delay) defers = [] if self.needsDNSUpdate: self.needsDNSUpdate = False d = deferToDatabase(transactional(dns_update_all_zones)) d.addCallback(self._checkSerial) d.addCallback(self._logDNSReload) # Order here matters, first needsDNSUpdate is set then pass the # failure onto `_onDNSReloadFailure` to do the correct thing # with the DNS server. d.addErrback(_onFailureRetry, "needsDNSUpdate") d.addErrback(self._onDNSReloadFailure) d.addErrback(log.err, "Failed configuring DNS.") defers.append(d) if self.needsProxyUpdate: self.needsProxyUpdate = False d = proxy_update_config(reload_proxy=True) d.addCallback(lambda _: log.msg("Successfully configured proxy.")) d.addErrback(_onFailureRetry, "needsProxyUpdate") d.addErrback(log.err, "Failed configuring proxy.") defers.append(d) if self.needsRBACUpdate: self.needsRBACUpdate = False d = deferToDatabase(self._rbacSync) d.addCallback(_rbacInit) d.addCallback(self._logRBACSync) d.addErrback(_onFailureRetry, "needsRBACUpdate") d.addErrback( _rbacFailure, self.rbacRetryOnFailureDelay if self.retryOnFailure else None, ) defers.append(d) if len(defers) == 0: # Nothing more to do. self.processing.stop() self.processingDefer = None else: return DeferredList(defers)
def test__doesnt_call_reloadService_when_reload_proxy_False(self): self.patch(settings, "PROXY_CONNECT", True) yield deferToDatabase(self.make_subnet) yield proxyconfig.proxy_update_config(reload_proxy=False) self.assertThat( self.service_monitor.reloadService, MockNotCalled())
def process(self): """Process the DNS and/or proxy update.""" defers = [] if self.needsDNSUpdate: self.needsDNSUpdate = False d = deferToDatabase(transactional(dns_update_all_zones)) d.addCallback( lambda _: log.msg( "Successfully configured DNS.")) d.addErrback( log.err, "Failed configuring DNS.") defers.append(d) if self.needsProxyUpdate: self.needsProxyUpdate = False d = proxy_update_config(reload_proxy=True) d.addCallback( lambda _: log.msg( "Successfully configured proxy.")) d.addErrback( log.err, "Failed configuring proxy.") defers.append(d) if len(defers) == 0: # Nothing more to do. self.processing.stop() self.processingDefer = None else: return DeferredList(defers)
def test__calls_reloadService(self): self.patch(settings, "PROXY_CONNECT", True) yield deferToDatabase(self.make_subnet) yield proxyconfig.proxy_update_config() self.assertThat( self.service_monitor.reloadService, MockCalledOnceWith("proxy", if_on=True))
def test__calls_restartService(self): self.patch(settings, "PROXY_CONNECT", True) self.patch(snappy, 'running_in_snap').return_value = True yield deferToDatabase(self.make_subnet) yield proxyconfig.proxy_update_config() self.assertThat(self.service_monitor.restartService, MockCalledOnceWith("proxy", if_on=True))
def test__with_prefer_v4_proxy_True(self): self.patch(settings, "PROXY_CONNECT", True) yield deferToDatabase(transactional(Config.objects.set_config), "prefer_v4_proxy", True) yield proxyconfig.proxy_update_config(reload_proxy=False) with self.proxy_path.open() as proxy_file: lines = [line.strip() for line in proxy_file.readlines()] self.assertIn("dns_v4_first on", lines)
def test__with_new_maas_proxy_port_changes_port(self): self.patch(settings, "PROXY_CONNECT", True) port = random.randint(1, 65535) yield deferToDatabase(transactional(Config.objects.set_config), "maas_proxy_port", port) yield proxyconfig.proxy_update_config(reload_proxy=False) with self.proxy_path.open() as proxy_file: lines = [line.strip() for line in proxy_file.readlines()] self.assertIn("http_port %s" % port, lines)
def test__only_enabled_subnets_are_present(self): self.patch(settings, "PROXY_CONNECT", True) disabled = yield deferToDatabase(self.make_subnet, allow_proxy=False) enabled = yield deferToDatabase(self.make_subnet) yield proxyconfig.proxy_update_config(reload_proxy=False) # enabled's cidr must be present matcher = Contains("acl localnet src %s" % enabled.cidr) self.assertThat( "%s/%s" % (self.tmpdir, config.MAAS_PROXY_CONF_NAME), FileContains(matcher=matcher)) # disabled's cidr must not be present matcher = Not(Contains("acl localnet src %s" % disabled.cidr)) self.assertThat( "%s/%s" % (self.tmpdir, config.MAAS_PROXY_CONF_NAME), FileContains(matcher=matcher))
def test__with_use_peer_proxy_without_http_proxy(self): self.patch(settings, "PROXY_CONNECT", True) yield deferToDatabase(transactional(Config.objects.set_config), "enable_http_proxy", True) yield deferToDatabase(transactional(Config.objects.set_config), "use_peer_proxy", True) yield deferToDatabase(transactional(Config.objects.set_config), "http_proxy", "") yield deferToDatabase(self.make_subnet, allow_proxy=False) yield deferToDatabase(self.make_subnet) yield proxyconfig.proxy_update_config(reload_proxy=False) with self.proxy_path.open() as proxy_file: lines = [line.strip() for line in proxy_file.readlines()] self.assertNotIn("never_direct allow all", lines) self.assertNotIn("cache_peer", lines)
def test__with_use_peer_proxy_with_http_proxy(self): self.patch(settings, "PROXY_CONNECT", True) yield deferToDatabase(transactional(Config.objects.set_config), "enable_http_proxy", True) yield deferToDatabase(transactional(Config.objects.set_config), "use_peer_proxy", True) yield deferToDatabase(transactional(Config.objects.set_config), "http_proxy", "http://example.com:8000/") yield deferToDatabase(self.make_subnet, allow_proxy=False) yield deferToDatabase(self.make_subnet) yield proxyconfig.proxy_update_config(reload_proxy=False) cache_peer_line = ( "cache_peer example.com parent 8000 0 no-query default") with self.proxy_path.open() as proxy_file: lines = [line.strip() for line in proxy_file.readlines()] self.assertIn('never_direct allow all', lines) self.assertIn(cache_peer_line, lines)