Merge commit '19031b24b523c872c1ac367821dc60c950a09755' as 'modules/libjuju'
[osm/N2VC.git] / modules / libjuju / examples / action.py
1 """
2 This example:
3
4 1. Connects to current model and resets it.
5 2. Deploys a git unit.
6 3. Runs an action against the unit.
7 4. Waits for the action results to come back, then exits.
8
9 """
10 import asyncio
11 import logging
12
13 from juju import loop
14 from juju.model import Model
15
16
17 async def run_action(unit):
18 logging.debug('Running action on unit %s', unit.name)
19
20 # unit.run() returns a juju.action.Action instance
21 action = await unit.run_action('add-repo', repo='myrepo')
22 # wait for the action to complete
23 action = await action.wait()
24
25 logging.debug("Action results: %s", action.results)
26
27
28 async def main():
29 model = Model()
30 await model.connect_current()
31 await model.reset(force=True)
32
33 app = await model.deploy(
34 'git',
35 application_name='git',
36 series='trusty',
37 channel='stable',
38 )
39
40 for unit in app.units:
41 await run_action(unit)
42
43 await model.disconnect()
44
45
46 if __name__ == '__main__':
47 logging.basicConfig(level=logging.DEBUG)
48 ws_logger = logging.getLogger('websockets.protocol')
49 ws_logger.setLevel(logging.INFO)
50 loop.run(main())