4 # Copyright 2016 RIFT.IO Inc
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
29 from gi
.repository
import RwcalYang
31 import rift
.rwcal
.cloudsim
32 import rift
.rwcal
.cloudsim
.lxc
as lxc
34 logger
= logging
.getLogger('rift.cal')
37 def main(argv
=sys
.argv
[1:]):
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.
44 In addition to the LVM backing store, it is assumed that there is a network
45 bridge called 'virbr0'.
48 logging
.basicConfig(level
=logging
.DEBUG
)
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')
55 args
= parser
.parse_args(argv
)
57 # Acquire the plugin from peas
58 plugin
= rw_peas
.PeasPlugin('rwcal-plugin', 'RwCal-1.0')
59 engine
, info
, extension
= plugin()
61 # Get the RwLogger context
62 rwloggerctx
= rwlogger
.RwLog
.Ctx
.new("Cal-Log")
64 cal
= plugin
.get_interface("Cloud")
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"
72 # Make sure that any containers that were previously created have been
73 # stopped and destroyed.
74 containers
= lxc
.containers()
76 for container
in containers
:
79 for container
in containers
:
80 lxc
.destroy(container
)
82 template
= os
.path
.join(
83 os
.environ
['RIFT_INSTALL'],
84 'etc/lxc-fedora-rift.lxctemplate',
88 logger
.info(args
.rootfs
)
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
97 cal
.create_image(account
, image
)
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
106 cal
.create_vm(account
, vm
)
110 # Create the default and data networks
111 network
= RwcalYang
.NetworkInfoItem(network_name
='virbr0')
112 cal
.create_network(account
, network
)
114 os
.system('/usr/sbin/brctl show')
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
)
125 cal
.create_port(account
, port
)
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")
134 cal
.start_vm(account
, vm
.vm_id
)
138 # Exit if the containers are not supposed to be terminated
139 if not args
.terminate
:
146 cal
.stop_vm(account
, vm
.vm_id
)
152 cal
.delete_vm(account
, vm
.vm_id
)
155 cal
.delete_image(account
, image
.id)
158 if __name__
== "__main__":