Charm-lib-template: A template repository for charm libs

Some time ago I started writing a generic charm library that did not belong to any specific charm. I created a ‘placeholder’ charm repository with charmcraft init and started coding. As the project got bigger, I realised I needed lib-specific tests, CI, and wrote a few scripts to automate the bump-version-and-release flow.

Then I thought that this setup could be useful to more projects, and so here we are.

Welcome to charm-lib-template, a template to create repositories for charm libraries.

How does it work?

You create a new repo for your library, using charm-lib-template as a template.

You run scripts/init.sh <CHARM_NAME> <LIB_NAME> [<LIB_VERSION>]. <CHARM_NAME> will be the name of a placeholder charm we use to store the lib within charmhub. As such, there will be no src/charm.py (there isn’t even a src folder!).

You write your library code in <LIB_NAME>.py under the project root, add your tests in /tests, set up your tox env as you please and configure your CI.

The __version__.py file (don’t edit it manually) contains the version and revision of the library, and the other ‘charmhub’ metadata charmcraft needs are stored in lib_template.jinja. In general you should not need to interact with those manually.

When you’re ready to publish the library, you can run scripts/update.sh. The script will bump the revision, temporarily copy the library to where it needs to be (lib.charms.<CHARM_NAME>.v0.<LIB_NAME>.py), run charmcraft publish-lib ...and then cleanup. Easy!

If you want to see an example project using this setup: https://github.com/PietroPasotti/relation-wrapper

The Future

If there’s enough traction for this, we might make this a charmcraft init profile; so that you can write charmcraft init --profile=charm-lib and get a charm-lib-template structure. So please let us know what you think, try it out, and submit any issues or feature requests you may have.

2 Likes