---
title: Charmhub | Deploy OSM Libs using Charmhub - The Open Operator Collection
description: Deploy the latest version of OSM Libs on any cloud.
url: https://charmhub.io/osm-libs/libraries/utils
---

# OSM Libs

[OSM Charmers](https://charmhub.io/publisher/osm-charmers "View all packages from OSM Charmers")

* [OSM Charmers](https://charmhub.io/publisher/osm-charmers "View all packages from OSM Charmers")

Platform:

20.04

edge 1

```
juju deploy osm-libs --channel edge
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/osm-libs/libraries/utils#drawer)

## charms.osm\_libs.v0.utils

* [*Docstrings*Docstrings](https://charmhub.io/osm-libs/libraries/utils)
  [*Code*Source code](https://charmhub.io/osm-libs/libraries/utils/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.osm_libs.v0.utils
    ```

    [Download utils.py](https://charmhub.io/osm-libs/libraries/utils/download)
  + *Last updated* 18 Jan 2023
  + *Revision* Library version 0.5

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()`

---

Index

* [class CharmError](https://charmhub.io/osm-libs/libraries/utils#charmerror)
* + [def \_\_init\_\_(
    self,
    message,
    status\_class)](https://charmhub.io/osm-libs/libraries/utils#charmerror-__init__)
* [def check\_container\_ready(
  container
  )](https://charmhub.io/osm-libs/libraries/utils#check_container_ready)
* [def check\_service\_active(
  container,
  service\_name
  )](https://charmhub.io/osm-libs/libraries/utils#check_service_active)
* [def get\_pod\_ip(
  )](https://charmhub.io/osm-libs/libraries/utils#get_pod_ip)
* [class SubModule](https://charmhub.io/osm-libs/libraries/utils#submodule)
* [class HostPath](https://charmhub.io/osm-libs/libraries/utils#hostpath)
* + [def \_\_init\_\_(
    self,
    config,
    container\_path,
    submodules)](https://charmhub.io/osm-libs/libraries/utils#hostpath-__init__)
* [class DebugMode](https://charmhub.io/osm-libs/libraries/utils#debugmode)
* + [def \_\_init\_\_(
    self,
    charm,
    stored,
    container,
    hostpaths,
    vscode\_workspace\_path)](https://charmhub.io/osm-libs/libraries/utils#debugmode-__init__)
* + [def started(
    self)](https://charmhub.io/osm-libs/libraries/utils#debugmode-started)
* + [def command(
    self)](https://charmhub.io/osm-libs/libraries/utils#debugmode-command)
* + [def password(
    self)](https://charmhub.io/osm-libs/libraries/utils#debugmode-password)
* + [def enable(
    self,
    service\_name)](https://charmhub.io/osm-libs/libraries/utils#debugmode-enable)
* + [def disable(
    self)](https://charmhub.io/osm-libs/libraries/utils#debugmode-disable)

#### 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
