← Back to team overview

canonical-ubuntu-qa team mailing list archive

[Merge] ~andersson123/autopkgtest-cloud:restart-worker-on-charm-update into autopkgtest-cloud:master

 

Tim Andersson has proposed merging ~andersson123/autopkgtest-cloud:restart-worker-on-charm-update into autopkgtest-cloud:master.

Commit message:
Commit #1:

feat: cloud: restart autopkgtest.target on when the charm is updated

This commit introduces a new mechanism in the `upgrade-charm` hook
which restarts the autopkgtest.target systemd target, which in turn
restarts all of the autopkgtest services, as well as other services on
the cloud worker units which are part of the charm.

Prior to this commit, the admins would have to assess the changes in the
charm, and if required, do a manual restart of the autopkgtest services,
which is cumbersome, innefficient, and something that can easily be
overlooked.

Without restarting the services, any changes to the worker code would
not come into effect, thus rendering a charm update often useless, so
this commit fixes that issue.

To implement this fix, the `upgrade-charm` [1] hook is utilised. This
is a hook that runs every time a unit is undergoing an upgrade. It is
customisable, and one can add any number of steps to run through in the
event of a charm upgrade. For this purpose, the commit adds a
`systemctl restart autopkgtest.target` call during the hook.

[1] https://juju.is/docs/sdk/upgrade-charm-event

Commit #2:

fix: web: restart apache2 and autopkgtest-web.target on charm update

This commit introduces a new mechanism in the `upgrade-charm` hook,
which restarts the apache2.service and the autopkgtest-web.target
systemd units. This will make the webpage take into effect any changes
to the webcontrol code, and make any services that have changes to the
code or service files have those changes take immediate effect.

Prior to this commit, restarting apache2 and the autopkgtest-web target
was something that was done manually by an admin. This can be easily
overlooked and should be automated.

To implement this fix, the `upgrade-charm` [1] hook is utilised. This
is a hook that runs every time a unit is undergoing an upgrade. It is
customisable, and one can add any number of steps to run through in the
event of a charm upgrade. For this purpose, the commit adds two calls:
`systemctl restart apache2.service`
`systemctl restart autopkgtest-web.target`
to the hook.

`apache2.service` is not a part of autopkgtest-web.target.

[1] https://juju.is/docs/sdk/upgrade-charm-event

Requested reviews:
  Canonical's Ubuntu QA (canonical-ubuntu-qa)

For more details, see:
https://code.launchpad.net/~andersson123/autopkgtest-cloud/+git/autopkgtest-cloud/+merge/467026

Having to manually restart the autopkgtest.target on charm update is a pain, and something that can easily be missed, and is easily automated.

The same goes for the apache2.service and the autopkgtest-web.target in the web charm.

This MP fixes these issues for both the cloud worker charm and the web charm, by including the necessary functionality in the `upgrade-charm` hook.
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of ~andersson123/autopkgtest-cloud:restart-worker-on-charm-update into autopkgtest-cloud:master.
diff --git a/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py b/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py
index ef46a0d..2a411d9 100644
--- a/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py
+++ b/charms/focal/autopkgtest-cloud-worker/reactive/autopkgtest_cloud_worker.py
@@ -227,6 +227,14 @@ def daemon_reload():
 
 
 @hook("upgrade-charm")
+def restart_autopkgtest_target():
+    status.maintenance(
+        "Charm has been updated, restarting autopkgtest services"
+    )
+    set_flag("autopkgtest.target-restart-needed")
+
+
+@hook("upgrade-charm")
 def install_apt_packages():
     """Install packages defined in layer.yaml"""
     set_flag("apt.needs_update")
diff --git a/charms/focal/autopkgtest-web/reactive/autopkgtest_web.py b/charms/focal/autopkgtest-web/reactive/autopkgtest_web.py
index cc38250..14587e6 100644
--- a/charms/focal/autopkgtest-web/reactive/autopkgtest_web.py
+++ b/charms/focal/autopkgtest-web/reactive/autopkgtest_web.py
@@ -37,6 +37,30 @@ PUBLIC_SWIFT_CREDS_PATH = os.path.expanduser("~ubuntu/public-swift-creds")
 
 
 @hook("upgrade-charm")
+def restart_all_autopkgtest_web_services():
+    status.maintenance("Setting flags for autopkgtest-web restart")
+    set_flag("autopkgtest-web.apache2-needs-restart")
+    set_flag("autopkgtest-web.autopkgtest-web-target-needs-restart")
+
+
+@when("autopkgtest-web.apache2-needs-restart")
+def restart_apache2():
+    status.maintenance("Restarting apache2")
+    subprocess.check_call(["systemctl", "restart", "apache2.service"])
+    status.active("apache2 has been restarted successfully")
+    clear_flag("autopkgtest-web.apache2-needs-restart")
+
+
+@when("autopkgtest-web.autopkgtest-web-target-needs-restart")
+def restart_autopkgtest_web_target():
+    status.maintenance("Restarting autopkgtest-web.target...")
+    subprocess.check_call(["systemctl", "daemon-reload"])
+    subprocess.check_call(["systemctl", "restart", "autopkgtest-web.target"])
+    status.active("autopkgtest-web.target has been restarted")
+    clear_flag("autopkgtest-web.autopkgtest-web-target-needs-restart")
+
+
+@hook("upgrade-charm")
 def install_apt_packages():
     """Install packages defined in layer.yaml"""
     set_flag("apt.needs_update")
@@ -148,12 +172,9 @@ def set_up_systemd_units():
         if "[Install]" in pathlib.Path(unit).read_text():
             subprocess.check_call(["systemctl", "enable", base])
 
+    status.maintenance("systemd units installed")
     if any_changed:
-        subprocess.check_call(["systemctl", "daemon-reload"])
-        subprocess.check_call(
-            ["systemctl", "restart", "autopkgtest-web.target", "--no-block"]
-        )
-    status.active("systemd units installed and enabled")
+        set_flag("autopkgtest-web.autopkgtest-web-target-needs-restart")
 
 
 @when_all(