9d4e5d77a0a94f0bedc9d06e7e6ca3c0c2eba22d
[osm/N2VC.git] / examples / unitrun.py
1 """
2 This example:
3
4 1. Connects to current model and resets it.
5 2. Deploys one ubuntu 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_command(unit):
17 logging.debug('Running command on unit %s', unit.name)
18
19 # unit.run() returns a juju.action.Action instance
20 action = await unit.run('unit-get public-address')
21 logging.debug("Action results: %s", action.results)
22
23
24 async def run():
25 model = Model()
26 await model.connect_current()
27 await model.reset(force=True)
28
29 app = await model.deploy(
30 'ubuntu-0',
31 application_name='ubuntu',
32 series='trusty',
33 channel='stable',
34 )
35
36 for unit in app.units:
37 await run_command(unit)
38
39 await model.disconnect()
40 model.loop.stop()
41
42
43 logging.basicConfig(level=logging.DEBUG)
44 ws_logger = logging.getLogger('websockets.protocol')
45 ws_logger.setLevel(logging.INFO)
46 loop = asyncio.get_event_loop()
47 loop.set_debug(False)
48 loop.create_task(run())
49 loop.run_forever()