OSM Libs

  • OSM Charmers
Channel Revision Published Runs on
latest/edge 1 12 May 2022
Ubuntu 20.04
juju deploy osm-libs --channel edge
Show information

Platform:

Ubuntu
20.04

charms.osm_libs.v0.utils

OSM Utils Library.

This library offers some utilities made for but not limited to Charmed OSM.

Getting started

Execute the following command inside your Charmed Operator folder to fetch the library.

charmcraft fetch-lib charms.osm_libs.v0.utils

CharmError Exception

An exception that takes to arguments, the message and the StatusBase class, which are useful to set the status of the charm when the exception raises.

Example:

from charms.osm_libs.v0.utils import CharmError

class MyCharm(CharmBase):
    def _on_config_changed(self, _):
        try:
            if not self.config.get("some-option"):
                raise CharmError("need some-option", BlockedStatus)

            if not self.mysql_ready:
                raise CharmError("waiting for mysql", WaitingStatus)

            # Do stuff...

        exception CharmError as e:
            self.unit.status = e.status

Pebble validations

The check_container_ready function checks that a container is ready, and therefore Pebble is ready.

The check_service_active function checks that a service in a container is running.

Both functions raise a CharmError if the validations fail.

Example:

from charms.osm_libs.v0.utils import check_container_ready, check_service_active

class MyCharm(CharmBase):
    def _on_config_changed(self, _):
        try:
            container: Container = self.unit.get_container("my-container")
            check_container_ready(container)
            check_service_active(container, "my-service")
            # Do stuff...

        exception CharmError as e:
            self.unit.status = e.status

Debug-mode

The debug-mode allows OSM developers to easily debug OSM modules.

Example:

from charms.osm_libs.v0.utils import DebugMode

class MyCharm(CharmBase):
    _stored = StoredState()

    def __init__(self, _):
        # ...
        container: Container = self.unit.get_container("my-container")
        hostpaths = [
            HostPath(
                config="module-hostpath",
                container_path="/usr/lib/python3/dist-packages/module"
            ),
        ]
        vscode_workspace_path = "files/vscode-workspace.json"
        self.debug_mode = DebugMode(
            self,
            self._stored,
            container,
            hostpaths,
            vscode_workspace_path,
        )

    def _on_update_status(self, _):
        if self.debug_mode.started:
            return
        # ...

    def _get_debug_mode_information(self):
        command = self.debug_mode.command
        password = self.debug_mode.password
        return command, password

More

  • Get pod IP with get_pod_ip()

class CharmError

Description

Charm Error Exception. None

Methods

CharmError. __init__( self , message: str , status_class: StatusBase )

def check_container_ready(container: Container)

Check Pebble has started in the container.

Arguments

container (Container)

Container to be checked.

def check_service_active(
    container: Container,
    service_name: str
)

Check if the service is running.

Arguments

container (Container)

Container to be checked.

service_name (str)

Name of the service to check.

def get_pod_ip()

Get Kubernetes Pod IP.

Returns

The IP of the Pod.

class SubModule

Description

Represent RO Submodules. None

class HostPath

Description

Represents a hostpath. None

Methods

HostPath. __init__( self , config: str , container_path: str , submodules: dict )

class DebugMode

Description

Class to handle the debug-mode. None

Methods

DebugMode. __init__( self , charm: CharmBase , stored: StoredState , container: Container , hostpaths , vscode_workspace_path: str )

DebugMode. started( self )

Description

Indicates whether the debug-mode has started or not. None

DebugMode. command( self )

Description

Command to launch vscode. None

DebugMode. password( self )

Description

SSH password. None

DebugMode. enable( self , service_name: str )

Enable debug-mode.

Arguments

service_name (str)

Pebble service name which has the desired environment variables. Mandatory if there is more than one Pebble service configured.

Description

This function mounts hostpaths of the OSM modules (if set), and configures the container so it can be easily debugged. The setup includes the configuration of SSH, environment variables, and VSCode workspace and plugins.

DebugMode. disable( self )

Description

Disable debug-mode. None