X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=examples%2Funitrun.py;h=9d4e5d77a0a94f0bedc9d06e7e6ca3c0c2eba22d;hb=8aca5190344dea43352b4d242291d729e3aa2328;hp=6dbb1b6d5d4314b1034da791631ef83e9a042197;hpb=173b900fcd95b2436af55df2618302146f4a2f40;p=osm%2FN2VC.git diff --git a/examples/unitrun.py b/examples/unitrun.py index 6dbb1b6..9d4e5d7 100644 --- a/examples/unitrun.py +++ b/examples/unitrun.py @@ -1,47 +1,49 @@ """ -Run this one against a model that has at least one unit deployed. +This example: + +1. Connects to current model and resets it. +2. Deploys one ubuntu unit. +3. Runs an action against the unit. +4. Waits for the action results to come back, then exits. """ import asyncio -import functools +import logging from juju.model import Model -from juju.unit import Unit -from juju.client.connection import Connection - - -loop = asyncio.get_event_loop() -conn = loop.run_until_complete(Connection.connect_current()) - -_seen_units = set() -async def run_stuff_on_unit(unit): - if unit.name in _seen_units: - return +async def run_command(unit): + logging.debug('Running command on unit %s', unit.name) - print('Running command on unit', unit.name) - # unit.run() returns a client.ActionResults instance - action_results = await unit.run('unit-get public-address') - _seen_units.add(unit.name) - 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__) +async def run(): + model = Model() + await model.connect_current() + await model.reset(force=True) -def on_model_change(delta, old, new, model): - if isinstance(new, Unit): - task = loop.create_task(run_stuff_on_unit(new)) + app = await model.deploy( + 'ubuntu-0', + application_name='ubuntu', + series='trusty', + channel='stable', + ) - if delta.entity == 'action': - print(delta.data) - print(new) + for unit in app.units: + await run_command(unit) + await model.disconnect() + model.loop.stop() -async def watch_model(): - model = Model(conn) - model.add_observer(on_model_change) - await model.watch() -loop.run_until_complete(watch_model()) +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()