The Python library behind great charms
Project description
The ops library
The ops library is a Python framework for developing and testing Kubernetes and machine charms. While charms can be written in any language, ops defines the latest standard, and charmers are encouraged to use Python with ops for all charms. The library is an official component of the Charm SDK, itself a part of the Juju universe.
opsis available on PyPI.- The latest version of
opsrequires Python 3.10 or above.- Read our docs for tutorials, how-to guides, the library reference, and more.
Give it a try
Let's use ops to build a Kubernetes charm:
Set up
See Juju | Set things up.
Choose the automatic track and MicroK8s.
Write your charm
On your Multipass VM, create a charm directory and use Charmcraft to initialise your charm file structure:
mkdir ops-example
cd ops-example
charmcraft init
This has created a standard charm directory structure:
$ ls -R
.:
CONTRIBUTING.md README.md pyproject.toml src tox.ini
LICENSE charmcraft.yaml requirements.txt tests
./src:
charm.py
./tests:
integration unit
./tests/integration:
test_charm.py
./tests/unit:
test_charm.py
Things to note:
-
The
charmcraft.yamlfile shows that what we have is an example charm calledops-example, which uses an OCI image resourcehttpbinfromkennethreitz/httpbin. -
The
requirements.txtfile lists the version ofopsto use. -
The
src/charm.pyfile importsopsand usesopsconstructs to create a charm classOpsExampleCharm, observe Juju events, and pair them to event handlers:
import ops
class OpsExampleCharm(ops.CharmBase):
"""Charm the service."""
def __init__(self, *args):
super().__init__(*args)
self.framework.observe(self.on['httpbin'].pebble_ready, self._on_httpbin_pebble_ready)
self.framework.observe(self.on.config_changed, self._on_config_changed)
def _on_httpbin_pebble_ready(self, event: ops.PebbleReadyEvent):
"""Define and start a workload using the Pebble API.
Change this example to suit your needs. You'll need to specify the right entrypoint and
environment configuration for your specific workload.
Learn more about interacting with Pebble at
https://documentation.ubuntu.com/ops/latest/reference/pebble/
"""
# Get a reference the container attribute on the PebbleReadyEvent
container = event.workload
# Add initial Pebble config layer using the Pebble API
container.add_layer("httpbin", self._pebble_layer, combine=True)
# Make Pebble reevaluate its plan, ensuring any services are started if enabled.
container.replan()
# Learn more about statuses at
# https://documentation.ubuntu.com/juju/3.6/reference/status/
self.unit.status = ops.ActiveStatus()
See more:
ops.PebbleReadyEvent
- The
tests/unit/test_charm.pyfile importsops.testingand uses it to set up a unit test:
import ops
from ops import testing
from charm import OpsExampleCharm
def test_httpbin_pebble_ready():
# Arrange:
ctx = testing.Context(OpsExampleCharm)
container = testing.Container("httpbin", can_connect=True)
state_in = testing.State(containers={container})
# Act:
state_out = ctx.run(ctx.on.pebble_ready(container), state_in)
# Assert:
updated_plan = state_out.get_container(container.name).plan
expected_plan = {
"services": {
"httpbin": {
"override": "replace",
"summary": "httpbin",
"command": "gunicorn -b 0.0.0.0:80 httpbin:app -k gevent",
"startup": "enabled",
"environment": {"GUNICORN_CMD_ARGS": "--log-level info"},
}
},
}
assert expected_plan == updated_plan
assert (
state_out.get_container(container.name).service_statuses["httpbin"]
== ops.pebble.ServiceStatus.ACTIVE
)
assert state_out.unit_status == testing.ActiveStatus()
See more:
ops.testing
Explore further, start editing the files, or skip ahead and pack the charm:
charmcraft pack
If you didn't take any wrong turn or simply left the charm exactly as it was, this has created a file called ops-example_ubuntu-22.04-amd64.charm (the architecture bit may be different depending on your system's architecture). Use this name and the resource from the metadata.yaml to deploy your example charm to your local MicroK8s cloud:
juju deploy ./ops-example_ubuntu-22.04-amd64.charm --resource httpbin-image=kennethreitz/httpbin
Congratulations, you’ve just built your first Kubernetes charm using ops!
Clean up
See Juju | Tear things down.
Choose the automatic track.
Next steps
- Read the docs.
- Read our Code of conduct and join our chat and forum or open an issue.
- Read our CONTRIBUTING guide and contribute!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ops-3.5.2.tar.gz.
File metadata
- Download URL: ops-3.5.2.tar.gz
- Upload date:
- Size: 578.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
849c9ed85eadf265b8a927d5e857cd112221dd71b35e4b13329ccb938c3afd18
|
|
| MD5 |
fac8decd9555feabae120c31775e6aef
|
|
| BLAKE2b-256 |
057250bdb29831b8ed92034e9657fec89301d8df3aa8da3da1d37ecbdf1baab6
|
Provenance
The following attestation bundles were made for ops-3.5.2.tar.gz:
Publisher:
publish.yaml on canonical/operator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ops-3.5.2.tar.gz -
Subject digest:
849c9ed85eadf265b8a927d5e857cd112221dd71b35e4b13329ccb938c3afd18 - Sigstore transparency entry: 939067129
- Sigstore integration time:
-
Permalink:
canonical/operator@a958024e2b25a176a86083fc467e2b0c8fea81fa -
Branch / Tag:
refs/tags/3.5.2 - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@a958024e2b25a176a86083fc467e2b0c8fea81fa -
Trigger Event:
push
-
Statement type:
File details
Details for the file ops-3.5.2-py3-none-any.whl.
File metadata
- Download URL: ops-3.5.2-py3-none-any.whl
- Upload date:
- Size: 211.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c715128a51ddcdf0fff463428b0f56a93e5963187e599b66594b4fc74458781b
|
|
| MD5 |
efa23d7cf318f482c9a5d8d81f10f377
|
|
| BLAKE2b-256 |
3e5c84b41a67c2dc904f92f424e981eb65641ce095936fb9e6d7b4a315072d1a
|
Provenance
The following attestation bundles were made for ops-3.5.2-py3-none-any.whl:
Publisher:
publish.yaml on canonical/operator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ops-3.5.2-py3-none-any.whl -
Subject digest:
c715128a51ddcdf0fff463428b0f56a93e5963187e599b66594b4fc74458781b - Sigstore transparency entry: 939067201
- Sigstore integration time:
-
Permalink:
canonical/operator@a958024e2b25a176a86083fc467e2b0c8fea81fa -
Branch / Tag:
refs/tags/3.5.2 - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@a958024e2b25a176a86083fc467e2b0c8fea81fa -
Trigger Event:
push
-
Statement type: