Merge ~mitchburton/landscape-charm:main into landscape-charm:main

Proposed by Mitch Burton
Status: Merged
Merged at revision: d7f6c3a59d44ced4999f4e7880138d98571ef43b
Proposed branch: ~mitchburton/landscape-charm:main
Merge into: landscape-charm:main
Diff against target: 119 lines (+56/-5)
4 files modified
.gitignore (+4/-1)
actions.yaml (+9/-3)
src/charm.py (+16/-0)
tests/test_charm.py (+27/-1)
Reviewer Review Type Date Requested Status
Kevin Nasto Approve
Review via email: mp+439188@code.launchpad.net

Commit message

add hash-id-databases action

Description of the change

This patch adds the hash-id-databases action, which simply runs the hash-id-databases script on-demand.

To test:
  1. charmcraft pack
  2. juju deploy ./bundle.yaml
  3. wait until everything is ready, go look at /var/lib/landscape/hash-id-databases on the landscape-server unit when things are done installing to see their file stats
  4. juju run-action landscape-server/0 hash-id-databases --wait

Confirm that the files in /var/lib/landscape/hash-id-databases are now newly created.

To post a comment you must log in.
Revision history for this message
Kevin Nasto (silverdrake11) wrote :

Not sure if this will be in issue but I noticed that cron job is /opt/canonical/landscape/scripts/hash_id_databases.sh but this one is in /landscape/. Seems like it's just checking if it already is running

Revision history for this message
Kevin Nasto (silverdrake11) wrote :

Also should the action wait until units are ready?

Revision history for this message
Kevin Nasto (silverdrake11) wrote :

When I did the manual test I noticed that the original files are owned by landscape but the new files are owned by root. So this should be running as landscape user

Revision history for this message
Mitch Burton (mitchburton) wrote :

> When I did the manual test I noticed that the original files are owned by
> landscape but the new files are owned by root. So this should be running as
> landscape user

Right. Will fix.

Revision history for this message
Mitch Burton (mitchburton) wrote :

> Also should the action wait until units are ready?

I don't think this is necessary. The only relation that is needed is the postgres one, and since this is a read-only action for the db, there shouldn't be any impact if other things aren't ready.

Revision history for this message
Mitch Burton (mitchburton) wrote :

> Not sure if this will be in issue but I noticed that cron job is
> /opt/canonical/landscape/scripts/hash_id_databases.sh but this one is in
> /landscape/. Seems like it's just checking if it already is running

The .sh just have some wrapping checks for if things are already running (like all the cron jobs do). I think it's fine to "jump the queue" and run this directly. It should be rare to run this one, anyways.

Revision history for this message
Kevin Nasto (silverdrake11) wrote :

LGTM +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.gitignore b/.gitignore
2index c5cb1af..ecb8c2f 100644
3--- a/.gitignore
4+++ b/.gitignore
5@@ -1 +1,4 @@
6-.coverage
7\ No newline at end of file
8+.coverage
9+venv
10+__pycache__
11+*.charm
12diff --git a/actions.yaml b/actions.yaml
13index 9289f0d..1b891ab 100644
14--- a/actions.yaml
15+++ b/actions.yaml
16@@ -6,9 +6,15 @@ pause:
17 resume:
18 description: Resume the Landscape services.
19 upgrade:
20- description: Upgrade software on the Landscape Server unit. This will update
21+ description: |
22+ Upgrade software on the Landscape Server unit. This will update
23 APT package indices and upgrade the landscape-server package. Unit must
24 already be paused using the 'pause' action.
25 migrate-schema:
26- description: Upgrade the Landscape database schemas on the related databases.
27- Unit must already be paused using the 'pause' action.
28\ No newline at end of file
29+ description: |
30+ Upgrade the Landscape database schemas on the related databases.
31+ Unit must already be paused using the 'pause' action.
32+hash-id-databases:
33+ description: |
34+ Regenerate the package hash to id mapping files that are used to
35+ speed up client package reporting.
36diff --git a/src/charm.py b/src/charm.py
37index 8af68d2..6c15478 100755
38--- a/src/charm.py
39+++ b/src/charm.py
40@@ -51,6 +51,7 @@ NRPE_D_DIR = "/etc/nagios/nrpe.d"
41 POSTFIX_CF = "/etc/postfix/main.cf"
42 SCHEMA_SCRIPT = "/usr/bin/landscape-schema"
43 BOOTSTRAP_ACCOUNT_SCRIPT = "/opt/canonical/landscape/bootstrap-account"
44+HASH_ID_DATABASES = "/opt/canonical/landscape/hash-id-databases"
45
46 LANDSCAPE_PACKAGES = (
47 "landscape-server",
48@@ -129,6 +130,8 @@ class LandscapeServerCharm(CharmBase):
49 self.framework.observe(self.on.upgrade_action, self._upgrade)
50 self.framework.observe(self.on.migrate_schema_action,
51 self._migrate_schema)
52+ self.framework.observe(self.on.hash_id_databases_action,
53+ self._hash_id_databases)
54
55 # State
56 self._stored.set_default(ready={
57@@ -945,6 +948,19 @@ command[check_{service}]=/usr/local/lib/nagios/plugins/check_systemd.py {service
58 else:
59 self.unit.status = prev_status
60
61+ def _hash_id_databases(self, event: ActionEvent) -> None:
62+ prev_status = self.unit.status
63+ self.unit.status = MaintenanceStatus("Hashing ID databases...")
64+ event.log("Running hash_id_databases")
65+
66+ try:
67+ subprocess.run(["sudo", "-u", "landscape", HASH_ID_DATABASES], check=True, text=True)
68+ except CalledProcessError as e:
69+ logger.error("Hashing ID databases failed with error code %s", e.returncode)
70+ event.fail("Hashing ID databases failed with error code %s", e.returncode)
71+ finally:
72+ self.unit.status = prev_status
73+
74
75 if __name__ == "__main__": # pragma: no cover
76 main(LandscapeServerCharm)
77diff --git a/tests/test_charm.py b/tests/test_charm.py
78index 516bc3f..08a4119 100644
79--- a/tests/test_charm.py
80+++ b/tests/test_charm.py
81@@ -24,7 +24,7 @@ from charms.operator_libs_linux.v0.apt import (
82
83 from charm import (
84 DEFAULT_SERVICES, HAPROXY_CONFIG_FILE, LANDSCAPE_PACKAGES, LEADER_SERVICES, LSCTL,
85- NRPE_D_DIR, SCHEMA_SCRIPT, LandscapeServerCharm)
86+ NRPE_D_DIR, SCHEMA_SCRIPT, HASH_ID_DATABASES, LandscapeServerCharm)
87
88
89 class TestCharm(unittest.TestCase):
90@@ -1287,3 +1287,29 @@ class TestBootstrapAccount(unittest.TestCase):
91 self.harness.update_config(config)
92 self.harness.update_config(config) # Third time
93 self.process_mock.assert_called_once()
94+
95+ @patch("subprocess.run")
96+ def test_hash_id_databases(self, run_mock):
97+ event = Mock(spec_set=ActionEvent)
98+
99+ self.harness.charm._hash_id_databases(event)
100+
101+ run_mock.assert_called_once_with(
102+ ["sudo", "-u", "landscape", HASH_ID_DATABASES],
103+ check=True,
104+ text=True
105+ )
106+
107+ @patch("subprocess.run")
108+ def test_hash_id_databases_error(self, run_mock):
109+ event = Mock(spec_set=ActionEvent)
110+ run_mock.side_effect = CalledProcessError(127, "ouchie")
111+
112+ self.harness.charm._hash_id_databases(event)
113+
114+ run_mock.assert_called_once_with(
115+ ["sudo", "-u", "landscape", HASH_ID_DATABASES],
116+ check=True,
117+ text=True
118+ )
119+ event.fail.assert_called_once()

Subscribers

People subscribed via source and target branches

to all changes: