Bring up lab --from Leo Li

#! /usr/bin/env python

'''
@File: bringup.py
@Author: Leo Li
@Version: 1.0
'''

import telnetlib
import logging
import sys
import string
import re
import time
import thread
import os
import ftplib
import datetime

#**************lab information*****************************
labname = ('tat04', 'tat07', 'qtat1')
IPset = ('135.1.68.226', '135.2.85.100', '135.252.132.164')
shorttime = 60
longtime = 900
tmpbuffer = ''
logger = ''
lab = 'default'
bladeMIA = 'default'
bladeMIB = 'default'
bladeFSDBA = 'default'
bladeOthers = []
ConfigurationFile = 'lab_configuration.ini'
#**************end*****************************************


#****************Function: ReadConfigurationFile ***********
def readConfiguration(filename, labname):
    global lab
    global bladeMIA
    global bladeMIB
    global bladeFSDBA
    global bladeOthers

    tagnum = 0 #verify '<LAB>' is match with '<END>'
    labtag = 0
    try:
        fl = open(filename)
        line = fl.readline()

        while line:
            if re.match(r'#.*', line):
                pass
            elif re.search(r'<LAB>', line):
                tagnum+= 1
            elif re.search(r'<END>', line):
                tagnum -= 1
                labtag = 0
            elif re.search(r'LabName', line):
                if re.search(labname, line):
                    labtag = 1
                    
            elif re.search(r'MI-A', line) and labtag:
                if re.search(labname+r'-s\d+c\d+h\d+', line):
                    bladeMIA = re.search(labname+r'-s\d+c\d+h\d+', line).group(0).lower()
                else :
                    print 'bladeMIA read error!\n'
                    return 0
            elif re.search(r'MI-B', line) and labtag:
                if re.search(labname+r'-s\d+c\d+h\d+', line):
                    bladeMIB = re.search(labname+r'-s\d+c\d+h\d+', line).group(0).lower()
                else :   
                    print 'bladeMIB read error!\n'
                    return 0
            elif re.search(r'FSDB-A', line) and labtag:
                if re.search(labname+r'-s\d+c\d+h\d+', line):
                    bladeFSDBA = re.search(labname+r'-s\d+c\d+h\d+', line).group(0).lower()
                else :   
                    print 'bladeFSDBA read error!\n'
                    return 0
            elif re.search(r'OtherDiskful', line) and labtag:
                bladeOthers = line.split()
                i = 0
                while i < len(bladeOthers):
                    if re.match(labname+r'-s\d+c\d+h\d+', bladeOthers[i]):
                        bladeOthers[i] = bladeOthers[i].lower()
                        i += 1
                    else:
                        bladeOthers.remove(bladeOthers[i])  
            
            line = fl.readline()
            
        if tagnum != 0 :
            print "<LAB> and <END> do not match!\n"
            return 0
        else :
            print 'MI-A is: ' + bladeMIA + '\n'
            print 'MI-B is: ' + bladeMIB + '\n'
            print 'FSDB-A is: ' + bladeFSDBA + '\n'
            print 'Other diskful blades are: '
            print bladeOthers
            print '\n'
        fl.close
        return 1

    except Exception:
        print "Read configuration file abnormal!\n"
        return 0
#**************end*****************************************

    

#******************Function: tel_input_s*******************
def tel_input_s(tn, input_str, read_str, log_str):
    tn.write(input_str)
    global shorttime
    global tmpbuffer
    global logger
    tmpbuffer = tn.read_until(read_str, shorttime)
    print tmpbuffer
    logger.debug(tmpbuffer)
    if read_str not in tmpbuffer:
        print log_str
        logger.error(log_str)
        exit(1)

#**********************end*********************************


#******************Function: tel_input_l*******************
def tel_input_l(tn, input_str, read_str, log_str):
    tn.write(input_str)
    global longtime
    global tmpbuffer
    global logger
    tmpbuffer = tn.read_until(read_str, longtime)
    print tmpbuffer
    logger.debug(tmpbuffer)
    if read_str not in tmpbuffer:
        print log_str
        logger.error(log_str)
        exit(1)
#**********************end*********************************



#******************Function: sulogin **********************
def sulogin(tn):  
    tel_input_s(tn, 'su\n', 'Password:', 'Timeout. su root failed.\n')
    tel_input_s(tn,'newsys\n', 'of the day:', 'telnet lab failed\n')
    tel_input_s(tn,'\n', '(default = vi):', 'telnet lab failed\n')
    tel_input_s(tn,'\n', 'root:', 'telnet lab failed\n')
#***********************end**********************************

       

#******************Function: telnet lab**********************
def telnetlab(tn, login, password, finishtag):
    'telnet lab: tat04, tat07, qtat1. If telnet failed, exit script.\
    e.g. tn = telnetlib.Telnet(labIP)\
    telnetlab(tn)'
    global logger
    global shorttime
    #telnet login
    tmpbuffer = tn.read_until('login: ', shorttime)
    if 'login: ' not in tmpbuffer:
       print 'cannot telnet lab'
       logger.error('cannot telnet lab\n')
       exit(1)
    tel_input_s(tn,login + '\n','Password:', 'cannot telnet lab\n')   

    #telnet password
    tel_input_s(tn,password + '\n','of the day:', 'telnet lab failed\n')

    #telnet lab
    tel_input_s(tn,'\n', '(default = vi):', 'telnet lab failed\n')
    tel_input_s(tn,'\n', finishtag, 'telnet lab failed\n')   
#***********************end**********************************


#******************Function: telnet other blade**********************
def telnetblade(tn, blade, login, password, finishtag):  
    #telnet login
    tn.write('telnet '+ blade +'\n')
    telnetlab(tn, login, password, finishtag)
#***********************end**********************************


#******************Function: exit blade**********************
def exitblade(tn, finishtag):
    tel_input_s(tn, 'exit\n', finishtag, 'Timeout. can not exit blade.\n') 
#***********************end**********************************
      

#**************Function:check RCC status********************
def checkRCCstatus(tnl, labname, blade):
    global tmpbuffer
    global logger
    tel_input_s(tnl,'RCCcstat\n', '/export/home/lss:','Timeout. Check' + blade + 'status failed.\n')
    if re.search(blade+'.* A - ACTIVE', tmpbuffer) or re.search(blade+'.* L - LEAD', tmpbuffer):
        print  blade + ' status is OK!\n'
        logger.debug(blade + ' status is OK!\n')
        return 1
    else:
        print blade + ' status is error!\n'
        logger.debug(blade + ' status is error!\n')
        return 0    
#***********************end*********************************
    
#**************Function:check MI RCC status******************
def checkMIbladeRCC(labname, blade):
    global tmpbuffer
    global logger
    global shorttime

    for i in range(1, 20):
        try:
            tnl = telnetlib.Telnet(blade)
            tmpbuffer = tnl.read_until('login:', shorttime)
            print tmpbuffer
            if 'login:' in tmpbuffer:
                tnl.close()
                tnl = telnetlib.Telnet(blade)
                telnetlab(tnl, 'lss', 'lss', '/export/home/lss:')
                #check RCC status
                if checkRCCstatus(tnl, labname, blade):  
                   break
                print 'cannot telnet' + blade+', wait 60s......\n'
                logger.error('cannot telnet'+ blade+', wait 60s......\n')
                time.sleep(60)
        except Exception:
            print 'cannot telnet' + blade+', wait 60s......\n'
            logger.error('cannot telnet'+ blade+', wait 60s......\n')
            time.sleep(60)
    if i >= 20:
        print blade+'reboot failed, exit!\n'
        logger.error(blade+'reboot failed, exit!\n')
        exit(0)
         
    tnl.close()         
#***********************end*********************************


#******Function:check other(FSDB, CDR) blade RCC status******
def checkotherbladeRCC(labname, blade):
    global tmpbuffer
    global logger
    global shorttime
    global bladeMIA

    #telnet MI-A
    tnl = telnetlib.Telnet(bladeMIA)
    telnetlab(tnl, 'lss', 'lss', '/export/home/lss:')
    #try to telnet blade
    for i in range(1, 20):
        try:
            tnl.write('telnet ' + blade + '\n')
            tmpbuffer = tnl.read_until('login: ', shorttime)
            if 'login: ' in tmpbuffer:
                tel_input_s(tnl,'lss\n','Password:', 'cannot telnet lab\n')   
                tel_input_s(tnl,'lss\n','of the day:', 'telnet lab failed\n')
                tel_input_s(tnl,'\n', '(default = vi):', 'telnet lab failed\n')
                tel_input_s(tnl,'\n', '/export/home/lss:', 'telnet lab failed\n')
                #check RCC status
                if checkRCCstatus(tnl, labname, blade):   
                   break
                exitblade(tnl, '/export/home/lss:')
                print 'cannot telnet'+ blade+', wait 60s......\n'
                logger.error('cannot telnet'+ blade+', wait 60s......\n')
                time.sleep(60)
        except Exception:
            print 'cannot telnet'+ blade+', wait 60s......\n'
            logger.error('cannot telnet'+ blade+', wait 60s......\n')
            time.sleep(60)
            continue
        
        print 'cannot telnet' + blade+', wait 60s......\n'
        logger.error('cannot telnet'+ blade+', wait 60s......\n')
        time.sleep(60)
            
    if i >= 20:
        print  blade+'reboot failed, exit!\n'
        logger.error(blade+'reboot failed, exit!\n')
        exit(0)
        
    tnl.close()         
#***********************end*********************************
        
#**************Function: start other(FSDB, CDR) blade RCC serial************
def otherbladeRCCser(labname, blade):
    global tmpbuffer
    global logger
    global bladeMIA
    
    #telnet MI-A
    tnl = telnetlib.Telnet(bladeMIA)
    telnetlab(tnl, 'lss', 'lss', '/export/home/lss:')

    #start blade RCC
    telnetblade(tnl, blade, 'lss', 'lss', '/export/home/lss:')
    tel_input_s(tnl, 'sudo RCCmachonline -u\n', '/export/home/lss:','Timeout.' + blade + 'start RCC failed.\n')
    exitblade(tnl, '/export/home/lss:')

    tnl.close()    
#***********************end********************************


#**************Function: start MI blade RCC serial************
def MIbladeRCCser(labname, blade):
    global tmpbuffer
    global logger

    #telnet MI-A
    tnl = telnetlib.Telnet(blade)
    telnetlab(tnl, 'lss', 'lss', '/export/home/lss:')

    #start blade RCC
    tel_input_s(tnl, 'sudo RCCmachonline -u\n', '/export/home/lss:','Timeout.' + blade + 'start RCC failed.\n')
   
    tnl.close()    
#***********************end********************************
    

#**************Function: start RCC serial******************
def startRCCser(labname):
    global bladeMIA
    global bladeMIB
    global bladeFSDBA
    global bladeOthers
    
    MIbladeRCCser(labname, bladeMIA)
    print 'Will sleep 180s to wait reboot......'
    time.sleep(180)

    checkMIbladeRCC(labname, bladeMIA)
    MIbladeRCCser(labname, bladeMIB)
    print 'Will sleep 180s to wait reboot......'
    time.sleep(180)
    checkMIbladeRCC(labname, bladeMIB)
    
    if bladeFSDBA != 'default' and bladeFSDBA != bladeMIA:
        otherbladeRCCser(labname, bladeFSDBA)
        print 'Will sleep 180s to wait reboot......'
        time.sleep(180)
        checkotherbladeRCC(labname, bladeFSDBA)

    for blade in bladeOthers:    
        otherbladeRCCser(labname, blade)
        print 'Will sleep 180s to wait reboot......'
        time.sleep(180)
        checkotherbladeRCC(labname, blade)
#***********************end********************************


#***********Function: start RCC parallel***************
def startallRCCpar(labname):
    global bladeMIA
    global bladeMIB
    global bladeFSDBA
    global bladeOthers
    
    #MI-A and MI-B
    tn1 = telnetlib.Telnet(bladeMIA)
    telnetlab(tn1, 'lss', 'lss', '/export/home/lss:')
    tn2 = telnetlib.Telnet(bladeMIB)
    telnetlab(tn2, 'lss', 'lss', '/export/home/lss:')
    
    #FSDB-A and FSDB-B
    if bladeFSDBA != 'default' and bladeFSDBA != bladeMIA:
        tn3 = telnetlib.Telnet(bladeMIA)
        telnetlab(tn3, 'lss', 'lss', '/export/home/lss:')
        telnetblade(tn3, bladeFSDBA, 'lss', 'lss', '/export/home/lss:')
        
    tnother = []
    i = 0
    for blade in bladeOthers:   
        tnother.append(telnetlib.Telnet(bladeMIA))
        telnetlab(tnother[i], 'lss', 'lss', '/export/home/lss:')
        telnetblade(tnother[i], blade, 'lss', 'lss', '/export/home/lss:')
        i += 1
        
    start = datetime.datetime.now()
    if bladeFSDBA != 'default' and bladeFSDBA != bladeMIA:    
        tel_input_s(tn3, 'sudo RCCmachonline -u\n', '/export/home/lss:','Timeout.' + bladeFSDBA + ' start RCC failed.\n')
    i = 0    
    for blade in bladeOthers:     
        tel_input_s(tnother[i], 'sudo RCCmachonline -u\n', '/export/home/lss:','Timeout.' + blade + ' start RCC failed.\n')
        i += 1
    
    tel_input_s(tn1, 'sudo RCCmachonline -u\n', '/export/home/lss:','Timeout.' + bladeMIA +' start RCC failed.\n')
    tel_input_s(tn2, 'sudo RCCmachonline -u\n', '/export/home/lss:','Timeout.' + bladeMIB +' start RCC failed.\n')

    end = datetime.datetime.now()
    print (end - start).seconds
    
    tn1.close()
    tn2.close()
    if bladeFSDBA != 'default' and bladeFSDBA != bladeMIA:        
        tn3.close()
    i = 0    
    for blade in bladeOthers:         
        tnother[i].close()
        i += 1
    
#***********************end********************************
    

#**************Function: start RCC parallel****************
def startRCCpar(labname):
    global bladeMIA
    global bladeMIB
    global bladeFSDBA
    global bladeOthers
    
    startallRCCpar(labname)
    print 'Will sleep 180s to wait reboot......'
    time.sleep(180)
   
    checkMIbladeRCC(labname, bladeMIA)
    checkMIbladeRCC(labname, bladeMIB)
    if bladeFSDBA != 'default' and bladeFSDBA != bladeMIA:
        checkotherbladeRCC(labname, bladeFSDBA)
    for blade in bladeOthers:
        checkotherbladeRCC(labname, blade)
        checkotherbladeRCC(labname, blade)

#***********************end********************************

        
#**************Function: start RCC*************************
def startRCC(labname, RCCtype):
    global logger
 
    if RCCtype == 1:
        print 'Will start RCC serial......\n'
        logger.error('Will start RCC serial......\n')
        startRCCser(labname)
    elif RCCtype == 2:
        print 'Will start RCC parallel......\n'
        logger.error('Will start RCC parallel......\n') 
        startRCCpar(labname)      
    else:
        print 'RCC start type error!\n'
        logger.error('RCC start type error!\n')
        exit(0)

#**********************end*********************************
    

#**************Function: dbload ***************************
def dbload(labname, datafile):
    global logger
    global bladeMIA
    global bladeFSDBA
    try:
        #cp datafile to local path
        a = os.popen('mkdir loaddata').read()
        print a
        logger.debug(a)    
        a = os.popen('cp -r ' + datafile + '/* ./loaddata/').read()
        print a
        logger.debug(a)
        a = os.popen('zip -r loaddata.zip ./loaddata').read()
        print a
        logger.debug(a)
        a = os.popen('rm -r loaddata').read()
        print a
        logger.debug(a)

        #ftp
        f=ftplib.FTP(bladeMIA)
        f.login('lss', 'lss')
        localfile = 'loaddata.zip'
        f.storbinary('STOR %s' % localfile, open(localfile,'rb'))
        f.quit()
        
        a = os.popen('rm -r loaddata.zip').read()
        print a
        logger.debug(a)

        #ftp datafile from MI-A to FSDB-A
        tnl = telnetlib.Telnet(bladeMIA)
        telnetlab(tnl, 'lss', 'lss', '/export/home/lss:') 
        #unzip datafile in MI-A
        tel_input_s(tnl, 'unzip loaddata.zip\n', '/export/home/lss:','Timeout. unzip loaddata.zip failed in MI-A.\n')
        
        #dbload in MI-A
        tel_input_s(tnl, 'cd loaddata\n', 'loaddata:','Timeout. cd loaddata failed in MI-A.\n')
        tel_input_l(tnl, 'dbload -noversion -u lssdba -p lssdba -all -rm\n', 'loaddata:','Timeout. dbload failed in MI-A.\n')
        tel_input_s(tnl, 'cd ..\n', '/export/home/lss:','Timeout. cd loaddata failed in MI-A.\n')
        
        if bladeMIA == bladeFSDBA or bladeFSDBA == 'default':
            tel_input_s(tnl, 'rm -r loaddata.zip\n', '/export/home/lss:','Timeout. rm loaddata.zip failed in MI-A.\n')
            tel_input_s(tnl, 'rm -r loaddata\n', '/export/home/lss:','Timeout. rm loaddata failed in MI-A.\n')
            return;
           
        #unzip datafile in FSDB-A
        tel_input_s(tnl, 'ftp ' + bladeFSDBA + '\n', 'lss):','Timeout. ftp load data from MI-A to FSDB-A.\n')
        tel_input_s(tnl, 'lss\n', 'Password:','Timeout. ftp load data from MI-A to FSDB-A.\n')
        tel_input_s(tnl, 'lss\n', 'ftp>','Timeout. ftp load data from MI-A to FSDB-A.\n')
        tel_input_s(tnl, 'bin\n', 'ftp>','Timeout. ftp load data from MI-A to FSDB-A.\n')
        tel_input_s(tnl, 'put loaddata.zip\n', 'ftp>','Timeout. ftp load data from MI-A to FSDB-A.\n')
        tel_input_s(tnl, 'bye\n', '/export/home/lss:','Timeout. ftp load data from MI-A to FSDB-A.\n')

        #dbload in FSDB-A
        telnetblade(tnl, bladeFSDBA,'lss','lss', '/export/home/lss:')
        tel_input_s(tnl, 'unzip loaddata.zip\n', '/export/home/lss:','Timeout. unzip loaddata.zip failed in FSDB-A.\n')
        tel_input_s(tnl, 'cd loaddata\n', 'loaddata:','Timeout. cd loaddata failed in FSDB-A.\n')
        tel_input_l(tnl, 'dbload -noversion -u lssdba -p lssdba -all -rm\n', 'loaddata:','Timeout. dbload failed in FSDB-A.\n')
        tel_input_s(tnl, 'cd ..\n', '/export/home/lss:','Timeout. cd loaddata failed in FSDB-A.\n')
        
        #tel_input_s(tnl, 'rm -r loaddata.zip\n', '/export/home/lss:','Timeout. rm loaddata.zip failed in FSDB-A.\n')
        #tel_input_s(tnl, 'rm -r loaddata\n', '/export/home/lss:','Timeout. rm loaddata failed in FSDB-A.\n')
        exitblade(tnl,'/export/home/lss:')
        #tel_input_s(tnl, 'rm -r loaddata.zip\n', '/export/home/lss:','Timeout. rm loaddata.zip failed in MI-A.\n')
        #tel_input_s(tnl, 'rm -r loaddata\n', '/export/home/lss:','Timeout. rm loaddata failed in MI-A.\n')

    except Exception:
        print 'dbload error!\n'
        exit(1)
    
#**********************end*********************************
    
       
#*********************Main function************************** 
#choose lab
starttime = datetime.datetime.now()
try:    
    if len(sys.argv) != 7 or sys.argv[1] != '-lab' or sys.argv[3] != '-data'  or sys.argv[5] != '-load':
        print "Command format error!\n Command format: ./bringup.py -lab labname -data filename -load Rxx\n"
        exit(0)

    if sys.argv[2] not in labname:
        print "Now can not support lab" + sys.argv[2] + '!\n'
        exit(0)
    print 'test'
    len_cmd = len(sys.argv)   
    print 'Input parameters are:'
    for i in range(0, len_cmd):
       print 'sys.argv[%s] = %s\n'%(i, sys.argv[i])
    '''
    labtag = raw_input ('Do you want to bring up lab '+ sys.argv[2] + '? (Yes/No, default is No)\n')
    if labtag == 'Yes' or labtag == 'yes' or labtag =='Y' or labtag =='y':
        lab = sys.argv[2]
        print 'Start to bringup lab %s:\n'%lab
    else:
        print 'Exit!\n'
        exit(0)
    '''
    lab = sys.argv[2]
    datafile = sys.argv[4]
    load = sys.argv[6]

    if readConfiguration(ConfigurationFile, lab):
        print 'read configuration file successfully!\n'
    else:        
        print 'read configuration file failed, exit!\n'
        exit(0)
        
    print 'lab' + sys.argv[2] + 'will be bring up after 5s ......'
    time.sleep(5)
    
except Exception:
    print 'Input error, exit!\n'
    exit(0)

try:
    #log configuration
    logger = logging.getLogger()
    logfile = logging.FileHandler("bringup.log")
    logger.addHandler(logfile)
    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
    logger.setLevel(logging.NOTSET)

except Exception:
    print 'Configuration error!\n'
    exit(0)

try:
    
    #start RCC
    if load > 'R22':
        startRCC(lab, 2)
    else :   
        startRCC(lab, 1)
    
    #telnet configuration
    tmpblade = bladeMIA
    tn = telnetlib.Telnet(tmpblade)
    telnetlab(tn, 'lss', 'lss', '/export/home/lss:')
    #create_ht
    sulogin(tn)
    tel_input_l(tn,'create_ht\n', 'root:', 'Timeout. create_ht failed in MI-A.\n')
    exitblade(tn, '/export/home/lss:')
    telnetblade(tn, bladeMIB, 'root', 'newsys', 'root:')
    tel_input_l(tn,'create_ht\n', 'root:', 'Timeout. create_ht failed in MI-B.\n')
    exitblade(tn, '/export/home/lss:')

    #midbinit
    tel_input_l(tn,'midbinit\n', '/export/home/lss:', 'Timeout. startMI failed.\n')

    #dbload
    dbload(lab, datafile)
    
    #start MI
    tel_input_l(tn,'startMI\n', '/export/home/lss:', 'Timeout. startMI failed.\n')
    
    #check MI status
    tel_input_s(tn,'MIcmd state vc\n', '/export/home/lss:', 'Timeout. startMI failed.\n')
    if re.search(r"A - Active", tmpbuffer) and re.search(r"S - Standby", tmpbuffer):
        print 'MI state OK.\n'
        logger.debug('MI state OK.\n')
    else:
        print 'MI state error.\n'
        logger.error('MI state error.\n')    
        exit(1)       
       
    #init service
    tel_input_l(tn,'init_cli -t l -i all\n', '/export/home/lss:', 'Timeout. init_cli -t l -i all failed.\n')

    #start FS
    tel_input_l(tn,'startFS\n', '/export/home/lss:', 'Timeout. startFS failed.\n')

    #Start pm data
    tel_input_l(tn,'PMcontrol --master start\n', '/export/home/lss:', 'Timeout. start PM failed.\n')
      
    #check pm data
    tel_input_s(tn,'PMcontrol status\n', '/export/home/lss:', 'Timeout. check PM status failed.\n')
    if  re.search(r"PMEnabled true", tmpbuffer):
        print 'PM state OK.\n'
        logger.debug('PM state OK.\n')
    else:
        print 'PM state error.\n'
        logger.error('PM state error.\n')    
        exit(1)
        
    #check service status
    for i in range(1, 30):
        tel_input_s(tn,'REMcli 7375 0 0 0\n', '/export/home/lss:', 'Timeout. Can not get service status.\n')      
        #check whether status are OK
        if re.search(r"status=InserviceStbyCold", tmpbuffer) or re.search(r"status=OOSunreachable", tmpbuffer):
            print 'Waiting all service is OK......\n'
            logger.debug('Waiting all service is OK......\n')
            time.sleep(30)             
        else:
            print 'All service is OK.\n'
            logger.debug('All service is OK.\n')
            break
            
    if i == 30:
        print 'Time out. Service status error.\n'
        logger.error('Time out. Service status error.\n')
        exit(0)
            
    #su root
    sulogin(tn)
    
    #Start cpm
    #command 1
    tel_input_l(tn,'scdw_adm --action clean_cpm\n', 'root:', 'Timeout. start cpm command 1 failed.\n')    

    #command 2
    tel_input_l(tn,'scdw_adm --action setup_cpm\n', 'root:', 'Timeout. start cpm command 2 failed.\n')     

    #command 3
    tel_input_l(tn,'scdw_adm --action pre_enable_cpm\n', 'root:', 'Timeout. start cpm command 3 failed.\n')       

    #command 4
    tel_input_l(tn,'scdw_adm --action enable_cpm\n', 'root:', 'Timeout. start cpm command 4 failed.\n')     

    #command 5
    tel_input_l(tn,'scdm_adm --action sync_config\n', 'root:', 'Timeout. start cpm command 5 failed.\n')    

    #command 6
    tel_input_l(tn,'scdw_adm --action sync_data\n', 'root:', 'Timeout. start cpm command 6 failed.\n')     

    
    #telnet MI-B as root
    telnetblade(tn, bladeMIB, 'root', 'newsys', 'root:')

    
    #MI-B command
    tel_input_l(tn,'scdf_adm --action generate_host_all\n', 'root:', 'Timeout. start cpm command failed.\n') 
        
    #exit MI-B
    exitblade(tn, 'root:')

    #check cpm status
    tel_input_s(tn,'cpmconf_adm --action show_activated\n', 'root:', 'Timeout. check CPM status failed.\n')
    if  re.search(r'CPM activated = 1', tmpbuffer):
        print 'CPM state 1 OK.\n'
        logger.debug('CPM state 1 OK.\n')
    else:
        print 'CPM state 1 error.\n'
        logger.error('CPM state 1 error.\n')    
        exit(1)

    tel_input_s(tn,'cpmconf_adm --action status_host_all\n', 'root:', 'Timeout. check CPM status failed.\n')
    if  re.search(r'disabled, running', tmpbuffer) and re.search(r'disabled, not running', tmpbuffer):
        print 'CPM state 2 OK.\n'
        logger.debug('CPM state 2 OK.\n')
    else:
        print 'CPM state 2 error.\n'
        logger.error('CPM state 2 error.\n')    
        exit(1)

    tel_input_s(tn,'ldapconf_adm --action status_host_all\n', 'root:', 'Timeout. check CPM status failed.\n')
    if  re.search(r'enabled, running', tmpbuffer) and re.search(r'enabled, running', tmpbuffer):
        print 'CPM state 3 OK.\n'
        logger.debug('CPM state 3 OK.\n')
    else:
        print 'CPM state 3 error.\n'
        logger.error('CPM state 3 error.\n')    
        exit(1) 

    tel_input_s(tn,'radiusconf_adm --action status_host_all\n', 'root:', 'Timeout. check CPM status failed.\n')
    if  re.search(r'enabled, running', tmpbuffer) and re.search(r'enabled, running', tmpbuffer):
        print 'CPM state 4 OK.\n'
        logger.debug('CPM state 4 OK.\n')
    else:
        print 'CPM state 4 error.\n'
        logger.error('CPM state 4 error.\n')    
        exit(1)
         

    #Do health check in MI-A
    if load > 'R22':    
        tel_input_l(tn,'health --test all\n', 'root:', 'Timeout. health check failed.\n')
        if  re.search(r"chk_all returned WARNING", tmpbuffer):
            print 'chk_all returned WARNING.\n'
            logger.debug('chk_all returned WARNING.\n')
        elif re.search(r"chk_all completed successfully", tmpbuffer):
            print 'chk_all completed successfully.\n'
            logger.debug('chk_all completed successfully.\n')   
        else:    
            print 'Health check failed.\n'
            logger.error('Health check failed.\n')
    else:
         tel_input_l(tn,'su_health -a\n', 'root:', 'Timeout. health check failed.\n')
        
        
except Exception:
    print 'Bringup lab error!\n'
    exit(1)

try:        
    #close telnet
    tn.close()
    
except Exception:
    print 'Telnet close error!\n'
    exit(1)

endtime = datetime.datetime.now()
print 'spent times is:\n'
print (endtime -starttime).seconds
logger.debug('spent times is:\n')
logger.debug ((endtime -starttime).seconds)


#***********************end*********************************



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值