import pexpect import re import django from django.core.exceptions import AppRegistryNotReady from django.conf import settings try: django.setup() from caas.models import Cluster from caas.models import Namespace except django.core.exceptions.AppRegistryNotReady as exp: pass process_started = False def set_process_started(): global process_started if not process_started: process_started = True return process_started def get_pairs(imageList, remoteRegions): pairList = [] for image in imageList: for region in remoteRegions: pair = {"image": image, "region": region} pairList.append(pair) return pairList def get_docker_reg_connection_details(): dr_username = settings.CENTRAL_DR_CREDS['username'] dr_password = settings.CENTRAL_DR_CREDS['password'] return dr_username, dr_password def get_temp_file_location(): file_loc = settings.ORCH_TEMP_FILE_LOCATION return file_loc def get_host_connection_details(region_oam_ip): host_username = settings.HOST_CREDS['username'] host_password = settings.HOST_CREDS['password'] ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t ' + host_username + '@' + region_oam_ip return host_username, host_password, ssh_prefix def run_commands(commands, host_password, logger, block=False, timeout=None): successful = False logger.info("Inside run_commands") for command in commands: logger.info(" Executing.." + str(command)) child = pexpect.spawn(command) child.timeout=timeout try: child.expect(['password: '], timeout=timeout) child.sendline(host_password) all_lines = child.read() all_lines = all_lines.rstrip().lstrip() all_lines = all_lines.decode('utf-8').replace('\r\n', '\n') successful = True # Tentative for line in all_lines.split("\n"): logger.info(line) if re.search('error', line, re.IGNORECASE): successful = False if re.search('unable', line, re.IGNORECASE): successful = False except: logger.info(str(child)) logger.info("Status:" + str(successful)) return successful def run_commands_scp(commands, host_password, logger, block=False, timeout=settings.SCP_TIMEOUT): successful = False logger.info("Inside run_commands") logger.info("settings.RUNSERVER:" + str(settings.RUNSERVER)) for command in commands: logger.info(" Executing.." + str(command)) try: child = pexpect.spawn(str(command)) except: logger.info(str(child)) #child.timeout=timeout child.timeout = None try: #child.expect(['(yes/no)? '], timeout=timeout) #child.expect(['Are you sure you want to continue connecting (yes/no)? ']) #child.expect(['.+'], timeout=timeout) #child.expect([':b5:9c:21:14:6f:fa:45:20:c7:ba:49:3a:aa:31.\r\nAre you sure you want to continue connecting (yes/no)? '], timeout=timeout) if not settings.RUNSERVER: i = child.expect([b'', 'Are you .*(yes/no)? ', pexpect.EOF], timeout=None) if i == 0: child.sendline('') if i == 1: child.sendline('yes') child.expect(['password: '], timeout=timeout) child.sendline(host_password) all_lines = child.read() all_lines = all_lines.rstrip().lstrip() all_lines = all_lines.decode('utf-8').replace('\r\n', '\n') successful = True # Tentative for line in all_lines.split("\n"): logger.info(line) if re.search('error', line, re.IGNORECASE): successful = False if re.search('unable', line, re.IGNORECASE): successful = False except: logger.info(str(child)) logger.info("Status:" + str(successful)) return successful