diff --git a/README.md b/README.md index 8d34eef..bda6279 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,6 @@ all units is possible to configure via the relevant config options. # Deploy -Note: The charm currently depends on this to-be-merged PR https://github.com/canonical/operator/pull/109 -for working with network spaces. - ```bash juju deploy --resource nats= ``` diff --git a/mod/operator b/mod/operator index 3a73427..04712d9 160000 --- a/mod/operator +++ b/mod/operator @@ -1 +1 @@ -Subproject commit 3a73427dee96f49acfe25880924528a3e57834cc +Subproject commit 04712d98b1fb662f0b34ae840753abacd6073b2e diff --git a/src/charm.py b/src/charm.py index 883e859..96f3d53 100755 --- a/src/charm.py +++ b/src/charm.py @@ -4,6 +4,7 @@ import string import random import sys +import logging sys.path.append('lib') # noqa from ops.charm import CharmBase, CharmEvents @@ -23,6 +24,9 @@ from cryptography.x509 import load_pem_x509_certificate +logger = logging.getLogger(__name__) + + class NatsStartedEvent(EventBase): pass @@ -79,6 +83,7 @@ def on_install(self, event): if nats_res is not None and Path(nats_res).stat().st_size: nats_cmd = cmd + ['--dangerous', nats_res] subprocess.check_call(nats_cmd) + subprocess.check_call(['snap', 'stop', 'nats', '--disable']) self.SERVER_PATH.mkdir(exist_ok=True, mode=0o0700) def handle_tls_config(self): @@ -112,6 +117,7 @@ def handle_tls_config(self): self.client.set_tls_ca(tls_ca_cert) def reconfigure_nats(self): + logger.info('Reconfiguring NATS') self.handle_tls_config() ctxt = { 'client_port': self.model.config['client-port'], @@ -140,10 +146,12 @@ def reconfigure_nats(self): content_hash = hash(rendered_content) old_hash = self.state.nats_config_hash if old_hash != content_hash: + logging.info(f'Config has changed - re-rendering a template to {self.NATS_SERVER_CONFIG_PATH}') + logger.info('') self.state.rendered_content_hash = content_hash self.NATS_SERVER_CONFIG_PATH.write_text(rendered_content) if self.state.is_started: - subprocess.check_call(['systemctl', 'restart', f'{self.NATS_SERVICE}']) + subprocess.check_call(['systemctl', 'restart', self.NATS_SERVICE]) self.client.expose_nats() def get_auth_token(self, length=None): @@ -155,10 +163,7 @@ def get_auth_token(self, length=None): return ''.join([rng.choice(alphanumeric_chars) for _ in range(length)]) def on_start(self, event): - if not self.cluster.is_joined and not self.model.config['listen-on-all-addresses']: - event.defer() - return - subprocess.check_call(['systemctl', 'start', f'{self.NATS_SERVICE}']) + subprocess.check_call(['snap', 'start', 'nats', '--enable']) self.state.is_started = True self.on.nats_started.emit() self.model.unit.status = ActiveStatus() @@ -170,9 +175,6 @@ def on_client_relation_joined(self, event): self.reconfigure_nats() def on_config_changed(self, event): - if not self.cluster.is_joined and not self.model.config['listen-on-all-addresses']: - event.defer() - return self.reconfigure_nats() def on_upgrade_charm(self, event): diff --git a/src/interfaces.py b/src/interfaces.py index b01bf76..cc56673 100644 --- a/src/interfaces.py +++ b/src/interfaces.py @@ -34,7 +34,7 @@ def peer_addresses(self): @property def listen_address(self): if self._listen_address is None: - self._listen_address = self.model.get_binding(self.relation).network.bind_address + self._listen_address = self.model.get_binding(self._relation_name).network.bind_address return self._listen_address @@ -55,17 +55,17 @@ def __init__(self, charm, relation_name, listen_on_all_addresses, client_port): @property def listen_address(self): if self._listen_address is None: - addresses = [] + addresses = set() for relation in self.model.relations[self._relation_name]: address = self.model.get_binding(relation).network.bind_address - if address not in addresses: - addresses.append(address) + addresses.add(address) if len(addresses) > 1: raise Exception('Multiple potential listen addresses detected: NATS does not support that') elif addresses == 1: - self._listen_address = addresses[0] + self._listen_address = addresses.pop() else: - self._listen_address = ipaddress.ip_address('127.0.0.1') + # Default to network information associated with an endpoint binding itself in absence of relations. + self._listen_address = self.model.get_binding(self._relation_name).network.bind_address return self._listen_address def set_tls_ca(self, tls_ca):