"""
-Run this one against a model that has at least one unit deployed.
+Deploy a charm, wait until it's idle, then destroy the unit and application.
"""
import asyncio
from juju.client.connection import Connection
-loop = asyncio.get_event_loop()
-conn = loop.run_until_complete(Connection.connect_current())
-model = Model(conn)
-
-
class MyModelObserver(ModelObserver):
def on_unit_add(self, delta, old, new, model):
logging.info(
async def run():
+ conn = await Connection.connect_current()
+ model = Model(conn)
model.add_observer(MyModelObserver())
await model.deploy(
'ubuntu-0',
)
await model.watch()
+
logging.basicConfig(level=logging.INFO)
-loop.create_task(run())
-loop.run_forever()
+loop = asyncio.get_event_loop()
+loop.run_until_complete(run())
self.connection = connection
self.observers = set()
self.state = dict()
- self._watching = False
@property
def applications(self):
def units(self):
return self.state.get('unit', {})
- def stop_watching(self):
- self.connection.cancel()
- self._watching = False
-
def add_observer(self, callable_):
"""Register an "on-model-change" callback
self._watching = True
allwatcher = watcher.AllWatcher()
allwatcher.connect(await self.connection.clone())
- while self._watching:
+ while True:
results = await allwatcher.Next()
for delta in results.deltas:
delta = get_entity_delta(delta)