9 from vstf.common.utils import my_sleep
10 from vstf.agent.env.fsmonitor import constant
12 LOG = logging.getLogger(__name__)
15 class VMConfigBy9pfs(object):
17 host side implemetation of a self-defined communication protocol using libvirt 9pfs to give commands to the Virtual Machine.
21 def __init__(self, vm_9p_path):
23 :param vm_9p_path: The host path of libvirt 9pfs for a vm.
26 self.vm_9p_path = vm_9p_path
29 self._unlink(self._path(constant.VM_CMD_RETURN_CODE_FILE))
30 self._unlink(self._path(constant.VM_CMD_DONE_FLAG_FILE))
32 def _path(self, relative_path):
33 return os.path.join(self.vm_9p_path, relative_path)
35 def _unlink(self, file_path):
37 LOG.info("os.unlink(%s)", file_path)
39 def _read(self, filename):
40 filepath = self._path(filename)
41 with open(filepath, 'r') as f:
43 LOG.info("read(%s) -> %s", filepath, ret)
46 def _write(self, filename, cmd):
47 filepath = self._path(filename)
48 with open(filepath, 'w') as f:
50 LOG.info("write(%s) <- %s", filepath, cmd)
52 def _wait_flag_file_to_exist(self, filename, timeout):
53 filepath = self._path(filename)
55 if os.path.exists(filepath):
56 LOG.info("wait and find file:%s", filepath)
60 LOG.info("waiting file to exist:%s", filepath)
63 def _get_cmd_return_code(self):
64 ret = self._read(constant.VM_CMD_RETURN_CODE_FILE)
65 return ret == constant.VM_CMD_EXCUTE_SUCCES_FLAG_CONTENT
67 def _wait_command_done(self):
68 done = self._wait_flag_file_to_exist(constant.VM_CMD_DONE_FLAG_FILE, constant.VM_COMMON_CMD_EXCUTE_TIME_OUT)
70 return self._get_cmd_return_code()
74 def _set_cmd(self, cmd):
75 self._write(constant.VM_CMD_CONTENT_FILE, cmd)
76 self._write(constant.VM_CMD_SET_FLAG_FILE, '')
77 ret = self._wait_command_done()
82 raise Exception("9pfs command failure: timeout.")
85 return self._wait_flag_file_to_exist(constant.VM_UP_Flag_FILE, constant.VM_UP_TIME_OUT)
87 def config_ip(self, mac, ip):
88 cmd = 'config_ip %s %s' % (mac, ip)
89 return self._set_cmd(cmd)
91 def config_gw(self, ip):
92 cmd = 'config_gw %s' % ip
93 return self._set_cmd(cmd)
95 def set_pktloop_dpdk(self, macs):
97 To connect two network devices together in the vm and loop the packets received to another.
98 Use dpdk testpmd to loop the packets. See FSMonitor.
100 :param macs: the mac address list of network cards of the vm.
101 :return: True for success, Exception for Failure.
103 mac_str = ' '.join(macs)
104 cmd = 'set_pktloop_dpdk ' + mac_str
105 return self._set_cmd(cmd)
107 def recover_nic_binding(self, macs):
109 in contrast to set_pktloop_dpdk, disconnect the looping.
110 :param macs: the mac address list of network cards of the vm.
111 :return: True for success, Exception for Failure.
113 mac_str = ' '.join(macs)
114 cmd = 'recover_nic_binding ' + mac_str
115 return self._set_cmd(cmd)
117 def config_amqp(self, identity, server, port=5672, user="guest", passwd="guest"):
132 file_name = "amqp.ini"
133 dedented_text = textwrap.dedent(content)
134 self._write(file_name, header+dedented_text)
135 cmd = 'config_amqp %s' % file_name
136 return self._set_cmd(cmd)
140 return self._set_cmd(cmd)
143 return self.__class__.__name__ + ':' + self.vm_9p_path
146 if __name__ == '__main__':
147 fs = VMConfigBy9pfs('/tmp/tmp4T6p7L')
148 print os.listdir(os.curdir)
149 print fs.config_ip('56:6f:44:a5:3f:a4', '192.168.188.200/23')
150 print fs.config_gw('192.168.188.1')
151 print fs.set_pktloop_dpdk(['56:6f:44:a5:3f:a2', '56:6f:44:a5:3f:a3'])
152 print fs.recover_nic_binding(['56:6f:44:a5:3f:a2', '56:6f:44:a5:3f:a3'])
153 print fs.config_amqp('192.168.188.200', '192.168.188.10')
154 print os.listdir(os.curdir)