From e6c0e146e8fb28e328f033836ad377a51a1d0d3d Mon Sep 17 00:00:00 2001
From: Liam Young <liam.young@canonical.com>
Date: Mon, 16 Aug 2021 13:19:42 +0000
Subject: [PATCH] Add update-trilio action

Add update-trilio action which can be run after a Trilio
upgrade.

Trilio sometimes updates packages in package repositiories that
require DB migrations or config file changes. Since these package
updates are not applied by the charm as they would be by changing the
openstack-origin or triliovault-pkg-source the charm has no event to
react to to automatically perform the post package upgrade actions.
This change adds an action `update-trilio` which can
be run to apply the changes.

Change-Id: I4c12bc9ff9b8b20a5ddc16a040f70b87f4cac30b
---
 src/actions.yaml          |  4 ++++
 src/actions/actions.py    | 26 +++++++++++++++++++++++++-
 src/actions/update-trilio |  1 +
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 120000 src/actions/update-trilio

diff --git a/src/actions.yaml b/src/actions.yaml
index 7846a19..f04b038 100644
--- a/src/actions.yaml
+++ b/src/actions.yaml
@@ -6,3 +6,7 @@ ghost-share:
       description: Comma separated nfs-shares configuration option from secondary deployment. NFS shares must be provided in the same order as the nfs-shares configuration option for the local deployment.
   required:
     - nfs-shares
+update-trilio:
+  description: |
+    Update the trilio packages and run post-update steps such as rerender
+    configuration files and run any upgrade tasks such as database migrations.
diff --git a/src/actions/actions.py b/src/actions/actions.py
index 996963c..2889637 100755
--- a/src/actions/actions.py
+++ b/src/actions/actions.py
@@ -26,6 +26,8 @@ basic.init_config_states()
 
 import charmhelpers.core.hookenv as hookenv
 
+import charms.reactive as reactive
+
 import charms_openstack.charm
 
 # import the trilio_wlm module to get the charm definitions created.
@@ -40,12 +42,33 @@ def ghost_share(*args):
         trilio_dm_charm.ghost_nfs_share(secondary_nfs_share)
 
 
+def update_trilio(*args):
+    """Run setup after Trilio upgrade.
+    """
+    with charms_openstack.charm.provide_charm_instance() as trilio_charm:
+        interfaces = ["shared-db", "amqp"]
+        endpoints = [
+            reactive.relations.endpoint_from_flag("{}.available".format(i))
+            for i in interfaces]
+        ceph = reactive.endpoint_from_flag("ceph.available")
+        if ceph:
+            endpoints.append(ceph)
+        trilio_charm.run_trilio_upgrade(endpoints)
+        trilio_charm._assess_status()
+
+
 # Actions to function mapping, to allow for illegal python action names that
 # can map to a python function.
-ACTIONS = {"ghost-share": ghost_share}
+ACTIONS = {
+    "ghost-share": ghost_share,
+    "update-trilio": update_trilio
+}
 
 
 def main(args):
+    # Manually trigger any register atstart events to ensure all endpoints
+    # are correctly setup.
+    hookenv._run_atstart()
     action_name = os.path.basename(args[0])
     try:
         action = ACTIONS[action_name]
@@ -56,6 +79,7 @@ def main(args):
             action(args)
         except Exception as e:
             hookenv.function_fail(str(e))
+    hookenv._run_atexit()
 
 
 if __name__ == "__main__":
diff --git a/src/actions/update-trilio b/src/actions/update-trilio
new file mode 120000
index 0000000..405a394
--- /dev/null
+++ b/src/actions/update-trilio
@@ -0,0 +1 @@
+actions.py
\ No newline at end of file