X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=modules%2Flibjuju%2Fexamples%2Fadd_model.py;fp=modules%2Flibjuju%2Fexamples%2Fadd_model.py;h=3e46490b20f6f45fcb89a173407c848467b03c74;hp=0000000000000000000000000000000000000000;hb=68858c1915122c2dbc8999a5cd3229694abf5f3a;hpb=032a71b2a6692b8b4e30f629a1f906d246f06736 diff --git a/modules/libjuju/examples/add_model.py b/modules/libjuju/examples/add_model.py new file mode 100644 index 0000000..3e46490 --- /dev/null +++ b/modules/libjuju/examples/add_model.py @@ -0,0 +1,66 @@ +""" +This example: + +1. Creates a model on the current controller +2. Deploys a charm to it. +3. Attempts to ssh into the charm + +""" +from juju import loop +from juju import utils +from juju.controller import Controller +import asyncio +from logging import getLogger +import uuid + +LOG = getLogger(__name__) + + +async def main(): + controller = Controller() + print("Connecting to controller") + await controller.connect_current() + + try: + model_name = "addmodeltest-{}".format(uuid.uuid4()) + print("Adding model {}".format(model_name)) + model = await controller.add_model(model_name) + + print('Deploying ubuntu') + application = await model.deploy( + 'ubuntu-10', + application_name='ubuntu', + series='trusty', + channel='stable', + ) + + print('Waiting for active') + await asyncio.sleep(10) + await model.block_until( + lambda: all(unit.workload_status == 'active' + for unit in application.units)) + + print("Verifying that we can ssh into the created model") + ret = await utils.execute_process( + 'juju', 'ssh', '-m', model_name, 'ubuntu/0', 'ls /', log=LOG) + assert ret + + print('Removing ubuntu') + await application.remove() + + print("Destroying model") + await controller.destroy_model(model.info.uuid) + + except Exception: + LOG.exception( + "Test failed! Model {} may not be cleaned up".format(model_name)) + + finally: + print('Disconnecting from controller') + if model: + await model.disconnect() + await controller.disconnect() + + +if __name__ == '__main__': + loop.run(main())