Skip to content

Commit

Permalink
Merge branch 'main' into css-4116/backport-crossmodel-queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ale8k authored Jun 22, 2023
2 parents dac2853 + df43a22 commit 9e301ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion charms/jimm/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ provides:

requires:
db:
interface: pgsql
interface: postgresql_client
vault:
interface: vault-kv
optional: true
Expand Down
42 changes: 33 additions & 9 deletions charms/jimm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import urllib

import hvac
from charms.data_platform_libs.v0.data_interfaces import (
DatabaseRequires,
DatabaseRequiresEvent,
)
from charmhelpers.contrib.charmsupport.nrpe import NRPE
from jinja2 import Environment, FileSystemLoader
from ops.main import main
Expand All @@ -28,16 +32,14 @@

logger = logging.getLogger(__name__)

DATABASE_NAME = "jimm"

class JimmCharm(SystemdCharm):
"""Charm for the JIMM service."""

def __init__(self, *args):
super().__init__(*args)
self.framework.observe(self.on.config_changed, self._on_config_changed)
self.framework.observe(
self.on.db_relation_changed, self._on_db_relation_changed
)
self.framework.observe(self.on.install, self._on_install)
self.framework.observe(self.on.leader_elected, self._on_leader_elected)
self.framework.observe(self.on.start, self._on_start)
Expand All @@ -60,6 +62,18 @@ def __init__(self, *args):
self.on.dashboard_relation_joined,
self._on_dashboard_relation_joined,
)

self.database = DatabaseRequires(
self,
relation_name="db",
database_name=DATABASE_NAME,
)
self.framework.observe(self.database.on.database_created, self._on_database_event)
self.framework.observe(
self.database.on.endpoints_changed,
self._on_database_event,
)

self._agent_filename = "/var/snap/jimm/common/agent.json"
self._vault_secret_filename = "/var/snap/jimm/common/vault_secret.json"
self._workload_filename = "/snap/bin/jimm"
Expand Down Expand Up @@ -125,6 +139,7 @@ def _on_config_changed(self, _):
}
)


def _on_leader_elected(self, _):
"""Update the JIMM configuration that comes from unit
leadership."""
Expand All @@ -138,20 +153,29 @@ def _on_leader_elected(self, _):
self.restart()
self._on_update_status(None)

def _on_db_relation_changed(self, event):
"""Update the JIMM configuration that comes from database
relations."""

dsn = event.relation.data[event.unit].get("master")
if not dsn:
def _on_database_event(self, event: DatabaseRequiresEvent):
"""Handle database event"""

if not event.endpoints:
logger.info("received empty database host address")
event.defer()
return
args = {"dsn": "pgx:" + dsn}

# get the first endpoint from a comma separate list
host = event.endpoints.split(",", 1)[0]
# compose the db connection string
uri = f"postgresql://{event.username}:{event.password}@{host}/{DATABASE_NAME}"
logger.info("received database uri: {}".format(uri))

args = {"dsn": uri}
with open(self._env_filename("db"), "wt") as f:
f.write(self._render_template("jimm-db.env", **args))
if self._ready():
self.restart()
self._on_update_status(None)


def _on_stop(self, _):
"""Stop the JIMM service."""
self.stop()
Expand Down

0 comments on commit 9e301ba

Please sign in to comment.