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
This commit is contained in:
Luciano Lo Giudice 2023-03-08 19:31:35 -03:00
parent 011dddf14d
commit 616c4e3367
2 changed files with 27 additions and 1 deletions

View File

@ -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)

View File

@ -27,7 +27,7 @@ install_command =
commands = stestr run --slowest {posargs}
allowlist_externals =
charmcraft
rename.sh
{toxinidir}/rename.sh
passenv =
HOME
TERM