efa325b56fa544701ca0ea001ab82b6f6e6e8001
[osm/N2VC.git] / 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
15 LOG = getLogger(__name__)
16
17
18 async def main():
19 controller = Controller()
20 print("Connecting to controller")
21 await controller.connect_current()
22
23 try:
24 model_name = "quux"
25 model = await controller.add_model(model_name)
26
27 print('Deploying ubuntu')
28 application = await model.deploy(
29 'ubuntu-10',
30 application_name='ubuntu',
31 series='trusty',
32 channel='stable',
33 )
34
35 print('Waiting for active')
36 await asyncio.sleep(10)
37 await model.block_until(
38 lambda: all(unit.workload_status == 'active'
39 for unit in application.units))
40
41 print("Verifying that we can ssh into the created model")
42 ret = await utils.execute_process(
43 'juju', 'ssh', '-m', model_name, 'ubuntu/0', 'ls /', log=LOG)
44 assert ret
45
46 print('Removing ubuntu')
47 await application.remove()
48
49 print("Destroying model")
50 await controller.destroy_model(model.info.uuid)
51
52 finally:
53 print('Disconnecting from controller')
54 await model.disconnect()
55 await controller.disconnect()
56
57
58 loop.run(main())