Clean up deploy example
authorTim Van Steenburgh <tvansteenburgh@gmail.com>
Thu, 8 Sep 2016 23:04:27 +0000 (19:04 -0400)
committerTim Van Steenburgh <tvansteenburgh@gmail.com>
Thu, 8 Sep 2016 23:04:27 +0000 (19:04 -0400)
examples/deploy.py
juju/model.py

index 5c1aae5..a1ae503 100644 (file)
@@ -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
 
 """
 import asyncio
@@ -9,11 +9,6 @@ from juju.model import Model, ModelObserver
 from juju.client.connection import Connection
 
 
 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(
 class MyModelObserver(ModelObserver):
     def on_unit_add(self, delta, old, new, model):
         logging.info(
@@ -39,6 +34,8 @@ class MyModelObserver(ModelObserver):
 
 
 async def run():
 
 
 async def run():
+    conn = await Connection.connect_current()
+    model = Model(conn)
     model.add_observer(MyModelObserver())
     await model.deploy(
         'ubuntu-0',
     model.add_observer(MyModelObserver())
     await model.deploy(
         'ubuntu-0',
@@ -48,6 +45,7 @@ async def run():
     )
     await model.watch()
 
     )
     await model.watch()
 
+
 logging.basicConfig(level=logging.INFO)
 logging.basicConfig(level=logging.INFO)
-loop.create_task(run())
-loop.run_forever()
+loop = asyncio.get_event_loop()
+loop.run_until_complete(run())
index 480813c..e56bfb4 100644 (file)
@@ -53,7 +53,6 @@ class Model(object):
         self.connection = connection
         self.observers = set()
         self.state = dict()
         self.connection = connection
         self.observers = set()
         self.state = dict()
-        self._watching = False
 
     @property
     def applications(self):
 
     @property
     def applications(self):
@@ -63,10 +62,6 @@ class Model(object):
     def units(self):
         return self.state.get('unit', {})
 
     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
 
     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())
         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)
             results = await allwatcher.Next()
             for delta in results.deltas:
                 delta = get_entity_delta(delta)