def server_allow_icmp_pkts(se, svr_vm_hdl, svr_ip): # Get server side IP for the SE se_back_ip = se_get_ip_for_server(se, svr_ip) server_vm = get_server_by_handle(svr_vm_hdl).vm() cmd = 'iptables -D INPUT -p icmp -s %s -j DROP' % se_back_ip out = server_vm.execute_command(cmd) logger.debug('Cmd %s Output: %s' % (cmd, out))
def negative_update_server(pool_name, handle, **kwargs): """ :param pool_name: :param handle: :param kwargs: :return: """ server = infra_utils.get_server_by_handle(handle) response_code, json_pool_data = rest.get('pool', name=pool_name) if kwargs.get('port'): for index, rest_server in enumerate(json_pool_data.get('servers')): json_server_data = server.get_json() server_ip = json_server_data.get('ip') if server_ip and server_ip.get( 'addr') == rest_server['ip']['addr']: json_pool_data['servers'][index]['port'] = kwargs.get('port') try: rest.put('pool', name=pool_name, data=json_pool_data) logger_utils.fail('No exception was raised in negative test case') except Exception as e: logger.info('Field port must be in the range 1-65535') return True
def get_hmon_stats(pool_name, hm_name, handle, field1='', field2=''): """ :param pool_name: :param hm_name: :param handle: :param field1: :param field2: :return: """ resp_code, resp_data = rest.get('pool', name=pool_name, path='/runtime/server/hmonstat') common.check_response_for_errors(resp_data) # Check if server is in handle format or name if ':' in handle: name = handle else: server = infra_utils.get_server_by_handle(handle) name = server.ip() + ':' + str(server.port()) logger.debug('server_name', name) shm = resp_data[0].get('server_hm_stat') for server in shm: if name == server.get('server_name'): for hm in server[field1]: if hm_name == hm.get('health_monitor_name'): if field2: return hm[field2] else: return hm
def udp_server(servers, port, **kwargs): """ API Helps to start UDP Listen on given Server and Port Args: :param servers: list of server handles want to Listen on Server :type servers: List :param port: listener port :type port: int/str Raises: KeyError """ if not servers: servers = get_all_server_handle() elif isinstance(servers, basestring): servers = [servers] for server_handle in servers: server = get_server_by_handle(server_handle) server_vm = server.vm() server_ip = server.ip() cmd = 'python /root/common/scripts/udp_server.py --ip %s --p %s &> /tmp/udp_server_out_%s &' % ( server_ip, port, server_ip) logger.info("udp_server command: %s" % cmd) server_vm.execute_command(cmd, log_error=False) sleep(10) out = server_vm.execute_command( 'ps aux | grep \'udp_server.py\' | grep -v grep ') if not out: fail("UDP Server Process not started .. %s " % out) out = server_vm.execute_command('cat /tmp/udp_server_out_%s' % server_ip) if 'starting' not in ''.join(out): error("UDP Server not started .. %s " % out)
def _update_server_model(handle, **kwargs): """ :param handle: :param kwargs: :return: """ server = infra_utils.get_server_by_handle(handle) server.updateServer(**kwargs)
def iptables_drop_from_source(server_handle, source_ip): """ :param server_handle: :param source_ip: :return: """ server = get_server_by_handle(server_handle) server_vm = server.vm() server_vm.execute_command('iptables -A INPUT -s %s -j DROP' % source_ip)
def get_server_ip(pool_name, handle, core=0): """ :param pool_name: :param handle: :param core: :return: """ server = infra_utils.get_server_by_handle(handle) return server.ip()
def nginx_down(handle): """ :param handle: :return: """ server = infra_utils.get_server_by_handle(handle) logger.info('nginx_down : %s' % (handle)) server.vm().bring_nginx_server_down(handle) server.vm().reload_server_context_nginx()
def nginx_up(handle): """ :param handle: :return: """ server = infra_utils.get_server_by_handle(handle) logger.info('nginx_up: %s ' % server) server.vm().bring_nginx_server_up(handle) server.vm().reload_server_context_nginx() server.vm().check_if_servers_up()
def parse_server_string(servers): """ :param servers: :return: """ parsed = [] if not servers: return parsed servers = servers.split(';') for server in servers: server_model = infra_utils.get_server_by_handle(server) parsed.append('%s:%s' % (server_model.ip(), server_model.port())) return parsed
def scapy_server(server, command): """ API Helps to start scapy from Client by executing given scapy command Args: :param server: server handles to send scapy traffic :type server: server :param command: sacpy command to send :type command: str """ cmd = 'echo "%s\n">>/tmp/scapy_cmd && touch /tmp/scapy_cmd_done' % command logger.info("scapy_server command: %s" % cmd) server_vm = get_server_by_handle(server).vm() out = server_vm.execute_command(cmd) sleep(1) logger.info("Started scapy on server : %s" % out)
def get_all_hmon_stats(pool_name, handle): resp_code, json_pool_data = rest.get('pool', name=pool_name, path='/runtime/server/hmonstat') common.check_response_for_errors(json_pool_data) # Check if server is in handle format or name if ':' in handle: name = handle else: server = infra_utils.get_server_by_handle(handle) name = server.ip() + ':' + str(server.port()) logger.info('server_name: %s' % name) all_shm = json_pool_data[0].get('server_hm_stat') logger.info("type_all_shm: %s, all_shm: %s" % (type(all_shm), all_shm)) for shm in all_shm: logger.info("type_shm: %s, shm: %s" % (type(shm), shm)) if name == shm.get('server_name'): return shm
def stop_udp_clients_and_servers(client=None, servers=None, server_handle=None): """ API helps to stop udp on client and servers Args: :param client: client handles to send UDP Traffic :type client: str :param servers: list of server handles want to Listen on Server :type servers: List :param server_handle: server handles want to stop udp listen, if case want to stop on signle server :type server_handle: str Raises: KeyError """ kill_client_command = 'pkill -9 -f udp_client.py' if client: client_vm, client_ip = get_client_by_handle(client) client_vm.execute_command(kill_client_command, log_error=False) if not servers: servers = get_all_server_handle() elif isinstance(servers, basestring): servers = [servers] for server in servers: server_vm = get_server_by_handle(server).vm() if server_handle == server: pids = 'ps -aux |grep \"%s\" | grep -v grep |awk \"{print $2;}\"' % server pids = ' '.join(pids).replace('\n', '') kill_server_command = 'sudo kill -9 %s' % pids else: kill_server_command = 'pkill -9 -f udp_server.py' server_vm.execute_command(kill_server_command, log_error=False)
def stop_scapy_clients_and_servers(client, server): """ API helps to stop scapy on client and servers Args: :param client: client handles to stop Scapy Traffic :type client: str :param server: server handles to stop scapy Listen on Server :type server: str Raises: KeyError """ reset_iptables_command = 'iptables -F' kill_client_command = 'pkill -9 -f scapy' client_vm, client_ip = get_client_by_handle(client) client_vm.execute_command(kill_client_command, log_error=False) client_vm.execute_command(reset_iptables_command) kill_server_command = 'pkill -9 -f scapy' server_vm = get_server_by_handle(server).vm() server_vm.execute_command(kill_server_command, log_error=False) server_vm.execute_command(reset_iptables_command)
def _delete_server_backend(server_handle, pool, cleanup_backend): server = infra_utils.get_server_by_handle(server_handle) if not server: logger_utils.fail('Server:%s, Could not able find' % server_handle) server.deleteBackend(cleanup_backend)
def scapy_listen(servers, port, **kwargs): """ API helps to start scapy listen on servers for given port Args: :param server: server handles want to Listen on Server :type server: str :param port: On which port Scapy listen :type port: str Raises: KeyError """ out = [] params = [] for key in kwargs: found = re.match('(.*)_(delay|drop|upon)', key) params.append('-' + found.group(1)) params.append(found.group(2)) if found.group(2) == 'delay' or found.group(2) == 'upon': params.append(kwargs.get(key)) found = re.match('([^\d]+)([\d]+)-?([\d]+)?', servers) if found is None: fail('ERROR! Example server range format s1 or s1-10') server_prefix = found.group(1) server_start = int(found.group(2)) if found.group(3): server_end = int(found.group(3)) else: server_end = 1 tmp_args = "-server -H" server_vm = 0 eth_int = '' for i in range(server_start, server_end + 1): server = get_server_by_handle(server_prefix + str(i)) server_vm = server.vm() server_ip = server.ip() tmp_args = tmp_args + ' ' + server_ip iptable_cmd = 'iptables -A OUTPUT -p tcp --tcp-flags RST RST -s ' + \ server_ip + ' -j DROP' server_vm.execute_command(iptable_cmd) eth_int = server_vm.get_eth_of_app_server(server_prefix + str(i)) tmp_args = tmp_args + ' -P ' + str(port) + ' -i ' + eth_int + ' '.join( params) logger.info( 'nohup /root/common/scapy/scapy_terminals.py %s >& /tmp/tmp &' % tmp_args) server_vm.execute_command_tcptest( 'nohup /root/common/scapy/scapy_terminals.py %s >& /tmp/tmp &' % tmp_args) tmp_out = server_vm.execute_command('pgrep scapy') proc_infos = tmp_out try: procId = proc_infos[0] except: logger.warning('Cannot get process id for scapy server') procId = 'None' out.append(procId) return out
def get_server_runtime(pool_name, handle=None, field1=None, field2=None, fetch_all=False, **kwargs): """ :param pool_name: :param handle: :param field1: :param field2: :param fetch_all: :param kwargs: :return: """ # pool_runtime = get_pool_runtime(pool_name) # if 'server_detail' not in pool_runtime: # raise RuntimeError("pool_lib.get_server_runtime: \ # 'server_detail' not found in GET response") # server_detail = pool_runtime['server_detail'] fetch_all = ast.literal_eval(str(fetch_all)) server_detail = get_pool_nested(pool_name, 'server/detail?page_size=500', fetch_all=fetch_all, **kwargs) if not handle: # all servers if field1: if field1 in server_detail[0]: if field2: return [s[field1][field2] for s in server_detail] else: return [s[field1] for s in server_detail] else: logger_utils.fail("pool_lib.get_server_runtime: field " + field1 + " \ not found in server_detail") else: return server_detail else: server_model = infra_utils.get_server_by_handle(handle) # ?not sure the format of this list in json for server in server_detail: port = server_model.port() if isinstance(port, list): port = port[0] if server['ip_addr']['addr'] == server_model.ip() and \ server['port'] == port: if field1: if field1 in server: if field2: return server[field1][field2] else: return server[field1] else: logger.info("SERVER:%s" % str(server)) logger_utils.fail( "pool_lib.get_server_runtime: field " + field1 + " not found in server with ip " + server_model.ip()) else: return server logger_utils.fail('pool_lib.get_server_runtime: server ip not found \ in list of server_details: %s' % server_model.ip())