Juju Ops Framework

3.7.0 · active · verified Wed Apr 15

Ops is the official Python library for writing Juju charms, enabling developers to build robust and reactive operators for cloud-native applications. It provides high-level abstractions for interacting with Juju, handling lifecycle events, managing application status, and interacting with container workloads via Pebble. The library is actively maintained, with frequent releases addressing bug fixes, performance improvements, and compatibility with the latest Juju versions, typically every few weeks.

Warnings

Install

Imports

Quickstart

This minimal example demonstrates a basic Juju charm that handles `install` and `config_changed` events. It sets the unit's status and workload version, and uses `StoredState` to persist simple charm data across hooks. The `ops.main()` function is essential for running the charm in the Juju environment.

import ops
from ops.charm import CharmBase
from ops.framework import StoredState


class MyCharm(CharmBase):
    _stored = StoredState()

    def __init__(self, framework: ops.Framework):
        super().__init__(framework)
        self.framework.observe(self.on.install, self._on_install)
        self.framework.observe(self.on.config_changed, self._on_config_changed)
        self._stored.set_default(initialized=False)

    def _on_install(self, event: ops.InstallEvent):
        # Example: Set initial status and workload version
        self.unit.status = ops.BlockedStatus("Waiting for configuration")
        self.unit.set_workload_version("v1.0.0")
        self._stored.initialized = True
        self.logger.info("Charm installed.")

    def _on_config_changed(self, event: ops.ConfigChangedEvent):
        # Example: Update status after configuration change
        if self._stored.initialized:
            self.unit.status = ops.ActiveStatus("Ready")
            self.logger.info("Configuration changed and charm is active.")


if __name__ == "__main__":
    # The main entry point for a Juju charm
    ops.main(MyCharm)

view raw JSON →