| 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() |
| Adam Israel | b094366 | 2018-08-02 15:32:00 -0400 | [diff] [blame] | 43 | # connect to current model with current user, per Juju CLI |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 44 | await model.connect() |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 45 | |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 46 | try: |
| 47 | model.add_observer(MyRemoveObserver()) |
| 48 | await model.reset(force=True) |
| 49 | model.add_observer(MyModelObserver()) |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 50 | |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 51 | ubuntu_app = await model.deploy( |
| 52 | 'ubuntu', |
| 53 | application_name='ubuntu', |
| 54 | series='trusty', |
| 55 | channel='stable', |
| 56 | ) |
| 57 | ubuntu_app.on_change(asyncio.coroutine( |
| 58 | lambda delta, old_app, new_app, model: |
| 59 | print('App changed: {}'.format(new_app.entity_id)) |
| 60 | )) |
| 61 | ubuntu_app.on_remove(asyncio.coroutine( |
| 62 | lambda delta, old_app, new_app, model: |
| 63 | print('App removed: {}'.format(old_app.entity_id)) |
| 64 | )) |
| 65 | ubuntu_app.on_unit_add(asyncio.coroutine( |
| 66 | lambda delta, old_unit, new_unit, model: |
| 67 | print('Unit added: {}'.format(new_unit.entity_id)) |
| 68 | )) |
| 69 | ubuntu_app.on_unit_remove(asyncio.coroutine( |
| 70 | lambda delta, old_unit, new_unit, model: |
| 71 | print('Unit removed: {}'.format(old_unit.entity_id)) |
| 72 | )) |
| 73 | unit_a, unit_b = await ubuntu_app.add_units(count=2) |
| 74 | unit_a.on_change(asyncio.coroutine( |
| 75 | lambda delta, old_unit, new_unit, model: |
| 76 | print('Unit changed: {}'.format(new_unit.entity_id)) |
| 77 | )) |
| 78 | await model.deploy( |
| 79 | 'nrpe', |
| 80 | application_name='nrpe', |
| 81 | series='trusty', |
| 82 | channel='stable', |
| 83 | # subordinates must be deployed without units |
| 84 | num_units=0, |
| 85 | ) |
| 86 | my_relation = await model.add_relation( |
| 87 | 'ubuntu', |
| 88 | 'nrpe', |
| 89 | ) |
| 90 | my_relation.on_remove(asyncio.coroutine( |
| 91 | lambda delta, old_rel, new_rel, model: |
| 92 | print('Relation removed: {}'.format(old_rel.endpoints)) |
| 93 | )) |
| 94 | finally: |
| 95 | await model.disconnect() |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 96 | |
| 97 | |
| 98 | if __name__ == '__main__': |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 99 | logging.basicConfig(level=logging.INFO) |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 100 | ws_logger = logging.getLogger('websockets.protocol') |
| 101 | ws_logger.setLevel(logging.INFO) |
| 102 | loop.run(main()) |