Add action example and docs
[osm/N2VC.git] / 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.model import Model
14
15
16 async def run_action(unit):
17 logging.debug('Running action on unit %s', unit.name)
18
19 # unit.run() returns a juju.action.Action instance
20 action = await unit.run_action('add-repo', repo='myrepo')
21 # wait for the action to complete
22 action = await action.wait()
23
24 logging.debug("Action results: %s", action.results)
25
26
27 async def run():
28 model = Model()
29 await model.connect_current()
30 await model.reset(force=True)
31
32 app = await model.deploy(
33 'git',
34 application_name='git',
35 series='trusty',
36 channel='stable',
37 )
38
39 for unit in app.units:
40 await run_action(unit)
41
42 await model.disconnect()
43 model.loop.stop()
44
45
46 logging.basicConfig(level=logging.DEBUG)
47 ws_logger = logging.getLogger('websockets.protocol')
48 ws_logger.setLevel(logging.INFO)
49 loop = asyncio.get_event_loop()
50 loop.set_debug(False)
51 loop.create_task(run())
52 loop.run_forever()