4 1. Connects to the current model
6 3. Deploys two charms and relates them
7 4. Waits for units to be idle, then exits
13 from juju
.model
import Model
, ModelObserver
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)
25 assert(new
.previous().dead
)
26 assert(not new
.previous().current
)
27 assert(not new
.previous().connected
)
30 class MyModelObserver(ModelObserver
):
31 _shutting_down
= False
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()
43 # connect to current model with current user, per Juju CLI
47 model
.add_observer(MyRemoveObserver())
48 await model
.reset(force
=True)
49 model
.add_observer(MyModelObserver())
51 ubuntu_app
= await model
.deploy(
53 application_name
='ubuntu',
57 ubuntu_app
.on_change(asyncio
.coroutine(
58 lambda delta
, old_app
, new_app
, model
:
59 print('App changed: {}'.format(new_app
.entity_id
))
61 ubuntu_app
.on_remove(asyncio
.coroutine(
62 lambda delta
, old_app
, new_app
, model
:
63 print('App removed: {}'.format(old_app
.entity_id
))
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
))
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
))
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
))
80 application_name
='nrpe',
83 # subordinates must be deployed without units
86 my_relation
= await model
.add_relation(
90 my_relation
.on_remove(asyncio
.coroutine(
91 lambda delta
, old_rel
, new_rel
, model
:
92 print('Relation removed: {}'.format(old_rel
.endpoints
))
95 await model
.disconnect()
98 if __name__
== '__main__':
99 logging
.basicConfig(level
=logging
.INFO
)
100 ws_logger
= logging
.getLogger('websockets.protocol')
101 ws_logger
.setLevel(logging
.INFO
)