def install(self): """ Using TIBCO Universal Installer to install AMX build via Silent Mode""" import os, subprocess from lib.util.Properties.Properties import Properties curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "..\config\serverInfo.config") as configFile: testD = Properties() testD.load(configFile) # if self.removeAMXHome: # os.removedirs(testD.getProperty("installationRoot")) self.installCommand = ( self.targetFolder + os.sep + str(testD["installer"]).split("/")[-1] + " -silent -V responseFile=" + self.silentFile ) try: self.logger.debug(self.installCommand) # Wait child process to finish and then process otehrs tasks proc = subprocess.call(self.installCommand, shell=True) # proc = subprocess.Popen(self.installCommand, shell=True) yibu self.logger.debug("TIBCO Universal Installer PID is " + proc.pid) # proc.communicate() proc.wait() except Exception, e: self.logger.error(e)
def modifyStgFile(self): from xml.etree import ElementTree from lib.util.Properties.Properties import Properties import os,shutil curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "../config/serverInfo.config") as configFile: testD = Properties() testD.load(configFile) xmlDoc = ElementTree.parse(curPath + "../config/templates/amsg.stg") lableV = xmlDoc.getroot().find("EventConsumer").find("params").find("labelVersion") lableV.text = testD.getProperty("temp.install.amx.version") lableB = xmlDoc.getroot().find("EventConsumer").find("params").find("labelBuild") lableB.text = testD.getProperty("temp.install.amx.build") if testD.getProperty("temp.install.amx.createEnv") == "false": lableHF = xmlDoc.getroot().find("EventConsumer").find("params").find("labelExtra") lableHF.text = testD.getProperty("temp.install.amx.hf_lable") self.logger.info("Successfully generate stg file : amsg.stg") xmlDoc.write("amsg.stg",encoding='utf-8', xml_declaration=True) self.logger.debug("Ready to copy amsg.stg file to user profile dir") for i in self.userDirLisa: shutil.copy("amsg.stg", self.baseUserDir + i) self.logger.info("Copy amsg.stg file to " + self.baseUserDir + i)
def download(self): Downloader.download(self) import urllib2 from lib.util.Properties.Properties import Properties from FailHandler import DecodeEncodde import os curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "..\config\serverInfo.config") as configFile: testD = Properties() testD.load(configFile) decoder = DecodeEncodde() if testD.getProperty("needAuth") == "true": passwordM = urllib2.HTTPPasswordMgrWithDefaultRealm() self.logger.debug("Checking " + self.FileURL) passwordM.add_password(None,self.FileURL,decoder.decode(100,testD.getProperty("auth_User")),decoder.decode(100,testD.getProperty("auth_Password"))) handler = urllib2.HTTPBasicAuthHandler(passwordM) opener = urllib2.build_opener(handler) self.logger.debug("Fill in HTTP authentication information.............") else: opener = urllib2.build_opener() req = opener.open(self.FileURL) urllib2.install_opener(opener) # Get information of download file meta = req.info() self.FileSize = int(meta.getheaders("Content-Length")[0]) self.FileType = meta.getheaders("Content-Type")[0].split(";")[0] self.logger.debug("File size of downloading file is " + str(self.FileSize/1024/1024) + "M") u = urllib2.urlopen(self.FileURL) blockSize = int(testD.getProperty("DownloadBlockSize")) file_size_dl = 0 # Remember to use wb mode to write block, otherwise the data is not completed fileName = str(self.FileURL).split("/")[-1] with open(fileName,"wb") as configFile: while True: buffer = u.read(blockSize) if not buffer: break file_size_dl += len(buffer) configFile.write(buffer) status = r"%10dM [%3.2f%%]" % (file_size_dl/1024/1024, file_size_dl * 100. / self.FileSize) status = status + chr(8)*(len(status)+1) self.logger.debug(status) AMXInstaller().copyFileToLocation(fileName, self.Location)
def eMailNotification(self, subject=None, content=None): # http://blog.csdn.net/menglei8625/article/details/7721746 from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # python 2.3.*: email.Utils email.Encoders from email.utils import COMMASPACE, formatdate from email import encoders from lib.util.Properties.Properties import Properties curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "../config/serverInfo.config") as configFile: testD = Properties() testD.load(configFile) server = testD.getProperty("email.SMTP.Server") user = testD.getProperty("email.SMTP.User") passwd = testD.getProperty("email.SMTP.Pwd") fro_email = testD.getProperty("email.SMTP.User") to = testD.getProperty("send.mail.address") msg = MIMEMultipart() msg["From"] = fro_email msg["Subject"] = subject msg["To"] = to msg["Date"] = formatdate(localtime=True) msg.attach(MIMEText(content)) # for file in files: # part = MIMEBase('application', 'octet-stream') #'octet-stream': binary data # part.set_payload(open(file, 'rb'.read())) # encoders.encode_base64(part) # part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) # msg.attach(part) import smtplib try: smtp = smtplib.SMTP(server) smtp.login(user, passwd) smtp.sendmail(fro_email, to, msg.as_string()) self.logger.info("Sending Notification Mail") smtp.close() except Exception, e: self.logger.debug(e)
def CheckInstalled(self): # Check if target amx folder exist or not, if yes, it means the target AMX had been installed, no need to install again from lib.util.Properties.Properties import Properties import os curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep # Loading the Pre-defined message with open(curPath + "..\config\serverInfo.config") as confiFile: testD = Properties() testD.load(confiFile) if os.path.exists(testD.getProperty("installationRoot") + testD.getProperty("temp.install.amx.environmentName")): self.logger.debug("Target Installation folder exist, no need to download and install again") return True else: self.logger.debug("Target Installation folder not exist, install product into target folder..") return False
def generateMD5(self,FileLocation=None): import hashlib,os myHashCode = hashlib.md5() import os curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep if os.path.exists(FileLocation) and os.path.isfile(FileLocation): with open(FileLocation,"rb") as AMXBuild, open(curPath + "..\config\serverInfo.config") as configFile: from lib.util.Properties.Properties import Properties testD = Properties() testD.load(configFile) blockSize = int(testD.getProperty("DownloadBlockSize")) while True: b = AMXBuild.read(blockSize) if not b: break myHashCode.update(b) return myHashCode.hexdigest() else: return None
def calculateDep(self): import os from lib.util.Properties.Properties import Properties curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "..\config\serverInfo.config") as configFile, open( curPath + "..\config\dependence.config" ) as configFile2: testD = Properties() testD.load(configFile) testP = Properties() testP.load(configFile2) dependString = "" if "HF" in str(testD.getProperty("DownloadURL")).split("/")[-1]: dependString += "HF." + testD.getProperty("temp.install.amx.version") + ".dependOn" if testP.getProperty(dependString) is not None: return testP.getProperty(dependString) else: return None
def __init__(self,workingDir=None): self.logger = logging.getLogger("com.tibco.amxqa.automation.loader") # working dir should locate {Automation}/bin self.workingDir = workingDir self.reportFolder = "" self.userDirLisa = [] self.LisaSuite = {"userProile":"","configFile":"","suiteFile":""} # Load Lisa command string, to put amsg.stg file and local.properties file into different user folder from lib.util.Properties.Properties import Properties import os curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "../config/serverInfo.config") as configFile: testD = Properties() testD.load(configFile) for i in str(testD.getProperty("lisa.suites")).split(";"): #To get single lisa command index = 0 for j in str(i).split(" "): if str(i).split(" ")[index] == "-u": self.userDirLisa.append(str(i).split(" ")[index + 1]) break index += 1 self.logger.debug("Get user profile dir list here:") self.logger.debug(self.userDirLisa) self.baseUserDir = os.path.split(testD.getProperty("lisa.working.dir"))[0] + os.sep + "cmds" + os.sep + "userdir" + os.sep self.logger.debug("Get baseUserDir here is " + self.baseUserDir)
def parpareSilentFile(self, templateFile, ouputFile): import os from lib.util.Properties.Properties import Properties if not os.path.exists(templateFile) or not os.path.isfile(templateFile): raise IOError(templateFile + " not exit") curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "..\config\serverInfo.config") as configFile: testD = Properties() testD.load(configFile) xmlDoc = ElementTree.parse(templateFile, parser=CommentedTreeBuilder()) for index in xmlDoc.getroot().findall("entry"): if index.attrib.get("key") == "acceptLicense": index.text = "true" if index.attrib.get("key") == "createNewEnvironment": index.text = testD.getProperty("temp.install.amx.createEnv") if index.attrib.get("key") == "installationRoot": if str(testD.getProperty("installationRoot")).endswith("\\") or str( testD.getProperty("installationRoot") ).endswith("/"): index.text = testD.getProperty("installationRoot") + testD.getProperty( "temp.install.amx.environmentName" ) else: index.text = ( testD.getProperty("installationRoot") + os.sep + testD.getProperty("temp.install.amx.environmentName") ) if index.attrib.get("key") == "environmentName": index.text = testD.getProperty("temp.install.amx.environmentName") if "HF" in self.installedFile: if index.attrib.get("key") == "useInstallProfile": xmlDoc.getroot().remove(index) if index.attrib.get("key") == "selectedProfiles": xmlDoc.getroot().remove(index) else: if index.attrib.get("key") == "useInstallProfile": index.text = "true" if index.attrib.get("key") == "selectedProfiles": index.text = "Administration,Runtime Host,SOA Development" if index.attrib.get("key") == "configDirectoryRoot": index.text = testD.getProperty("installationRoot") xmlDoc.write(ouputFile, encoding="utf-8", xml_declaration=True) # Very ugly way to handle DOCTYPE with open(ouputFile) as silentFile: tempBuffer = "" while True: buffer = silentFile.readline() if buffer == "": break tempBuffer += buffer if r"<?xml" in buffer: tempBuffer += r'<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">' with open(ouputFile, "w") as f: f.write(tempBuffer) self.logger.info("Generate AMX Slient file " + ouputFile) self.silentFile = ouputFile
def start(self): from framework.amxAutomation.Detector.DetectorHandler import NetDetector from framework.amxAutomation.DownloadAMXBuild.Download import HTTPDownloader from framework.amxAutomation.InstallAMXBuild.InstallAMX import AMXInstaller from framework.amxAutomation.LanuchAMXAutomation.LisaLoader import Loader import socket curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep hostname = socket.gethostname() ip_address = socket.gethostbyname(hostname) with open(curPath + "../config/serverInfo.config") as configFile, open( curPath + "../config/dependence.config" ) as configFile2: testD = Properties() testD.load(configFile) testP = Properties() testP.load(configFile2) FileUrl = testD.getProperty("DownloadURL") tempFolder = testD.getProperty("tempFolderToInstall") detector = NetDetector(FileUrl) while True: import time if detector.detect(): break print "Cannot find any build available information, wait and move on, next try is in (s)" + testD.getProperty( "DownloadInterval" ) time.sleep(float(testD.getProperty("DownloadInterval"))) if not detector.CheckInstalled(): download = HTTPDownloader(FileUrl, tempFolder) download.download() if testD.getProperty("send.mail") == "true": subject = "AMX Build " + testD.getProperty("temp.install.amx.environmentName") + " is available" buildAvailable = ( "AMX Build " + testD.getProperty("temp.install.amx.environmentName") + " is available and downloaded on " + hostname + "(" + ip_address + ")" ) content = ( "Hi AMX QAs, \n\n\n " + buildAvailable + "\n\n" + "You can find it under " + testD.getProperty("tempFolderToInstall") + ", and is been copied and pasted to shared file server\n\n" + "AMX CDC Automation" ) self.eMailNotification(subject, content) installer = AMXInstaller(FileUrl) if installer.calculateDep() is not None: print "Found dependent Build" dependBuildFile = testP.getProperty(installer.calculateDep()) print dependBuildFile dependInstaller = AMXInstaller(dependBuildFile) dependInstaller.unzipBuild(dependBuildFile, tempFolder + os.sep + "amx") dependInstaller.parpareSilentFile( curPath + "../config/templates/TIBCOUniversalInstaller-amsg3x_3.3.0.silent", tempFolder + os.sep + "amx" + os.sep + testD.getProperty("tempSilentFileName"), ) dependInstaller.copyFileToLocation( testD.getProperty("lgplFolder"), tempFolder + os.sep + "amx" + os.sep + "assemblies" ) dependInstaller.install() installer.unzipBuild( tempFolder + os.sep + testD.getProperty("temp.install.amx.build.name"), tempFolder + os.sep + "Installation", ) installer.parpareSilentFile( curPath + "../config/templates/TIBCOUniversalInstaller-amsg3x_3.3.0.silent", tempFolder + os.sep + "Installation" + os.sep + testD.getProperty("tempSilentFileName"), ) installer.copyFileToLocation( testD.getProperty("lgplFolder"), tempFolder + os.sep + "Installation" + os.sep + "assemblies" ) installer.copyFileToLocation(testD.getProperty("installer"), tempFolder + os.sep + "Installation") installer.install() loader = Loader(self.workingDir) loader.modifyStgFile() loader.validateLisaAccount() import platform if "Windows" in platform.system(): loader.loadBATFile() else: loader.loadShellFile() for command in str(testD.getProperty("lisa.suites")).split(";"): loader.launchLisa(command) suiteInfo = loader.parseAutomationSuite(command) if testD.getProperty("send.mail") == "true": subject = "AMX Automation Result for " + suiteInfo["suiteFile"] + " is available" AutomationResult = ( "AMX Automation Result for " + suiteInfo["suiteFile"] + " with config file " + suiteInfo["configFile"] + " is available on host " + hostname + "(" + ip_address + ")" ) content = "Hi AMX QAs, \n\n\n " + AutomationResult + "\n\n" + "AMX CDC Automation" self.eMailNotification(subject, content) self.stop()
if xmlDoc.getroot().find("EventConsumer").find("params").find("reportingPlatform").text is None: self.logger.debug("Not set reportingPlatform, just backup report folder") else: self.reportFolder += ( os.sep + xmlDoc.getroot().find("EventConsumer").find("params").find("reportingPlatform").text ) self.logger.debug(self.reportFolder) shutil.move(self.reportFolder, self.reportFolder + "_" + str(self.reportIndex)) self.logger.debug("Backup reporting folder to " + self.reportFolder + "_" + str(self.reportIndex)) os.mkdir(self.reportFolder) self.reportIndex += 1 # add number to make back up folder as _0 _1 _2....... self.reportFolder = "" # Restore Report folder if __name__ == "__main__": # Loading global variable and logging configuration initEnv() curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "../config/serverInfo.config") as configFile: testD = Properties() testD.load(configFile) tester = AMXAutomation(testD.getProperty("lisa.working.dir")) tester.start()
def loadBATFile(self): import os import shutil from lib.util.Properties.Properties import Properties curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "../config/serverInfo.config") as configFile: testD = Properties() testD.load(configFile) # Set Lisa Home, set AMX TIBCO_Home # SET LISA_HOME=C:/Lisa # SET TIBCO_HOME=D:/tibco/AMX_INSTALLER/TIB_amx_3.3.0_HF-012_V175 if testD.getProperty("temp.install.amx.version") == "3.3.0": self.logger.info("Find AMX 3.3.0 matched version...") with open("setenv.bat","w") as batFile: batFile.write("SET LISA_HOME=" + testD.getProperty("lisa.home") + "\n") batFile.write("SET TIBCO_HOME=" + testD.getProperty("installationRoot") + "/" + testD.getProperty("temp.install.amx.environmentName") + "\n") with open(curPath + "../config/templates/330_setenv.bat") as batTemplate: while True: buffer = batTemplate.readline() if buffer == "": break batFile.write(buffer) batFile.flush() elif testD.getProperty("temp.install.amx.version") == "3.2.0": self.logger.info("Find AMX 3.2.0 matched version...") with open("setenv.bat","w") as batFile: batFile.write("SET LISA_HOME=" + testD.getProperty("lisa.home")) batFile.write("SET TIBCO_HOME=" + testD.getProperty("installationRoot") + "/" + testD.getProperty("temp.install.amx.environmentName")) with open(curPath + "../config/templates/320_setenv.bat") as batTemplate: while True: buffer = batTemplate.readline() if buffer == "": break batFile.write(buffer) batFile.flush() elif testD.getProperty("temp.install.amx.version") == "3.1.5": self.logger.info("Find AMX 3.1.5 matched version...") with open("setenv.bat","w") as batFile: batFile.write("SET LISA_HOME=" + testD.getProperty("lisa.home")) batFile.write("SET TIBCO_HOME=" + testD.getProperty("installationRoot") + "/" + testD.getProperty("temp.install.amx.environmentName")) with open(curPath + "../config/templates/315_setenv.bat") as batTemplate: while True: buffer = batTemplate.readline() if buffer == "": break batFile.write(buffer) batFile.flush() else: self.logger.debug("No Matched Setup BAT file founded, Please contact @author: [email protected] to get more support") return None for i in self.userDirLisa: shutil.copy("setenv.bat", self.baseUserDir + i) self.logger.info("Copy setenv.bat file to " + self.baseUserDir + i)
def __init__(self): try: import logging import os curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep with open(curPath + "../config/serverInfo.config") as configFile: testD = Properties() testD.load(configFile) # FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s' # logging.basicConfig(format=FORMAT) c_handler = logging.StreamHandler() f_handler = logging.FileHandler(curPath + os.sep + "../log/amxautomation.log") logger_envSetup = logging.getLogger("com.tibco.amxqa.automation.init") logger_Detector = logging.getLogger("com.tibco.amxqa.automation.detector") logger_Download = logging.getLogger("com.tibco.amxqa.automation.download") logger_Installer = logging.getLogger("com.tibco.amxqa.automation.installer") logger_loader = logging.getLogger("com.tibco.amxqa.automation.loader") logger_setup = logging.getLogger("com.tibco.amxqa.automation.setup") logger_envSetup.setLevel(logging.DEBUG) logger_envSetup.addHandler(c_handler) logger_envSetup.debug("Set logger level of AMX Automation with " + testD.getProperty("loggingLevel")) if testD.getProperty("loggingLevel") == "debug": logger_Detector.setLevel(logging.DEBUG) logger_Download.setLevel(logging.DEBUG) logger_Installer.setLevel(logging.DEBUG) logger_loader.setLevel(logging.DEBUG) c_handler.setLevel(logging.DEBUG) f_handler.setLevel(logging.DEBUG) logger_setup.setLevel(logging.DEBUG) elif testD.getProperty("loggingLevel") == "info": logger_Detector.setLevel(logging.INFO) logger_Download.setLevel(logging.INFO) logger_Installer.setLevel(logging.INFO) logger_loader.setLevel(logging.INFO) c_handler.setLevel(logging.INFO) f_handler.setLevel(logging.INFO) logger_setup.setLevel(logging.INFO) else: logger_Detector.setLevel(logging.ERROR) logger_Download.setLevel(logging.ERROR) logger_Installer.setLevel(logging.ERROR) logger_loader.setLevel(logging.ERROR) c_handler.setLevel(logging.ERROR) f_handler.setLevel(logging.ERROR) logger_setup.setLevel(logging.ERROR) logger_Detector.addHandler(c_handler) logger_Download.addHandler(c_handler) logger_Installer.addHandler(c_handler) logger_loader.addHandler(c_handler) logger_setup.addHandler(c_handler) logger_Detector.addHandler(f_handler) logger_Download.addHandler(f_handler) logger_Installer.addHandler(f_handler) logger_loader.addHandler(f_handler) logger_setup.addHandler(f_handler) except Exception,e: print e exit(1)
def detect(self): import os curPath = os.path.abspath(os.path.dirname(__file__)) + os.sep Detector.detect(self) import urllib2 from framework.amxAutomation.DownloadAMXBuild.FailHandler import DecodeEncodde decoder = DecodeEncodde() # Loading the Pre-defined message with open(curPath + "..\config\serverInfo.config") as confiFile: testP = Properties() testP.load(confiFile) if testP.getProperty("needAuth") == "true": passwordM = urllib2.HTTPPasswordMgrWithDefaultRealm() passwordM.add_password(None,self.completeURL,decoder.decode(100,testP.getProperty("auth_User")),decoder.decode(100,testP.getProperty("auth_Password"))) handler = urllib2.HTTPBasicAuthHandler(passwordM) opener = urllib2.build_opener(handler) else: opener = urllib2.build_opener() try: req = opener.open(self.completeURL) urllib2.install_opener(opener) # Get information of download file meta = req.info() self.FileSize = int(meta.getheaders("Content-Length")[0]) self.FileType = meta.getheaders("Content-Type")[0].split(";")[0] if self.FileSize > 0: self.logger.info(self.completeURL + " is available now!") testP.setProperty("temp.install.amx.version", self.VersionInfo) self.logger.debug("Generate amx version " + self.VersionInfo) testP.setProperty("temp.install.amx.build", self.BuildInfo) self.logger.debug("Generate build version " + self.BuildInfo) if self.isHF: testP.setProperty("temp.install.amx.createEnv", "false") self.logger.debug("Going to install one HF") else: testP.setProperty("temp.install.amx.createEnv", "true") self.logger.debug("Going to install one AMX build") testP.setProperty("temp.install.amx.environmentName", self.envName) self.logger.debug("Generate env name is " + self.envName) testP.setProperty("temp.install.amx.hf_lable", self.hf_lable) self.logger.debug("Generate HF Lable is " + self.hf_lable) testP.setProperty("temp.install.amx.build.name",self.downloadFileName) self.logger.debug("Generate AMX Build File name is " + self.downloadFileName) testP.store(open(curPath + "..\config\serverInfo.config","w+"),"Append the temp var") return True else: return False except Exception,e: self.logger.debug(e) pass