From e97890043c59a02d075f1c1b01165ef1cb241bc3 Mon Sep 17 00:00:00 2001 From: Luciano Lo Giudice Date: Tue, 12 Sep 2023 19:23:43 -0300 Subject: [PATCH] Sync charm libraries Change-Id: Ie6ae6fa1369db537fa606fa04df538f9a5587a0c --- .../contrib/openstack/cert_utils.py | 33 ++++++++++------ .../charmhelpers/contrib/openstack/context.py | 8 ++-- .../contrib/storage/linux/ceph.py | 8 +++- hooks/charmhelpers/core/unitdata.py | 39 ++++++++++++++++++- hooks/charmhelpers/fetch/snap.py | 2 +- lib/charms_ceph/utils.py | 8 +++- 6 files changed, 77 insertions(+), 21 deletions(-) diff --git a/hooks/charmhelpers/contrib/openstack/cert_utils.py b/hooks/charmhelpers/contrib/openstack/cert_utils.py index a25ca995..6620f59f 100644 --- a/hooks/charmhelpers/contrib/openstack/cert_utils.py +++ b/hooks/charmhelpers/contrib/openstack/cert_utils.py @@ -414,18 +414,27 @@ def get_requests_for_local_unit(relation_name=None): is_legacy_request = set(sent).intersection(legacy_keys) for unit in related_units(rid): data = relation_get(rid=rid, unit=unit) - if data.get(raw_certs_key): - bundles.append({ - 'ca': data['ca'], - 'chain': data.get('chain'), - 'certs': json.loads(data[raw_certs_key])}) - elif is_legacy_request: - bundles.append({ - 'ca': data['ca'], - 'chain': data.get('chain'), - 'certs': {sent['common_name']: - {'cert': data.get(local_name + '.server.cert'), - 'key': data.get(local_name + '.server.key')}}}) + # Note: Bug#2028683 - data may not be available if the certificates + # relation hasn't been populated by the providing charm. If no 'ca' + # in the data then don't attempt the bundle at all. + if data.get('ca'): + if data.get(raw_certs_key): + bundles.append({ + 'ca': data['ca'], + 'chain': data.get('chain'), + 'certs': json.loads(data[raw_certs_key]) + }) + elif is_legacy_request: + bundles.append({ + 'ca': data['ca'], + 'chain': data.get('chain'), + 'certs': { + sent['common_name']: { + 'cert': data.get(local_name + '.server.cert'), + 'key': data.get(local_name + '.server.key') + } + } + }) return bundles diff --git a/hooks/charmhelpers/contrib/openstack/context.py b/hooks/charmhelpers/contrib/openstack/context.py index 24a13d0d..42f15032 100644 --- a/hooks/charmhelpers/contrib/openstack/context.py +++ b/hooks/charmhelpers/contrib/openstack/context.py @@ -1748,9 +1748,9 @@ class WSGIWorkerConfigContext(WorkerConfigContext): def __call__(self): total_processes = _calculate_workers() - enable_wsgi_rotation = config('wsgi-rotation') - if enable_wsgi_rotation is None: - enable_wsgi_rotation = True + enable_wsgi_socket_rotation = config('wsgi-socket-rotation') + if enable_wsgi_socket_rotation is None: + enable_wsgi_socket_rotation = True ctxt = { "service_name": self.service_name, "user": self.user, @@ -1764,7 +1764,7 @@ class WSGIWorkerConfigContext(WorkerConfigContext): "public_processes": int(math.ceil(self.public_process_weight * total_processes)), "threads": 1, - "wsgi_rotation": enable_wsgi_rotation, + "wsgi_socket_rotation": enable_wsgi_socket_rotation, } return ctxt diff --git a/hooks/charmhelpers/contrib/storage/linux/ceph.py b/hooks/charmhelpers/contrib/storage/linux/ceph.py index 2e1fc1b5..6ec67cba 100644 --- a/hooks/charmhelpers/contrib/storage/linux/ceph.py +++ b/hooks/charmhelpers/contrib/storage/linux/ceph.py @@ -158,15 +158,19 @@ def get_osd_settings(relation_name): return _order_dict_by_key(osd_settings) -def send_application_name(relid=None): +def send_application_name(relid=None, app_name=None): """Send the application name down the relation. :param relid: Relation id to set application name in. :type relid: str + :param app_name: Application name to send in the relation. + :type app_name: str """ + if app_name is None: + app_name = application_name() relation_set( relation_id=relid, - relation_settings={'application-name': application_name()}) + relation_settings={'application-name': app_name}) def send_osd_settings(): diff --git a/hooks/charmhelpers/core/unitdata.py b/hooks/charmhelpers/core/unitdata.py index 8f4bbc61..65153f1f 100644 --- a/hooks/charmhelpers/core/unitdata.py +++ b/hooks/charmhelpers/core/unitdata.py @@ -151,6 +151,7 @@ import contextlib import datetime import itertools import json +import logging import os import pprint import sqlite3 @@ -521,6 +522,42 @@ _KV = None def kv(): global _KV + + # If we are running unit tests, it is useful to go into memory-backed KV store to + # avoid concurrency issues when running multiple tests. This is not a + # problem when juju is running normally. + + env_var = os.environ.get("CHARM_HELPERS_TESTMODE", "auto").lower() + if env_var not in ["auto", "no", "yes"]: + logging.warning("Unknown value for CHARM_HELPERS_TESTMODE '%s'" + ", assuming 'no'", env_var) + env_var = "no" + + if env_var == "no": + in_memory_db = False + elif env_var == "yes": + in_memory_db = True + elif env_var == "auto": + # If UNIT_STATE_DB is set, respect this request + if "UNIT_STATE_DB" in os.environ: + in_memory_db = False + # Autodetect normal juju execution by looking for juju variables + elif "JUJU_CHARM_DIR" in os.environ or "JUJU_UNIT_NAME" in os.environ: + in_memory_db = False + else: + # We are probably running in unit test mode + logging.warning("Auto-detected unit test environment for KV store.") + in_memory_db = True + else: + # Help the linter realise that in_memory_db is always set + raise Exception("Cannot reach this line") + if _KV is None: - _KV = Storage() + if in_memory_db: + _KV = Storage(":memory:") + else: + _KV = Storage() + else: + if in_memory_db and _KV.db_path != ":memory:": + logging.warning("Running with in_memory_db and KV is not set to :memory:") return _KV diff --git a/hooks/charmhelpers/fetch/snap.py b/hooks/charmhelpers/fetch/snap.py index 36d6bce9..7ab7ce3e 100644 --- a/hooks/charmhelpers/fetch/snap.py +++ b/hooks/charmhelpers/fetch/snap.py @@ -52,7 +52,7 @@ def _snap_exec(commands): :param commands: List commands :return: Integer exit code """ - assert type(commands) == list + assert isinstance(commands, list) retry_count = 0 return_code = None diff --git a/lib/charms_ceph/utils.py b/lib/charms_ceph/utils.py index 01fb9ac9..41eff9b4 100644 --- a/lib/charms_ceph/utils.py +++ b/lib/charms_ceph/utils.py @@ -1223,6 +1223,11 @@ def get_upgrade_key(): return get_named_key('upgrade-osd', _upgrade_caps) +def is_internal_client(name): + keys = ('osd-upgrade', 'osd-removal', 'admin', 'rbd-mirror', 'mds') + return any(name.startswith(key) for key in keys) + + def get_named_key(name, caps=None, pool_list=None): """Retrieve a specific named cephx key. @@ -1236,7 +1241,8 @@ def get_named_key(name, caps=None, pool_list=None): key = ceph_auth_get(key_name) if key: - upgrade_key_caps(key_name, caps) + if is_internal_client(name): + upgrade_key_caps(key_name, caps) return key log("Creating new key for {}".format(name), level=DEBUG)