summaryrefslogtreecommitdiff
path: root/src/orchestration/utils.py
blob: 24f6b9c8274725f97e3e31f13c0cd97f9b5726ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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