Skip to content

Commit ff00a89

Browse files
lengaumedubelko
andauthored
docs: add howto guides for switching from the charm plugin (#1967)
This adds howto guides to switch from the charm plugin to the python and poetry plugins. --------- Co-authored-by: Michael DuBelko <michael.dubelko@gmail.com>
1 parent dd63629 commit ff00a89

File tree

5 files changed

+258
-1
lines changed

5 files changed

+258
-1
lines changed

docs/howto/charm-to-poetry.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.. _howto-migrate-to-poetry:
2+
3+
Migrate from the Charm plugin to the Poetry plugin
4+
==================================================
5+
6+
Many charms use `Poetry`_ to manage their Python projects. For these charms, Charmcraft
7+
has a :ref:`craft_parts_poetry_plugin`. Migrating from the Charm plugin provides some
8+
benefits, such as no longer having to maintain a ``requirements.txt`` file. If the
9+
charm to be migrated does not currently use poetry, refer to the
10+
`Poetry documentation <https://python-poetry.org/docs/basic-usage/>`_ for instructions
11+
on how to use poetry for a Python project.
12+
13+
Update ``charmcraft.yaml``
14+
--------------------------
15+
16+
The first step is to update ``charmcraft.yaml`` to include the correct parts definition.
17+
Depending on the history of a specific charm, it may not have an explicitly-included
18+
``parts`` section determining how to build the charm. In this case, a ``parts`` section
19+
can be created as follows:
20+
21+
.. code-block:: yaml
22+
23+
parts:
24+
my-charm: # This can be named anything you want
25+
plugin: poetry
26+
source: .
27+
28+
Select compatible versions of ``pip`` and ``poetry``
29+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30+
31+
The Poetry plugin requires at least `pip 22.3
32+
<https://pypi.org/project/pip/22.3>`_, released in October 2022. If the
33+
charm's base uses an older version of pip, a newer version can be installed in the
34+
build environment using a dependency part. Likewise, a charm may require a newer
35+
version of Poetry than is available in the distribution's repositories. The following
36+
``parts`` section can be used in place of the section above to upgrade pip and Poetry
37+
for charms that build on Ubuntu 22.04 or earlier:
38+
39+
.. code-block:: yaml
40+
:emphasize-lines: 2-9,11
41+
42+
parts:
43+
poetry-deps:
44+
plugin: nil
45+
build-packages:
46+
- curl
47+
override-build: |
48+
/usr/bin/python3 -m pip install pip==24.2
49+
curl -sSL https://install.python-poetry.org | python3 -
50+
ln -sf $HOME/.local/bin/poetry /usr/local/bin/poetry
51+
my-charm: # This can be named anything you want
52+
after: [poetry-deps]
53+
plugin: poetry
54+
source: .
55+
56+
Add optional dependency groups
57+
------------------------------
58+
59+
If the charm has optional `dependency groups`_ that should be included when creating
60+
the virtual environment, the ``poetry-with`` key can be used to include those groups
61+
when creating the virtual environment.
62+
63+
.. note::
64+
This is useful and encouraged, though not mandatory, for keeping track of
65+
library dependencies, as covered in the next section. For an example, see
66+
`postgresql-operator`_.
67+
68+
Include charm library dependencies
69+
----------------------------------
70+
71+
Unlike the Charm plugin, the Poetry plugin does not install the dependencies for
72+
included charmlibs. If any of the charm libraries used have PYDEPS, these will
73+
need to be added to the charm's dependencies, potentially as their own
74+
`dependency group <dependency groups_>`_.
75+
76+
To find these dependencies, check each library file for its ``PYDEPS``. A command
77+
that can find these is::
78+
79+
find lib -name "*.py" -exec awk '/PYDEPS = \[/,/\]/' {} +
80+
81+
If run from the base directory of a charm, this will show all the PYDEPS declarations
82+
from all loaded charm libs.
83+
84+
Include extra files
85+
-------------------
86+
87+
A Poetry plugin only includes the contents of the ``src`` and ``lib`` directories
88+
as well as the generated virtual environment. If other files were previously included
89+
from the main directory, they can be included again using the
90+
:ref:`craft_parts_dump_plugin`:
91+
92+
.. code-block:: yaml
93+
:emphasize-lines: 5-9
94+
95+
parts:
96+
my-charm: # This can be named anything you want
97+
plugin: poetry
98+
source: .
99+
version-file:
100+
plugin: dump
101+
source: .
102+
stage:
103+
- charm_version
104+
105+
106+
.. _dependency groups: https://python-poetry.org/docs/managing-dependencies/#dependency-groups
107+
.. _Poetry: https://python-poetry.org
108+
.. _postgresql-operator: https://github.com/canonical/postgresql-operator/blob/3c7c783d61d4bee4ce64c190a9f7d4a78048e4e7/pyproject.toml#L22-L35

docs/howto/charm-to-python.rst

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
.. _howto-migrate-to-python:
2+
3+
Migrate from the Charm plugin to the Python plugin
4+
==================================================
5+
6+
The Python plugin in Charmcraft offers a faster, stricter means of packing an operator
7+
charm with a virtual environment. This guide shows how to migrate from a charm using
8+
the default Charm plugin to using the Python plugin.
9+
10+
Update ``charmcraft.yaml``
11+
--------------------------
12+
13+
The first step is to update ``charmcraft.yaml`` to include the correct parts definition.
14+
Depending on the history of a specific charm, it may not have an explicitly-included
15+
``parts`` section determining how to build the charm. In this case, a ``parts`` section
16+
can be created as follows:
17+
18+
.. code-block:: yaml
19+
20+
parts:
21+
my-charm: # This can be named anything you want
22+
plugin: python
23+
source: .
24+
python-requirements:
25+
- requirements.txt # Or whatever your requirements file is called.
26+
27+
Select a compatible version of ``pip``
28+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29+
30+
The Python plugin requires at least `pip 22.3`_, released in October 2022. If the
31+
charm's base uses an older version of pip, a newer version can be installed in the
32+
build environment using a dependency part. The following ``parts`` section can be
33+
used in place of the section above to upgrade pip for charms that build on Ubuntu
34+
22.04 or earlier:
35+
36+
.. code-block:: yaml
37+
:emphasize-lines: 2-5,7
38+
39+
parts:
40+
python-deps:
41+
plugin: nil
42+
override-build: |
43+
/usr/bin/python3 -m pip install pip==24.2
44+
my-charm: # This can be named anything you want
45+
after: [python-deps]
46+
plugin: python
47+
source: .
48+
python-requirements:
49+
- requirements.txt # Or whatever your requirements file is called.
50+
51+
Flatten ``requirements.txt``
52+
----------------------------
53+
54+
One difference between the Python plugin and the Charm plugin is that the Python
55+
plugin does not install dependencies, so the ``requirements.txt`` file must be a
56+
complete set of packages needed in the charm's virtual environment.
57+
58+
.. note::
59+
There are several tools for creating an exhaustive ``requirements.txt`` file.
60+
Charmcraft works with any as long as it generates a requirements file that ``pip``
61+
understands. Because different versions of packages may have different
62+
dependencies, it is recommended that the requirements file be generated using a
63+
tool that will lock the dependencies to specific versions.
64+
A few examples include:
65+
66+
- `uv export <https://docs.astral.sh/uv/reference/cli/#uv-export>`_
67+
- `pip-compile <https://pip-tools.readthedocs.io/en/stable/cli/pip-compile/>`_
68+
- `pip freeze <https://pip.pypa.io/en/stable/cli/pip_freeze/>`_
69+
70+
A basic ``requirements.txt`` file for a charm with no dependencies other than the
71+
Operator framework may look something like::
72+
73+
ops==2.17.0
74+
pyyaml==6.0.2
75+
websocket-client==1.8.0
76+
77+
To check that the virtual environment for the charm would be valid, activate an
78+
empty virtual environment and then run::
79+
80+
pip install --no-deps -r requirements.txt
81+
pip check
82+
83+
Include charm library dependencies
84+
----------------------------------
85+
86+
Unlike the Charm plugin, the Python plugin does not install the dependencies
87+
for included charmlibs. If any of the charm libraries used have PYDEPS, these will
88+
need to be added to a requirements file as well.
89+
90+
.. note::
91+
All requirements files are included in the same ``pip`` command to prevent
92+
conflicting requirements from overriding each other. However, this means
93+
that a charm will fail to build if it has conflicting requirements. A single
94+
``requirements.txt`` file, while not mandatory, is recommended.
95+
96+
To find these dependencies, check each library file for its ``PYDEPS``. A command
97+
that can find these is::
98+
99+
find lib -name "*.py" -exec awk '/PYDEPS = \[/,/\]/' {} +
100+
101+
If run from the base directory of a charm, this will show all the PYDEPS declarations
102+
from all loaded charm libs, which can be used to help generate the input for a tool
103+
that generates ``requirements.txt``.
104+
105+
Include extra files
106+
-------------------
107+
108+
The Python plugin only includes the contents of the ``src`` and ``lib`` directories
109+
as well as the generated virtual environment. If other files were previously included
110+
from the main directory, they can be included again using the
111+
:ref:`craft_parts_dump_plugin`:
112+
113+
.. code-block:: yaml
114+
:emphasize-lines: 7-11
115+
116+
parts:
117+
my-charm: # This can be named anything you want
118+
plugin: python
119+
source: .
120+
python-requirements:
121+
- requirements.txt # Or whatever your requirements file is called.
122+
version-file:
123+
plugin: dump
124+
source: .
125+
stage:
126+
- charm_version
127+
128+
129+
.. _pip 22.3: https://pip.pypa.io/en/stable/news/#v22-3

docs/howto/index.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. _howto:
2+
3+
How-To
4+
******
5+
6+
.. toctree::
7+
:maxdepth: 2
8+
9+
charm-to-poetry
10+
charm-to-python

docs/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ Most of Charmcraft's documentation is available there.
1010
:maxdepth: 1
1111
:hidden:
1212

13+
howto/index
1314
reference/index
1415
explanation/index
1516

17+
.. grid:: 1 1 2 2
18+
19+
.. grid-item-card:: `Tutorial <https://juju.is/docs/sdk/tutorials>`_
20+
21+
**Get started** with a hands-on introduction to Charmcraft
22+
23+
.. grid-item-card:: :ref:`How-to guides <howto>`
24+
25+
**Step-by-step guides** covering key operations and common tasks
26+
1627
.. grid:: 1 1 2 2
1728
:reverse:
1829

docs/reference/plugins/python_plugin.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ During the build step, the plugin performs the following actions:
4949
4. It copies any existing ``src`` and ``lib`` directories from your charm project into
5050
the final charm.
5151

52-
5352
Example
5453
-------
5554

0 commit comments

Comments
 (0)