summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/agent/env/basic/vm_manager.py
blob: e59d6c714c7859e15ae00862778d3ccbba589f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
Created on 2015-8-27

@author: y00228926
"""
import os
import shutil
import logging
from vstf.common.utils import check_and_kill, randomMAC, my_mkdir, check_call, check_output, my_sleep
from vstf.agent.env.basic.vm9pfs import VMConfigBy9pfs

LOG = logging.getLogger(__name__)


class VMControlOperation(object):
    """
    a libivrt virsh wrapper for creating virtual machine.
    """

    def __init__(self):
        """
        all tmp files will be created under '/tmp/atf_vm_manager'

        """
        work_dir = '/tmp/atf_vm_manager'
        shutil.rmtree(work_dir, ignore_errors=True)
        my_mkdir(work_dir)
        self.work_dir = work_dir
        self.vnc_index = 0
        self.pci_index = 3
        self.net_index = 0
        self.vm_9p_controllers = {}
        self.vm_configs = {}
        self.image_mgr = None

    @staticmethod
    def composite_xml(context):
        """
        composit a libvirt xml configuration for creating vm from context.

        :param context: a dict containing all necessary options for creating a vm.
        :return: libvirt xml configuration string
        """
        from vm_xml_help import xml_head, xml_disk, xml_ovs, xml_pci, xml_9p, xml_tail, xml_ctrl_br, xml_br
        xml = ''
        tmp = xml_head.replace('VM_NAME', context['vm_name'])
        tmp = tmp.replace('VM_MEMORY', str(context['vm_memory']))
        tmp = tmp.replace('CPU_NUM', str(context['vm_cpu']))
        xml += tmp
        tmp = xml_disk.replace('IMAGE_TYPE', context['image_type'])
        tmp = tmp.replace('IMAGE_PATH', context['image_path'])
        xml += tmp

        if context['9p_path']:
            tmp = xml_9p.replace('9P_PATH', context['9p_path'])
            xml += tmp

        if context['eth_pci']:
            for pci in context['eth_pci']:
                bus = pci[:2]
                slot = pci[3:5]
                func = pci[6:7]
                tmp = xml_pci.replace('BUS', bus)
                tmp = tmp.replace('SLOT', slot)
                tmp = tmp.replace('FUNCTION', func)
                xml += tmp

        if context['ctrl_br']:
            tmp = xml_ctrl_br.replace('CTRL_BR', context['ctrl_br'])
            tmp = tmp.replace('CTRL_MAC', context['ctrl_mac'])
            tmp = tmp.replace('CTRL_MODEL', context['ctrl_model'])
            xml += tmp

        for tap_cfg in context['taps']:
            if tap_cfg['br_type'] == "ovs":
                br_type = "openvswitch"
            else:
                br_type = tap_cfg['br_type']
            if br_type == 'bridge':
                xml_ovs = xml_br
            tmp = xml_ovs.replace('BR_TYPE', br_type)
            tmp = tmp.replace('TAP_MAC', tap_cfg['tap_mac'])
            tmp = tmp.replace('TAP_NAME', tap_cfg['tap_name'])
            tmp = tmp.replace('BR_NAME', tap_cfg['br_name'])
            xml += tmp

        xml += xml_tail
        return xml

    @staticmethod
    def check_required_options(context):
        for key in ('vm_name', 'vm_memory', 'vm_cpu', 'image_path', 'image_type', 'taps'):
            if not context.has_key(key):
                raise Exception("vm config error, must set %s option" % key)

    def set_vm_defaults(self, context):
        vm_9p_path = '%s/%s' % (self.work_dir, context['vm_name'])
        shutil.rmtree(vm_9p_path, ignore_errors=True)
        my_mkdir(vm_9p_path)
        default = {'vm_memory': 4194304,
                   'vm_cpu': 4,
                   'image_type': 'qcow2',
                   'br_type': 'ovs',
                   '9p_path': vm_9p_path,
                   'eth_pci': None,
                   'ctrl_br': 'br0',
                   'ctrl_mac': randomMAC(),
                   'ctrl_model': 'virtio',
                   'ctrl_ip_setting': '192.168.100.100/24',
                   'ctrl_gw': '192.168.100.1'
                   }
        for k, v in default.items():
            context.setdefault(k, v)

    def _shutdown_vm(self):
        out = check_output("virsh list | sed 1,2d | awk '{print $2}'", shell=True)
        vm_set = set(out.split())
        for vm in vm_set:
            check_call("virsh shutdown %s" % vm, shell=True)
        timeout = 60
        # wait for gracefully shutdown
        while timeout > 0:
            out = check_output("virsh list | sed 1,2d | awk '{print $2}'", shell=True)
            vm_set = set(out.split())
            if len(vm_set) == 0:
                break
            timeout -= 2
            my_sleep(2)
            LOG.info("waiting for vms:%s to shutdown gracefully", vm_set)
        # destroy by force
        for vm in vm_set:
            check_call("virsh destroy %s" % vm, shell=True)
        # undefine all
        out = check_output("virsh list --all | sed 1,2d | awk '{print $2}'", shell=True)
        vm_set = set(out.split())
        for vm in vm_set:
            check_call("virsh undefine %s" % vm, shell=True)
        # kill all qemu
        check_and_kill('qemu-system-x86_64')

    def clean_all_vms(self):
        self._shutdown_vm()
        for _, ctrl in self.vm_9p_controllers.items():
            LOG.debug("remove vm9pfs dir:%s", ctrl.vm_9p_path)
            shutil.rmtree(ctrl.vm_9p_path, ignore_errors=True)
        self.vm_9p_controllers = {}
        self.vm_configs = {}
        # shutil.rmtree(self.work_dir, ignore_errors=True)
        self.vnc_index = 0
        self.pci_index = 3
        self.net_index = 0
        self.vms = []
        return True

    def create_vm(self, context):
        self.set_vm_defaults(context)
        self.check_required_options(context)
        xml = self.composite_xml(context)
        vm_name = context['vm_name']
        file_name = os.path.join(self.work_dir, vm_name + '.xml')
        with open(file_name, 'w') as f:
            f.write(xml)
        check_call('virsh define %s' % file_name, shell=True)
        check_call('virsh start %s' % vm_name, shell=True)
        vm_name = context['vm_name']
        vm_9pfs = context['9p_path']
        self.vm_9p_controllers[vm_name] = VMConfigBy9pfs(vm_9pfs)
        self.vm_configs[vm_name] = context
        LOG.debug("%s's vm_9pfs path:%s", vm_name, vm_9pfs)
        return True

    def wait_vm(self, vm_name):
        vm9pctrl = self.vm_9p_controllers[vm_name]
        ret = vm9pctrl.wait_up()
        if ret not in (True,):
            raise Exception('vm running but stuck in boot process, please manully check.')
        LOG.debug('waitVM %s up ok, ret:%s', vm_name, ret)
        return True

    def init_config_vm(self, vm_name):
        """
        using libvirt 9pfs to config boot up options like network ip/gw.

        :param vm_name: the vm to be config with.
        :return: True if succeed, Exception if fail.
        """
        vm_cfg = self.vm_configs[vm_name]
        vm9pctrl = self.vm_9p_controllers[vm_name]
        # print self.vm_9p_controllers
        init_cfg = vm_cfg['init_config']
        if "ctrl_ip_setting" in init_cfg:
            ret = vm9pctrl.config_ip(vm_cfg['ctrl_mac'], init_cfg['ctrl_ip_setting'])
            assert ret == True
            LOG.info('initConfigVM config ip ok')
        if 'ctrl_gw' in init_cfg:
            ret = vm9pctrl.config_gw(init_cfg['ctrl_gw'])
            assert ret == True
            LOG.info('initConfigVM ctrl_gw ok')
        if "ctrl_ip_setting" in init_cfg and "amqp_server" in init_cfg:
            identity = init_cfg['ctrl_ip_setting'].split('/')[0]
            if init_cfg['amqp_id'].strip():
                identity = init_cfg['amqp_id'].strip()
            server = init_cfg['amqp_server']
            port = init_cfg['amqp_port']
            user = init_cfg['amqp_user']
            passwd = init_cfg['amqp_passwd']
            ret = vm9pctrl.config_amqp(identity, server, port, user, passwd)
            assert ret == True
            LOG.info('initConfigVM config_amqp ok')
        if 'tap_pktloop_config' in init_cfg:
            taps = vm_cfg['taps']
            macs = []
            for tap in taps:
                macs.append(tap['tap_mac'])
            ret = vm9pctrl.set_pktloop_dpdk(macs)
            assert ret == True
            LOG.info('initConfigVM set_pktloop_dpdk ok')
        return True