X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=examples%2Funitrun.py;h=9d4e5d77a0a94f0bedc9d06e7e6ca3c0c2eba22d;hb=8aca5190344dea43352b4d242291d729e3aa2328;hp=0436259ccb432ab8d5fe728c422e011cce72924b;hpb=ea3d50fc8145b8182ce8dd191ae750b42f5ff2ba;p=osm%2FN2VC.git diff --git a/examples/unitrun.py b/examples/unitrun.py index 0436259..9d4e5d7 100644 --- a/examples/unitrun.py +++ b/examples/unitrun.py @@ -10,39 +10,35 @@ This example: import asyncio import logging -from juju.model import Model, ModelObserver +from juju.model import Model -async def run_stuff_on_unit(unit): - print('Running command on unit', unit.name) - - # unit.run() returns a client.ActionResults instance - action = await unit.run('unit-get public-address') - - print("Action results: {}".format(action.results)) - - # Inform asyncio that we're done. - await unit.model.disconnect() - unit.model.loop.stop() +async def run_command(unit): + logging.debug('Running command on unit %s', unit.name) -class MyModelObserver(ModelObserver): - async def on_unit_add(self, delta, old, new, model): - loop.create_task(run_stuff_on_unit(new)) + # unit.run() returns a juju.action.Action instance + action = await unit.run('unit-get public-address') + logging.debug("Action results: %s", action.results) async def run(): model = Model() await model.connect_current() await model.reset(force=True) - model.add_observer(MyModelObserver()) - await model.deploy( + app = await model.deploy( 'ubuntu-0', application_name='ubuntu', series='trusty', channel='stable', ) + for unit in app.units: + await run_command(unit) + + await model.disconnect() + model.loop.stop() + logging.basicConfig(level=logging.DEBUG) ws_logger = logging.getLogger('websockets.protocol')