1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
13 from vstf.common.utils import my_sleep
14 from vstf.agent.env.fsmonitor import constant
16 LOG = logging.getLogger(__name__)
19 class VMConfigBy9pfs(object):
21 host side implemetation of a self-defined communication protocol using libvirt 9pfs to give commands to the Virtual Machine.
25 def __init__(self, vm_9p_path):
27 :param vm_9p_path: The host path of libvirt 9pfs for a vm.
30 self.vm_9p_path = vm_9p_path
33 self._unlink(self._path(constant.VM_CMD_RETURN_CODE_FILE))
34 self._unlink(self._path(constant.VM_CMD_DONE_FLAG_FILE))
36 def _path(self, relative_path):
37 return os.path.join(self.vm_9p_path, relative_path)
39 def _unlink(self, file_path):
41 LOG.info("os.unlink(%s)", file_path)
43 def _read(self, filename):
44 filepath = self._path(filename)
45 with open(filepath, 'r') as f:
47 LOG.info("read(%s) -> %s", filepath, ret)
50 def _write(self, filename, cmd):
51 filepath = self._path(filename)
52 with open(filepath, 'w') as f:
54 LOG.info("write(%s) <- %s", filepath, cmd)
56 def _wait_flag_file_to_exist(self, filename, timeout):
57 filepath = self._path(filename)
59 if os.path.exists(filepath):
60 LOG.info("wait and find file:%s", filepath)
64 LOG.info("waiting file to exist:%s", filepath)
67 def _get_cmd_return_code(self):
68 ret = self._read(constant.VM_CMD_RETURN_CODE_FILE)
69 return ret == constant.VM_CMD_EXCUTE_SUCCES_FLAG_CONTENT
71 def _wait_command_done(self):
72 done = self._wait_flag_file_to_exist(
73 constant.VM_CMD_DONE_FLAG_FILE,
74 constant.VM_COMMON_CMD_EXCUTE_TIME_OUT)
76 return self._get_cmd_return_code()
80 def _set_cmd(self, cmd):
81 self._write(constant.VM_CMD_CONTENT_FILE, cmd)
82 self._write(constant.VM_CMD_SET_FLAG_FILE, '')
83 ret = self._wait_command_done()
88 raise Exception("9pfs command failure: timeout.")
91 return self._wait_flag_file_to_exist(
92 constant.VM_UP_Flag_FILE, constant.VM_UP_TIME_OUT)
94 def config_ip(self, mac, ip):
95 cmd = 'config_ip %s %s' % (mac, ip)
96 return self._set_cmd(cmd)
98 def config_gw(self, ip):
99 cmd = 'config_gw %s' % ip
100 return self._set_cmd(cmd)
102 def set_pktloop_dpdk(self, macs):
104 To connect two network devices together in the vm and loop the packets received to another.
105 Use dpdk testpmd to loop the packets. See FSMonitor.
107 :param macs: the mac address list of network cards of the vm.
108 :return: True for success, Exception for Failure.
110 mac_str = ' '.join(macs)
111 cmd = 'set_pktloop_dpdk ' + mac_str
112 return self._set_cmd(cmd)
114 def recover_nic_binding(self, macs):
116 in contrast to set_pktloop_dpdk, disconnect the looping.
117 :param macs: the mac address list of network cards of the vm.
118 :return: True for success, Exception for Failure.
120 mac_str = ' '.join(macs)
121 cmd = 'recover_nic_binding ' + mac_str
122 return self._set_cmd(cmd)
145 file_name = "amqp.ini"
146 dedented_text = textwrap.dedent(content)
147 self._write(file_name, header + dedented_text)
148 cmd = 'config_amqp %s' % file_name
149 return self._set_cmd(cmd)
153 return self._set_cmd(cmd)
156 return self.__class__.__name__ + ':' + self.vm_9p_path
159 if __name__ == '__main__':
160 fs = VMConfigBy9pfs('/tmp/tmp4T6p7L')
161 print os.listdir(os.curdir)
162 print fs.config_ip('56:6f:44:a5:3f:a4', '192.168.188.200/23')
163 print fs.config_gw('192.168.188.1')
164 print fs.set_pktloop_dpdk(['56:6f:44:a5:3f:a2', '56:6f:44:a5:3f:a3'])
165 print fs.recover_nic_binding(['56:6f:44:a5:3f:a2', '56:6f:44:a5:3f:a3'])
166 print fs.config_amqp('192.168.188.200', '192.168.188.10')
167 print os.listdir(os.curdir)