| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 1 | A Python library for Juju |
| 2 | ========================= |
| 3 | |
| 4 | Source code: https://github.com/juju/python-libjuju |
| 5 | |
| 6 | Bug reports: https://github.com/juju/python-libjuju/issues |
| 7 | |
| 8 | Documentation: https://pythonlibjuju.readthedocs.io/en/latest/ |
| 9 | |
| 10 | |
| 11 | Requirements |
| 12 | ------------ |
| 13 | |
| 14 | * Python 3.5+ |
| 15 | * Juju 2.0+ |
| 16 | |
| 17 | |
| 18 | Design Notes |
| 19 | ------------ |
| 20 | |
| 21 | * Asynchronous - uses asyncio and async/await features of python 3.5 |
| 22 | * Websocket-level bindings are programmatically generated (indirectly) from the |
| 23 | Juju golang code, ensuring full api coverage |
| 24 | * Provides an OO layer which encapsulates much of the websocket api and |
| 25 | provides familiar nouns and verbs (e.g. Model.deploy(), Application.add_unit(), |
| 26 | etc.) |
| 27 | |
| 28 | |
| 29 | Installation |
| 30 | ------------ |
| 31 | |
| 32 | .. code:: bash |
| 33 | |
| 34 | pip3 install juju |
| 35 | |
| 36 | |
| 37 | Quickstart |
| 38 | ---------- |
| 39 | Here's a simple example that shows basic usage of the library. The example |
| 40 | connects to the currently active Juju model, deploys a single unit of the |
| Adam Israel | b094366 | 2018-08-02 15:32:00 -0400 | [diff] [blame] | 41 | ubuntu charm, then exits: |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 42 | |
| 43 | |
| 44 | .. code:: python |
| 45 | |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 46 | #!/usr/bin/python3 |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 47 | |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 48 | import logging |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 49 | import sys |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 50 | |
| 51 | from juju import loop |
| 52 | from juju.model import Model |
| 53 | |
| 54 | |
| 55 | async def deploy(): |
| 56 | # Create a Model instance. We need to connect our Model to a Juju api |
| 57 | # server before we can use it. |
| 58 | model = Model() |
| 59 | |
| 60 | # Connect to the currently active Juju model |
| 61 | await model.connect_current() |
| 62 | |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 63 | try: |
| 64 | # Deploy a single unit of the ubuntu charm, using the latest revision |
| 65 | # from the stable channel of the Charm Store. |
| 66 | ubuntu_app = await model.deploy( |
| 67 | 'ubuntu', |
| 68 | application_name='ubuntu', |
| 69 | series='xenial', |
| 70 | channel='stable', |
| 71 | ) |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 72 | |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 73 | if '--wait' in sys.argv: |
| 74 | # optionally block until the application is ready |
| 75 | await model.block_until(lambda: ubuntu_app.status == 'active') |
| 76 | finally: |
| 77 | # Disconnect from the api server and cleanup. |
| 78 | await model.disconnect() |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def main(): |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 82 | logging.basicConfig(level=logging.INFO) |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 83 | |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 84 | # If you want to see everything sent over the wire, set this to DEBUG. |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 85 | ws_logger = logging.getLogger('websockets.protocol') |
| 86 | ws_logger.setLevel(logging.INFO) |
| 87 | |
| 88 | # Run the deploy coroutine in an asyncio event loop, using a helper |
| 89 | # that abstracts loop creation and teardown. |
| 90 | loop.run(deploy()) |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | main() |
| Adam Israel | b094366 | 2018-08-02 15:32:00 -0400 | [diff] [blame] | 95 | |
| 96 | |
| 97 | More examples can be found in the docs, as well as in the ``examples/`` |
| 98 | directory of the source tree which can be run using ``tox``. For |
| 99 | example, to run ``examples/connect_current_model.py``, use: |
| 100 | |
| 101 | .. code:: bash |
| 102 | |
| 103 | tox -e example -- examples/connect_current_model.py |