X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=examples%2Fdeploy.py;h=e6c306a1aeedea0f7bd335d10576b95b18aa39e2;hb=refs%2Ftags%2F0.4.0;hp=5c1aae58e80547bbfe3f578ff7459cdcaadc8263;hpb=2ed7314a9ea1240883655bc521b6e27f149aa485;p=osm%2FN2VC.git diff --git a/examples/deploy.py b/examples/deploy.py index 5c1aae5..e6c306a 100644 --- a/examples/deploy.py +++ b/examples/deploy.py @@ -1,53 +1,40 @@ """ -Run this one against a model that has at least one unit deployed. +This example: + +1. Connects to the current model +2. Deploy a charm and waits until it reports itself active +3. Destroys the unit and application """ -import asyncio -import logging - -from juju.model import Model, ModelObserver -from juju.client.connection import Connection - - -loop = asyncio.get_event_loop() -conn = loop.run_until_complete(Connection.connect_current()) -model = Model(conn) - - -class MyModelObserver(ModelObserver): - def on_unit_add(self, delta, old, new, model): - logging.info( - 'New unit added: %s', new.name) - - def on_change(self, delta, old, new, model): - for unit in model.units.values(): - unit_status = unit.data['agent-status']['current'] - logging.info( - 'Unit %s status: %s', unit.name, unit_status) - if unit_status == 'idle': - logging.info( - 'Destroying unit %s', unit.name) - loop.create_task(unit.destroy()) - - def on_unit_remove(self, delta, old, new, model): - app_name = old.application - app = model.applications[app_name] - if not app.units: - logging.info( - 'Destroying application %s', app.name) - loop.create_task(app.destroy()) - - -async def run(): - model.add_observer(MyModelObserver()) - await model.deploy( - 'ubuntu-0', - service_name='ubuntu', - series='trusty', - channel='stable', - ) - await model.watch() - -logging.basicConfig(level=logging.INFO) -loop.create_task(run()) -loop.run_forever() +from juju import loop +from juju.model import Model + + +async def main(): + model = Model() + print('Connecting to model') + await model.connect_current() + + try: + print('Deploying ubuntu') + application = await model.deploy( + 'ubuntu-10', + application_name='ubuntu', + series='trusty', + channel='stable', + ) + + print('Waiting for active') + await model.block_until( + lambda: all(unit.workload_status == 'active' + for unit in application.units)) + + print('Removing ubuntu') + await application.remove() + finally: + print('Disconnecting from model') + await model.disconnect() + + +if __name__ == '__main__': + loop.run(main())