From: Tim Van Steenburgh Date: Thu, 8 Sep 2016 23:04:27 +0000 (-0400) Subject: Clean up deploy example X-Git-Tag: 0.1.0~85 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;ds=sidebyside;h=6a1c86e07844295a8a71d7c1935c35a8058fe15a;p=osm%2FN2VC.git Clean up deploy example --- diff --git a/examples/deploy.py b/examples/deploy.py index 5c1aae5..a1ae503 100644 --- a/examples/deploy.py +++ b/examples/deploy.py @@ -1,5 +1,5 @@ """ -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 @@ -9,11 +9,6 @@ from juju.model import Model, ModelObserver 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( @@ -39,6 +34,8 @@ class MyModelObserver(ModelObserver): async def run(): + conn = await Connection.connect_current() + model = Model(conn) model.add_observer(MyModelObserver()) await model.deploy( 'ubuntu-0', @@ -48,6 +45,7 @@ async def run(): ) 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()) diff --git a/juju/model.py b/juju/model.py index 480813c..e56bfb4 100644 --- a/juju/model.py +++ b/juju/model.py @@ -53,7 +53,6 @@ class Model(object): self.connection = connection self.observers = set() self.state = dict() - self._watching = False @property def applications(self): @@ -63,10 +62,6 @@ class Model(object): 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 @@ -101,7 +96,7 @@ class Model(object): 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)