0e96fa1de5f85c7584a44f6ddb9761462230b767
[osm/N2VC.git] / modules / libjuju / examples / add_model.py
1 """
2 This example:
3
4 1. Creates a model on the current controller
5 2. Deploys a charm to it.
6 3. Attempts to ssh into the charm
7
8 """
9 from juju import loop
10 from juju import utils
11 from juju.controller import Controller
12 import asyncio
13 from logging import getLogger
14 import uuid
15
16 LOG = getLogger(__name__)
17
18
19 async def main():
20 controller = Controller()
21 print("Connecting to controller")
22 await controller.connect()
23
24 try:
25 model_name = "addmodeltest-{}".format(uuid.uuid4())
26 print("Adding model {}".format(model_name))
27 model = await controller.add_model(model_name)
28
29 print('Deploying ubuntu')
30 application = await model.deploy(
31 'ubuntu-10',
32 application_name='ubuntu',
33 series='trusty',
34 channel='stable',
35 )
36
37 print('Waiting for active')
38 await asyncio.sleep(10)
39 await model.block_until(
40 lambda: all(unit.workload_status == 'active'
41 for unit in application.units))
42
43 print("Verifying that we can ssh into the created model")
44 ret = await utils.execute_process(
45 'juju', 'ssh', '-m', model_name, 'ubuntu/0', 'ls /', log=LOG)
46 assert ret
47
48 print('Removing ubuntu')
49 await application.remove()
50
51 print("Destroying model")
52 await controller.destroy_model(model.info.uuid)
53
54 except Exception:
55 LOG.exception(
56 "Test failed! Model {} may not be cleaned up".format(model_name))
57
58 finally:
59 print('Disconnecting from controller')
60 if model:
61 await model.disconnect()
62 await controller.disconnect()
63
64
65 if __name__ == '__main__':
66 loop.run(main())