<libname>.py
file¶
File <libname>.py
is a Python file in your charm project that holds a charm
library – that is, code that enables charm developers to easily share and reuse
auxiliary logic related to charms – for example, logic related to the relations
between charms.
Authors associate libraries with a specific charm and publish them to Charmhub with a reference to the origin charm. This does not prevent reuse, modification or sharing.
The publishing tools around libraries are deliberately kept simple. Libraries are however versioned and uniquely identified.
Location¶
Charm libraries are located in a subdirectory inside the charm with the following structure:
lib/charms/<charm-name>/v<API>/<libname>.py
where the <charm-name>
placeholder represents the name of charm responsible for
the library (converted to a valid module name), <libname>
represents this
particular library, and <API>
represents the API version of the library.
For example, inside a charm mysql
, the library db
with major version 3 will
be in a directory with the structure below:
lib/charms/mysql/v3/db.py
When you pack your charm, Charmcraft copies the top lib
directory into the root
directory of the charm. Thus, to import this library in Python use the full path
minus the top lib
directory, as below:
import charms.mysql.v3.db
Structure¶
A charm library is a Python file with the following structure:
Docstring¶
Your charm file begins with a long docstring. This docstring describes your library. Charmcraft publishes it as your library’s documentation on Charmhub. This documentation is updated whenever a new version of the library is published.
The docstring is expected to be in the CommonMark dialect of Markdown.
Metadata¶
After the docstring, there are a few metadata keys, as below.
LIBID
¶
Status: Required
Purpose: Contains the unique identifier for a library across the entire
universe of charms. The value is assigned by Charmhub to this particular library
automatically at library creation time. This key enables Charmhub and charmcraft
to track the library uniquely even if the charm or the library are renamed, which
allows updates to warn and guide users through the process.
Type: str
Value: Assigned by create-lib
LIBAPI
¶
Status: Required
Purpose: Declares the API version of this charm library.
Type: int
Value: LIBAPI`
is set to an initial state of 0
. In general,
LIBAPI
must match the major version in the import path.
LIBPATCH
¶
Status: Required
Purpose: Declares the patch version of this charm library.
Type: int
Value: LIBPATCH
is set to an initial state of 1
. In general, it must
match the current patch version (needs to be updated when changing).
Note
While LIBPATCH
can be set to 0
, it is not allowed to set both LIBAPI
and LIBPATCH
to 0
. As such, a charm lib may have a version 0.1
and
a version 1.0
, but not a version 0.0
.
PYDEPS
¶
Status: Optional
Purpose: Declares external Python dependencies for the library.
When using the charm
plugin, Charmcraft will make sure to install them in the
virtual environment created in any charm that includes the library.
Type: list[str]
Each string is a regular “pip installable” Python dependency that will be retrieved from PyPI in the usual way (subject to the user’s system configuration) and which supports all dependency formats (just the package name, a link to a Github project, etc.).
Examples
PYDEPS=["jinja2"]
PYDEPS = ["pyyaml", "httpcore<0.15.0,>=0.14.5"]
PYDEPS = [
"git+https://github.com/canonical/operator/#egg=ops",
"httpcore<0.15.0,>=0.14.5",
"requests",
]
Note that when called to install all the dependencies from the charm and all the
used libraries, pip
may detect conflicts between the requested packages and
their versions. This is a feature, because it’s always better to detect
incompatibilities between dependencies at this moment than when the charm is being
deployed or run after deployment.
Code¶
After the docstring and the metadata, there’s the library code. This is regular Python code.
Popular libraries¶
This is a list of some popular charm libraries available from Charmhub.
Note
This list does not and will not contain all charm libraries on Charmhub. However if you believe a library is missing from this list, please open a pull request adding the library you believe to be missing.
Libraries that define relations¶
The following libraries provide programmatic instructions for relating to a specific charm.
Library |
Used in |
Description |
---|---|---|
Defines both sides of a relation interface to the fluentbit charm. |
||
Import RedisRequires from this lib to relate your charm to the redis charm |
||
Defines a relation interface for charms that provide a dashboard to the grafana-k8s charm |
||
Defines a relation interface for charms that serve as a data source for the grafana-k8s charm |
||
Defines a relation interface for charms that want to expose metrics endpoints to the prometheus charm. |
||
Defines a relation to the alertmanager-dispatch charm. |
||
Defines an interface for charms wishing to consume or provide a karma-dashboard relation. |
||
Defines a relation interface for charms wishing to provide or consume the Loki Push API—e.g., a charm that wants to send logs to Loki. |
||
Defines a relation interface that allows a charm to act as a Log Proxy for Loki (via the Loki Push API). |
||
Defines a relation for charms wishing to set up a native server side proxy for Apache Guacamole. |
Libraries that provide tools¶
These libraries provide reusable tooling, typically to interact with cloud services, or to perform operations common to several charms.
Library |
Used in |
Description |
---|---|---|
Generates a self signed certificate. |
||
Helper for unit testing events. |
||
Provides tools for mocking networks. |
||
Provides utilities to track multiple independent statuses in charms. |
||
Provides utilities to periodically trigger charm hooks |
Libraries that provide tools for Kubernetes charms¶
These libraries provide tooling for charms that run on top of Kubernetes clouds.
Library |
Used in |
Description |
---|---|---|
Allows charm authors to simply and elegantly define service overrides that persist through a charm upgrade. |
||
Configures nginx to use an existing Kubernetes Ingress. |
||
Configures traefik to provide per-unit routing. |
Libraries that provide tools for machine charms¶
These libraries contain tools meant for use in machine charms, e.g., libraries that interact with package managers or other CLI tools that are often not present in containers.
Library |
Used in |
Description |
---|---|---|
Install and manage packages via |
||
Install and manage packages via |
||
Manage kernel configuration via |
||
Manage users and groups on a Linux system. |
||
Install and manage packages via |
||
Manage sysctl configuration. |
||
Interact with services via |