JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / agent / env / basic / vm_manager.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11 import shutil
12 import logging
13 from vstf.common.utils import check_and_kill, randomMAC, my_mkdir, check_call, check_output, my_sleep
14 from vstf.agent.env.basic.vm9pfs import VMConfigBy9pfs
15
16 LOG = logging.getLogger(__name__)
17
18
19 class VMControlOperation(object):
20     """
21     a libivrt virsh wrapper for creating virtual machine.
22     """
23
24     def __init__(self):
25         """
26         all tmp files will be created under '/tmp/atf_vm_manager'
27
28         """
29         work_dir = '/tmp/atf_vm_manager'
30         shutil.rmtree(work_dir, ignore_errors=True)
31         my_mkdir(work_dir)
32         self.work_dir = work_dir
33         self.vnc_index = 0
34         self.pci_index = 3
35         self.net_index = 0
36         self.vm_9p_controllers = {}
37         self.vm_configs = {}
38         self.image_mgr = None
39
40     @staticmethod
41     def composite_xml(context):
42         """
43         composit a libvirt xml configuration for creating vm from context.
44
45         :param context: a dict containing all necessary options for creating a vm.
46         :return: libvirt xml configuration string
47         """
48         from vm_xml_help import xml_head, xml_disk, xml_ovs, xml_pci, xml_9p, xml_tail, xml_ctrl_br, xml_br
49         xml = ''
50         tmp = xml_head.replace('VM_NAME', context['vm_name'])
51         tmp = tmp.replace('VM_MEMORY', str(context['vm_memory']))
52         tmp = tmp.replace('CPU_NUM', str(context['vm_cpu']))
53         xml += tmp
54         tmp = xml_disk.replace('IMAGE_TYPE', context['image_type'])
55         tmp = tmp.replace('IMAGE_PATH', context['image_path'])
56         xml += tmp
57
58         if context['9p_path']:
59             tmp = xml_9p.replace('9P_PATH', context['9p_path'])
60             xml += tmp
61
62         if context['eth_pci']:
63             for pci in context['eth_pci']:
64                 bus = pci[:2]
65                 slot = pci[3:5]
66                 func = pci[6:7]
67                 tmp = xml_pci.replace('BUS', bus)
68                 tmp = tmp.replace('SLOT', slot)
69                 tmp = tmp.replace('FUNCTION', func)
70                 xml += tmp
71
72         if context['ctrl_br']:
73             tmp = xml_ctrl_br.replace('CTRL_BR', context['ctrl_br'])
74             tmp = tmp.replace('CTRL_MAC', context['ctrl_mac'])
75             tmp = tmp.replace('CTRL_MODEL', context['ctrl_model'])
76             xml += tmp
77
78         for tap_cfg in context['taps']:
79             if tap_cfg['br_type'] == "ovs":
80                 br_type = "openvswitch"
81             else:
82                 br_type = tap_cfg['br_type']
83             if br_type == 'bridge':
84                 xml_ovs = xml_br
85             tmp = xml_ovs.replace('BR_TYPE', br_type)
86             tmp = tmp.replace('TAP_MAC', tap_cfg['tap_mac'])
87             tmp = tmp.replace('TAP_NAME', tap_cfg['tap_name'])
88             tmp = tmp.replace('BR_NAME', tap_cfg['br_name'])
89             xml += tmp
90
91         xml += xml_tail
92         return xml
93
94     @staticmethod
95     def check_required_options(context):
96         for key in ('vm_name', 'vm_memory', 'vm_cpu', 'image_path', 'image_type', 'taps'):
97             if not context.has_key(key):
98                 raise Exception("vm config error, must set %s option" % key)
99
100     def set_vm_defaults(self, context):
101         vm_9p_path = '%s/%s' % (self.work_dir, context['vm_name'])
102         shutil.rmtree(vm_9p_path, ignore_errors=True)
103         my_mkdir(vm_9p_path)
104         default = {'vm_memory': 4194304,
105                    'vm_cpu': 4,
106                    'image_type': 'qcow2',
107                    'br_type': 'ovs',
108                    '9p_path': vm_9p_path,
109                    'eth_pci': None,
110                    'ctrl_br': 'br0',
111                    'ctrl_mac': randomMAC(),
112                    'ctrl_model': 'virtio',
113                    'ctrl_ip_setting': '192.168.100.100/24',
114                    'ctrl_gw': '192.168.100.1'
115                    }
116         for k, v in default.items():
117             context.setdefault(k, v)
118
119     def _shutdown_vm(self):
120         out = check_output("virsh list | sed 1,2d | awk '{print $2}'", shell=True)
121         vm_set = set(out.split())
122         for vm in vm_set:
123             check_call("virsh shutdown %s" % vm, shell=True)
124         timeout = 60
125         # wait for gracefully shutdown
126         while timeout > 0:
127             out = check_output("virsh list | sed 1,2d | awk '{print $2}'", shell=True)
128             vm_set = set(out.split())
129             if len(vm_set) == 0:
130                 break
131             timeout -= 2
132             my_sleep(2)
133             LOG.info("waiting for vms:%s to shutdown gracefully", vm_set)
134         # destroy by force
135         for vm in vm_set:
136             check_call("virsh destroy %s" % vm, shell=True)
137         # undefine all
138         out = check_output("virsh list --all | sed 1,2d | awk '{print $2}'", shell=True)
139         vm_set = set(out.split())
140         for vm in vm_set:
141             check_call("virsh undefine %s" % vm, shell=True)
142         # kill all qemu
143         check_and_kill('qemu-system-x86_64')
144
145     def clean_all_vms(self):
146         self._shutdown_vm()
147         for _, ctrl in self.vm_9p_controllers.items():
148             LOG.debug("remove vm9pfs dir:%s", ctrl.vm_9p_path)
149             shutil.rmtree(ctrl.vm_9p_path, ignore_errors=True)
150         self.vm_9p_controllers = {}
151         self.vm_configs = {}
152         # shutil.rmtree(self.work_dir, ignore_errors=True)
153         self.vnc_index = 0
154         self.pci_index = 3
155         self.net_index = 0
156         self.vms = []
157         return True
158
159     def create_vm(self, context):
160         self.set_vm_defaults(context)
161         self.check_required_options(context)
162         xml = self.composite_xml(context)
163         vm_name = context['vm_name']
164         file_name = os.path.join(self.work_dir, vm_name + '.xml')
165         with open(file_name, 'w') as f:
166             f.write(xml)
167         check_call('virsh define %s' % file_name, shell=True)
168         check_call('virsh start %s' % vm_name, shell=True)
169         vm_name = context['vm_name']
170         vm_9pfs = context['9p_path']
171         self.vm_9p_controllers[vm_name] = VMConfigBy9pfs(vm_9pfs)
172         self.vm_configs[vm_name] = context
173         LOG.debug("%s's vm_9pfs path:%s", vm_name, vm_9pfs)
174         return True
175
176     def wait_vm(self, vm_name):
177         vm9pctrl = self.vm_9p_controllers[vm_name]
178         ret = vm9pctrl.wait_up()
179         if ret not in (True,):
180             raise Exception('vm running but stuck in boot process, please manully check.')
181         LOG.debug('waitVM %s up ok, ret:%s', vm_name, ret)
182         return True
183
184     def init_config_vm(self, vm_name):
185         """
186         using libvirt 9pfs to config boot up options like network ip/gw.
187
188         :param vm_name: the vm to be config with.
189         :return: True if succeed, Exception if fail.
190         """
191         vm_cfg = self.vm_configs[vm_name]
192         vm9pctrl = self.vm_9p_controllers[vm_name]
193         # print self.vm_9p_controllers
194         init_cfg = vm_cfg['init_config']
195         if "ctrl_ip_setting" in init_cfg:
196             ret = vm9pctrl.config_ip(vm_cfg['ctrl_mac'], init_cfg['ctrl_ip_setting'])
197             assert ret == True
198             LOG.info('initConfigVM config ip ok')
199         if 'ctrl_gw' in init_cfg:
200             ret = vm9pctrl.config_gw(init_cfg['ctrl_gw'])
201             assert ret == True
202             LOG.info('initConfigVM ctrl_gw ok')
203         if "ctrl_ip_setting" in init_cfg and "amqp_server" in init_cfg:
204             identity = init_cfg['ctrl_ip_setting'].split('/')[0]
205             if init_cfg['amqp_id'].strip():
206                 identity = init_cfg['amqp_id'].strip()
207             server = init_cfg['amqp_server']
208             port = init_cfg['amqp_port']
209             user = init_cfg['amqp_user']
210             passwd = init_cfg['amqp_passwd']
211             ret = vm9pctrl.config_amqp(identity, server, port, user, passwd)
212             assert ret == True
213             LOG.info('initConfigVM config_amqp ok')
214         if 'tap_pktloop_config' in init_cfg:
215             taps = vm_cfg['taps']
216             macs = []
217             for tap in taps:
218                 macs.append(tap['tap_mac'])
219             ret = vm9pctrl.set_pktloop_dpdk(macs)
220             assert ret == True
221             LOG.info('initConfigVM set_pktloop_dpdk ok')
222         return True