def getFileAbsolutePath(rootDir, fileName): fileSourcePathList = findFile(rootDir, fileName) if len(fileSourcePathList) > 1: raise myexception.MyException("Find too many files for %s" % fileName) elif len(fileSourcePathList) < 1: raise myexception.MyException("Didn't find file for %s." % fileName) else: fileAbsolutePathStr = fileSourcePathList[0] return fileAbsolutePathStr
def getDpmName(): cmdStr = 'kubectl get deployment -o name --namespace=%s' % CONFIG.NAME_SPACE p = subprocess.Popen(cmdStr, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) if p.stdout: rcNameList = p.stdout.readlines() else: raise myException.MyException("%s" % p.stderr) rcRealNameList = [] for rcName in rcNameList: if 'deployment/' in rcName: rcRealName = rcName.split('deployment/')[1] rcRealNameList.append(rcRealName) # 去掉换行符 rcLastList = [] for i in rcRealNameList: newName = i.strip() rcLastList.append(newName) # # 排除不需要更新镜像的RC # exincludeList = [] # for rc in rcLastList: # if rc in CONFIG.EXCLUDE_RC_LIST: # pass # else: # exincludeList.append(rc) return rcLastList
def bucketUploadFileByFtp(iTup): # ip, port, ftpDir, user, passwd, passiveVal, fileList #ftp = ftpModule.createFtpLink(iTup[0], iTup[1], iTup[2], iTup[3], iTup[4], iTup[5]) # logService.initLogging() uploadCount = 0 # 限流,如果令牌桶中有令牌,则上传图片,如果没有,则空循环 # 每秒往桶中存放3个令牌 token = tokenBucket.TokenBucket(3, 100000) token._current_amount = 1 index = 0 try: while index < len(iTup[6]): if token.consume(1): ftp = ftpModule.createFtpLink(iTup[0], iTup[1], iTup[2], iTup[3], iTup[4], iTup[5]) ftpModule.uploadFile(ftp, iTup[6][index]) timeStr = str( time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())) print "%s pid:%s--file: %s has been uploaded." % ( timeStr, os.getpid(), iTup[6][index]) uploadCount = uploadCount + 1 index = index + 1 ftp.quit() #ftp.quit() except Exception, e: raise MYEXCEPTION.MyException(e)
def gotoDBLastSnapshot(envDic): snapshotName = vmSnapshotOperation.getLastAndFirstSnapshotName( envDic['NEW_BOSS_DB_VMNAME'], envDic['NEW_BOSS_VMWARE_IP'], envDic['NEW_BOSS_VMWARE_USERNAME'], envDic['NEW_BOSS_VMWARE_PASSWORD']) logging.info('last snapshot name = ' + snapshotName[0]) snapStatus = vmSnapshotOperation.getSnapshotStat( snapshotName[0], envDic['NEW_BOSS_VMWARE_IP'], envDic['NEW_BOSS_VMWARE_USERNAME'], envDic['NEW_BOSS_VMWARE_PASSWORD'], envDic['NEW_BOSS_DB_VMNAME']) while True: res_db = vmSnapshotOperation.gotoSnapshot( envDic['NEW_BOSS_DB_VMNAME'], snapshotName[0], envDic['NEW_BOSS_VMWARE_IP'], envDic['NEW_BOSS_VMWARE_USERNAME'], envDic['NEW_BOSS_VMWARE_PASSWORD']) if res_db == 'ok': break if snapStatus != '1': while True: res_stat = poweronDevDBVmare(envDic) if res_stat == 'ok': break if res_db == 'ok' and res_stat == 'ok': logging.info( 'because the new boss test environment start faild, so back db snashot to the last stat: %s' % snapshotName[0]) else: err = 'ERROR! Please contact admin to check the virtual machine environment! gotoSnapshot error or poweron error' raise myException.MyException(err)
def getRcName(): cmdStr = 'kubectl get rc -o name --namespace=kube-system' p = subprocess.Popen(cmdStr, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) if p.stdout: rcNameList = p.stdout.readlines() else: raise myException.MyException("%s" % p.stderr) rcRealNameList = [] for rcName in rcNameList: if 'replicationcontroller/' in rcName: rcRealName = rcName.split('replicationcontroller/')[1] rcRealNameList.append(rcRealName) # 排除不需要更新镜像的RC # 去掉换行符 rcLastList = [] for i in rcRealNameList: newName = i.strip() rcLastList.append(newName) exincludeList = [] for rc in rcLastList: if rc in CONFIG.EXCLUDE_RC_LIST: pass else: exincludeList.append(rc) return exincludeList
def delCivilByRepoIDAndCivilID(repoID, civilID): url = "http://%s:%s/api/repositories/%s/entities/%s" % (DATA.DEEPCLOUD_IP, DATA.DEEPCLOUD_PORT, repoID, civilID) resDelCivilByRepoIDAndCivilIDObject = HTTP_REQUEST.del_request(url) if resDelCivilByRepoIDAndCivilIDObject[1] == 200: return resDelCivilByRepoIDAndCivilIDObject else: raise MYEXCEPTION.MyException("Delete civil by repoID and civilID error.")
def queryCivilByRepoID(repoID): url = "http://%s:%s/api/repositories/%s/entities" % (DATA.DEEPCLOUD_IP, DATA.DEEPCLOUD_PORT, repoID) resQueryCivilByRepoIDObject = HTTP_REQUEST.get_request(url) if resQueryCivilByRepoIDObject[1] == 200: return resQueryCivilByRepoIDObject else: raise MYEXCEPTION.MyException("Query civil by repoID error.")
def addRule(source): url = DATA.ADD_RULE_URL resObject = HTTP_REQUEST.post_request(url, source) print resObject if resObject[1] == 201: return resObject else: raise MYEXCEPTION.MyException("add rule error.")
def getSnapshotStat(snapName, ip, user, pwd, vmname): t = snapshotManager(ip, user, pwd, vmname, 'list', '', '') list1 = t.split('\n') statStr = '' for i in list1: if snapName in i: statStr = i if statStr == '': logging.info("%s is not exists" % snapName) raise myException.MyException("%s is not exists" % snapName) else: if 'poweredon' in statStr.lower(): return '1' elif 'poweredoff' in statStr.lower(): return '0' else: logging.info("Snapshot list Error") raise myException.MyException("Snapshot list Error")
def findFile(rootDir, fileName): print fileName allFileAbsolutePathList = [] wantFileAbsolutePathList = [] tupIterator = os.walk(rootDir) for i in tupIterator: for j in i[2]: allFileAbsolutePathList.append(os.path.join(i[0], j)) # 获得指定文件的绝对路径列表 for m in allFileAbsolutePathList: if m.endswith(fileName) and os.path.isfile(m): wantFileAbsolutePathList.append(m) if len(wantFileAbsolutePathList) > 1: raise myexception.MyException("Find too many files for %s" % fileName) elif len(wantFileAbsolutePathList) < 1: raise myexception.MyException("Didn't find file for %s." % fileName) else: return wantFileAbsolutePathList[0]
def delRuleById(ruleId): url = "http://%s:%s/api/monitors/%s" % (DATA.DEEPCLOUD_IP, DATA.DEEPCLOUD_PORT, ruleId) resDelRuleByIdObject = HTTP_REQUEST.del_request(url) if resDelRuleByIdObject[1] != 200: # 此处按照文档描述,应该是204 raise MYEXCEPTION.MyException( "Del rule by id error. errorCode: %s, errorMessage: %s" % (resDelRuleByIdObject[0], resDelRuleByIdObject[1])) else: return resDelRuleByIdObject
def getSuffixFileFromDir(theDir, suffix): suffixFileList = [] fileAndDirList = os.listdir(theDir) if len(fileAndDirList) == 0: raise myexception.MyException("The dir : %s is empty." % theDir) else: for i in fileAndDirList: # 如果文件以suffix结尾,返回文件的绝对路径 if os.path.isfile(os.path.join(theDir, i)) and i.endswith(suffix): suffixFileList.append(os.path.join(theDir, i)) return suffixFileList
def stopRunningList(ip): runningContainerList = checkRunningContainer(ip) logging.info("============================") logging.info("Start to stop running containers on %s" % ip) for i in runningContainerList: sshAndRun(['docker stop %s' % i], ip) resList = checkRunningContainer(ip) if not resList: return 'OK' else: raise myException.MyException( "The number of stopped is not equal to the number of detected")
def deleteAllImages(ip): imagesList = checkAllImages(ip) if imagesList: logging.info("=====================") logging.info("Start to delete all images on %s" % ip) sshAndRun(['docker rmi -f $(docker images -q)'], ip) newimagesList = checkAllImages(ip) if newimagesList: raise myException.MyException( "Delete images of %s error, please check it." % ip) logging.info("There is no image on %s." % ip) return 'OK'
def pingSTAF(ip): try: handle = PySTAF.STAFHandle("PatchTest") request = "ping" res = str(handle.submit(ip, "ping", request).resultContext) handle.unregister() return res except PySTAF.STAFException, e: logging.error("#pingSTAF()# -- Error registering with STAF, RC: %d" % e.rc) raise myException.MyException( "#pingSTAF()# -- Error registering with STAF, RC: %d" % e.rc)
def changeTimeByDateCmd(machineList, newTime): newTime = newTime + '.07' cmdStr = 'date %s' % newTime cmdList = [] cmdList.append(cmdStr) for machine in machineList: resTup = tool.sshAndRun(cmdList, machine) logging.info(' '.join(resTup[0][cmdStr])) if resTup[1][cmdStr]: logging.error(' '.join(resTup[1][cmdStr])) raise myException.MyException(' '.join(resTup[1][cmdStr])) logging.info("Time of node %s has been changed successfully." % machine)
def changeMasterTime(newTime, masterIp): newTime = newTime + '.07' cmds = [] cmdStr = 'date %s' % newTime cmds.append(cmdStr) print "**************************" resTup = tool.sshAndRun(cmds, masterIp) print len(resTup[0][cmdStr]) logging.info(' '.join(resTup[0][cmdStr])) if resTup[1][cmdStr]: logging.error(' '.join(resTup[1][cmdStr])) raise myException.MyException(' '.join(resTup[1][cmdStr])) logging.info("Time of master %s has been changed successfully." % masterIp)
def stafCopyDirectory(handle, sourceDirPath, sourceMachine, destinationDirPath, destinationMachine): ''' 通过STAF复制文件夹 ''' try: request = "copy DIRECTORY %s TODIRECTORY %s TOMACHINE %s RECURSE KEEPEMPTYDIRECTORIES" % ( sourceDirPath, destinationDirPath, destinationMachine) handle.submit(sourceMachine, "FS", request) except PySTAF.STAFException, e: logging.error("Error registering with STAF, RC: %d" % e.rc) raise myException.MyException("Error registering with STAF, RC: %d" % e.rc)
def stafCopyFile(handle, sourcePath, sourceMachine, destinationPath, destinationMachine): ''' 通过STAF复制文件 ''' try: request = "copy file %s TOFILE %s TOMACHINE %s" % ( sourcePath, destinationPath, destinationMachine) handle.submit(sourceMachine, "FS", request) except PySTAF.STAFException, e: logging.error("Error registering with STAF, RC: %d" % e.rc) raise myException.MyException("Error registering with STAF, RC: %d" % e.rc)
def startWarContainers(mapIp, mapWarAndIpDic, tagName, warContainersNameList): cmdDic = getStartWarContainerCmd(mapIp, tagName, warContainersNameList) containersList = warContainersNameList for container in containersList: cmdList = [] cmdList.append(cmdDic[container]) resTup = sshAndRun(cmdList, mapWarAndIpDic[container]) resKeyList = resTup[0].keys() if resTup[0][resKeyList[0]]: logging.info("Start container for %s success on %s" % (container, mapWarAndIpDic[container])) logging.info("ContainID is %s" % resTup[0][resKeyList[0]][0]) else: raise myException.MyException(resTup[1][resKeyList[0]])
def syncTimeWithMater(nodeIpList, masterIp): for nodeIp in nodeIpList: cmdStrList = [] print masterIp print nodeIp cmdStr = 'ntpdate -u %s' % masterIp cmdStrList.append(cmdStr) print cmdStrList resTup = tool.sshAndRun(cmdStrList, nodeIp) logging.info(' '.join(resTup[0][cmdStr])) if resTup[1][cmdStr]: logging.error(' '.join(resTup[1][cmdStr])) raise myException.MyException(' '.join(resTup[1][cmdStr])) logging.info("Time of node %s has been changed successfully." % nodeIp)
def createSecret(): cmdStr = 'kubectl create -f %smysecret.yaml' % CONFIG.TEMP_DPMDIR p = subprocess.Popen(cmdStr, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) p.wait() str1 = p.stdout.read() str2 = p.stderr.read() if len(str1) > 0: logging.info("%s" % str1) else: logging.error("%s" % str2) raise myException.MyException("%s" % str2)
def scpTomcatServerFile(ip, fromPath, toPath): formPathList = [] toPathList = [] formPathList.append(fromPath) toPathList.append(toPath) # 如果ip上不存在指定的目录,先创建指定的目录 for i in toPathList: t = 'if [ ! -d "%s" ]; then mkdir -p %s; fi' % (i, i) m = [] m.append(t) sshAndRun(m, ip) if remoteShell.uploadViaSCP(formPathList, toPathList, ip) == 'ok': return True else: logging.info("scp server.xml to %s error" % ip) raise myException.MyException("scp server.xml to %s error" % ip)
def getModuleDir(rootDir, moduleDirName): moduleDirNameList = [] allDirList = [] tupIterator = os.walk(rootDir) for i in tupIterator: for j in i[1]: allDirList.append(os.path.join(i[0], j)) for m in allDirList: if os.path.isdir(m) and m.endswith(moduleDirName): moduleDirNameList.append(m) if len(moduleDirNameList) != 1: raise myexception.MyException("Dir of module %s is not unique. " % moduleDirName) else: tempDirStr = moduleDirNameList[0].strip() relativeDirStr = tempDirStr.split(rootDir)[1] return relativeDirStr
def getModuleDir(rootDir, moduleDirName): moduleDirNameList = [] allDirList = [] tupIterator = os.walk(rootDir) for i in tupIterator: for j in i[1]: allDirList.append(os.path.join(i[0], j)) for m in allDirList: if os.path.isdir(m) and m.endswith("/" + moduleDirName): moduleDirNameList.append(m) if len(moduleDirNameList) != 1: msg = "Dir of module %s is not unique. " % moduleDirName TOOL.exception_write("getModuleDir", msg) raise myexception.MyException(msg) else: relativeDirStr = moduleDirNameList[0].strip() return relativeDirStr
def pureZookeeperConfig(ip, configerFileDirStr): shellPath = CONFIG.REDIS_AND_ZOOKEEPER_CONFIG_DIR_IN_CONTAINER + CONFIG.REDIS_AND_ZOOKEEPER_CONFIG_RELATIVEDIR[ configerFileDirStr] + '/execConfig.sh' cmdList = [] # 给予文件权限 shellChmod = 'chmod 777 %s' % shellPath cmdList.append(shellChmod) # 执行shell shellRun = 'sh %s' % shellPath cmdList.append(shellRun) resTup = sshAndRun(cmdList, ip) resDic = resTup[0] for i in resDic[shellRun]: logging.info(i) if resTup[1][shellRun]: for j in resTup[1][shellRun]: logging.info(j) raise myException.MyException("Pure config for zookeeper error.")
def logInImageRegistory(): rightStr = 'docker login -u %s -p %s %s' % (CONFIG.DOCKER_INFO['registoryusername'], CONFIG.DOCKER_INFO['registorypassword'], CONFIG.DOCKER_INFO['registryaddress']) # logging.info("exec: %s" % rightStr) p = subprocess.Popen(rightStr, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) resList = p.stdout.readlines() if len(resList) != 0: #for i in resList: # logging.info("%s" % i.strip()) pass else: errStrList = p.stderr.readlines() errStrStripList = [] for j in errStrList: errStrStripList.append(j.strip()) errStr = ','.join(errStrStripList) raise myexception.MyException("%s" % errStr)
def getSVCName(): cmdStr = 'kubectl get svc -o name --namespace=%s' % CONFIG.NAME_SPACE p = subprocess.Popen(cmdStr, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) if p.stdout: svcNameList = p.stdout.readlines() else: raise myException.MyException("%s" % p.stderr) svcRealNameList = [] for svcName in svcNameList: if 'service/' in svcName: svcRealName = svcName.split('service/')[1] svcRealNameList.append(svcRealName) # 去掉换行符 svcLastList = [] for i in svcRealNameList: newName = i.strip() svcLastList.append(newName) return svcLastList
def stafCopyFileToDir(handle, sourcePath, destinationMachine, destinationPath, sourceMachine='192.168.32.94'): ''' 该方法默认通过STAF将用例文件从本地复制到soapui所在服务器(32.95) 也可调用该方法传入源文件地址和目标机器地址进行文件copy ''' try: request = "copy file %s TODIRECTORY %s TOMACHINE %s" % ( sourcePath, destinationPath, destinationMachine) handle.submit(sourceMachine, "FS", request) except PySTAF.STAFException, e: logging.error( "#stafCopyFile()# -- Error registering with STAF, RC: %d" % e.rc) raise myException.MyException( "#stafCopyFile()# -- Error registering with STAF, RC: %d" % e.rc)
def getModuleDir(rootDir, moduleDirName, jobname): moduleDirNameList = [] allDirList = [] tupIterator = os.walk(rootDir) for i in tupIterator: for j in i[1]: allDirList.append(os.path.join(i[0], j)) for m in allDirList: if os.path.isdir(m) and m.endswith("/" + moduleDirName) and os.path.exists( '%s/build.gradle' % m): moduleDirNameList.append(m) if len(moduleDirNameList) != 1: msg = "Dir of module %s is not exits.Please check parameter : moduleStr! " % moduleDirName TOOL.exception_write("getModuleDir", msg, jobname) raise myexception.MyException(msg) else: tempDirStr = moduleDirNameList[0].strip() relativeDirStr = tempDirStr.split(rootDir)[1] return relativeDirStr