-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathrelate.py
122 lines (106 loc) · 3.47 KB
/
relate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright 2023 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.
"""This example:
1. Connects to the current model
2. Resets it
3. Deploys two charms and relates them
4. Waits for units to be idle, then exits
"""
import asyncio
import logging
from juju.model import Model, ModelObserver
class MyRemoveObserver(ModelObserver):
async def on_change(self, delta, old, new, model):
if delta.type == "remove":
assert new.latest() == new
assert new.next() is None
assert new.dead
assert new.current
assert new.connected
assert new.previous().dead
assert not new.previous().current
assert not new.previous().connected
class MyModelObserver(ModelObserver):
_shutting_down = False
async def on_change(self, delta, old, new, model):
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()
async def main():
model = Model()
# connect to current model with current user, per Juju CLI
await model.connect()
try:
model.add_observer(MyRemoveObserver())
await model.reset(force=True)
model.add_observer(MyModelObserver())
ubuntu_app = await model.deploy(
"ch:ubuntu",
application_name="ubuntu",
series="trusty",
channel="stable",
)
ubuntu_app.on_change(
asyncio.coroutine(
lambda delta, old_app, new_app, model: print(
f"App changed: {new_app.entity_id}"
)
)
)
ubuntu_app.on_remove(
asyncio.coroutine(
lambda delta, old_app, new_app, model: print(
f"App removed: {old_app.entity_id}"
)
)
)
ubuntu_app.on_unit_add(
asyncio.coroutine(
lambda delta, old_unit, new_unit, model: print(
f"Unit added: {new_unit.entity_id}"
)
)
)
ubuntu_app.on_unit_remove(
asyncio.coroutine(
lambda delta, old_unit, new_unit, model: print(
f"Unit removed: {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(
f"Unit changed: {new_unit.entity_id}"
)
)
)
await model.deploy(
"ch:nrpe",
application_name="nrpe",
series="trusty",
channel="stable",
# subordinates must be deployed without units
num_units=0,
)
my_relation = await model.relate(
"ubuntu",
"nrpe",
)
my_relation.on_remove(
asyncio.coroutine(
lambda delta, old_rel, new_rel, model: print(
f"Relation removed: {old_rel.endpoints}"
)
)
)
finally:
await model.disconnect()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
ws_logger = logging.getLogger("websockets.protocol")
ws_logger.setLevel(logging.INFO)
asyncio.run(main())