def request(api_data, api_link, data): if api_data is None: req = urllib2.Request('http://'+address+'/grafana'+api_link+'') else: req = urllib2.Request('http://'+address+'/grafana'+api_link+''+api_data) req.add_header('Content-Type', 'application/json') req.add_header('Authorization', api_key) #try: try: if data is None: response = urllib2.urlopen(req) else: response = urllib2.urlopen(req, json.dumps(data)) except: if api_link=='/api/dashboards/uid/': background.configuration_status[0]['status']=2 background.report_status('Grafana Connection', 'Wrong default dashboard id', 1) #print 'Wrong Dashboard ID' if api_link=='/api/auth/keys': #print 'Wrong api key' background.configuration_status[0]['status']=2 background.report_status('Grafana Connection', 'Wrong api key or ip data', 1) return 0 else: response=json.load(response) return response
def ipRange(start_ip, end_ip): try: start = list(map(int, start_ip.split("."))) except: background.report_status('IP List', 'Wrong Ip List: first part', 1) background.configuration_status[0]['status'] = 2 return 0 else: try: end = list(map(int, end_ip.split("."))) except: background.report_status('IP List', 'Wrong Ip List: second part', 1) background.configuration_status[0]['status'] = 2 return 0 else: temp = start ip_range = [] ip_range.append(start_ip) while temp != end: start[3] += 1 for i in (3, 2, 1): if temp[i] == 255: temp[i] = 0 temp[i - 1] += 1 ip_range.append(".".join(map(str, temp))) return ip_range
def create_new_template(name, panels): ##creating data data = { "dashboard": { "id": None, "title": name, "timezone": "browser", "panels": [], "version": 0 }, "overwrite": True } api_link='/api/dashboards/db' ###make headers ##iterating in example dashboard and changing group for panel in panels: for target in panel['targets']: target['group']['filter']= name data['dashboard']['panels'].append(panel) response=request('', api_link, data) try: response_uid=response['uid'] except: background.configuration_status[0]['status']=2 background.report_status('Grafana', 'Dashboard failed', 1) return 0 else: return response_uid
def execute_command(HOST, USER, PASSWORD, SUDO, command, name): global error_text global error_text2 global error_text3 global error_text4 global valid_text client = pm.SSHClient() client.set_missing_host_key_policy(AllowAllKeys()) client.connect(HOST, username=USER, password=PASSWORD) channel = client.invoke_shell() enable = """enable """ + SUDO + """ configure t """ channel.send(enable) nvm = channel.recv(6000) time.sleep(2) channel.send(command) time.sleep(10) output = channel.recv(6000) #print output error = output.find(error_text) error2 = output.find(error_text2) error3 = output.find(error_text3) error4 = output.find(error_text4) valid = output.find(valid_text) client.close() if (error != -1 and valid != -1) or (error == -1 and error2 == -1 and error3 == -1 and error4 == -1): return 0 else: background.report_status('CCAP: ' + name, output, '1') #print output return 1
def clearCCAPconf(TEST_configuration): global cbr8_clear_list cbr8_fibernode = '' cbr8_controllerUS = '' cbr8_controllerDS = '' cbr8_wideband = '' cbr8_cable = '' ccap_configuration_id = TEST_configuration[0]['ccap_configuration_id'] HOST = TEST_configuration[0]['ccap_ip'] USER = TEST_configuration[0]['ccap_login'] PASSWORD = TEST_configuration[0]['ccap_password'] SUDO = TEST_configuration[0]['ccap_sudo'] CCAP_part = db_get.CCAP_part() flag = ssh.check_connection(HOST, USER, PASSWORD) if flag == 1: background.report_status('SSH Connection', 'wrong CCAP ip/login/password', '1') background.configuration_status[0]['status'] = 2 return 0 time.sleep(2) ##Check SUDO PASSWORD flag = ssh.check_sudo(HOST, USER, PASSWORD, SUDO) if flag == 1: background.report_status('CCAP Authentication', 'wrong CCAP enable password', '1') background.configuration_status[0]['status'] = 2 return 0 for parts in CCAP_part: if str(parts['configuration_id']) == str(ccap_configuration_id): ##clear US Controler if parts['name'].find('controller Upstream-Cable') > -1: cbr8_controllerUS = parts['name'] + ' ' + parts['nr'] #CBR8controllerUS(HOST, USER, PASSWORD, SUDO, parts['name']) ##clear DS Controler if parts['name'].find('controller Integrated-Cable') > -1: cbr8_controllerDS = parts['name'] + ' ' + parts['nr'] #CBR8controllerDS(HOST, USER, PASSWORD, SUDO, parts['name']) if parts['name'].find('cable Fiber-Node') > -1: cbr8_fibernode = parts['name'] + ' ' + parts['nr'] #CBR8default(HOST, USER, PASSWORD, SUDO, parts['name']) if parts['name'].find('interface Wideband-Cable') > -1: cbr8_wideband = parts['name'] + ' ' + parts['nr'] #CBR8default(HOST, USER, PASSWORD, SUDO, parts['name']) if parts['name'].find('interface Cable') > -1: cbr8_cable = parts['name'] + ' ' + parts['nr'] #CBR8default(HOST, USER, PASSWORD, SUDO, parts['name']) CBR8controllerUS(HOST, USER, PASSWORD, SUDO, cbr8_controllerUS) CBR8controllerDS(HOST, USER, PASSWORD, SUDO, cbr8_controllerDS) CBR8default(HOST, USER, PASSWORD, SUDO, cbr8_fibernode) CBR8default(HOST, USER, PASSWORD, SUDO, cbr8_wideband) CBR8default(HOST, USER, PASSWORD, SUDO, cbr8_cable)
def createCCAPfiles(TEST_configuration): ccap_configuration_id = TEST_configuration[0]['ccap_configuration_id'] HOST = TEST_configuration[0]['ccap_ip'] USER = TEST_configuration[0]['ccap_login'] PASSWORD = TEST_configuration[0]['ccap_password'] SUDO = TEST_configuration[0]['ccap_sudo'] #configuration_table=background.configuration_status[0] ##CCAP Configuration Started flag = ssh.check_connection(HOST, USER, PASSWORD) ###Check SSH Connection if flag == 1: background.report_status('SSH Connection', 'wrong CCAP ip/login/password', '1') background.configuration_status[0]['status'] = 2 return 0 time.sleep(2) ##Check SUDO PASSWORD flag = ssh.check_sudo(HOST, USER, PASSWORD, SUDO) if flag == 1: background.report_status('CCAP Authentication', 'wrong CCAP enable password', '1') background.configuration_status[0]['status'] = 2 return 0 time.sleep(2) ##execute commands, line by line CCAP_part = db_get.CCAP_part() for parts in CCAP_part: if str(parts['configuration_id']) == str(ccap_configuration_id): name = parts['name'] + ' ' + parts['nr'] command = name + '\n' for line in parts['content'].splitlines(): line = str(line).replace('\\r\\n', '\n').replace('\\"', '"') command += line #execute commands command += """ """ flag = ssh.execute_command(HOST, USER, PASSWORD, SUDO, command, name) time.sleep(2) if flag == 1: background.configuration_status[0]['status'] = 2 return 0
def execute_ansible(task_name, description, name): status, output = commands.getstatusoutput('ansible-playbook Ansible/' + task_name + '_start.yml -i Ansible/tests/' + name + '/hosts --extra-vars "test=' + name + '"') if task_name == 'dhcp_disable': print 0 else: if task_name == 'dhcp_enable_0': if status == 0: background.report_status(description, 'DHCP Server exists', 1) return 1 else: return 0 else: if status != 0: background.report_status(description, 'error', 1) return 1 else: background.report_status(description, 'OK', 0) return 0
def preparePROVmain(TEST_configuration): #get All Information about specified test configuration id = TEST_configuration[0]['prov_configuration_id'] ## get id of PROV info name = TEST_configuration[0]['name'] ## get test name dir = "Ansible/tests/" + TEST_configuration[0]['name'] ## get config dir ###Getting rest of information, needed to successfuly create prov and cm data PROV_cm = db_get.PROV_cm(id) PROV_configuration = db_get.PROV_configuration(id) PROV_ipv4 = db_get.PROV_ipv4(id) PROV_ipv6 = db_get.PROV_ipv6(id) CM_List = db_get.CM_List() dir = "Ansible/tests/" + name cable_modems_ip = [] single_ip = {} payload = [] content = {} if PROV_configuration[0]['ip_version'] == 'IPv4': ip_version = 0 if PROV_configuration[0]['ip_version'] == 'IPv6': ip_version = 1 if PROV_configuration[0]['ip_version'] == 'IPv4 and IPv6': ip_version = 2 for rows in PROV_cm: CM = rows['cable_modem'] cmList = CM.split('-') ####IPv4 or both if ip_version == 0 or ip_version == 2: IPv4 = rows['ipv4'] ipv4List = IPv4.split('-') ip_range = ipRange(ipv4List[0], ipv4List[1]) if background.configuration_status[0]['status'] == 2: return 0 routersv4 = rows['routersv4'] #####IPv6 or borh if ip_version == 1 or ip_version == 2: IPv6 = rows['ipv6'] ipv6List = IPv6.split('-') ip_range6 = ipRange6(ipv6List[0], ipv6List[1]) routersv6 = rows['routersv6'] config_file_nr = rows['cm_configuration_id'] CM_configuration = db_get.CM_configuration(config_file_nr) config_file = CM_configuration[0]['name'] licznik = 0 if len(cmList) == 1: cmList.append(cmList[0]) for nr in range(int(cmList[0]), int(cmList[1]) + 1): for modems in CM_List: if modems['nr'] == nr: content = { "mac": modems['mac'], "ipv4": ip_range[licznik], "routersv4": routersv4, "config_file": config_file } if ip_version == 0 or ip_version == 2: ipv4 = { "ipv4": ip_range[licznik], "routersv4": routersv4 } content.update(ipv4) if ip_version == 1 or ip_version == 2: ipv6 = { "ipv6": ip_range6[licznik], "routersv6": routersv6 } content.update(ipv6) single_ip = {"ip": ip_range[licznik]} cable_modems_ip.append(single_ip) payload.append(content) content = {} single_ip = {} licznik = licznik + 1 default_cm = PROV_configuration[0]['default_cm_configuration_id'] dhcp_interface = PROV_configuration[0]['dhcp_interface'] dhcp_ip = PROV_configuration[0]['dhcp_ip'] MainYmlFile = '' MainYmlFile += "config_file: '" + name + "'" MainYmlFile += "\ninterface: '" + dhcp_interface + "'" MainYmlFile += "\nip: '" + dhcp_ip + "'" MainYmlFile += "\nip_version: '" + str(ip_version) + "'" if ip_version == 0 or ip_version == 2: time_serverv4 = PROV_ipv4[0]['time'] tftp_serverv4 = PROV_ipv4[0]['tftp'] routersv4 = PROV_ipv4[0]['routers'] poolv4 = PROV_ipv4[0]['pool_range'] subnetv4 = PROV_ipv4[0]['subnet'] MainYmlFile += "\ntime_server: '" + time_serverv4 + "'" MainYmlFile += "\ntftp_server: '" + tftp_serverv4 + "'" MainYmlFile += "\nrouters: '" + routersv4 + "'" MainYmlFile += "\npool: '" + poolv4 + "'" MainYmlFile += "\nsubnet: '" + subnetv4 + "'" if ip_version == 1 or ip_version == 2: time_serverv6 = PROV_ipv6[0]['time'] tftp_serverv6 = PROV_ipv6[0]['tftp'] routersv6 = PROV_ipv6[0]['routers'] poolv6 = PROV_ipv6[0]['pool_range'] subnetv6 = PROV_ipv6[0]['subnet'] MainYmlFile += "\ntime_server6: '" + time_serverv6 + "'" MainYmlFile += "\ntftp_server6: '" + tftp_serverv6 + "'" MainYmlFile += "\nrouters6: '" + routersv6 + "'" MainYmlFile += "\npool6: '" + poolv6 + "'" MainYmlFile += "\nsubnet6: '" + subnetv6 + "'" if default_cm > 0: CM_configuration = db_get.CM_configuration(default_cm) MainYmlFile += "\ndefault_config_file: '" + CM_configuration[0][ 'name'] + "'" MainYmlFile += "\nconfig_files:" for modems in payload: MainYmlFile += "\n - {mac: '" + modems['mac'] if ip_version == 0 or ip_version == 2: MainYmlFile += "', ip: '" + modems['ipv4'] MainYmlFile += "', routers: '" + modems['routersv4'] if ip_version == 1 or ip_version == 2: MainYmlFile += "', ipv6: '" + modems['ipv6'] MainYmlFile += "', routersv6: '" + modems['routersv6'] MainYmlFile += "', config_file: '" + modems['config_file'] + "'}" try: file_yml = open(dir + "/CMmain.yml", "w") except: background.report_status('Creating Cable Modem data', 'Error', 1) background.configuration_status['status'] = 2 return 0 else: file_yml.write(MainYmlFile) file_yml.close() return cable_modems_ip