First stabs with libjuju, discuss

I’m starting exploring libjuju to try to automate some activities and tests. I’ve failed with this some years ago, but I thought to pick it up again.

I’d like to write an application that:

  1. Connects to a named controller.
  2. Creates a model.
  3. Deploys a charm.
  4. Runs an action on the charm.
  5. Validates the output from the action.
  6. Removes the charm and model.

I’ve not managed to do it just yet.

I’ve only just started, but its kind of challenging and I’ll use this thread to communicate my efforts and spawn a discussion.

My first piece of code looks like this, taken from documentation. (ChatGPT did’t produce useful code as it didn’t have data from later versions)

#!/usr/bin/python3

import logging
import sys
from juju import loop
from juju.model import Model

from juju.controller import Controller


async def deploy():

    # Create controller
    controller = Controller()

    # Connect to my named controller 'iceberg'
    await controller.connect(controller_name='iceberg')

    # Add new model
    model = await controller.add_model(
      'mymodel',  
    )

    # Connect to the model
    await model.connect('mymodel')

    try:
        # Deploy the charm
        ubuntu_app = await model.deploy(
          'tiny-bash',
          application_name='tiny-bash',
          series='jammy',
          channel='stable',
        )

        if '--wait' in sys.argv:
            # optionally block until the application is ready
            await model.block_until(lambda: ubuntu_app.status == 'active')
    finally:
        # Disconnect from the api server and cleanup.
        await model.disconnect()


def main():
    logging.basicConfig(level=logging.INFO)

    # If you want to see everything sent over the wire, set this to DEBUG.
    ws_logger = logging.getLogger('websockets.protocol')
    ws_logger.setLevel(logging.INFO)

    # Run the deploy coroutine in an asyncio event loop, using a helper
    # that abstracts loop creation and teardown.
    loop.run(deploy())


if __name__ == '__main__':
    main()

This is what I encounter:

juju.errors.JujuConnectionError: juju server-version 2.9.38 not supported

Q: What version of libjuju should I use?

/home/erik/libjuju/venv/lib/python3.10/site-packages/juju/loop.py:4: UserWarning: juju.loop module is being deprecated by 3.0, use juju.jasyncio instead

Q: The documentation mentions nothing about how to use this deprecation and the examples are not using it. It would be useful to tell a user what version of the lib should be used with which controller setup?

Q: The documentation should ideally point to “working examples”. I’m sure I can make it out from digging in code etc. but its not that pleasant.

@tiradojm @tmihoc @awnns @ppasotti @joakimnyman

[Update] So, it seems libjuju is only supported on a juju 3? @tiradojm

In order to connect to a Juju 2.9.38 controller you have to use a python-libjuju from version 2.9.x. After the release of Juju 3.x python-libjuju major versioning must match for python-libjuju to successfully connect to the controller. In your case python-libjuju 2.9.42.1 should work

1 Like