6dbb1b6d5d4314b1034da791631ef83e9a042197
2 Run this one against a model that has at least one unit deployed.
8 from juju
.model
import Model
9 from juju
.unit
import Unit
10 from juju
.client
.connection
import Connection
13 loop
= asyncio
.get_event_loop()
14 conn
= loop
.run_until_complete(Connection
.connect_current())
19 async def run_stuff_on_unit(unit
):
20 if unit
.name
in _seen_units
:
23 print('Running command on unit', unit
.name
)
24 # unit.run() returns a client.ActionResults instance
25 action_results
= await unit
.run('unit-get public-address')
26 _seen_units
.add(unit
.name
)
27 action_result
= action_results
.results
[0]
29 print('Results from unit', unit
.name
)
30 print(action_result
.__dict
__)
33 def on_model_change(delta
, old
, new
, model
):
34 if isinstance(new
, Unit
):
35 task
= loop
.create_task(run_stuff_on_unit(new
))
37 if delta
.entity
== 'action':
42 async def watch_model():
44 model
.add_observer(on_model_change
)
47 loop
.run_until_complete(watch_model())