Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the latest framework code + fix race condition #1

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<path-to-nats-snap-file> <nats-charm-dir>
```
Expand Down
18 changes: 10 additions & 8 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import string
import random
import sys
import logging
sys.path.append('lib') # noqa

from ops.charm import CharmBase, CharmEvents
Expand All @@ -23,6 +24,9 @@
from cryptography.x509 import load_pem_x509_certificate


logger = logging.getLogger(__name__)


class NatsStartedEvent(EventBase):
pass

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions src/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand Down