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=88766f15b08e346515f040a024b3fd56a037ca0c;hp=0000000000000000000000000000000000000000;hb=e2051cca7dac12aa09f6ed33555dcc4548c4b52b;hpb=9d18c22a0dc9e295adda50601fc5e2f45d2c9b8a diff --git a/modules/libjuju/examples/add_model.py b/modules/libjuju/examples/add_model.py new file mode 100644 index 0000000..88766f1 --- /dev/null +++ b/modules/libjuju/examples/add_model.py @@ -0,0 +1,67 @@ +""" +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") + # connect to current controller with current user, per Juju CLI + await controller.connect() + + 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())