Skip to content

Commit

Permalink
Merge pull request #914 from alesstimec/update-to-juju-2.9.42
Browse files Browse the repository at this point in the history
Update to Juju 2.9.42 and some compatibility changes.
  • Loading branch information
alesstimec authored Mar 21, 2023
2 parents 32097b1 + b2111b5 commit 65473ed
Show file tree
Hide file tree
Showing 10 changed files with 1,172 additions and 254 deletions.
148 changes: 92 additions & 56 deletions charms/jimm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
from charmhelpers.contrib.charmsupport.nrpe import NRPE
from jinja2 import Environment, FileSystemLoader
from ops.main import main
from ops.model import (ActiveStatus, BlockedStatus, MaintenanceStatus,
ModelError, WaitingStatus)

from ops.model import (
ActiveStatus,
BlockedStatus,
MaintenanceStatus,
ModelError,
WaitingStatus,
)
from systemd import SystemdCharm

logger = logging.getLogger(__name__)
Expand All @@ -31,19 +35,31 @@ class JimmCharm(SystemdCharm):
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.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)
self.framework.observe(self.on.stop, self._on_stop)
self.framework.observe(self.on.update_status, self._on_update_status)
self.framework.observe(self.on.upgrade_charm, self._on_upgrade_charm)
self.framework.observe(self.on.nrpe_relation_joined, self._on_nrpe_relation_joined)
self.framework.observe(self.on.website_relation_joined, self._on_website_relation_joined)
self.framework.observe(self.on.vault_relation_joined, self._on_vault_relation_joined)
self.framework.observe(self.on.vault_relation_changed, self._on_vault_relation_changed)
self.framework.observe(self.on.dashboard_relation_joined,
self._on_dashboard_relation_joined)
self.framework.observe(
self.on.nrpe_relation_joined, self._on_nrpe_relation_joined
)
self.framework.observe(
self.on.website_relation_joined, self._on_website_relation_joined
)
self.framework.observe(
self.on.vault_relation_joined, self._on_vault_relation_joined
)
self.framework.observe(
self.on.vault_relation_changed, self._on_vault_relation_changed
)
self.framework.observe(
self.on.dashboard_relation_joined,
self._on_dashboard_relation_joined,
)
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 @@ -81,29 +97,33 @@ def _on_config_changed(self, _):
config."""

args = {
"admins": self.config.get('controller-admins', ''),
"admins": self.config.get("controller-admins", ""),
"bakery_agent_file": self._bakery_agent_file(),
"candid_url": self.config.get('candid-url'),
"dns_name": self.config.get('dns-name'),
"log_level": self.config.get('log-level'),
"uuid": self.config.get('uuid'),
"dashboard_location": self.config.get('juju-dashboard-location')
"candid_url": self.config.get("candid-url"),
"dns_name": self.config.get("dns-name"),
"log_level": self.config.get("log-level"),
"uuid": self.config.get("uuid"),
"dashboard_location": self.config.get("juju-dashboard-location"),
}
if os.path.exists(self._dashboard_path):
args["dashboard_location"] = self._dashboard_path
with open(self._env_filename(), "wt") as f:
f.write(self._render_template('jimm.env', **args))
f.write(self._render_template("jimm.env", **args))
if self._ready():
self.restart()
self._on_update_status(None)

dashboard_relation = self.model.get_relation("dashboard")
if dashboard_relation:
dashboard_relation.data[self.app].update({
'controller-url': self.config['dns-name'],
'identity-provider-url': self.config['candid-url'],
'is-juju': str(False),
})
dashboard_relation.data[self.app].update(
{
"controller-url": "wss://{}".format(
self.config["dns-name"]
),
"identity-provider-url": self.config["candid-url"],
"is-juju": str(False),
}
)

def _on_leader_elected(self, _):
"""Update the JIMM configuration that comes from unit
Expand All @@ -127,7 +147,7 @@ def _on_db_relation_changed(self, event):
return
args = {"dsn": "pgx:" + dsn}
with open(self._env_filename("db"), "wt") as f:
f.write(self._render_template('jimm-db.env', **args))
f.write(self._render_template("jimm-db.env", **args))
if self._ready():
self.restart()
self._on_update_status(None)
Expand Down Expand Up @@ -168,9 +188,9 @@ def _on_nrpe_relation_joined(self, event):
nrpe.add_check(
shortname="JIMM",
description="check JIMM running",
check_cmd='check_http -w 2 -c 10 -I {} -p 8080 -u /debug/info'.format(
check_cmd="check_http -w 2 -c 10 -I {} -p 8080 -u /debug/info".format(
self.model.get_binding(event.relation).network.ingress_address,
)
),
)
nrpe.write()

Expand All @@ -179,20 +199,28 @@ def _on_website_relation_joined(self, event):
event.relation.data[self.unit]["port"] = "8080"

def _on_vault_relation_joined(self, event):
event.relation.data[self.unit]['secret_backend'] = json.dumps("charm-jimm-creds")
event.relation.data[self.unit]['hostname'] = json.dumps(socket.gethostname())
event.relation.data[self.unit]['access_address'] = \
json.dumps(str(
self.model.get_binding(event.relation).network.egress_subnets[0].network_address))
event.relation.data[self.unit]['isolated'] = json.dumps(False)
event.relation.data[self.unit]["secret_backend"] = json.dumps(
"charm-jimm-creds"
)
event.relation.data[self.unit]["hostname"] = json.dumps(
socket.gethostname()
)
event.relation.data[self.unit]["access_address"] = json.dumps(
str(
self.model.get_binding(event.relation)
.network.egress_subnets[0]
.network_address
)
)
event.relation.data[self.unit]["isolated"] = json.dumps(False)

def _on_vault_relation_changed(self, event):
if not os.path.exists(os.path.dirname(self._vault_secret_filename)):
# if the snap is yet to be installed wait for it.
event.defer()
return

addr = _json_data(event, 'vault_url')
addr = _json_data(event, "vault_url")
if not addr:
return
role_id = _json_data(event, "{}_role_id".format(self.unit.name))
Expand All @@ -203,22 +231,22 @@ def _on_vault_relation_changed(self, event):
return
client = hvac.Client(url=addr, token=token)
secret = client.sys.unwrap()
secret['data']['role_id'] = role_id
secret["data"]["role_id"] = role_id
with open(self._vault_secret_filename, "wt") as f:
json.dump(secret, f)
args = {
"vault_secret_file": self._vault_secret_filename,
"vault_addr": addr,
"vault_auth_path": "/auth/approle/login",
"vault_path": "charm-jimm-creds"
"vault_path": "charm-jimm-creds",
}
with open(self._env_filename("vault"), "wt") as f:
f.write(self._render_template("jimm-vault.env", **args))

def _is_snap_installed(self):
returncode = 1
try:
return self._snap('list', 'jimm') is None
return self._snap("list", "jimm") is None
except subprocess.CalledProcessError as cpe:
return False if cpe.returncode == returncode else True

Expand All @@ -233,8 +261,8 @@ def _install_snap(self):
return

if self._is_snap_installed():
self._snap('remove', 'jimm')
self._snap('install', '--dangerous', path)
self._snap("remove", "jimm")
self._snap("install", "--dangerous", path)

def _install_dashboard(self):
try:
Expand All @@ -246,8 +274,8 @@ def _install_dashboard(self):
return

if self._dashboard_resource_nonempty():
new_dashboard_path = self._dashboard_path + '.new'
old_dashboard_path = self._dashboard_path + '.old'
new_dashboard_path = self._dashboard_path + ".new"
old_dashboard_path = self._dashboard_path + ".old"
shutil.rmtree(new_dashboard_path, ignore_errors=True)
shutil.rmtree(old_dashboard_path, ignore_errors=True)
os.mkdir(new_dashboard_path)
Expand All @@ -266,26 +294,32 @@ def _install_dashboard(self):

def _setup_logging(self):
"""Install the logging configuration."""
shutil.copy(os.path.join(self.charm_dir, "files", "logrotate"), self._logrotate_conf_path)
shutil.copy(os.path.join(self.charm_dir, "files", "rsyslog"), self._rsyslog_conf_path)
shutil.copy(
os.path.join(self.charm_dir, "files", "logrotate"),
self._logrotate_conf_path,
)
shutil.copy(
os.path.join(self.charm_dir, "files", "rsyslog"),
self._rsyslog_conf_path,
)
self._systemctl("restart", "rsyslog")

def _dashboard_resource_nonempty(self):
dashboard_file = self.model.resources.fetch('dashboard')
dashboard_file = self.model.resources.fetch("dashboard")
if dashboard_file:
return os.path.getsize(dashboard_file) != 0
return False

def _bakery_agent_file(self):
url = self.config.get('candid-url', '')
username = self.config.get('candid-agent-username', '')
private_key = self.config.get('candid-agent-private-key', '')
public_key = self.config.get('candid-agent-public-key', '')
url = self.config.get("candid-url", "")
username = self.config.get("candid-agent-username", "")
private_key = self.config.get("candid-agent-private-key", "")
public_key = self.config.get("candid-agent-public-key", "")
if not url or not username or not private_key or not public_key:
return ""
data = {
"key": {"public": public_key, "private": private_key},
"agents": [{"url": url, "username": username}]
"agents": [{"url": url, "username": username}],
}
try:
with open(self._agent_filename, "wt") as f:
Expand All @@ -299,14 +333,14 @@ def _write_service_file(self):
"conf_file": self._env_filename(),
"db_file": self._env_filename("db"),
"leader_file": self._env_filename("leader"),
"vault_file": self._env_filename("vault")
"vault_file": self._env_filename("vault"),
}
with open(self.service_file, "wt") as f:
f.write(self._render_template('jimm.service', **args))
f.write(self._render_template("jimm.service", **args))

def _render_template(self, name, **kwargs):
"""Load the template with the given name."""
loader = FileSystemLoader(os.path.join(self.charm_dir, 'templates'))
loader = FileSystemLoader(os.path.join(self.charm_dir, "templates"))
env = Environment(loader=loader)
return env.get_template(name).render(**kwargs)

Expand All @@ -326,16 +360,18 @@ def _env_filename(self, part=None):
return self.charm_dir.joinpath(filename)

def _snap(self, *args):
cmd = ['snap']
cmd = ["snap"]
cmd.extend(args)
subprocess.run(cmd, capture_output=True, check=True)

def _on_dashboard_relation_joined(self, event):
event.relation.data[self.app].update({
'controller-url': self.config['dns-name'],
'identity-provider-url': self.config['candid-url'],
'is-juju': str(False),
})
event.relation.data[self.app].update(
{
"controller-url": "wss://{}".format(self.config["dns-name"]),
"identity-provider-url": self.config["candid-url"],
"is-juju": str(False),
}
)


def _json_data(event, key):
Expand Down
48 changes: 24 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,46 @@ module github.com/CanonicalLtd/jimm
go 1.16

require (
cloud.google.com/go/compute v1.8.0 // indirect
github.com/Azure/go-autorest/autorest v0.11.24 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/canonical/candid v1.10.1
github.com/canonical/go-dqlite v1.8.0
github.com/canonical/candid v1.12.2
github.com/canonical/go-dqlite v1.11.1
github.com/canonical/go-service v1.0.0
github.com/frankban/quicktest v1.13.0
github.com/go-macaroon-bakery/macaroon-bakery/v3 v3.0.0-20220204130128-afeebcc9521d
github.com/frankban/quicktest v1.14.3
github.com/go-macaroon-bakery/macaroon-bakery/v3 v3.0.1
github.com/gobwas/glob v0.2.4-0.20181002190808-e7a84e9525fe // indirect
github.com/google/go-cmp v0.5.8
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.4.2
github.com/gorilla/websocket v1.5.0
github.com/gosuri/uitable v0.0.1
github.com/hashicorp/vault/api v1.8.2
github.com/jackc/pgconn v1.7.0
github.com/jackc/pgx/v4 v4.9.0
github.com/juju/aclstore/v2 v2.1.0 // indirect
github.com/juju/charm/v8 v8.0.0-20220509231111-ed6d505a46f4
github.com/juju/charm/v8 v8.0.6
github.com/juju/cmd v0.0.0-20200108104440-8e43f3faa5c9 // indirect
github.com/juju/cmd/v3 v3.0.0-20220203030511-039f3566372a
github.com/juju/errors v0.0.0-20220316043928-e10eb17a9eeb
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d
github.com/juju/juju v0.0.0-20220525044642-0f2ce8e528a6
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4
github.com/juju/cmd/v3 v3.0.0
github.com/juju/errors v1.0.0
github.com/juju/gnuflag v1.0.0
github.com/juju/juju v0.0.0-20230228224222-7b871e782195
github.com/juju/loggo v1.0.0
github.com/juju/mgomonitor v0.0.0-20181029151116-52206bb0cd31
github.com/juju/mutex v0.0.0-20180619145857-d21b13acf4bf // indirect
github.com/juju/names/v4 v4.0.0-20220518060443-d77cb46f6093
github.com/juju/rpcreflect v0.0.0-20200416001309-bb46e9ba1476
github.com/juju/testing v0.0.0-20220203020004-a0ff61f03494
github.com/juju/names/v4 v4.0.0
github.com/juju/rpcreflect v1.0.0
github.com/juju/testing v1.0.2
github.com/juju/utils v0.0.0-20200604140309-9d78121a29e0 // indirect
github.com/juju/utils/v2 v2.0.0-20210305225158-eedbe7b6b3e2
github.com/juju/version v0.0.0-20210303051006-2015802527a8
github.com/juju/version/v2 v2.0.0-20220204124744-fc9915e3d935
github.com/juju/version/v2 v2.0.0
github.com/juju/zaputil v0.0.0-20190326175239-ef53049637ac
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0 // indirect
github.com/prometheus/client_golang v1.13.0
github.com/rogpeppe/fastuuid v1.2.0
go.uber.org/zap v1.10.0
go.uber.org/zap v1.17.0
golang.org/x/net v0.4.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
gopkg.in/errgo.v1 v1.0.1
gopkg.in/juju/names.v2 v2.0.0-20190813004204-e057c73bd1be // indirect
gopkg.in/macaroon-bakery.v2 v2.3.0
gopkg.in/macaroon.v2 v2.1.0
gorm.io/driver/postgres v1.0.5
Expand All @@ -56,7 +53,10 @@ require (

replace (
github.com/altoros/gosigma => github.com/juju/gosigma v0.0.0-20170523021020-a27b59fe2be9
github.com/mattn/go-sqlite3 => github.com/mattn/go-sqlite3 v1.14.5
// This is copied from the go.mod file in github.com/lxc/lxd
// It is needed to avoid this error when running go list -m
// go: google.golang.org/grpc/naming@v0.0.0-00010101000000-000000000000: invalid version: unknown revision 000000000000
google.golang.org/grpc/naming => google.golang.org/grpc v1.29.1
gopkg.in/yaml.v2 => github.com/juju/yaml v0.0.0-20200420012109-12a32b78de07
)

replace github.com/mattn/go-sqlite3 => github.com/mattn/go-sqlite3 v1.14.5
Loading

0 comments on commit 65473ed

Please sign in to comment.