|
| 1 | + |
| 2 | +import logging,sys,threading,os,time,re,socket |
| 3 | +from comm.ssh_connections import manage_connections, create_connections |
| 4 | +from comm.ssh_engine import go_thread |
| 5 | + |
| 6 | +class case_template_00(manage_connections): |
| 7 | + conn_dict = {} |
| 8 | + required_connections = {} |
| 9 | + |
| 10 | + def __init__(self, conn_cfg, case_parameters, log_folder, log_on_screen): |
| 11 | + self.conn_cfg = conn_cfg |
| 12 | + self.log_folder = log_folder |
| 13 | + self.parameters = case_parameters |
| 14 | + self.log_on_screen = log_on_screen |
| 15 | + self.case_result = None |
| 16 | + |
| 17 | + def run(self): |
| 18 | + self.create_connections() |
| 19 | + logging.critical('=========UAS prepare =========') |
| 20 | + self.case_prepare() |
| 21 | + logging.critical('=========UAS steps ===========') |
| 22 | + self.case_steps() |
| 23 | + logging.critical('=========UAS steps OK ========') |
| 24 | + |
| 25 | + def end(self): |
| 26 | + self.close_all_connections() |
| 27 | + |
| 28 | + def case_cleanup(self): |
| 29 | + pass |
| 30 | + |
| 31 | + def press_y_2_continue(self): |
| 32 | + choice = '' |
| 33 | + while choice != 'y': |
| 34 | + choice=input('Ready to continue? (y/n):').lower() |
| 35 | + |
| 36 | + def get_disk_colon(self): |
| 37 | + return os.path.abspath(__file__)[:2] |
| 38 | + |
| 39 | + def zip_folder(self, target_folder, zip_path_file): |
| 40 | + list_7z = [r'C:/Program Files/7-Zip/7z.exe', r'D:\tools\7-Zip\7z.exe'] |
| 41 | + for win_7z in list_7z: |
| 42 | + if os.path.isfile(win_7z): |
| 43 | + system_cmd = 'del %s & "%s" a -tZip %s %s' % (zip_path_file, win_7z, zip_path_file, target_folder) |
| 44 | + logging.info(system_cmd) |
| 45 | + if 0 != os.system(system_cmd): |
| 46 | + raise Exception(zip_path_file, system_cmd) |
| 47 | + return |
| 48 | + raise Exception('NO_7Z_EXE', system_cmd) |
| 49 | + |
| 50 | + def uas(self, conn_name): |
| 51 | + # python -m http.server 8000 |
| 52 | + try: |
| 53 | + target_folder = self.conn_dict[conn_name].config['uas_root'] |
| 54 | + except: |
| 55 | + target_folder = '/opt' |
| 56 | + |
| 57 | + connection = self.create_conn(conn_name) |
| 58 | + channel = self.create_conn_chan(conn_name, 'linux', close_and_open=True) |
| 59 | + |
| 60 | + zip_file = 'uas.tar.bz2' |
| 61 | + |
| 62 | + if os.name == 'nt': |
| 63 | + tmp_file = 'uas.tar' |
| 64 | + win_7z = r'C:/Program Files/7-Zip/7z.exe' |
| 65 | + os.system('rmdir/S/Q log; del results.log;') |
| 66 | + uas_folder = os.path.basename(os.path.dirname(os.path.realpath(sys.modules['__main__'].__file__))) |
| 67 | + logging.info(uas_folder) |
| 68 | + os.system('cd .. & del %s & "%s" a -ttar %s %s & "%s" a -tgzip %s %s & del %s' %(zip_file,win_7z,tmp_file, uas_folder,win_7z,zip_file,tmp_file,tmp_file)) |
| 69 | + else: |
| 70 | + uas_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 71 | + os.system('cd %s;rm -rf log results.log;cd ..;rm uas.tar.bz2;tar -cjf %s uas/;' %(uas_path,zip_file)) |
| 72 | + |
| 73 | + connection.upload('../%s' %zip_file, target_folder) |
| 74 | + channel.exec_command('''sh -c 'cd %s; rm uas -rf; mkdir -p %s/uas;tar -xf %s -C %s/uas --strip-components=1; cd %s/uas; rm log -rf; rm results.log -f;pyclean .';''' %(target_folder,target_folder,zip_file,target_folder,target_folder), timeout=20) |
| 75 | + |
| 76 | + recv_buff = channel.exec_command('cat ~/.bashrc') |
| 77 | + if not recv_buff.count('alias uu='): |
| 78 | + channel.exec_command('''echo "alias uu='cd %s/uas/;python uas.py -r '" >> ~/.bashrc''' %(target_folder)) |
| 79 | + |
| 80 | + def ssh(self, conn_name,): |
| 81 | + self.create_conn(conn_name) |
| 82 | + channel = self.create_conn_chan(conn_name, 'linux') |
| 83 | + channel.posix_shell() |
| 84 | + |
| 85 | + def dl(self, conn_name, remote_folder, local_folder, file_wildcard=None): |
| 86 | + self.create_conn(conn_name) |
| 87 | + conn = self.conn_dict[conn_name] |
| 88 | + conn.download_folder(remote_folder, local_folder, file_wildcard=file_wildcard) |
| 89 | + |
| 90 | + def ul(self, conn_name, local_folder, remote_folder, file_wildcard=None): |
| 91 | + self.create_conn(conn_name) |
| 92 | + conn = self.conn_dict[conn_name] |
| 93 | + if os.path.isfile(local_folder): |
| 94 | + return conn.upload(local_folder, remote_folder) |
| 95 | + |
| 96 | + conn.upload_folder(local_folder, remote_folder, file_wildcard=file_wildcard) |
| 97 | + |
| 98 | + def ping_test(self, channel, target_ip, source_ip='', retry_times=3): |
| 99 | + if source_ip: |
| 100 | + source_ip = '-I %s' %(source_ip) |
| 101 | + for iii in range(retry_times): |
| 102 | + recv_buff = channel.exec_command('ping %s -c3 -i0.1 -W1 %s' % (target_ip, source_ip)) |
| 103 | + if recv_buff.count(', 0% packet loss'): |
| 104 | + return |
| 105 | + raise Exception('PING', recv_buff) |
| 106 | + |
| 107 | + def web_check(self, channel, url): |
| 108 | + recv_buff = '' |
| 109 | + for iii in range(5): |
| 110 | + try: |
| 111 | + recv_buff = channel.exec_command(url, timeout=3) |
| 112 | + if recv_buff.count('Failed') or recv_buff.count('404'): |
| 113 | + raise Exception('WEB', recv_buff) |
| 114 | + return |
| 115 | + except: |
| 116 | + logging.exception('---- web_check %s -----' %(channel.hostname)) |
| 117 | + time.sleep(1) |
| 118 | + raise Exception(channel.hostname, recv_buff) |
| 119 | + |
| 120 | + def create_ssh_by_list(self, conn_list): |
| 121 | + def create_linux(conn_name): |
| 122 | + self.create_conn(conn_name) |
| 123 | + self.create_conn_chan(conn_name, 'linux') |
| 124 | + |
| 125 | + for conn_name in conn_list: |
| 126 | + go_thread(create_linux, args=(conn_name,), kwargs={}) |
| 127 | + go_thread.join_threads(60) |
| 128 | + |
| 129 | + def threading_channel(self, chan, command, timeout=2592000): |
| 130 | + try: |
| 131 | + if chan.thread_handler.isAlive(): |
| 132 | + logging.critical('%s is occupied.' %(chan.set_name())) |
| 133 | + raise Exception('go_thread_channel', '%s is occupied.' %(chan.set_name())) |
| 134 | + except AttributeError: |
| 135 | + pass |
| 136 | + |
| 137 | + def wrapper(): |
| 138 | + try: |
| 139 | + chan.threading_buff = chan.exec_command(command, timeout=timeout) |
| 140 | + except: |
| 141 | + logging.exception(f'====== threading_channel {chan.get_name()} [{command}] timeout={timeout} ======') |
| 142 | + |
| 143 | + chan.thread_handler = threading.Thread(target=wrapper, args=()) |
| 144 | + chan.thread_handler.setDaemon(True) |
| 145 | + chan.thread_handler.start() |
| 146 | + |
| 147 | + def threading_channel_join(self, timeout=9): |
| 148 | + for conn_name, conn in self.conn_dict.items(): |
| 149 | + for chan_name, chan in conn.chan_dict.items(): |
| 150 | + try: |
| 151 | + if chan.thread_handler.isAlive(): |
| 152 | + chan.thread_handler.join(timeout=timeout) |
| 153 | + except AttributeError: |
| 154 | + pass |
| 155 | + |
| 156 | + def threading_channel_end(self): |
| 157 | + for conn_name, conn in self.conn_dict.items(): |
| 158 | + for chan_name, chan in conn.chan_dict.items(): |
| 159 | + try: |
| 160 | + #logging.info(f'{conn_name} {chan_name}') |
| 161 | + chan.send(chr(3)) |
| 162 | + except: #AttributeError: |
| 163 | + pass |
| 164 | + |
| 165 | + def throughput_translate(self, throughput): |
| 166 | + if throughput.endswith('M'): |
| 167 | + return throughput[:-1] |
| 168 | + elif throughput.endswith('K'): |
| 169 | + return float(throughput[:-1]) / 1024 |
| 170 | + elif throughput.endswith('G'): |
| 171 | + return float(throughput[:-1]) * 1024 |
| 172 | + else: |
| 173 | + return 0 |
| 174 | + |
| 175 | + def kvm(self, conn_name): |
| 176 | + self.create_conn(conn_name) |
| 177 | + channel = self.create_conn_chan(conn_name, 'linux') |
| 178 | + channel.exec_command('yum install -y --nogpgcheck libvirt qemu-kvm virt-install libvirt-daemon*', timeout=600) |
| 179 | + channel.exec_command('systemctl start libvirtd.service', timeout=60) |
| 180 | + recv_buff = channel.exec_command('service libvirtd status', ) |
| 181 | + if not recv_buff.count('running'): |
| 182 | + raise Exception('libvirtd', recv_buff) |
| 183 | + |
| 184 | + # qcows access permission denied: |
| 185 | + # vi /etc/libvirt/qemu.conf |
| 186 | + # user = "root" |
| 187 | + # group = "root" |
| 188 | + # service libvirtd restart |
| 189 | + # error: Cannot access storage file |
| 190 | + channel.exec_command(''' |
| 191 | +sed -i 's/#user = "root"/user = "root"/' /etc/libvirt/qemu.conf |
| 192 | +sed -i 's/#group = "root"/group = "root"/' /etc/libvirt/qemu.conf |
| 193 | +''') |
| 194 | + channel.exec_command('systemctl enable libvirtd;systemctl restart libvirtd') |
| 195 | + |
| 196 | + recv_buff = channel.exec_command('virsh list') |
| 197 | + if recv_buff.count(' Id Name State'): |
| 198 | + return |
| 199 | + if recv_buff.count('error: failed to connect to the hypervisor'): |
| 200 | + channel.exec_command('ssystemctl start libvirtd.service') |
| 201 | + recv_buff = channel.exec_command('virsh list') |
| 202 | + if recv_buff.count(' Id Name State'): |
| 203 | + return |
| 204 | + raise Exception('virsh', recv_buff) |
| 205 | + |
| 206 | + recv_buff = channel.exec_command('lsmod | grep kvm --color=never') |
| 207 | + if not recv_buff.count('kvm_intel'): |
| 208 | + raise Exception('KVM_Module', recv_buff) |
| 209 | + |
| 210 | + def date(self, host_list=None): |
| 211 | + if not host_list: |
| 212 | + host_list = self.host_list |
| 213 | + else: |
| 214 | + if host_list.__class__ == str: |
| 215 | + host_list = [host_list,] |
| 216 | + |
| 217 | + for conn_name in host_list: |
| 218 | + self.create_conn(conn_name) |
| 219 | + self.create_conn_chan(conn_name, 'linux') |
| 220 | + |
| 221 | + ts_now = time.strftime("%d %b %Y %H:%M:%S") |
| 222 | + for conn_name in host_list: |
| 223 | + channel = self.conn_dict[conn_name].linux |
| 224 | + go_thread(channel.exec_command, args=(f'date -s "{ts_now}";sed -i "s/#MaxSessions 10/MaxSessions 100/" /etc/ssh/sshd_config; service sshd restart',), kwargs={'timeout': 9}) |
| 225 | + go_thread.join_threads() |
| 226 | + |
| 227 | + def vcpupin(self, vm_name): |
| 228 | + vm_dict = self.vm_cfg_dict[vm_name] |
| 229 | + conn_name = vm_dict['conn_name'] |
| 230 | + connection = self.create_conn(conn_name) |
| 231 | + channel = self.create_conn_chan(conn_name, 'linux') |
| 232 | + |
| 233 | + host_cpu_list = vm_dict['vcpupin'] |
| 234 | + if vm_dict['vcpus'] != len(host_cpu_list): |
| 235 | + raise Exception('vcpupin', '%s CPU numbers not match!' %(vm_name)) |
| 236 | + for iii in range(vm_dict['vcpus']): |
| 237 | + # virsh vcpupin ipvs1 0 1 |
| 238 | + channel.exec_command('virsh vcpupin %s %d %d' %(vm_name, iii, host_cpu_list[iii])) |
| 239 | + |
| 240 | + def create_ns_chan(self, ns_name, conn_name, chan_name=''): |
| 241 | + if not chan_name: |
| 242 | + chan_name = f'linux_{ns_name}' |
| 243 | + self.create_conn(conn_name) |
| 244 | + chan = self.create_conn_chan(conn_name, chan_name, close_and_open=True) |
| 245 | + recv_buff = chan.exec_command(f'ip netns exec {ns_name} bash') |
| 246 | + if recv_buff.count('Cannot open network namespace'): |
| 247 | + raise Exception(ns_name, recv_buff) |
| 248 | + return chan |
| 249 | + |
| 250 | + # lldptool -n -t -i ens3f0 |
| 251 | + def vf(self, pf_name, host_list=None, host_name=None, vf_num=4): |
| 252 | + # pf_name = 'enp59s0f1' |
| 253 | + if not host_list: |
| 254 | + if not host_name: |
| 255 | + raise Exception('host_list or host_name') |
| 256 | + host_list = [host_name, ] |
| 257 | + |
| 258 | + grub2_cfg = '/boot/efi/EFI/centos/grub.cfg' |
| 259 | + for host_name in host_list: |
| 260 | + reboot_request = False |
| 261 | + connection = self.create_conn(host_name) |
| 262 | + channel = self.create_conn_chan(host_name, 'linux') |
| 263 | + |
| 264 | + #recv_buff = channel.exec_command(f'cat {grub2_cfg}') |
| 265 | + #if not recv_buff.count('hugepagesz=1G'): |
| 266 | + # channel.exec_command(f"sed -i 's/linux16 \/vmlinuz-3.10.0-693.el7.x86_64 root=.*$/&default_hugepagesz=1G hugepagesz=1G hugepages=16/' {grub2_cfg}") |
| 267 | + # reboot_request = True |
| 268 | + |
| 269 | + channel.exec_command(f'ethtool -K {pf_name} rxvlan off') |
| 270 | + channel.exec_command(f'ethtool -K {pf_name} txvlan off') |
| 271 | + channel.exec_command('modprobe i40evf') |
| 272 | + |
| 273 | + recv_buff = channel.exec_command(f'ethtool -i {pf_name}') |
| 274 | + # bus-info: 0000:3b:00.1 |
| 275 | + bus_info = re.search(r'bus-info:\s+(\S+)', recv_buff).group(1) |
| 276 | + recv_buff = channel.exec_command(f'find /sys/devices -name sriov_numvfs | grep {bus_info} --color=never') |
| 277 | + # /sys/devices/pci0000:3a/0000:3a:00.0/0000:3b:00.1/sriov_numvfs |
| 278 | + config_path = re.search(r'[\r\n]+(\/sys\/devices\S+\/sriov_numvfs)', recv_buff).group(1) |
| 279 | + channel.exec_command(f'echo {vf_num} > {config_path}', timeout=66) |
| 280 | + channel.exec_command('lspci | grep Ethernet --color=never') |
| 281 | + # 3b:0a.0 Ethernet controller: Intel Corporation XL710/X710 Virtual Function (rev 02) |
| 282 | + # 3b:0a.1 Ethernet controller: Intel Corporation XL710/X710 Virtual Function (rev 02) |
| 283 | + # 3b:0a.2 Ethernet controller: Intel Corporation XL710/X710 Virtual Function (rev 02) |
| 284 | + # 3b:0a.3 Ethernet controller: Intel Corporation XL710/X710 Virtual Function (rev 02) |
| 285 | + |
| 286 | + ''' |
| 287 | +/opt/mellanox/iproute2/sbin/tc qdisc del dev vxlan1cx5 ingress |
| 288 | + ''' |
| 289 | + |
| 290 | + def iperf(self, server_conn='vm69188', client_conn='testbed', iperf_opt = '-u -l64 -b0 -R', port_num=24, server_ip=None, duration=120): |
| 291 | + self.port_list = [i for i in range(61000, 61000 + port_num)] |
| 292 | + |
| 293 | + if not server_ip: |
| 294 | + server_ip = self.conn_cfg[server_conn]['host'] |
| 295 | + |
| 296 | + self.create_conn(server_conn) |
| 297 | + chan_server = self.create_conn_chan(server_conn, 'linux') |
| 298 | + |
| 299 | + self.create_conn(client_conn) |
| 300 | + chan_client = self.create_conn_chan(client_conn, 'linux') |
| 301 | + |
| 302 | + for port in self.port_list: |
| 303 | + self.create_conn_chan(client_conn, f"linux_{port}") |
| 304 | + chan_server.exec_command(f'iperf3 -s -p{port} -1 -D') |
| 305 | + |
| 306 | + #12000 #86400 |
| 307 | + timeout = duration + 120 |
| 308 | + time.sleep(1) |
| 309 | + |
| 310 | + # for port in self.port_list: |
| 311 | + port_num = len(self.port_list) |
| 312 | + for iii in range(port_num): |
| 313 | + port = self.port_list[iii] |
| 314 | + |
| 315 | + chan = self.conn_dict[client_conn].chan_dict[f"linux_{port}"] |
| 316 | + chan.log_on_screen = False |
| 317 | + |
| 318 | + go_thread(chan.exec_command, |
| 319 | + args=( |
| 320 | + f'timeout {duration} iperf3 -c {server_ip} -p{port} -i3 -t{duration} -P1 -Z --get-server-output {iperf_opt}',), # 16 streams |
| 321 | + kwargs={'timeout': timeout}) |
| 322 | + |
| 323 | + go_thread(chan_server.exec_command, args=('sar -n DEV 3',), kwargs={'timeout': timeout}, wait_finish=False) |
| 324 | + go_thread(chan_client.exec_command, args=('sar -n DEV 3',), kwargs={'timeout': timeout}, wait_finish=False) |
| 325 | + |
| 326 | + go_thread.join_threads(timeout) |
| 327 | + self.threading_channel_end() |
| 328 | + |
| 329 | + time.sleep(1) |
| 330 | + chan_server.exec_command("ps -ef |grep iperf3 |awk '{print $2}'|xargs kill -9") |
| 331 | + |
0 commit comments