Skip to content

Commit

Permalink
[DPE-5588] Create architecture helpers lib (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclert-canonical authored Dec 10, 2024
1 parent 5fefa33 commit f1cce24
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
86 changes: 86 additions & 0 deletions lib/charms/mysql/v0/architecture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2024 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Library to provide hardware architecture checks for VMs and K8s charms.
The WrongArchitectureWarningCharm class is designed to be used alongside
the is-wrong-architecture helper function, as follows:
```python
import sys
from ops import main
from charms.mysql.v0.architecture import WrongArchitectureWarningCharm, is_wrong_architecture
if __name__ == "__main__":
if is_wrong_architecture():
main(WrongArchitectureWarningCharm)
```
"""

import logging
import os
import pathlib
import platform
import sys

import yaml
from ops import BlockedStatus, CharmBase

# The unique Charmhub library identifier, never change it
LIBID = "827e04542dba4c2a93bdc70ae40afdb1"
LIBAPI = 0
LIBPATCH = 1

PYDEPS = ["ops>=2.0.0", "pyyaml>=5.0"]


logger = logging.getLogger(__name__)


class WrongArchitectureWarningCharm(CharmBase):
"""A fake charm class that only signals a wrong architecture deploy."""

def __init__(self, *args):
super().__init__(*args)

hw_arch = platform.machine()
self.unit.status = BlockedStatus(f"Error: Charm incompatible with {hw_arch} architecture")
sys.exit(0)


def is_wrong_architecture() -> bool:
"""Checks if charm was deployed on wrong architecture."""
manifest_path = pathlib.Path(os.environ["CHARM_DIR"], "manifest.yaml")

if not manifest_path.exists():
logger.error("Cannot check architecture: manifest file not found in %s", manifest_path)
return False

manifest = yaml.safe_load(manifest_path.read_text())

manifest_archs = []
for base in manifest["bases"]:
base_archs = base.get("architectures", [])
manifest_archs.extend(base_archs)

hardware_arch = platform.machine()
if ("amd64" in manifest_archs and hardware_arch == "x86_64") or (
"arm64" in manifest_archs and hardware_arch == "aarch64"
):
logger.debug("Charm architecture matches")
return False

logger.error("Charm architecture does not match")
return True
52 changes: 52 additions & 0 deletions tests/unit/test_architecture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

from unittest.mock import patch

from charms.mysql.v0.architecture import is_wrong_architecture

TEST_MANIFEST = """
bases:
- architectures:
- {arch}
channel: '22.04'
name: ubuntu
"""


def test_wrong_architecture_file_not_found():
"""Tests if the function returns False when the charm file doesn't exist."""
with (
patch("os.environ", return_value={"CHARM_DIR": "/tmp"}),
patch("pathlib.Path.exists", return_value=False),
):
assert not is_wrong_architecture()


def test_wrong_architecture_amd64():
"""Tests if the function correctly identifies arch when charm is AMD."""
with (
patch("os.environ", return_value={"CHARM_DIR": "/tmp"}),
patch("pathlib.Path.exists", return_value=True),
patch("pathlib.Path.read_text", return_value=TEST_MANIFEST.format(arch="amd64")),
patch("platform.machine") as machine,
):
machine.return_value = "x86_64"
assert not is_wrong_architecture()
machine.return_value = "aarch64"
assert is_wrong_architecture()


def test_wrong_architecture_arm64():
"""Tests if the function correctly identifies arch when charm is ARM."""
with (
patch("os.environ", return_value={"CHARM_DIR": "/tmp"}),
patch("pathlib.Path.exists", return_value=True),
patch("pathlib.Path.read_text", return_value=TEST_MANIFEST.format(arch="arm64")),
patch("platform.machine") as machine,
):
machine.return_value = "x86_64"
assert is_wrong_architecture()
machine.return_value = "aarch64"
assert not is_wrong_architecture()

0 comments on commit f1cce24

Please sign in to comment.