|
| 1 | +# Copyright 2025 Canonical Ltd. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this |
| 4 | +# file except in compliance with the License. You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 9 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 10 | +# ANY KIND, either express or implied. See the License for the specific language |
| 11 | +# governing permissions and limitations under the License. |
| 12 | +"""Workarounds for various Juju bugs. |
| 13 | +
|
| 14 | +https://github.com/juju/charm/pull/435 |
| 15 | +
|
| 16 | +> It is important to note that this change will only ensure the proper cleanup of files |
| 17 | +> for charms that are newly deployed, as charms that are already deployed have their |
| 18 | +> manifests written to the manifest files on disk. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import logging |
| 24 | +import os |
| 25 | +import shutil |
| 26 | +from collections import defaultdict |
| 27 | +from typing import Any |
| 28 | + |
| 29 | +from importlib_metadata import distributions # type: ignore |
| 30 | + |
| 31 | +logger = logging.getLogger(__name__) |
| 32 | + |
| 33 | + |
| 34 | +def remove_stale_otel_sdk_packages() -> None: |
| 35 | + """Remove stale opentelemetry sdk packages from the charm's Python venv. |
| 36 | +
|
| 37 | + Charmcraft doesn't record empty directories in the charm (zip) file. |
| 38 | + Juju creates directories on demand when a contained file is unpacked. |
| 39 | + Juju removes what it has installed before the upgrade is unpacked. |
| 40 | + Juju prior to 3.5.4 left unrecorded, stale directories. |
| 41 | +
|
| 42 | + See https://github.com/canonical/grafana-agent-operator/issues/146 |
| 43 | + and https://bugs.launchpad.net/juju/+bug/2058335 |
| 44 | +
|
| 45 | + This only has an effect if executed on an upgrade-charm event. |
| 46 | + """ |
| 47 | + if os.getenv('JUJU_DISPATCH_PATH') != 'hooks/upgrade-charm': |
| 48 | + return |
| 49 | + |
| 50 | + logger.debug('Applying _remove_stale_otel_sdk_packages patch on charm upgrade') |
| 51 | + # group by name all distributions starting with "opentelemetry_" |
| 52 | + otel_distributions: dict[str, list[Any]] = defaultdict(list) |
| 53 | + for distribution in distributions(): |
| 54 | + name = distribution._normalized_name |
| 55 | + if name.startswith('opentelemetry_'): |
| 56 | + otel_distributions[name].append(distribution) |
| 57 | + |
| 58 | + logger.debug(f'Found {len(otel_distributions)} opentelemetry distributions') |
| 59 | + |
| 60 | + # If we have multiple distributions with the same name, remove any that have 0 |
| 61 | + # associated files |
| 62 | + for name, distributions_ in otel_distributions.items(): |
| 63 | + if len(distributions_) <= 1: |
| 64 | + continue |
| 65 | + |
| 66 | + logger.debug(f'Package {name} has multiple ({len(distributions_)}) distributions.') |
| 67 | + for distribution in distributions_: |
| 68 | + if not distribution.files: # Not None or empty list |
| 69 | + path = distribution._path |
| 70 | + logger.info(f'Removing empty distribution of {name} at {path}.') |
| 71 | + shutil.rmtree(path) |
| 72 | + |
| 73 | + logger.debug('Successfully applied _remove_stale_otel_sdk_packages patch. ') |
| 74 | + |
| 75 | + |
| 76 | +remove_stale_otel_sdk_packages() |
0 commit comments