X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=examples%2Frelate.py;h=1952ffea06afc6e44ff31050cc381e4668838adc;hb=4c1f68ce9871fa965fe9683aeeb5ddd66b42a5bc;hp=bdfb2d7814d84aaa3c247087410348c069fbf0cf;hpb=46887b86181254efd72f59a8d6be468342afad83;p=osm%2FN2VC.git diff --git a/examples/relate.py b/examples/relate.py index bdfb2d7..1952ffe 100644 --- a/examples/relate.py +++ b/examples/relate.py @@ -27,9 +27,13 @@ class MyRemoveObserver(ModelObserver): class MyModelObserver(ModelObserver): + _shutting_down = False + async def on_change(self, delta, old, new, model): - if model.all_units_idle(): + if model.units and model.all_units_idle() and not self._shutting_down: + self._shutting_down = True logging.debug('All units idle, disconnecting') + await model.reset(force=True) await model.disconnect() model.loop.stop() @@ -42,23 +46,48 @@ async def run(): await model.reset(force=True) model.add_observer(MyModelObserver()) - await model.deploy( - 'ubuntu-0', + ubuntu_app = await model.deploy( + 'ubuntu', service_name='ubuntu', series='trusty', channel='stable', ) + ubuntu_app.on_change(asyncio.coroutine( + lambda delta, old_app, new_app, model: + print('App changed: {}'.format(new_app.entity_id)) + )) + ubuntu_app.on_remove(asyncio.coroutine( + lambda delta, old_app, new_app, model: + print('App removed: {}'.format(old_app.entity_id)) + )) + ubuntu_app.on_unit_add(asyncio.coroutine( + lambda delta, old_unit, new_unit, model: + print('Unit added: {}'.format(new_unit.entity_id)) + )) + ubuntu_app.on_unit_remove(asyncio.coroutine( + lambda delta, old_unit, new_unit, model: + print('Unit removed: {}'.format(old_unit.entity_id)) + )) + unit_a, unit_b = await ubuntu_app.add_units(count=2) + unit_a.on_change(asyncio.coroutine( + lambda delta, old_unit, new_unit, model: + print('Unit changed: {}'.format(new_unit.entity_id)) + )) await model.deploy( - 'nrpe-11', + 'nrpe', service_name='nrpe', series='trusty', channel='stable', num_units=0, ) - await model.add_relation( + my_relation = await model.add_relation( 'ubuntu', 'nrpe', ) + my_relation.on_remove(asyncio.coroutine( + lambda delta, old_rel, new_rel, model: + print('Relation removed: {}'.format(old_rel.endpoints)) + )) logging.basicConfig(level=logging.DEBUG) ws_logger = logging.getLogger('websockets.protocol')