From 616c4e3367f2816774d8629396340a5b3b48f680 Mon Sep 17 00:00:00 2001 From: Luciano Lo Giudice Date: Wed, 8 Mar 2023 19:31:35 -0300 Subject: [PATCH] Use a different name for the local key/value store The operator framework and charmhelpers use the same path for the local K/V store, which causes problems when running certain hooks like 'pre-series-upgrade'. In order to work around this issue, this patchset makes the charmhelpers lib use a different path, while migrating the DB file before doing so. Closes-Bug: #2005137 Change-Id: Ic2e024371ff431888731753d29fff8538232009a --- src/charm.py | 26 ++++++++++++++++++++++++++ tox.ini | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/charm.py b/src/charm.py index 8a1786b4..66021235 100755 --- a/src/charm.py +++ b/src/charm.py @@ -1,5 +1,7 @@ #! /usr/bin/python3 import logging +import os +import shutil from ops.main import main @@ -47,6 +49,8 @@ class CephMonCharm(ops_openstack.core.OSBaseCharm): 'radosgw', 'lvm2', 'parted', 'smartmontools', ] + NEW_DB_PATH = '.charmhelpers-unit-state.db' + on = CephCharmEvents() # General charm control callbacks. @@ -83,12 +87,29 @@ class CephMonCharm(ops_openstack.core.OSBaseCharm): if hooks.config_changed(): self.on.notify_clients.emit() + def make_db_path(self, suffix): + return os.path.join(os.environ.get('CHARM_DIR', ''), suffix) + + def migrate_db(self): + """ + Migrate the Key/Value database into a new location. + This is done to avoid conflicts between charmhelpers and + the ops library, since they both use the same path and + with excluding lock semantics. + """ + db_path = self.make_db_path('.unit-state.db') + new_db_path = self.make_db_path(self.NEW_DB_PATH) + if os.path.exists(db_path) and not os.path.exists(new_db_path): + # The new DB doesn't exist yet. Copy it over. + shutil.copy(db_path, new_db_path) + def on_pre_series_upgrade(self, event): hooks.pre_series_upgrade() def on_upgrade(self, event): self._initialise_config() self.metrics_endpoint.update_alert_rules() + self.migrate_db() hooks.upgrade_charm() self.on.notify_clients.emit() @@ -175,6 +196,11 @@ class CephMonCharm(ops_openstack.core.OSBaseCharm): "Not running hook, CMR detected and not supported") return + # Make the charmhelpers lib use a different DB path. This is done + # so as to avoid conflicts with what the ops framework uses. + # See: https://bugs.launchpad.net/charm-ceph-mon/+bug/2005137 + os.environ['UNIT_STATE_DB'] = self.make_db_path(self.NEW_DB_PATH) + fw = self.framework self.clients = ceph_client.CephClientProvides(self) diff --git a/tox.ini b/tox.ini index 75e28749..cdfdb44c 100644 --- a/tox.ini +++ b/tox.ini @@ -27,7 +27,7 @@ install_command = commands = stestr run --slowest {posargs} allowlist_externals = charmcraft - rename.sh + {toxinidir}/rename.sh passenv = HOME TERM