X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=examples%2Funitrun.py;h=3dfacd62e4c078592dc984246aabc2ed27cbc413;hb=95c05eaf1dcffffe16a86c80d98dcd19f4aeed45;hp=0f8b556a5b8a37d8b981d17dcd1d109178d3bbe1;hpb=1aaf1efd3ecb1fbb91c63dd88a90e15bf4268bd0;p=osm%2FN2VC.git diff --git a/examples/unitrun.py b/examples/unitrun.py index 0f8b556..3dfacd6 100644 --- a/examples/unitrun.py +++ b/examples/unitrun.py @@ -10,51 +10,38 @@ This example: 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) +async def run_command(unit): + logging.debug('Running command on unit %s', unit.name) - # unit.run() returns a client.ActionResults instance - action_results = await unit.run('unit-get public-address') - action_result = action_results.results[0] + # unit.run() returns a juju.action.Action instance + action = await unit.run('unit-get public-address') + logging.debug("Action results: %s", action.results) - print('Results from unit', unit.name) - print(action_result.__dict__) - -class MyModelObserver(ModelObserver): - async def on_unit_add(self, delta, old, new, model): - loop.create_task(run_stuff_on_unit(new)) - - async def on_action_change(self, delta, old, new, model): - print(delta.data) - - action = new - if action.status == 'completed': - await action.model.disconnect() - action.model.loop.stop() - - -async def run(): +async def main(): model = Model() await model.connect_current() await model.reset(force=True) - model.add_observer(MyModelObserver()) - await model.deploy( + app = await model.deploy( 'ubuntu-0', - service_name='ubuntu', + application_name='ubuntu', 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())