| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame^] | 1 | """ |
| 2 | This example: |
| 3 | |
| 4 | 1. Connects to the current model |
| 5 | 2. Resets it |
| 6 | 3. Deploys two charms and relates them |
| 7 | 4. Waits for units to be idle, then exits |
| 8 | |
| 9 | """ |
| 10 | import asyncio |
| 11 | import logging |
| 12 | |
| 13 | from juju.model import Model, ModelObserver |
| 14 | from juju import loop |
| 15 | |
| 16 | |
| 17 | class MyRemoveObserver(ModelObserver): |
| 18 | async def on_change(self, delta, old, new, model): |
| 19 | if delta.type == 'remove': |
| 20 | assert(new.latest() == new) |
| 21 | assert(new.next() is None) |
| 22 | assert(new.dead) |
| 23 | assert(new.current) |
| 24 | assert(new.connected) |
| 25 | assert(new.previous().dead) |
| 26 | assert(not new.previous().current) |
| 27 | assert(not new.previous().connected) |
| 28 | |
| 29 | |
| 30 | class MyModelObserver(ModelObserver): |
| 31 | _shutting_down = False |
| 32 | |
| 33 | async def on_change(self, delta, old, new, model): |
| 34 | if model.units and model.all_units_idle() and not self._shutting_down: |
| 35 | self._shutting_down = True |
| 36 | logging.debug('All units idle, disconnecting') |
| 37 | await model.reset(force=True) |
| 38 | await model.disconnect() |
| 39 | |
| 40 | |
| 41 | async def main(): |
| 42 | model = Model() |
| 43 | await model.connect_current() |
| 44 | |
| 45 | model.add_observer(MyRemoveObserver()) |
| 46 | await model.reset(force=True) |
| 47 | model.add_observer(MyModelObserver()) |
| 48 | |
| 49 | ubuntu_app = await model.deploy( |
| 50 | 'ubuntu', |
| 51 | application_name='ubuntu', |
| 52 | series='trusty', |
| 53 | channel='stable', |
| 54 | ) |
| 55 | ubuntu_app.on_change(asyncio.coroutine( |
| 56 | lambda delta, old_app, new_app, model: |
| 57 | print('App changed: {}'.format(new_app.entity_id)) |
| 58 | )) |
| 59 | ubuntu_app.on_remove(asyncio.coroutine( |
| 60 | lambda delta, old_app, new_app, model: |
| 61 | print('App removed: {}'.format(old_app.entity_id)) |
| 62 | )) |
| 63 | ubuntu_app.on_unit_add(asyncio.coroutine( |
| 64 | lambda delta, old_unit, new_unit, model: |
| 65 | print('Unit added: {}'.format(new_unit.entity_id)) |
| 66 | )) |
| 67 | ubuntu_app.on_unit_remove(asyncio.coroutine( |
| 68 | lambda delta, old_unit, new_unit, model: |
| 69 | print('Unit removed: {}'.format(old_unit.entity_id)) |
| 70 | )) |
| 71 | unit_a, unit_b = await ubuntu_app.add_units(count=2) |
| 72 | unit_a.on_change(asyncio.coroutine( |
| 73 | lambda delta, old_unit, new_unit, model: |
| 74 | print('Unit changed: {}'.format(new_unit.entity_id)) |
| 75 | )) |
| 76 | await model.deploy( |
| 77 | 'nrpe', |
| 78 | application_name='nrpe', |
| 79 | series='trusty', |
| 80 | channel='stable', |
| 81 | # subordinates must be deployed without units |
| 82 | num_units=0, |
| 83 | ) |
| 84 | my_relation = await model.add_relation( |
| 85 | 'ubuntu', |
| 86 | 'nrpe', |
| 87 | ) |
| 88 | my_relation.on_remove(asyncio.coroutine( |
| 89 | lambda delta, old_rel, new_rel, model: |
| 90 | print('Relation removed: {}'.format(old_rel.endpoints)) |
| 91 | )) |
| 92 | |
| 93 | |
| 94 | if __name__ == '__main__': |
| 95 | logging.basicConfig(level=logging.DEBUG) |
| 96 | ws_logger = logging.getLogger('websockets.protocol') |
| 97 | ws_logger.setLevel(logging.INFO) |
| 98 | loop.run(main()) |