s/service_name/application_name/g
[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, ModelObserver
14
15 async def run_stuff_on_unit(unit):
16 print('Running command on unit', unit.name)
17
18 # unit.run() returns a client.ActionResults instance
19 action = await unit.run('unit-get public-address')
20
21 print("Action results: {}".format(action.results))
22
23 # Inform asyncio that we're done.
24 await unit.model.disconnect()
25 unit.model.loop.stop()
26
27
28 class MyModelObserver(ModelObserver):
29 async def on_unit_add(self, delta, old, new, model):
30 loop.create_task(run_stuff_on_unit(new))
31
32
33 async def run():
34 model = Model()
35 await model.connect_current()
36 await model.reset(force=True)
37 model.add_observer(MyModelObserver())
38
39 await model.deploy(
40 'ubuntu-0',
41 application_name='ubuntu',
42 series='trusty',
43 channel='stable',
44 )
45
46
47 logging.basicConfig(level=logging.DEBUG)
48 ws_logger = logging.getLogger('websockets.protocol')
49 ws_logger.setLevel(logging.INFO)
50 loop = asyncio.get_event_loop()
51 loop.set_debug(False)
52 loop.create_task(run())
53 loop.run_forever()