5c1aae58e80547bbfe3f578ff7459cdcaadc8263
2 Run this one against a model that has at least one unit deployed.
8 from juju
.model
import Model
, ModelObserver
9 from juju
.client
.connection
import Connection
12 loop
= asyncio
.get_event_loop()
13 conn
= loop
.run_until_complete(Connection
.connect_current())
17 class MyModelObserver(ModelObserver
):
18 def on_unit_add(self
, delta
, old
, new
, model
):
20 'New unit added: %s', new
.name
)
22 def on_change(self
, delta
, old
, new
, model
):
23 for unit
in model
.units
.values():
24 unit_status
= unit
.data
['agent-status']['current']
26 'Unit %s status: %s', unit
.name
, unit_status
)
27 if unit_status
== 'idle':
29 'Destroying unit %s', unit
.name
)
30 loop
.create_task(unit
.destroy())
32 def on_unit_remove(self
, delta
, old
, new
, model
):
33 app_name
= old
.application
34 app
= model
.applications
[app_name
]
37 'Destroying application %s', app
.name
)
38 loop
.create_task(app
.destroy())
42 model
.add_observer(MyModelObserver())
45 service_name
='ubuntu',
51 logging
.basicConfig(level
=logging
.INFO
)
52 loop
.create_task(run())