def db_table_contain(table_name, db_type, condition, line, db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd): if db_type in ["mysql", "MySQL"]: command = 'mysql -u {db_user} -p{db_passwd} -e "select * from {table_name}"' \ ' {db_name} --silent --raw > /tmp/mysql_out'.format(**locals()) fabric_run_command(command) result = fabric_sudo_command('grep -c "{line}" /tmp/mysql_out'.format(**locals())) elif db_type in ["postgresql", "PostgreSQL"]: command = 'psql -U {db_user} -d {db_name} -c "select * from {table_name}" > /tmp/pgsql_out'.format(**locals()) fabric_run_command(command) result = fabric_sudo_command('grep -c "{line}" /tmp/pgsql_out'.format(**locals())) elif world.f_cfg.db_type == "cql": result = -1 # command = 'for table_name in dhcp_option_scope host_reservations lease4 lease6;' \ # ' do cqlsh --keyspace=keatest --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ # ' ; done'.format(**locals()) # fabric_run_command(command) else: assert False, "db type {db_type} not recognized/not supported".format(**locals()) if condition is not None: if int(result) > 0: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".' \ ' That is to much.'.format(db_type, table_name, result, line) else: if int(result) < 1: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".'.format(db_type, table_name, result, line)
def use_config_set(number): if number not in config_file_set: assert False, "There is no such config file set" make_file('named.conf', _patch_config(config_file_set[number][0])) make_file('rndc.conf', config_file_set[number][1]) make_file('fwd.db', config_file_set[number][2]) make_file('rev.db', config_file_set[number][3]) world.cfg["dns_log_file"] = '/tmp/dns.log' make_file('bind.keys', keys) fabric_sudo_command('mkdir -p %s' % os.path.join(world.f_cfg.dns_data_path, 'namedb')) fabric_send_file('named.conf', os.path.join(world.f_cfg.dns_data_path, 'named.conf')) copy_configuration_file('named.conf', 'dns/DNS_named.conf') remove_local_file('named.conf') fabric_send_file('rndc.conf', os.path.join(world.f_cfg.dns_data_path, 'rndc.conf')) copy_configuration_file('rndc.conf', 'dns/DNS_rndc.conf') remove_local_file('rndc.conf') fabric_send_file('fwd.db', os.path.join(world.f_cfg.dns_data_path, 'namedb/fwd.db')) copy_configuration_file('fwd.db', 'dns/DNS_fwd.db') remove_local_file('fwd.db') fabric_send_file('rev.db', os.path.join(world.f_cfg.dns_data_path, 'namedb/rev.db')) copy_configuration_file('rev.db', 'dns/DNS_rev.db') remove_local_file('rev.db') fabric_send_file('bind.keys', os.path.join(world.f_cfg.dns_data_path, 'managed-keys.bind')) copy_configuration_file('bind.keys', 'dns/DNS_managed-keys.bind') remove_local_file('bind.keys')
def regular_file_contain(file_name, condition, line, destination=None): if destination is None: result = fabric_sudo_command('grep -c "%s" %s' % (line, file_name)) else: result = fabric_sudo_command('grep -c "%s" %s' % (line, file_name), destination_host=destination) if condition is not None: if result.succeeded: assert False, 'File {0} contains line/phrase: {1} But it should NOT.'.format(file_name, line) else: if result.failed: assert False, 'File {0} does NOT contain line/phrase: {1} .'.format(file_name, line)
def regular_file_contain(file_name, condition, line, destination=None): if destination is None: result = fabric_sudo_command('grep -c "%s" %s' % (line, file_name)) else: result = fabric_sudo_command('grep -c "%s" %s' % (line, file_name), destination_host=destination) if condition is not None: if result.succeeded: assert False, 'File {0} contains line/phrase: {1} But it should NOT.'.format( file_name, line) else: if result.failed: assert False, 'File {0} does NOT contain line/phrase: {1} .'.format( file_name, line)
def upload_db_reservation(exp_failed=False): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd fail_spotted = False while list_of_all_reservations: each_record = list_of_all_reservations.pop() each_record.build_script() db_reservation = open("db_reservation", 'w') db_reservation.write(each_record.configuration_script) db_reservation.close() remote_db_path = world.f_cfg.tmp_join("db_reservation") fabric_send_file("db_reservation", remote_db_path) copy_configuration_file("db_reservation") remove_local_file("db_reservation") result = fabric_sudo_command('PGPASSWORD={db_passwd} psql -h localhost -U {db_user} -d {db_name} < {remote_db_path}'.format(**locals()), ignore_errors=True) # pgsql insert do not return non zero status on failed command, we need to check stdout if exp_failed: if 'ERROR: current transaction is aborted' in result.stdout: fail_spotted = True else: assert result.succeeded if exp_failed: assert fail_spotted
def execute_shell_cmd(path, save_results=True, dest=world.f_cfg.mgmt_address): result = fabric_sudo_command(path, hide_all=False, ignore_errors=True, destination_host=dest) if save_results: file_name = path.split("/")[-1] + '_output' file_name = generate_file_name(1, file_name) # assert False, type(result.stdout) if not os.path.exists(world.cfg["test_result_dir"]): os.makedirs(world.cfg["test_result_dir"]) myfile = open(world.cfg["test_result_dir"] + '/' + file_name, 'w') myfile.write('Script: ' + path) if result.failed: myfile.write('\nStatus: FAILED\n') else: myfile.write('\nStatus: SUCCEED\n') myfile.write('\nScript stdout:\n' + result.stdout + '\n') myfile.close() assert result.succeeded return result
def upload_db_reservation(exp_failed=False): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd fail_spotted = False while list_of_all_reservations: each_record = list_of_all_reservations.pop() each_record.build_script() db_reservation = open("db_reservation", 'w') db_reservation.write(each_record.configuration_script) db_reservation.close() remote_db_path = world.f_cfg.tmp_join("db_reservation") fabric_send_file("db_reservation", remote_db_path) copy_configuration_file("db_reservation") remove_local_file("db_reservation") result = fabric_sudo_command( 'psql -U {db_user} -d {db_name} < {remote_db_path}'.format( **locals())) if exp_failed: if result.failed: fail_spotted = True else: assert result.succeeded if exp_failed: assert fail_spotted
def execute_shell_cmd(path, arguments=''): result = fabric_sudo_command(path + ' ' + arguments, hide_all=False) file_name = path.split("/")[-1] + '_output' file_name = generate_file_name(1, file_name) # assert False, type(result.stdout) if not os.path.exists(world.cfg["test_result_dir"]): os.makedirs(world.cfg["test_result_dir"]) myfile = open(world.cfg["test_result_dir"] + '/' + file_name, 'w') myfile.write(unicode('Script: ' + path)) if arguments == '': arguments = "no arguments used!" myfile.write(unicode('\nwith arguments: ' + arguments + '\n')) if result.failed: myfile.write(unicode('\nStatus: FAILED\n')) else: myfile.write(unicode('\nStatus: SUCCEED\n')) myfile.write(unicode('\nScript stdout:\n' + result.stdout)) myfile.close() assert result.succeeded return result
def upload_db_reservation(exp_failed=False): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd fail_spotted = False while list_of_all_reservations: each_record = list_of_all_reservations.pop() log.info(each_record.__dict__) each_record.build_script() db_reservation = open("db_reservation", 'w') db_reservation.write(each_record.configuration_script) db_reservation.close() remote_db_path = world.f_cfg.tmp_join("db_reservation") fabric_send_file("db_reservation", remote_db_path) copy_configuration_file("db_reservation") remove_local_file("db_reservation") result = fabric_sudo_command('cqlsh --keyspace=keatest --user=keatest --password=keatest -f ' + remote_db_path)) if exp_failed: if result.failed: fail_spotted = True else: assert result.succeeded if exp_failed: assert fail_spotted
def start_srv(start, process): """ Start ISC-DHCP with generated config. """ if not "conf_option" in world.cfg: world.cfg["conf_option"] = "" world.cfg['log_file'] = build_log_path() fabric_sudo_command('cat /dev/null >' + world.cfg['log_file']) world.cfg["dhcp_log_file"] = world.cfg['log_file'] log = "local7" if world.f_cfg.isc_dhcp_log_facility != "": log = world.f_cfg.isc_dhcp_log_facility world.cfg['log_facility'] = '''\nlog-facility {log};\n'''.format( **locals()) add_defaults() cfg_write() log.debug("Start ISC-DHCP with generated config:") convert_cfg_file(world.cfg["cfg_file"]) fabric_send_file(world.cfg["cfg_file"] + '_processed', world.cfg["cfg_file"] + '_processed') copy_configuration_file(world.cfg["cfg_file"] + '_processed') remove_local_file(world.cfg["cfg_file"]) #set_ethernet_interface() stop_srv() world.cfg['leases'] = build_leases_path() #fabric_sudo_command('echo y |rm ' + world.cfg['leases']) fabric_sudo_command('touch ' + world.cfg['leases']) result = fabric_sudo_command( '(' + os.path.join(world.f_cfg.software_install_path, 'sbin/dhcpd') + ' -cf server.cfg_processed' + ' -lf ' + world.cfg['leases'] + '&); sleep ' + str(world.f_cfg.sleep_time_1) + ';') check_process_result(start, result, process) # clear configs in case we would like make couple configs in one test world.cfg["conf_time"] = "" world.cfg["log_facility"] = "" world.cfg["custom_lines"] = "" world.cfg["conf_option"] = "" world.cfg["conf_vendor"] = ""
def start_srv(start, process): """ Start ISC-DHCP with generated config. """ if not "conf_option" in world.cfg: world.cfg["conf_option"] = "" world.cfg['log_file'] = build_log_path() fabric_sudo_command('cat /dev/null >' + world.cfg['log_file']) world.cfg["dhcp_log_file"] = world.cfg['log_file'] log = "local7" if world.f_cfg.isc_dhcp_log_facility != "": log = world.f_cfg.isc_dhcp_log_facility world.cfg['log_facility'] = '''\nlog-facility {log};\n'''.format(**locals()) add_defaults() cfg_write() log.debug("Start ISC-DHCP with generated config:") convert_cfg_file(world.cfg["cfg_file"]) fabric_send_file(world.cfg["cfg_file"] + '_processed', world.cfg["cfg_file"] + '_processed') copy_configuration_file(world.cfg["cfg_file"] + '_processed') remove_local_file(world.cfg["cfg_file"]) #set_ethernet_interface() stop_srv() world.cfg['leases'] = build_leases_path() #fabric_sudo_command('echo y |rm ' + world.cfg['leases']) fabric_sudo_command('touch ' + world.cfg['leases']) result = fabric_sudo_command('(' + os.path.join(world.f_cfg.software_install_path, 'sbin/dhcpd') + ' -cf server.cfg_processed' + ' -lf ' + world.cfg['leases'] + '&); sleep ' + str(world.f_cfg.sleep_time_1) + ';') check_process_result(start, result, process) # clear configs in case we would like make couple configs in one test world.cfg["conf_time"] = "" world.cfg["log_facility"] = "" world.cfg["custom_lines"] = "" world.cfg["conf_option"] = "" world.cfg["conf_vendor"] = ""
def start_srv(start, process): """ Start kea with generated config """ build_and_send_config_files() v6, v4 = check_kea_status() if process is None: process = "starting" # check process - if None add some. if not v4: result = fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/keactrl start ' + ' & ); sleep ' + str(SLEEP_TIME_1)) check_kea_process_result(start, result, process) else: result = fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/keactrl stop ' + ' & ); sleep ' + str(SLEEP_TIME_1)) check_kea_process_result(start, result, process) result = fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/keactrl start ' + ' & ); sleep ' + str(SLEEP_TIME_1)) check_kea_process_result(start, result, process)
def log_contains_count(server_type, count, line): if server_type == "DHCP": log_file = world.cfg["dhcp_log_file"] elif server_type == "DNS": log_file = world.cfg["dns_log_file"] else: assert False, "No such name as: {server_type}".format(**locals()) result = fabric_sudo_command('grep -c "%s" %s' % (line, log_file)) if count != result: assert False, 'Log has {0} of expected {1} of line: "{2}".'.format(result, count, line)
def db_table_contain(table_name, db_type, condition, line, db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd): if db_type in ["mysql", "MySQL"]: command = 'mysql -u {db_user} -p{db_passwd} -e "select * from {table_name}"' \ ' {db_name} --silent --raw > /tmp/mysql_out'.format(**locals()) fabric_run_command(command) result = fabric_sudo_command( 'grep -c "{line}" /tmp/mysql_out'.format(**locals())) elif db_type in ["postgresql", "PostgreSQL"]: command = 'psql -U {db_user} -d {db_name} -c "select * from {table_name}" > /tmp/pgsql_out'.format( **locals()) fabric_run_command(command) result = fabric_sudo_command( 'grep -c "{line}" /tmp/pgsql_out'.format(**locals())) elif world.f_cfg.db_type == "cql": result = -1 # command = 'for table_name in dhcp_option_scope host_reservations lease4 lease6;' \ # ' do cqlsh --keyspace=keatest --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ # ' ; done'.format(**locals()) # fabric_run_command(command) else: assert False, "db type {db_type} not recognized/not supported".format( **locals()) if condition is not None: if int(result) > 0: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".' \ ' That is to much.'.format(db_type, table_name, result, line) else: if int(result) < 1: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".'.format( db_type, table_name, result, line)
def restart_srv(): stop_srv() fabric_sudo_command('echo y |rm ' + world.cfg['leases']) fabric_sudo_command('touch ' + world.cfg['leases']) fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/dhcpd -cf server.cfg_processed -lf ' + world.cfg['leases'] + '); sleep ' + str(SLEEP_TIME_1) + ';')
def send_ctrl_cmd_via_socket(command, socket_name=None, destination_address=world.f_cfg.mgmt_address, exp_result=0, exp_failed=False): # if command is expected to fail it does not make sense to check response details if exp_failed: # expected result should be default (0) or None assert exp_result in [0, None] # force expected result to None so it is not checked exp_result = None log.info(pprint.pformat(command)) if isinstance(command, dict): command = json.dumps(command) command_file = open(world.cfg["test_result_dir"] + '/command_file', 'w') try: command_file.write(command) except: command_file.close() command_file = open(world.cfg["test_result_dir"] + '/command_file', 'wb') # TODO: why 'w' / 'wb' command_file.write(command) command_file.close() fabric_send_file(world.cfg["test_result_dir"] + '/command_file', 'command_file', destination_host=destination_address) if socket_name is not None: socket_path = world.f_cfg.run_join(socket_name) else: socket_path = world.f_cfg.run_join('control_socket') cmd = 'socat -t 5 UNIX:' + socket_path + ' - <command_file' attempts = 0 while attempts < 3: response = fabric_sudo_command(cmd, hide_all=True, destination_host=destination_address, ignore_errors=exp_failed) if exp_failed: assert response.failed else: assert response.succeeded if str(response) != '': break attempts += 1 fabric_remove_file_command('command_file') result = _process_ctrl_response(response, exp_result) return result
def log_contains_count(server_type, count, line): if server_type == "DHCP": log_file = world.cfg["dhcp_log_file"] elif server_type == "DNS": log_file = world.cfg["dns_log_file"] else: assert False, "No such name as: {server_type}".format(**locals()) result = fabric_sudo_command('grep -c "%s" %s' % (line, log_file)) if count != result: assert False, 'Log has {0} of expected {1} of line: "{2}".'.format( result, count, line)
def start_kea(destination_host): # Start kea services and check if they started ok. # - nohup to shield kea services from getting SIGHUP from SSH # - in a loop check if there is 'server version .* started' expression in the logs; # repeat the loop only for 4 seconds # - sync to disk any logs traced by keactrl or kea services # - display these logs to screen using cat so forge can catch errors in the logs start_cmd = 'nohup ' + os.path.join(world.f_cfg.software_install_path, 'sbin/keactrl') start_cmd += " start < /dev/null > /tmp/keactrl.log 2>&1; SECONDS=0; while (( SECONDS < 4 ));" start_cmd += " do tail /usr/local/var/kea/kea.log 2>/dev/null | grep 'server version .* started' 2>/dev/null;" start_cmd += " if [ $? -eq 0 ]; then break; fi done;" start_cmd += " sync; cat /tmp/keactrl.log" return fabric_sudo_command(start_cmd, destination_host=destination_host)
def restart_srv(): stop_srv() fabric_sudo_command('echo y |rm ' + world.cfg['leases']) fabric_sudo_command('touch ' + world.cfg['leases']) fabric_sudo_command( '(' + os.path.join(world.f_cfg.software_install_path, 'sbin/dhcpd') + ' -6 -cf server.cfg_processed -lf ' + world.cfg['leases'] + '); sleep ' + str(world.f_cfg.sleep_time_1) + ';')
def start_srv(start, process): """ Start kea with generated config """ #build_and_send_config_files() it's now separate step v6, v4 = check_kea_status() if process is None: process = "starting" if not v6: result = fabric_sudo_command('(' + world.f_cfg.software_install_path + 'sbin/keactrl start ' + ' & ); sleep ' + str(world.f_cfg.sleep_time_1)) check_kea_process_result(start, result, process) else: result = fabric_sudo_command('(' + world.f_cfg.software_install_path + 'sbin/keactrl stop ' + ' & ); sleep ' + str(world.f_cfg.sleep_time_1)) result = fabric_sudo_command('(' + world.f_cfg.software_install_path + 'sbin/keactrl start ' + ' & ); sleep ' + str(world.f_cfg.sleep_time_1)) check_kea_process_result(start, result, process)
def upload_db_reservation(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd while list_of_all_reservations: each_record = list_of_all_reservations.pop() each_record.build_script() db_reservation = open("db_reservation", 'w') db_reservation.write(each_record.configuration_script) db_reservation.close() fabric_send_file("db_reservation", os.path.join(world.f_cfg.software_install_path, "etc/kea/db_reservation")) copy_configuration_file("db_reservation") remove_local_file("db_reservation") result = fabric_sudo_command('cat ' + os.path.join(world.f_cfg.software_install_path, 'etc/kea/db_reservation') + ' | psql -U {db_user} -d {db_name}'.format(**locals()))
def _start_kea_with_keactrl(destination_host): # Start kea services and check if they started ok. # - nohup to shield kea services from getting SIGHUP from SSH # - in a loop check if there is 'server version .* started' expression in the logs; # repeat the loop only for 4 seconds # - sync to disk any logs traced by keactrl or kea services # - display these logs to screen using cat so forge can catch errors in the logs start_cmd = 'nohup ' + os.path.join(world.f_cfg.software_install_path, 'sbin/keactrl') start_cmd += " start < /dev/null > /tmp/keactrl.log 2>&1; SECONDS=0; while (( SECONDS < 4 ));" start_cmd += " do tail /usr/local/var/kea/kea.log 2>/dev/null | grep 'server version .* started' 2>/dev/null;" start_cmd += " if [ $? -eq 0 ]; then break; fi done;" start_cmd += " sync; cat /tmp/keactrl.log" return fabric_sudo_command(start_cmd, destination_host=destination_host)
def check_leases(leases_list, backend='memfile', destination=world.f_cfg.mgmt_address): if not isinstance(leases_list, list): leases_list = [leases_list] if backend == 'memfile': for lease in leases_list: if world.f_cfg.proto == 'v4': cmd = "grep -E %s %s | grep -E %s | grep -E -c %s" % ( lease["address"], world.f_cfg.get_leases_path(), lease["hwaddr"], lease["valid_lifetime"]) elif world.f_cfg.proto == 'v6': cmd = "grep -E %s %s | grep -E %s | grep -E -c %s" % ( lease["address"], world.f_cfg.get_leases_path(), lease["duid"], lease["idid"]) else: assert False, "There is something bad, you should never see this :)" result = fabric_sudo_command(cmd, ignore_errors=True, destination_host=destination) if not result.succeeded: assert False, "Lease do NOT contain lease: %s" % json.dumps( lease) # TODO write check if there is more than one entry of the same type elif backend.lower() in ['mysql', 'postgresql', 'pgsql']: for lease in leases_list: if world.f_cfg.proto == 'v4': table = 'lease4' cmd = "grep -E -i %s /tmp/db_out | grep -E -i %s | grep -E -c %s" % ( convert_address_to_hex(lease["address"]), lease["hwaddr"].replace(":", ""), lease["valid_lifetime"]) elif world.f_cfg.proto == 'v6': table = 'lease6' cmd = "grep -E %s /tmp/db_out | grep -E -i %s | grep -E -c %s" % ( lease["address"], lease["duid"].replace(":", ""), lease["idid"]) else: assert False, "There is something bad, you should never see this :)" db_table_contain(table, backend, grep_cmd=cmd, destination=destination) elif backend == 'cassandra': # TODO implement this sometime in the future pass
def db_table_contain(table_name, db_type, line="", grep_cmd=None, expect=True, db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd, destination=world.f_cfg.mgmt_address): # TODO add checking count of records if db_type.lower() == "mysql": if table_name == 'lease6': select = 'select hex(duid), address, iaid, valid_lifetime' elif table_name == 'lease4': select = 'select hex(address), hex(hwaddr), valid_lifetime' else: select = 'select *' command = 'mysql -u {db_user} -p{db_passwd} -e "{select} from {table_name}"' \ ' {db_name} --silent > /tmp/db_out'.format(**locals()) elif db_type.lower() in ["postgresql", "pgsql"]: if table_name == 'lease4': select = "select to_hex(address), encode(hwaddr,'hex'), valid_lifetime" else: select = 'select *' command = 'PGPASSWORD={db_passwd} psql -h localhost -U {db_user} -d {db_name} ' \ '-c "{select} from {table_name}" > /tmp/db_out'.format(**locals()) else: assert False, "db type {db_type} not recognized/not supported".format( **locals()) fabric_run_command(command, destination_host=destination) cmd = 'grep -c "{line}" /tmp/db_out'.format(**locals()) if grep_cmd is not None: cmd = grep_cmd result = fabric_sudo_command(cmd, ignore_errors=True, destination_host=destination) if not expect: if int(result) > 0: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".' \ ' That is to much.'.format(db_type, table_name, result, line) else: if int(result) < 1: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".'.format( db_type, table_name, result, line)
def check_kea_status(destination_address=world.f_cfg.mgmt_address): v6 = 0 v4 = 0 result = fabric_sudo_command(os.path.join(world.f_cfg.software_install_path, "sbin/keactrl") + " status", destination_host=destination_address) # not very sophisticated but easiest fastest way ;) if "DHCPv4 server: inactive" in result: v4 = 0 elif "DHCPv4 server: active" in result: v4 = 1 if "DHCPv6 server: inactive" in result: v6 = 0 elif "DHCPv6 server: active" in result: v6 = 1 return v6, v4
def check_kea_status(destination_address=world.f_cfg.mgmt_address): v6 = 0 v4 = 0 result = fabric_sudo_command(world.f_cfg.software_install_path + "sbin/keactrl status", destination_host=destination_address) # not very sophisticated but easiest fastest way ;) if "DHCPv4 server: inactive" in result: v4 = 0 elif "DHCPv4 server: active" in result: v4 = 1 if "DHCPv6 server: inactive" in result: v6 = 0 elif "DHCPv6 server: active" in result: v6 = 1 return v6, v4
def upload_db_reservation(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd while list_of_all_reservations: each_record = list_of_all_reservations.pop() log.info(each_record.__dict__) each_record.build_script() db_reservation = open("db_reservation", 'w') db_reservation.write(each_record.configuration_script) db_reservation.close() fabric_send_file("db_reservation", os.path.join(world.f_cfg.software_install_path, "etc/kea/db_reservation")) copy_configuration_file("db_reservation") remove_local_file("db_reservation") result = fabric_sudo_command('cqlsh --keyspace=keatest --user=keatest --password=keatest -f ' + os.path.join(world.f_cfg.software_install_path, "etc/kea/db_reservation"))
def log_contains(line, condition, log_file=None): if world.f_cfg.install_method == 'make': if log_file is None: log_file = 'kea.log' log_file = world.f_cfg.log_join(log_file) result = fabric_sudo_command('grep -c "%s" %s' % (line, log_file)) else: if log_file is None: cmd = 'ts=`systemctl show -p ActiveEnterTimestamp isc-kea-dhcp%s-server.service | awk \'{{print $2 $3}}\'`;' % world.proto[ 1] cmd += ' journalctl -u isc-kea-dhcp%s-server.service --since $ts |' % world.proto[ 1] cmd += 'grep -c "%s"' % line result = fabric_sudo_command(cmd) else: log_file = world.f_cfg.log_join(log_file) result = fabric_sudo_command('grep -c "%s" %s' % (line, log_file)) if condition is not None: if result.succeeded: assert False, 'Log contains line: "%s" But it should NOT.' % line else: if result.failed: assert False, 'Log does NOT contain line: "%s"' % line
def log_contains(server_type, condition, line): if server_type == "DHCP": log_file = world.cfg["dhcp_log_file"] elif server_type == "DNS": log_file = world.cfg["dns_log_file"] else: assert False, "No such name as: {server_type}".format(**locals()) result = fabric_sudo_command('grep -c "%s" %s' % (line, log_file)) if condition is not None: if result.succeeded: assert False, 'Log contains line: "%s" But it should NOT.' % line else: if result.failed: assert False, 'Log does NOT contain line: "%s"' % line
def _check_kea_status(destination_address=world.f_cfg.mgmt_address): v4 = False v6 = False result = fabric_sudo_command( os.path.join(world.f_cfg.software_install_path, "sbin/keactrl") + " status", destination_host=destination_address) # not very sophisticated but easiest fastest way ;) if "DHCPv4 server: inactive" in result: v4 = False elif "DHCPv4 server: active" in result: v4 = True if "DHCPv6 server: inactive" in result: v6 = False elif "DHCPv6 server: active" in result: v6 = True return v4, v6
def start_srv(start, process): """ Start ISC-DHCPv6 with generated config. """ if not "conf_option" in world.cfg: world.cfg["conf_option"] = "" world.cfg['log_file'] = build_log_path() fabric_run_command('cat /dev/null >' + world.cfg['log_file']) world.cfg["dhcp_log_file"] = world.cfg['log_file'] log = "local7" if ISC_DHCP_LOG_FACILITY != "": log = ISC_DHCP_LOG_FACILITY world.cfg['log_facility'] = '''\nlog-facility {log};\n'''.format(**locals()) add_defaults() cfg_write() get_common_logger().debug("Start ISC-DHCPv6 with generated config:") convert_cfg_file(world.cfg["cfg_file"]) fabric_send_file(world.cfg["cfg_file"] + '_processed', world.cfg["cfg_file"] + '_processed') copy_configuration_file(world.cfg["cfg_file"] + '_processed') remove_local_file(world.cfg["cfg_file"]) #set_ethernet_interface() stop_srv() world.cfg['leases'] = build_leases_path() fabric_run_command('echo y |rm ' + world.cfg['leases']) fabric_run_command('touch ' + world.cfg['leases']) result = fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/dhcpd -6 -cf server.cfg_processed' + ' -lf ' + world.cfg['leases'] + '&); sleep ' + str(SLEEP_TIME_1) + ';') check_process_result(start, result, process) # clear configs in case we would like make couple configs in one test world.cfg["conf_time"] = "" world.cfg["log_facility"] = "" world.cfg["custom_lines"] = "" world.cfg["conf_option"] = "" world.cfg["conf_vendor"] = ""
def upload_db_reservation(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd while list_of_all_reservations: each_record = list_of_all_reservations.pop() print each_record.__dict__ each_record.build_script() db_reservation = open("db_reservation", 'w') db_reservation.write(each_record.configuration_script) db_reservation.close() fabric_send_file( "db_reservation", world.f_cfg.software_install_path + "etc/kea/db_reservation") copy_configuration_file("db_reservation") remove_local_file("db_reservation") result = fabric_sudo_command( 'cqlsh --keyspace=keatest --user=keatest --password=keatest -f ' + world.f_cfg.software_install_path + "etc/kea/db_reservation")
def upload_db_reservation(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd while list_of_all_reservations: each_record = list_of_all_reservations.pop() each_record.build_script() db_reservation = open("db_reservation", 'w') db_reservation.write(each_record.configuration_script) db_reservation.close() fabric_send_file( "db_reservation", world.f_cfg.software_install_path + "etc/kea/db_reservation") copy_configuration_file("db_reservation") remove_local_file("db_reservation") result = fabric_sudo_command( 'cat ' + world.f_cfg.software_install_path + 'etc/kea/db_reservation | psql -U {db_user} -d {db_name}'.format( **locals()))
def stop_srv(value=False, destination_address=world.f_cfg.mgmt_address): if world.f_cfg.install_method == 'make': result = _stop_kea_with_keactrl( destination_address) # TODO: check result else: if hasattr(world, 'proto'): cmd = 'systemctl stop isc-kea-dhcp%s-server' % world.proto[1] fabric_sudo_command(cmd, destination_host=destination_address) else: for v in [4, 6]: cmd = 'systemctl stop isc-kea-dhcp%s-server' % v fabric_sudo_command(cmd, destination_host=destination_address) cmd = 'systemctl stop isc-kea-ctrl-agent' fabric_sudo_command(cmd, destination_host=destination_address)
def send_through_socket_server_site(socket_path, command, destination_address=world.f_cfg.mgmt_address): if type(command) is unicode: command = command.encode('ascii', 'ignore') command_file = open(world.cfg["dir_name"] + '/command_file', 'w') try: command_file.write(command) except: command_file.close() command_file = open(world.cfg["dir_name"] + '/command_file', 'wb') # TODO: why 'w' / 'wb' command_file.write(command) command_file.close() fabric_send_file(world.cfg["dir_name"] + '/command_file', 'command_file', destination_host=destination_address) world.control_channel = fabric_sudo_command('socat UNIX:' + socket_path + ' - <command_file', hide_all=True, destination_host=destination_address) fabric_remove_file_command('command_file') try: result = json.loads(world.control_channel) log.info(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': '))) world.cmd_resp = result except: log.exception('Problem with parsing json: %s', str(world.control_channel)) world.cmd_resp = world.control_channel return world.cmd_resp
def execute_shell_script(path, arguments): result = fabric_sudo_command(path + ' ' + arguments, hide_all=False) file_name = path.split("/")[-1] + '_output' file_name = generate_file_name(1, file_name) # assert False, type(result.stdout) if not os.path.exists(world.cfg["dir_name"]): os.makedirs(world.cfg["dir_name"]) myfile = open(world.cfg["dir_name"] + '/' + file_name, 'w') myfile.write(unicode('Script: ' + path)) if arguments == '': arguments = "no arguments used!" myfile.write(unicode('\nwith arguments: ' + arguments + '\n')) if result.failed: myfile.write(unicode('\nStatus: FAILED\n')) else: myfile.write(unicode('\nStatus: SUCCEED\n')) myfile.write(unicode('\nScript stdout:\n' + result.stdout)) myfile.close() forge_sleep(3, "seconds")
def stop_srv(value=False): try: fabric_sudo_command("killall dhcpd &>/dev/null", hide_all=value) except: pass
def reconfigure_srv(): build_and_send_config_files() result = fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/keactrl reload ' + ' & ); sleep ' + str(SLEEP_TIME_1)) check_kea_process_result(True, result, 'reconfigure')
def stop_srv(value=False, destination_address=world.f_cfg.mgmt_address): fabric_sudo_command('(' + world.f_cfg.software_install_path + 'sbin/keactrl stop ' + ' & ); sleep ' + str(world.f_cfg.sleep_time_1), hide_all=value, destination_host=destination_address)
def db_setup(): if world.f_cfg.disable_db_setup: return db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd # MYSQL # cmd = "mysql -u root -N -B -e \"DROP DATABASE IF EXISTS '{db_name}';\"".format(**locals()) # result = fabric_sudo_command(cmd) cmd = "mysql -u root -N -B -e \"SHOW DATABASES LIKE '{db_name}';\"".format( **locals()) result = fabric_sudo_command(cmd) if result == db_name: # db exsists, so try migration cmd = "kea-admin db-upgrade mysql -u {db_user} -p {db_passwd} -n {db_name}".format( **locals()) fabric_run_command(cmd) else: # no db, create from scratch cmd = "mysql -u root -e 'CREATE DATABASE {db_name};'".format( **locals()) fabric_sudo_command(cmd) cmd = "mysql -u root -e \"CREATE USER IF NOT EXISTS '{db_user}'@'localhost' IDENTIFIED BY '{db_passwd}';\"".format( **locals()) fabric_sudo_command(cmd) cmd = "mysql -u root -e 'GRANT ALL ON {db_name}.* TO {db_user}@localhost;'".format( **locals()) fabric_sudo_command(cmd) cmd = "kea-admin db-init mysql -u {db_user} -p {db_passwd} -n {db_name}".format( **locals()) fabric_run_command(cmd) # POSTGRESQL # cmd = "psql -U postgres -t -c \"DROP DATABASE IF EXISTS {db_name}\"".format(**locals()) # result = fabric_sudo_command(cmd, sudo_user='******') cmd = "psql -U postgres -t -c \"SELECT datname FROM pg_database WHERE datname = '{db_name}'\"".format( **locals()) result = fabric_sudo_command(cmd, sudo_user='******') if result.strip() == db_name: # db exsists, so try migration cmd = "kea-admin db-upgrade pgsql -u {db_user} -p {db_passwd} -n {db_name}".format( **locals()) fabric_run_command(cmd) else: # no db, create from scratch cmd = "psql -U postgres -c \"CREATE DATABASE {db_name};\"".format( **locals()) fabric_sudo_command(cmd, sudo_user='******') cmd = "psql -U postgres -c \"DROP USER IF EXISTS {db_user};\"".format( **locals()) fabric_sudo_command(cmd, sudo_user='******') cmd = "psql -U postgres -c \"CREATE USER {db_user} WITH PASSWORD '{db_passwd}';\"".format( **locals()) fabric_sudo_command(cmd, sudo_user='******') cmd = "psql -U postgres -c \"GRANT ALL PRIVILEGES ON DATABASE {db_name} TO {db_user};\"".format( **locals()) fabric_sudo_command(cmd, sudo_user='******') cmd = "kea-admin db-init pgsql -u {db_user} -p {db_passwd} -n {db_name}".format( **locals()) fabric_run_command(cmd)
def restart_srv(): stop_srv() fabric_sudo_command('echo y |rm ' + world.cfg['leases']) fabric_sudo_command('touch ' + world.cfg['leases']) fabric_sudo_command('(' + os.path.join(world.f_cfg.software_install_path, 'sbin/dhcpd') + ' -cf server.cfg_processed -lf ' + world.cfg['leases'] + '); sleep ' + str(world.f_cfg.sleep_time_1) + ';')
def start_srv(success, process, destination_address=world.f_cfg.mgmt_address): fabric_sudo_command('(' + os.path.join(world.f_cfg.dns_server_install_path, 'named') + ' -c ' + os.path.join(world.f_cfg.dns_data_path, 'named.conf') + ' & ); sleep ' + str(world.f_cfg.sleep_time_1), destination_host=destination_address)
def stop_srv(value=False, destination_address=world.f_cfg.mgmt_address): fabric_sudo_command('(killall named & ); sleep ' + str(world.f_cfg.sleep_time_1), hide_all=value, destination_host=destination_address)
def reconfigure_srv(): #build_and_send_config_files() result = fabric_sudo_command('(' + world.f_cfg.software_install_path + 'sbin/keactrl reload ' + ' & ); sleep ' + str(world.f_cfg.sleep_time_1)) check_kea_process_result(True, result, 'reconfigure')
def restart_srv(): fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/keactrl stop ' + ' & ); sleep ' + str(SLEEP_TIME_1)) fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/keactrl start ' + ' & ); sleep ' + str(SLEEP_TIME_1))
def _reload_kea_with_keactrl(destination_host): stop_cmd = os.path.join(world.f_cfg.software_install_path, 'sbin/keactrl') + ' reload' return fabric_sudo_command(stop_cmd, destination_host=destination_host)
def execute_shell_command(command): fabric_sudo_command(command, hide_all=False)
def stop_srv(value = False): fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/keactrl stop ' + ' & ); sleep ' + str(SLEEP_TIME_1), value)
def reload_kea(destination_host): stop_cmd = os.path.join(world.f_cfg.software_install_path, 'sbin/keactrl') + ' reload' return fabric_sudo_command(stop_cmd, destination_host=destination_host)
def stop_srv(value=False): fabric_sudo_command( '(' + world.f_cfg.software_install_path + 'sbin/keactrl stop ' + ' & ); sleep ' + str(world.f_cfg.sleep_time_1), value)
def stop_srv(value = False): fabric_sudo_command('(killall named & ); sleep ' + str(SLEEP_TIME_1), value)
def reconfigure_srv(destination_address=world.f_cfg.mgmt_address): result = fabric_sudo_command('(' + world.f_cfg.software_install_path + 'sbin/keactrl reload ' + ' & ); sleep ' + str(world.f_cfg.sleep_time_1), destination_host=destination_address) check_kea_process_result(True, result, 'reconfigure')
def connect_socket(command): fabric_sudo_command(command, hide_all=False)
def start_srv(success, process): fabric_sudo_command('(' + DNS_SERVER_INSTALL_DIR + 'named -c ' + DNS_DATA_DIR + 'named.conf & ); sleep ' + str(SLEEP_TIME_1))