summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/agent/env/basic
diff options
context:
space:
mode:
Diffstat (limited to 'vstf/vstf/agent/env/basic')
-rwxr-xr-xvstf/vstf/agent/env/basic/Readme39
-rwxr-xr-xvstf/vstf/agent/env/basic/__init__.py14
-rwxr-xr-xvstf/vstf/agent/env/basic/collect.py101
-rwxr-xr-xvstf/vstf/agent/env/basic/commandline.py46
-rwxr-xr-xvstf/vstf/agent/env/basic/device_manager.py144
-rwxr-xr-xvstf/vstf/agent/env/basic/image_manager.py124
-rwxr-xr-xvstf/vstf/agent/env/basic/source_manager.py74
-rwxr-xr-xvstf/vstf/agent/env/basic/vm9pfs.py154
-rwxr-xr-xvstf/vstf/agent/env/basic/vm_manager.py218
-rwxr-xr-xvstf/vstf/agent/env/basic/vm_xml_help.py81
10 files changed, 995 insertions, 0 deletions
diff --git a/vstf/vstf/agent/env/basic/Readme b/vstf/vstf/agent/env/basic/Readme
new file mode 100755
index 00000000..cf7a877f
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/Readme
@@ -0,0 +1,39 @@
+the modules in this directory include some basic common purpose functions that can be used for other plugins.
+you can see the modules in this directory as libraries that you can import for ease of creating your own plugin.
+
+the main functions of the modules are listed as follows:
+
+ImageManger:
+ role:
+ virtual machine images manager. used by VmManager.
+ wraps 'qemu-img' command as underlying mechanism.
+ features:
+ provides a function to create child image from a parent image.
+ provides a function to delete child image.
+
+SourceManager:
+ role:
+ source code manager.
+ using 'pramiko' as underlying mechanism.
+ features:
+ provides a function to download source code
+ provides a function which wrap 'make clean;make;make install' to compile and install.
+
+VmManager:
+ role:
+ virtual machine manager which can create vm and configure vm on bootstrap.
+ use libvirt as underlying mechanism. wrap 'virsh' command for creating vm.
+ features:
+ provides a function to create vm from a vm configuration context.
+ provides functions to detect if vm boots up successfully and onfigure bootstrap options for vm like ip and rabbitmq conf file.
+
+vm9pfs:
+ used by VmManager.
+ host side 9pfs operation (mainly file operations) to communicate with vm by libvirt 9pfs.
+ see as communication protocol on client side. The vm runs a agent for receiving the commands.
+
+VmXmlHelp:
+ used by VmManager.
+ just some divided libvirt xml string templates for building a complete libvirt xml file on demand by VmManager.
+
+
diff --git a/vstf/vstf/agent/env/basic/__init__.py b/vstf/vstf/agent/env/basic/__init__.py
new file mode 100755
index 00000000..89dcd4e2
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/__init__.py
@@ -0,0 +1,14 @@
+# Copyright Huawei Technologies Co., Ltd. 1998-2015.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the License); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an AS IS BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
diff --git a/vstf/vstf/agent/env/basic/collect.py b/vstf/vstf/agent/env/basic/collect.py
new file mode 100755
index 00000000..bc4f1ee4
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/collect.py
@@ -0,0 +1,101 @@
+import os
+import platform
+import logging
+from collections import OrderedDict
+
+from vstf.agent.env.basic.commandline import CommandLine
+from vstf.common import constants as const
+
+log = logging.getLogger(__name__)
+CMD = CommandLine()
+
+
+class Collect(object):
+ """collect host information such as _cpu, memory and so on"""
+
+ def __init__(self):
+ super(Collect, self).__init__()
+ self._system = self._system()
+ self._cpu = self._cpu()
+
+ def _system(self):
+ """the base _system info
+ {'os info':{'_system':'ubuntu', 'kernel': '3.13.3'}}"""
+ return {const.OS_INFO:
+ {
+ '_system': open('/etc/issue.net').readline().strip(),
+ 'kernel': platform.uname()[2]
+ }
+ }
+
+ def _memery(self):
+ """ Return the information in /proc/meminfo
+ as a dictionary """
+ meminfo = OrderedDict()
+ with open('/proc/meminfo') as f:
+ for line in f:
+ meminfo[line.split(':')[0]] = line.split(':')[1].strip()
+
+ return {const.MEMORY_INFO:
+ {
+ "Mem Total": meminfo['MemTotal'],
+ "Mem Swap": meminfo['SwapTotal']
+ }
+ }
+
+ def _lscpu(self):
+ ret = {}
+ with os.popen("lscpu") as f:
+ for line in f:
+ ret[line.split(':')[0].strip()] = line.split(':')[1].strip()
+ return ret
+
+ def _cpu(self):
+ ret = []
+ with open('/proc/cpuinfo') as f:
+ cpuinfo = OrderedDict()
+ for line in f:
+ if not line.strip():
+ ret.append(cpuinfo)
+ cpuinfo = OrderedDict()
+ elif len(line.split(':')) == 2:
+ cpuinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()
+ else:
+ log.error("_cpu info unknow format <%(c)s>", {'c': line})
+ return {const.CPU_INFO:
+ dict(
+ {
+ "Model Name": ret[0]['model name'],
+ "Address sizes": ret[0]['address sizes']
+ },
+ **(self._lscpu())
+ )
+ }
+
+ def _hw_sysinfo(self):
+ cmdline = "dmidecode | grep -A 2 'System Information' | grep -v 'System Information'"
+ ret, output = CMD.execute(cmdline, shell=True)
+ if ret:
+ result = {}
+ # del the stderr
+ for tmp in output.strip().split('\n'):
+ if tmp is None or tmp is "":
+ continue
+ # split the items
+ tmp = tmp.split(":")
+ if len(tmp) >= 2:
+ # first item as key, and the other as value
+ result[tmp[0].strip("\t")] = ";".join(tmp[1:])
+ return {const.HW_INFO: result}
+ else:
+ return {const.HW_INFO: "get hw info failed. check the host by cmd: dmidecode"}
+
+ def collect_host_info(self):
+ return [self._system, self._cpu, self._memery(), self._hw_sysinfo()]
+
+
+if __name__ == "__main__":
+ c = Collect()
+ import json
+
+ print json.dumps(c.collect_host_info(), indent=4)
diff --git a/vstf/vstf/agent/env/basic/commandline.py b/vstf/vstf/agent/env/basic/commandline.py
new file mode 100755
index 00000000..0df037d8
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/commandline.py
@@ -0,0 +1,46 @@
+import subprocess
+import threading
+import logging
+from vstf.common import constants
+
+LOG = logging.getLogger(__name__)
+
+
+class CommandLine(object):
+ def __init__(self):
+ super(CommandLine, self).__init__()
+ self.proc = None
+ self.is_timeout = False
+
+ def __kill_proc(self):
+ self.is_timeout = True
+ self.proc.kill()
+
+ def execute(self, cmd, timeout=constants.TIMEOUT, shell=False):
+ """this func call subprocess.Popen(),
+ here setup a timer to deal with timeout.
+ :param cmd: cmd list like ['ls', 'home']
+ :param timeout: for timer count for timeout
+ :return: (ret, output) the output (stdout+'\n'+stderr)
+ """
+ # reset the timeout flag
+ self.is_timeout = False
+ self.proc = subprocess.Popen(cmd,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ shell=shell)
+
+ timer = threading.Timer(timeout, self.__kill_proc, [])
+ timer.start()
+ stdout, stderr = self.proc.communicate()
+ timer.cancel()
+
+ if self.proc.returncode or self.is_timeout:
+ if self.is_timeout:
+ LOG.error("run cmd<%(cmd)s> timeout", {"cmd": cmd})
+ ret = False
+ output = "".join([stderr, stdout])
+ else:
+ ret = True
+ output = stdout
+ return ret, output
diff --git a/vstf/vstf/agent/env/basic/device_manager.py b/vstf/vstf/agent/env/basic/device_manager.py
new file mode 100755
index 00000000..3209d7c9
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/device_manager.py
@@ -0,0 +1,144 @@
+"""
+Created on 2015-9-25
+
+@author: y00228926
+"""
+
+import re
+import logging
+from vstf.agent.perf import netns
+from vstf.common.utils import check_output, get_device_name, my_sleep, check_call, call, IPCommandHelper
+
+LOG = logging.getLogger(__name__)
+
+default_drivers = {
+ '82599': 'ixgbe',
+ '82576': 'igb',
+}
+
+
+class LspciHelper(object):
+ def __init__(self):
+ self.bdf_desc_map = {}
+ self.bdf_device_map = {}
+ self.device_bdf_map = {}
+ self.bdf_ip_map = {}
+ self.bdf_driver_map = {}
+ self.mac_bdf_map = {}
+ self.bdf_mac_map = {}
+ self._get_bdfs()
+ self._get_devices()
+ self._get_drivers()
+ self._get_ip_macs()
+
+ def _get_bdfs(self):
+ self.bdf_desc_map = {}
+ out = check_output('lspci |grep Eth', shell=True)
+ for line in out.splitlines():
+ bdf, desc = line.split(' ', 1)
+ self.bdf_desc_map[bdf] = desc
+
+ def _get_devices(self):
+ for bdf, desc in self.bdf_desc_map.items():
+ device = get_device_name(bdf)
+ if device is None:
+ LOG.info("cann't find device name for bdf:%s, no driver is available.", bdf)
+ try:
+ self._load_driver(desc)
+ except:
+ LOG.warn("!!!unable to load_driver for device:%s", bdf)
+ my_sleep(0.2)
+ device = get_device_name(bdf)
+ self.bdf_device_map[bdf] = device
+ if device:
+ self.device_bdf_map[device] = bdf
+ check_call("ip link set dev %s up" % device, shell=True)
+
+ def _get_drivers(self):
+ for device, bdf in self.device_bdf_map.items():
+ buf = check_output('ethtool -i %s | head -n1' % device, shell=True)
+ driver = buf.split()[1]
+ self.bdf_driver_map[bdf] = driver
+
+ def _get_ip_macs(self):
+ for device, bdf in self.device_bdf_map.items():
+ buf = check_output("ip addr show dev %s" % device, shell=True)
+ macs = re.compile("[A-F0-9]{2}(?::[A-F0-9]{2}){5}", re.IGNORECASE | re.MULTILINE)
+ for mac in macs.findall(buf):
+ if mac.lower() in ('00:00:00:00:00:00', 'ff:ff:ff:ff:ff:ff'):
+ continue
+ else:
+ break
+ ips = re.compile(r"inet (\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}/\d{1,2})", re.MULTILINE)
+ ip = ips.findall(buf)
+ if ip:
+ self.bdf_ip_map[bdf] = ip[0]
+ else:
+ self.bdf_ip_map[bdf] = None
+ self.bdf_mac_map[bdf] = mac
+ self.mac_bdf_map[mac] = bdf
+
+ def _load_driver(self, desc):
+ for key in default_drivers:
+ if key in desc:
+ driver = default_drivers[key]
+ LOG.info("try to load default driver [%s]", driver)
+ check_call('modprobe %s' % driver, shell=True)
+ break
+ else:
+ LOG.warn("unsupported nic type:%s", desc)
+
+
+class DeviceManager(object):
+ def __init__(self):
+ super(DeviceManager, self).__init__()
+ mgr = netns.NetnsManager()
+ mgr.clean_all_namespace()
+ self.lspci_helper = LspciHelper()
+
+ def _get_device_detail(self, bdf):
+ device = self.lspci_helper.bdf_device_map[bdf]
+ mac = self.lspci_helper.bdf_mac_map[bdf]
+ ip = self.lspci_helper.bdf_ip_map[bdf]
+ desc = self.lspci_helper.bdf_desc_map[bdf]
+ driver = self.lspci_helper.bdf_driver_map[bdf]
+ detail = {
+ 'bdf': bdf,
+ 'device': device,
+ 'mac': mac,
+ 'ip': ip,
+ 'desc': desc,
+ 'driver': driver
+ }
+ return detail
+
+ def get_device_detail(self, identity):
+ """
+ Gets the detail of a network card.
+
+ :param identity: be it the mac address, bdf, device name of a network card.
+ :return: device detail of a network card.
+ """
+ if identity in self.lspci_helper.bdf_device_map:
+ bdf = identity
+ elif identity in self.lspci_helper.device_bdf_map:
+ bdf = self.lspci_helper.device_bdf_map[identity]
+ elif identity in self.lspci_helper.mac_bdf_map:
+ bdf = self.lspci_helper.mac_bdf_map[identity]
+ else:
+ raise Exception("cann't find the device by identity:%s" % identity)
+ return self._get_device_detail(bdf)
+
+ def get_device_verbose(self, identity):
+ return IPCommandHelper().get_device_verbose(identity)
+
+ def list_nic_devices(self):
+ """
+ Get all the details of network devices in the host.
+ :return: a list of network card detail.
+ """
+ device_list = []
+ for bdf in self.lspci_helper.bdf_device_map.keys():
+ detail = self._get_device_detail(bdf)
+ device_list.append(detail)
+ return device_list
diff --git a/vstf/vstf/agent/env/basic/image_manager.py b/vstf/vstf/agent/env/basic/image_manager.py
new file mode 100755
index 00000000..6c7df709
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/image_manager.py
@@ -0,0 +1,124 @@
+"""
+Created on 2015-7-28
+
+@author: y00228926
+"""
+from vstf.common.utils import check_call
+import os
+import logging
+
+LOG = logging.getLogger(__name__)
+
+
+class _ImageManager(object):
+ """
+ A qemu-img wrapper to create qcow2 child image from a parent image.
+
+ """
+ def __init__(self, parent_image_path, child_image_dir):
+ """
+ :param parent_image_path str: the parent image path.
+ :param child_image_dir str: the destination path to put child images.
+ """
+ self._create_child_str = 'qemu-img create -f %(image_type)s %(child_path)s -o backing_file=%(parent_path)s'
+ self._convert_str = "qemu-img convert -O %(image_type)s %(parent_path)s %(child_path)s"
+ self.child_image_dir = child_image_dir
+ self.parent_image_path = parent_image_path
+ assert os.path.isfile(self.parent_image_path)
+ assert os.path.isdir(self.child_image_dir)
+
+ def create_child_image(self, child_name, full_clone=False, image_type='qcow2'):
+ """
+ create a child image and put it in self.child_image_dir.
+
+ :param child_name: the image name to be created..
+ :return: return the path of child image.
+ """
+
+ image_path = os.path.join(self.child_image_dir, child_name) + '.' + image_type
+ if full_clone:
+ cmd = self._convert_str % {'image_type': image_type, 'child_path': image_path, 'parent_path': self.parent_image_path}
+ else:
+ cmd = self._create_child_str % {'child_path': image_path, 'parent_path': self.parent_image_path, 'image_type':image_type}
+ check_call(cmd.split())
+ return image_path
+
+
+class ImageManager(object):
+ def __init__(self, cfg):
+ """
+ ImageManager creates images from configuration context.
+
+ :param cfg: dict, example:
+ {
+ 'parent_image': "/mnt/sdb/ubuntu_salt_master.img",
+ 'dst_location': "/mnt/sdb",
+ 'full_clone':False,
+ 'type': "qcow2",
+ 'names': ['vm1','vm2','vm3','vm4']
+ }
+ :return:
+ """
+ super(ImageManager, self).__init__()
+ cfg = self._check_cfg(cfg)
+ self.parent_image = cfg['parent_image']
+ self.image_dir = cfg['dst_location']
+ self.full_clone = cfg['full_clone']
+ self.image_type = cfg['type']
+ self.names = cfg['names']
+ self.mgr = _ImageManager(self.parent_image, self.image_dir)
+
+ @staticmethod
+ def _check_cfg(cfg):
+ for key in ('parent_image', 'dst_location', 'full_clone', 'type', 'names'):
+ if key not in cfg:
+ raise Exception("does't find %s config" % key)
+ if cfg['type'] not in ('raw', 'qcow2'):
+ raise Exception("type:%s not supported, only support 'raw' and 'qcow2'" % cfg['type'])
+ if not cfg['full_clone'] and cfg['type'] == 'raw':
+ raise Exception("only support 'qcow2' for not full_clone image creation" % cfg['type'])
+ return cfg
+
+ def create_all(self):
+ """
+ create images by configuration context.
+
+ :return: True for success, False for failure.
+ """
+ for name in self.names:
+ image = self.mgr.create_child_image(name, self.full_clone, self.image_type)
+ LOG.info("image: %s created", image)
+ return True
+
+ def clean_all(self):
+ """
+ remove all the images created in one go.
+
+ :return: True for success. Raise exception otherwise.
+ """
+ for name in self.names:
+ image_path = os.path.join(self.image_dir, name + '.' + self.image_type)
+ try:
+ os.unlink(image_path)
+ LOG.info("remove:%s successfully", image_path)
+ except Exception:
+ LOG.info("cann't find path:%s", image_path)
+ return True
+
+
+if __name__ == '__main__':
+ import argparse
+ import json
+ parser = argparse.ArgumentParser()
+ parser.add_argument('action', choices = ('create','clean'), help='action:create|clean')
+ parser.add_argument('--config', help='config file to parse')
+ args = parser.parse_args()
+ logging.basicConfig(level=logging.INFO)
+ image_cfg = json.load(open(args.config))
+ mgr = ImageManager(image_cfg)
+ if args.action == 'create':
+ mgr.create_all()
+ if args.action == 'clean':
+ mgr.clean_all()
+
+
diff --git a/vstf/vstf/agent/env/basic/source_manager.py b/vstf/vstf/agent/env/basic/source_manager.py
new file mode 100755
index 00000000..4267cbd2
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/source_manager.py
@@ -0,0 +1,74 @@
+"""
+Created on 2015-8-27
+
+@author: y00228926
+"""
+import os
+import logging
+import contextlib
+from subprocess import CalledProcessError
+from vstf.common.utils import check_call
+
+LOG = logging.getLogger(__name__)
+
+
+@contextlib.contextmanager
+def my_chdir(file_path):
+ old_cwd = os.path.realpath(os.curdir)
+ os.chdir(file_path)
+ LOG.info("cd %s", file_path)
+ yield
+ os.chdir(old_cwd)
+ LOG.info("cd %s", old_cwd)
+
+
+class SourceCodeManager(object):
+ def __init__(self):
+ super(SourceCodeManager, self).__init__()
+ self.base_path = '/opt/vstf/'
+
+ @staticmethod
+ def _git_pull(url, dest):
+ if not os.path.isdir(dest):
+ check_call("git clone %s %s" % (url, dest), shell=True)
+ else:
+ with my_chdir(dest):
+ check_call("git pull", shell=True)
+
+ @staticmethod
+ def _install(dest):
+ with my_chdir(dest):
+ try:
+ check_call("make && make install", shell=True)
+ except CalledProcessError:
+ LOG.info("retry make again")
+ check_call("make clean; make && make install", shell=True)
+
+ def src_install(self, cfg):
+ for key, item in cfg.items():
+ repo_type = item['repo_type']
+ url = item['url']
+ install = item['install']
+ if install is True:
+ LOG.info("installing src repo:%s", key)
+ if repo_type == "git":
+ target = self.base_path + key
+ self._git_pull(url, target)
+ self._install(target)
+ else:
+ raise Exception("unsupported repo type:%s" % repo_type)
+ else:
+ LOG.info("skip src repo:%s", key)
+ return True
+
+
+if __name__ == '__main__':
+ import argparse
+ import json
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--config', help='config file to parse')
+ args = parser.parse_args()
+ logging.basicConfig(level=logging.INFO)
+ cfg = json.load(open(args.config))
+ mgr = SourceCodeManager()
+ mgr.src_install(cfg)
diff --git a/vstf/vstf/agent/env/basic/vm9pfs.py b/vstf/vstf/agent/env/basic/vm9pfs.py
new file mode 100755
index 00000000..f3a2c2ce
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/vm9pfs.py
@@ -0,0 +1,154 @@
+"""
+Created on 2015-8-27
+
+@author: y00228926
+"""
+import os
+import logging
+import textwrap
+from vstf.common.utils import my_sleep
+from vstf.agent.env.fsmonitor import constant
+
+LOG = logging.getLogger(__name__)
+
+
+class VMConfigBy9pfs(object):
+ """
+ host side implemetation of a self-defined communication protocol using libvirt 9pfs to give commands to the Virtual Machine.
+
+ """
+
+ def __init__(self, vm_9p_path):
+ """
+ :param vm_9p_path: The host path of libvirt 9pfs for a vm.
+ :return:
+ """
+ self.vm_9p_path = vm_9p_path
+
+ def clean(self):
+ self._unlink(self._path(constant.VM_CMD_RETURN_CODE_FILE))
+ self._unlink(self._path(constant.VM_CMD_DONE_FLAG_FILE))
+
+ def _path(self, relative_path):
+ return os.path.join(self.vm_9p_path, relative_path)
+
+ def _unlink(self, file_path):
+ os.unlink(file_path)
+ LOG.info("os.unlink(%s)", file_path)
+
+ def _read(self, filename):
+ filepath = self._path(filename)
+ with open(filepath, 'r') as f:
+ ret = f.read()
+ LOG.info("read(%s) -> %s", filepath, ret)
+ return ret
+
+ def _write(self, filename, cmd):
+ filepath = self._path(filename)
+ with open(filepath, 'w') as f:
+ f.write("%s" % cmd)
+ LOG.info("write(%s) <- %s", filepath, cmd)
+
+ def _wait_flag_file_to_exist(self, filename, timeout):
+ filepath = self._path(filename)
+ while timeout > 0:
+ if os.path.exists(filepath):
+ LOG.info("wait and find file:%s", filepath)
+ return True
+ my_sleep(1)
+ timeout -= 1
+ LOG.info("waiting file to exist:%s", filepath)
+ return False
+
+ def _get_cmd_return_code(self):
+ ret = self._read(constant.VM_CMD_RETURN_CODE_FILE)
+ return ret == constant.VM_CMD_EXCUTE_SUCCES_FLAG_CONTENT
+
+ def _wait_command_done(self):
+ done = self._wait_flag_file_to_exist(constant.VM_CMD_DONE_FLAG_FILE, constant.VM_COMMON_CMD_EXCUTE_TIME_OUT)
+ if done:
+ return self._get_cmd_return_code()
+ else:
+ return 'timeout'
+
+ def _set_cmd(self, cmd):
+ self._write(constant.VM_CMD_CONTENT_FILE, cmd)
+ self._write(constant.VM_CMD_SET_FLAG_FILE, '')
+ ret = self._wait_command_done()
+ if ret:
+ self.clean()
+ return ret
+ else:
+ raise Exception("9pfs command failure: timeout.")
+
+ def wait_up(self):
+ return self._wait_flag_file_to_exist(constant.VM_UP_Flag_FILE, constant.VM_UP_TIME_OUT)
+
+ def config_ip(self, mac, ip):
+ cmd = 'config_ip %s %s' % (mac, ip)
+ return self._set_cmd(cmd)
+
+ def config_gw(self, ip):
+ cmd = 'config_gw %s' % ip
+ return self._set_cmd(cmd)
+
+ def set_pktloop_dpdk(self, macs):
+ """
+ To connect two network devices together in the vm and loop the packets received to another.
+ Use dpdk testpmd to loop the packets. See FSMonitor.
+
+ :param macs: the mac address list of network cards of the vm.
+ :return: True for success, Exception for Failure.
+ """
+ mac_str = ' '.join(macs)
+ cmd = 'set_pktloop_dpdk ' + mac_str
+ return self._set_cmd(cmd)
+
+ def recover_nic_binding(self, macs):
+ """
+ in contrast to set_pktloop_dpdk, disconnect the looping.
+ :param macs: the mac address list of network cards of the vm.
+ :return: True for success, Exception for Failure.
+ """
+ mac_str = ' '.join(macs)
+ cmd = 'recover_nic_binding ' + mac_str
+ return self._set_cmd(cmd)
+
+ def config_amqp(self, identity, server, port=5672, user="guest", passwd="guest"):
+ data = {
+ 'server': server,
+ 'port': port,
+ 'id': identity,
+ 'user': user,
+ 'passwd': passwd
+ }
+ header = "[rabbit]"
+ content = '''
+ user=%(user)s
+ passwd=%(passwd)s
+ host=%(server)s
+ port=%(port)s
+ id=%(id)s''' % data
+ file_name = "amqp.ini"
+ dedented_text = textwrap.dedent(content)
+ self._write(file_name, header+dedented_text)
+ cmd = 'config_amqp %s' % file_name
+ return self._set_cmd(cmd)
+
+ def stop_vstf(self):
+ cmd = "stop_vstf"
+ return self._set_cmd(cmd)
+
+ def __repr__(self):
+ return self.__class__.__name__ + ':' + self.vm_9p_path
+
+
+if __name__ == '__main__':
+ fs = VMConfigBy9pfs('/tmp/tmp4T6p7L')
+ print os.listdir(os.curdir)
+ print fs.config_ip('56:6f:44:a5:3f:a4', '192.168.188.200/23')
+ print fs.config_gw('192.168.188.1')
+ print fs.set_pktloop_dpdk(['56:6f:44:a5:3f:a2', '56:6f:44:a5:3f:a3'])
+ print fs.recover_nic_binding(['56:6f:44:a5:3f:a2', '56:6f:44:a5:3f:a3'])
+ print fs.config_amqp('192.168.188.200', '192.168.188.10')
+ print os.listdir(os.curdir)
diff --git a/vstf/vstf/agent/env/basic/vm_manager.py b/vstf/vstf/agent/env/basic/vm_manager.py
new file mode 100755
index 00000000..e59d6c71
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/vm_manager.py
@@ -0,0 +1,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
diff --git a/vstf/vstf/agent/env/basic/vm_xml_help.py b/vstf/vstf/agent/env/basic/vm_xml_help.py
new file mode 100755
index 00000000..d3116259
--- /dev/null
+++ b/vstf/vstf/agent/env/basic/vm_xml_help.py
@@ -0,0 +1,81 @@
+"""
+Created on 2015-7-2
+
+@author: y00228926
+"""
+xml_head = '''
+<domain type='kvm'>
+ <name>VM_NAME</name>
+ <memory unit='KiB'>VM_MEMORY</memory>
+ <currentMemory unit='KiB'>VM_MEMORY</currentMemory>
+ <!--numatune>
+ <memory mode='strict' nodeset='0'/>
+ </numatune-->
+ <vcpu placement='static'>CPU_NUM</vcpu>
+ <cpu mode='host-passthrough'>
+ </cpu>
+ <os>
+ <type arch='x86_64' >hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <features>
+ <acpi/>
+ <apic/>
+ <pae/>
+ </features>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>restart</on_crash>
+ <devices>
+ <emulator>/usr/bin/qemu-system-x86_64</emulator>'''
+xml_disk = '''
+ <disk type='file' device='disk'>
+ <driver name='qemu' type='IMAGE_TYPE' cache='none' io='native'/>
+ <source file='IMAGE_PATH'/>
+ <target dev='vda' bus='virtio'/>
+ </disk>'''
+
+xml_ctrl_br = '''
+<interface type='bridge'>
+ <mac address='CTRL_MAC'/>
+ <source bridge='CTRL_BR'/>
+ <model type='CTRL_MODEL'/>
+</interface>
+'''
+xml_ovs = '''
+ <interface type='bridge'>
+ <mac address='TAP_MAC'/>
+ <source bridge='BR_NAME'/>
+ <virtualport type='BR_TYPE'>
+ </virtualport>
+ <model type='virtio'/>
+ <driver name='vhost' queues='4'/>
+ <target dev='TAP_NAME'/>
+ </interface>'''
+xml_br = '''
+ <interface type='bridge'>
+ <mac address='TAP_MAC'/>
+ <source bridge='BR_NAME'/>
+ <model type='virtio'/>
+ <target dev='TAP_NAME'/>
+ </interface>'''
+
+xml_pci = '''
+ <hostdev mode='subsystem' type='pci' managed='yes'>
+ <driver name='kvm'/>
+ <source>
+ <address domain='0x0000' bus='0xBUS' slot='0xSLOT' function='0xFUNCTION' />
+ </source>
+ </hostdev>'''
+xml_9p = '''
+ <filesystem type='mount' accessmode='passthrough'>
+ <source dir='9P_PATH'/>
+ <target dir='9pfs'/>
+ </filesystem>'''
+xml_tail = '''
+ <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
+ <listen type='address' address='0.0.0.0'/>
+ </graphics>
+ </devices>
+</domain>'''
+