Bug 733 fixed
[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 # connect to current controller with current user, per Juju CLI
23 await controller.connect()
24
25 try:
26 model_name = "addmodeltest-{}".format(uuid.uuid4())
27 print("Adding model {}".format(model_name))
28 model = await controller.add_model(model_name)
29
30 print('Deploying ubuntu')
31 application = await model.deploy(
32 'ubuntu-10',
33 application_name='ubuntu',
34 series='trusty',
35 channel='stable',
36 )
37
38 print('Waiting for active')
39 await asyncio.sleep(10)
40 await model.block_until(
41 lambda: all(unit.workload_status == 'active'
42 for unit in application.units))
43
44 print("Verifying that we can ssh into the created model")
45 ret = await utils.execute_process(
46 'juju', 'ssh', '-m', model_name, 'ubuntu/0', 'ls /', log=LOG)
47 assert ret
48
49 print('Removing ubuntu')
50 await application.remove()
51
52 print("Destroying model")
53 await controller.destroy_model(model.info.uuid)
54
55 except Exception:
56 LOG.exception(
57 "Test failed! Model {} may not be cleaned up".format(model_name))
58
59 finally:
60 print('Disconnecting from controller')
61 if model:
62 await model.disconnect()
63 await controller.disconnect()
64
65
66 if __name__ == '__main__':
67 loop.run(main())