blob: 886550d0c151f51f5bed30eb64b84a0d5c68eaff [file] [log] [blame]
Adam Israeldcdf82b2017-08-15 15:26:43 -04001A Python library for Juju
2=========================
3
4Source code: https://github.com/juju/python-libjuju
5
6Bug reports: https://github.com/juju/python-libjuju/issues
7
8Documentation: https://pythonlibjuju.readthedocs.io/en/latest/
9
10
11Requirements
12------------
13
14* Python 3.5+
15* Juju 2.0+
16
17
18Design 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
29Installation
30------------
31
32.. code:: bash
33
34 pip3 install juju
35
36
37Quickstart
38----------
39Here's a simple example that shows basic usage of the library. The example
40connects to the currently active Juju model, deploys a single unit of the
41ubuntu charm, then exits.
42
43More examples can be found in the `examples/` directory of the source tree,
44and in the documentation.
45
46
47.. code:: python
48
Adam Israelc3e6c2e2018-03-01 09:31:50 -050049 #!/usr/bin/python3
Adam Israeldcdf82b2017-08-15 15:26:43 -040050
Adam Israeldcdf82b2017-08-15 15:26:43 -040051 import logging
Adam Israelc3e6c2e2018-03-01 09:31:50 -050052 import sys
Adam Israeldcdf82b2017-08-15 15:26:43 -040053
54 from juju import loop
55 from juju.model import Model
56
57
58 async def deploy():
59 # Create a Model instance. We need to connect our Model to a Juju api
60 # server before we can use it.
61 model = Model()
62
63 # Connect to the currently active Juju model
64 await model.connect_current()
65
Adam Israelc3e6c2e2018-03-01 09:31:50 -050066 try:
67 # Deploy a single unit of the ubuntu charm, using the latest revision
68 # from the stable channel of the Charm Store.
69 ubuntu_app = await model.deploy(
70 'ubuntu',
71 application_name='ubuntu',
72 series='xenial',
73 channel='stable',
74 )
Adam Israeldcdf82b2017-08-15 15:26:43 -040075
Adam Israelc3e6c2e2018-03-01 09:31:50 -050076 if '--wait' in sys.argv:
77 # optionally block until the application is ready
78 await model.block_until(lambda: ubuntu_app.status == 'active')
79 finally:
80 # Disconnect from the api server and cleanup.
81 await model.disconnect()
Adam Israeldcdf82b2017-08-15 15:26:43 -040082
83
84 def main():
Adam Israelc3e6c2e2018-03-01 09:31:50 -050085 logging.basicConfig(level=logging.INFO)
Adam Israeldcdf82b2017-08-15 15:26:43 -040086
Adam Israelc3e6c2e2018-03-01 09:31:50 -050087 # If you want to see everything sent over the wire, set this to DEBUG.
Adam Israeldcdf82b2017-08-15 15:26:43 -040088 ws_logger = logging.getLogger('websockets.protocol')
89 ws_logger.setLevel(logging.INFO)
90
91 # Run the deploy coroutine in an asyncio event loop, using a helper
92 # that abstracts loop creation and teardown.
93 loop.run(deploy())
94
95
96 if __name__ == '__main__':
97 main()