projects
/
osm
/
N2VC.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Updated examples to use juju.loop
[osm/N2VC.git]
/
examples
/
unitrun.py
diff --git
a/examples/unitrun.py
b/examples/unitrun.py
index
d283e36
..
3dfacd6
100644
(file)
--- a/
examples/unitrun.py
+++ b/
examples/unitrun.py
@@
-10,44
+10,38
@@
This example:
import asyncio
import logging
import asyncio
import logging
-from juju.model import Model, ModelObserver
+from juju.model import Model
+from juju import loop
-async def run_stuff_on_unit(unit):
- print('Running command on unit', unit.name)
- # unit.run() returns a client.ActionResults instance
-
stdout, stderr, code = await unit.run('unit-get public-address'
)
+async def run_command(unit):
+
logging.debug('Running command on unit %s', unit.name
)
- print('Unit public address is', stdout)
+ # unit.run() returns a juju.action.Action instance
+ action = await unit.run('unit-get public-address')
+ logging.debug("Action results: %s", action.results)
- # Inform asyncio that we're done.
- await unit.model.disconnect()
- unit.model.loop.stop()
-
-class MyModelObserver(ModelObserver):
- async def on_unit_add(self, delta, old, new, model):
- loop.create_task(run_stuff_on_unit(new))
-
-
-async def run():
+async def main():
model = Model()
await model.connect_current()
await model.reset(force=True)
model = Model()
await model.connect_current()
await model.reset(force=True)
- model.add_observer(MyModelObserver())
- await model.deploy(
+ a
pp = a
wait model.deploy(
'ubuntu-0',
'ubuntu-0',
-
service
_name='ubuntu',
+
application
_name='ubuntu',
series='trusty',
channel='stable',
)
series='trusty',
channel='stable',
)
+ for unit in app.units:
+ await run_command(unit)
+
+ await model.disconnect()
+
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
-loop = asyncio.get_event_loop()
-loop.set_debug(False)
-loop.create_task(run())
-loop.run_forever()
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
+ loop.run(main())