Clean up deploy example
[osm/N2VC.git] / examples / deploy.py
1 """
2 Deploy a charm, wait until it's idle, then destroy the unit and application.
3
4 """
5 import asyncio
6 import logging
7
8 from juju.model import Model, ModelObserver
9 from juju.client.connection import Connection
10
11
12 class MyModelObserver(ModelObserver):
13 def on_unit_add(self, delta, old, new, model):
14 logging.info(
15 'New unit added: %s', new.name)
16
17 def on_change(self, delta, old, new, model):
18 for unit in model.units.values():
19 unit_status = unit.data['agent-status']['current']
20 logging.info(
21 'Unit %s status: %s', unit.name, unit_status)
22 if unit_status == 'idle':
23 logging.info(
24 'Destroying unit %s', unit.name)
25 loop.create_task(unit.destroy())
26
27 def on_unit_remove(self, delta, old, new, model):
28 app_name = old.application
29 app = model.applications[app_name]
30 if not app.units:
31 logging.info(
32 'Destroying application %s', app.name)
33 loop.create_task(app.destroy())
34
35
36 async def run():
37 conn = await Connection.connect_current()
38 model = Model(conn)
39 model.add_observer(MyModelObserver())
40 await model.deploy(
41 'ubuntu-0',
42 service_name='ubuntu',
43 series='trusty',
44 channel='stable',
45 )
46 await model.watch()
47
48
49 logging.basicConfig(level=logging.INFO)
50 loop = asyncio.get_event_loop()
51 loop.run_until_complete(run())