enable package builds
[osm/SO.git] / rwcal / test / test_container_cal.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19
20 import argparse
21 import logging
22 import os
23 import sys
24 import time
25
26 import rw_peas
27 import rwlogger
28
29 from gi.repository import RwcalYang
30
31 import rift.rwcal.cloudsim
32 import rift.rwcal.cloudsim.lxc as lxc
33
34 logger = logging.getLogger('rift.cal')
35
36
37 def main(argv=sys.argv[1:]):
38 """
39 Assuming that an LVM backing-store has been created with a volume group
40 called 'rift', the following creates an lxc 'image' and a pair of 'vms'.
41 In the LXC based container CAL, an 'image' is container and a 'vm' is a
42 snapshot of the original container.
43
44 In addition to the LVM backing store, it is assumed that there is a network
45 bridge called 'virbr0'.
46
47 """
48 logging.basicConfig(level=logging.DEBUG)
49
50 parser = argparse.ArgumentParser()
51 parser.add_argument('--rootfs', '-r')
52 parser.add_argument('--num-vms', '-n', type=int, default=2)
53 parser.add_argument('--terminate', '-t', action='store_true')
54
55 args = parser.parse_args(argv)
56
57 # Acquire the plugin from peas
58 plugin = rw_peas.PeasPlugin('rwcal-plugin', 'RwCal-1.0')
59 engine, info, extension = plugin()
60
61 # Get the RwLogger context
62 rwloggerctx = rwlogger.RwLog.Ctx.new("Cal-Log")
63
64 cal = plugin.get_interface("Cloud")
65 cal.init(rwloggerctx)
66
67 # The account object is not currently used, but it is required by the CAL
68 # interface, so we create an empty object here to represent it.
69 account = RwcalYang.CloudAccount()
70 account.account_type = "lxc"
71
72 # Make sure that any containers that were previously created have been
73 # stopped and destroyed.
74 containers = lxc.containers()
75
76 for container in containers:
77 lxc.stop(container)
78
79 for container in containers:
80 lxc.destroy(container)
81
82 template = os.path.join(
83 os.environ['RIFT_INSTALL'],
84 'etc/lxc-fedora-rift.lxctemplate',
85 )
86
87 logger.info(template)
88 logger.info(args.rootfs)
89
90 # Create an image that can be used to create VMs
91 image = RwcalYang.ImageInfoItem()
92 image.name = 'rift-master'
93 image.lxc.size = '2.5G'
94 image.lxc.template_path = template
95 image.lxc.tarfile = args.rootfs
96
97 cal.create_image(account, image)
98
99 # Create a VM
100 vms = []
101 for index in range(args.num_vms):
102 vm = RwcalYang.VMInfoItem()
103 vm.vm_name = 'rift-s{}'.format(index + 1)
104 vm.image_id = image.id
105
106 cal.create_vm(account, vm)
107
108 vms.append(vm)
109
110 # Create the default and data networks
111 network = RwcalYang.NetworkInfoItem(network_name='virbr0')
112 cal.create_network(account, network)
113
114 os.system('/usr/sbin/brctl show')
115
116 # Create pairs of ports to connect the networks
117 for index, vm in enumerate(vms):
118 port = RwcalYang.PortInfoItem()
119 port.port_name = "eth0"
120 port.network_id = network.network_id
121 port.vm_id = vm.vm_id
122 port.ip_address = "192.168.122.{}".format(index + 101)
123 port.lxc.veth_name = "rws{}".format(index)
124
125 cal.create_port(account, port)
126
127 # Swap out the current instance of the plugin to test that the data is
128 # shared among different instances
129 cal = plugin.get_interface("Cloud")
130 cal.init()
131
132 # Start the VMs
133 for vm in vms:
134 cal.start_vm(account, vm.vm_id)
135
136 lxc.ls()
137
138 # Exit if the containers are not supposed to be terminated
139 if not args.terminate:
140 return
141
142 time.sleep(3)
143
144 # Stop the VMs
145 for vm in vms:
146 cal.stop_vm(account, vm.vm_id)
147
148 lxc.ls()
149
150 # Delete the VMs
151 for vm in vms:
152 cal.delete_vm(account, vm.vm_id)
153
154 # Delete the image
155 cal.delete_image(account, image.id)
156
157
158 if __name__ == "__main__":
159 main()