Skip to content

Commit 21c8052

Browse files
Migrate to charmcraft 3 poetry plugin (#178)
data-platform-workflows and charmcraftcache are migrating from charmcraft 2 to 3 to enable 24.04 based charms & to enable use of the poetry plugin, which fixes several longstanding issues with charmcraft (e.g. canonical/charmcraft#1077)
1 parent 19916cd commit 21c8052

7 files changed

Lines changed: 98 additions & 48 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ jobs:
4040

4141
build:
4242
name: Build charms
43-
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v24.0.5
43+
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v26.0.0
4444
with:
4545
cache: true
46+
charmcraft-snap-channel: latest/beta/data-platform # TODO: remove after charmcraft 3.3 stable release
4647
path-to-charm-directory: ${{ matrix.path }}
4748
strategy:
4849
matrix:

.github/workflows/release.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ jobs:
3030

3131
build:
3232
name: Build charm
33-
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v24.0.5
33+
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v26.0.0
3434
with:
35-
cache: false
35+
charmcraft-snap-channel: latest/beta/data-platform # TODO: remove after charmcraft 3.3 stable release
3636

3737
release:
3838
name: Release to Charmhub
3939
needs:
4040
- lib-check
4141
- ci-tests
4242
- build
43-
uses: canonical/data-platform-workflows/.github/workflows/release_charm.yaml@v24.0.5
43+
uses: canonical/data-platform-workflows/.github/workflows/release_charm.yaml@v26.0.0
4444
with:
45+
charmcraft-snap-channel: latest/beta/data-platform # TODO: remove after charmcraft 3.3 stable release
4546
channel: 3/edge
4647
artifact-prefix: ${{ needs.build.outputs.artifact-prefix }}
4748
secrets:

charmcraft.yaml

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,86 @@
1-
# Copyright 2023 Canonical Ltd.
1+
# Copyright 2022 Canonical Ltd.
22
# See LICENSE file for licensing details.
33

44
type: charm
5+
platforms:
6+
ubuntu@22.04:amd64:
7+
# Files implicitly created by charmcraft without a part:
8+
# - dispatch (https://github.com/canonical/charmcraft/pull/1898)
9+
# - manifest.yaml
10+
# (https://github.com/canonical/charmcraft/blob/9ff19c328e23b50cc06f04e8a5ad4835740badf4/charmcraft/services/package.py#L259)
11+
# Files implicitly copied/"primed" by charmcraft without a part:
12+
# - actions.yaml, config.yaml, metadata.yaml
13+
# (https://github.com/canonical/charmcraft/blob/9ff19c328e23b50cc06f04e8a5ad4835740badf4/charmcraft/services/package.py#L290-L293
14+
# https://github.com/canonical/charmcraft/blob/9ff19c328e23b50cc06f04e8a5ad4835740badf4/charmcraft/services/package.py#L156-L157)
515
parts:
6-
charm:
7-
build-snaps:
8-
- rustup
16+
# "poetry-deps" part name is a magic constant
17+
# https://github.com/canonical/craft-parts/pull/901
18+
poetry-deps:
19+
plugin: nil
920
build-packages:
10-
- pkg-config
11-
- libffi-dev
12-
- libssl-dev
21+
- curl
1322
override-build: |
14-
# get latest rust stable toolchain
15-
rustup default stable
23+
# Use environment variable instead of `--break-system-packages` to avoid failing on older
24+
# versions of pip that do not recognize `--break-system-packages`
25+
# `--user` needed (in addition to `--break-system-packages`) for Ubuntu >=24.04
26+
PIP_BREAK_SYSTEM_PACKAGES=true python3 -m pip install --user --upgrade pip==24.3.1 # renovate: charmcraft-pip-latest
1627
17-
# Convert subset of poetry.lock to requirements.txt
18-
curl -sSL https://install.python-poetry.org | python3 -
19-
/root/.local/bin/poetry self add poetry-plugin-export
20-
/root/.local/bin/poetry export --only main,charm-libs --output requirements.txt
28+
# Use uv to install poetry so that a newer version of Python can be installed if needed by poetry
29+
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/astral-sh/uv/releases/download/0.5.15/uv-installer.sh | sh # renovate: charmcraft-uv-latest
30+
# poetry 2.0.0 requires Python >=3.9
31+
if ! "$HOME/.local/bin/uv" python find '>=3.9'
32+
then
33+
# Use first Python version that is >=3.9 and available in an Ubuntu LTS
34+
# (to reduce the number of Python versions we use)
35+
"$HOME/.local/bin/uv" python install 3.10.12 # renovate: charmcraft-python-ubuntu-22.04
36+
fi
37+
"$HOME/.local/bin/uv" tool install --no-python-downloads --python '>=3.9' poetry==2.0.0 --with poetry-plugin-export==1.8.0 # renovate: charmcraft-poetry-latest
38+
39+
ln -sf "$HOME/.local/bin/poetry" /usr/local/bin/poetry
40+
# "charm-poetry" part name is arbitrary; use for consistency
41+
# Avoid using "charm" part name since that has special meaning to charmcraft
42+
charm-poetry:
43+
# By default, the `poetry` plugin creates/primes these directories:
44+
# - lib, src
45+
# (https://github.com/canonical/charmcraft/blob/9ff19c328e23b50cc06f04e8a5ad4835740badf4/charmcraft/parts/plugins/_poetry.py#L76-L78)
46+
# - venv
47+
# (https://github.com/canonical/charmcraft/blob/9ff19c328e23b50cc06f04e8a5ad4835740badf4/charmcraft/parts/plugins/_poetry.py#L95
48+
# https://github.com/canonical/craft-parts/blob/afb0d652eb330b6aaad4f40fbd6e5357d358de47/craft_parts/plugins/base.py#L270)
49+
plugin: poetry
50+
source: .
51+
after:
52+
- poetry-deps
53+
poetry-export-extra-args: ['--only', 'main,charm-libs']
54+
build-packages:
55+
- libffi-dev # Needed to build Python dependencies with Rust from source
56+
- libssl-dev # Needed to build Python dependencies with Rust from source
57+
- pkg-config # Needed to build Python dependencies with Rust from source
58+
override-build: |
59+
# Workaround for https://github.com/canonical/charmcraft/issues/2068
60+
# rustup used to install rustc and cargo, which are needed to build Python dependencies with Rust from source
61+
if [[ "$CRAFT_PLATFORM" == ubuntu@20.04:* || "$CRAFT_PLATFORM" == ubuntu@22.04:* ]]
62+
then
63+
snap install rustup --classic
64+
else
65+
apt-get install rustup -y
66+
fi
67+
68+
# If Ubuntu version < 24.04, rustup was installed from snap instead of from the Ubuntu
69+
# archive—which means the rustup version could be updated at any time. Print rustup version
70+
# to build log to make changes to the snap's rustup version easier to track
71+
rustup --version
72+
73+
# rpds-py (Python package) >=0.19.0 requires rustc >=1.76, which is not available in the
74+
# Ubuntu 22.04 archive. Install rustc and cargo using rustup instead of the Ubuntu archive
75+
rustup set profile minimal
76+
rustup default 1.83.0 # renovate: charmcraft-rust-latest
2177
2278
craftctl default
23-
charm-strict-dependencies: true
24-
charm-requirements: [requirements.txt]
25-
bases:
26-
- build-on:
27-
- name: "ubuntu"
28-
channel: "22.04"
29-
run-on:
30-
- name: "ubuntu"
31-
channel: "22.04"
79+
# Include requirements.txt in *.charm artifact for easier debugging
80+
cp requirements.txt "$CRAFT_PART_INSTALL/requirements.txt"
81+
# "files" part name is arbitrary; use for consistency
82+
files:
83+
plugin: dump
84+
source: .
85+
prime:
86+
- LICENSE

poetry.lock

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ extend-exclude = "tests/integration/app-charm/lib"
2424
profile = "black"
2525

2626
[tool.poetry]
27-
name = "zookeeper-operator"
28-
version = "1.0"
29-
description = "zookeeper-operator"
30-
authors = []
3127
package-mode = false
28+
requires-poetry = ">=2.0.0"
3229

3330
[tool.poetry.dependencies]
3431
python = "^3.10"
@@ -93,10 +90,10 @@ pytest = ">=7.2"
9390
juju = "^3.6.0"
9491
coverage = { extras = ["toml"], version = ">7.0" }
9592
pytest-operator = ">0.20"
96-
pytest-operator-cache = { git = "https://github.com/canonical/data-platform-workflows", tag = "v24.0.0", subdirectory = "python/pytest_plugins/pytest_operator_cache" }
93+
pytest-operator-cache = { git = "https://github.com/canonical/data-platform-workflows", tag = "v26.0.0", subdirectory = "python/pytest_plugins/pytest_operator_cache" }
9794
# To be enabled if we are using groups on integration tests
9895
# pytest-operator-groups = {git = "https://github.com/canonical/data-platform-workflows", tag = "v6.1.1", subdirectory = "python/pytest_plugins/pytest_operator_groups"}
99-
pytest-microceph = { git = "https://github.com/canonical/data-platform-workflows", tag = "v24.0.5", subdirectory = "python/pytest_plugins/microceph" }
96+
pytest-microceph = { git = "https://github.com/canonical/data-platform-workflows", tag = "v26.0.0", subdirectory = "python/pytest_plugins/microceph" }
10097

10198
[tool.ruff]
10299
line-length = 99

tests/integration/app-charm/charmcraft.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
# See LICENSE file for licensing details.
33

44
type: charm
5-
bases:
6-
- build-on:
7-
- name: "ubuntu"
8-
channel: "22.04"
9-
run-on:
10-
- name: "ubuntu"
11-
channel: "22.04"
5+
platforms:
6+
ubuntu@22.04:amd64:
7+
parts:
8+
charm:
9+
plugin: charm

tox.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ commands =
7575

7676
[testenv:integration-{charm,provider,password-rotation,tls,upgrade,backup,ha,replication,scaling}]
7777
description = Run integration tests
78-
set_env =
79-
{[testenv]set_env}
80-
# Workaround for https://github.com/python-poetry/poetry/issues/6958
81-
POETRY_INSTALLER_PARALLEL = false
8278
pass_env =
8379
{[testenv]pass_env}
8480
CI

0 commit comments

Comments
 (0)