X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=examples%2Funitrun.py;h=9d4e5d77a0a94f0bedc9d06e7e6ca3c0c2eba22d;hb=b7e54690262eb1021274aabb8e93188e49508150;hp=0f8b556a5b8a37d8b981d17dcd1d109178d3bbe1;hpb=1aaf1efd3ecb1fbb91c63dd88a90e15bf4268bd0;p=osm%2FN2VC.git diff --git a/examples/unitrun.py b/examples/unitrun.py index 0f8b556..9d4e5d7 100644 --- a/examples/unitrun.py +++ b/examples/unitrun.py @@ -10,46 +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) +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] - - 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() + # 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', - service_name='ubuntu', + 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')