def setOutFile(self): """ function: set out file input : NA output : NA """ # get directory component of a pathname dirName = os.path.dirname(self.opts.outFile) # judge if directory if not os.path.isdir(dirName): g_file.createDirectory(dirName, True, DefaultValue.KEY_DIRECTORY_MODE) # create output file and modify permission g_file.createFile(self.opts.outFile, True, DefaultValue.KEY_FILE_MODE) g_file.changeOwner(self.opts.user, self.opts.outFile) self.logger.log("Performing performance check. " "Output the checking result to the file %s." % self.opts.outFile) # write file self.opts.outFile_tmp = os.path.join( DefaultValue.getTmpDirFromEnv(self.opts.user), (os.path.split(self.opts.outFile)[1] + "_tmp_%s" % os.getpid())) if not os.path.isfile(self.opts.outFile_tmp): g_file.createFile(self.opts.outFile_tmp, True, DefaultValue.KEY_FILE_MODE) g_file.changeOwner(self.opts.user, self.opts.outFile_tmp) fp = open(self.opts.outFile_tmp, "w") outputInfo = fp return outputInfo
def __decompressBinPackage(self): """ function: Install database binary file. input : NA output: NA """ if self.dws_mode: self.logger.log("Copying bin file.") bin_image_path = DefaultValue.DWS_APP_PAHT srcPath = "'%s'/*" % bin_image_path destPath = "'%s'/" % self.installPath cmd = g_file.SHELL_CMD_DICT["copyFile"] % (srcPath, destPath) self.logger.debug("Copy command: " + cmd) status, output = subprocess.getstatusoutput(cmd) if status != 0: self.logger.logExit(ErrorCode.GAUSS_502["GAUSS_50214"] % srcPath + " Error: " + output) else: self.logger.log("Decompressing bin file.") tarFile = g_OSlib.getBz2FilePath() # let bin executable g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, tarFile) cmd = "export LD_LIBRARY_PATH=$GPHOME/script/gspylib/clib:" \ "$LD_LIBRARY_PATH && " # decompress tar file. strCmd = cmd + "tar -xpf \"" + tarFile + "\" -C \"" + \ self.installPath + "\"" self.logger.log("Decompress command: " + strCmd) status, output = subprocess.getstatusoutput(strCmd) if status != 0: self.logger.logExit(ErrorCode.GAUSS_502["GAUSS_50217"] % tarFile + " Error: \n%s" % str(output)) # mv $GPHOME/script/transfer.py to $GAUSSHOME/bin/ dirName = os.path.dirname(os.path.realpath(__file__)) transferFile = dirName + "/../../script/transfer.py" if os.path.exists(transferFile): g_file.cpFile(transferFile, self.installPath + "/bin/") g_file.removeFile(transferFile) # cp $GPHOME/script to $GAUSSHOME/bin/ g_file.cpFile(dirName + "/../../script", self.installPath + "/bin/") # cp $GAUSSHOME/bin/script/gspylib/etc/sql/pmk to /share/postgresql destPath = self.installPath + "/share/postgresql/" pmkPath = self.installPath + "/bin/script/gspylib/etc/sql/" pmkFile = pmkPath + "pmk_schema.sql" if os.path.exists(pmkFile): g_file.cpFile(pmkFile, destPath) pmkSingeInstFile = pmkPath + "pmk_schema_single_inst.sql" if os.path.exists(pmkSingeInstFile): g_file.cpFile(pmkSingeInstFile, destPath) # change owner for tar file. g_file.changeOwner(self.user, self.installPath, True) self.logger.log("Successfully decompressed bin file.")
def createFile(fileName, path, permission=FILE_MODE, user=""): # file path fileName = os.path.join(path, fileName) # Create a file g_file.createFile(fileName, True, permission) # change owner if (user): g_file.changeOwner(user, fileName) return fileName
def createFolder(folderName, path, permission=DIRECTORY_MODE, user=""): # Folder path folderName = os.path.join(path, folderName) # Create a folder g_file.createDirectory(folderName, True, permission) # change owner if (user): g_file.changeOwner(user, folderName) return folderName
def __createStaticConfig(self): """ function: Save cluster info to static config input : NA output: NA """ staticConfigPath = "%s/bin/cluster_static_config" % self.installPath # save static config nodeId = self.dbNodeInfo.id self.clusterInfo.saveToStaticConfig(staticConfigPath, nodeId) g_file.changeMode(DefaultValue.KEY_FILE_MODE, staticConfigPath) g_file.changeOwner(self.user, staticConfigPath, False)
def writeFile(fileName, content, path, permission=FILE_MODE, user=""): """ function: write file input : NA output : NA """ filePath = os.path.join(path, fileName) # Create a file g_file.createFile(filePath, True, permission) # Modify the file permissions if (user): g_file.changeOwner(user, filePath) g_file.writeFile(filePath, [content])
def checkSingleSysTable(self, Instance): tablelist = [ "pg_attribute", "pg_class", "pg_constraint", "pg_partition", "pgxc_class", "pg_index", "pg_stats" ] localPath = os.path.dirname(os.path.realpath(__file__)) resultMap = {} try: for i in tablelist: sqlFile = "%s/sqlFile_%s_%s.sql" % (self.tmpPath, i, Instance.instanceId) resFile = "%s/resFile_%s_%s.out" % (self.tmpPath, i, Instance.instanceId) g_file.createFile(sqlFile, True, DefaultValue.SQL_FILE_MODE) g_file.createFile(resFile, True, DefaultValue.SQL_FILE_MODE) g_file.changeOwner(self.user, sqlFile) g_file.changeOwner(self.user, resFile) sql = "select * from pg_table_size('%s');" % i sql += "select count(*) from %s;" % i sql += "select * from pg_column_size('%s');" % i g_file.writeFile(sqlFile, [sql]) cmd = "gsql -d %s -p %s -f %s --output %s -t -A -X" % ( self.database, Instance.port, sqlFile, resFile) if (self.mpprcFile != "" and self.mpprcFile is not None): cmd = "source '%s' && %s" % (self.mpprcFile, cmd) SharedFuncs.runShellCmd(cmd, self.user) restule = g_file.readFile(resFile) g_file.removeFile(sqlFile) g_file.removeFile(resFile) size = restule[0].strip() line = restule[1].strip() width = restule[2].strip() Role = "" if (Instance.instanceRole == INSTANCE_ROLE_COODINATOR): Role = "CN" elif (Instance.instanceRole == INSTANCE_ROLE_DATANODE): Role = "DN" instanceName = "%s_%s" % (Role, Instance.instanceId) resultMap[i] = [instanceName, size, line, width] return resultMap except Exception as e: if os.path.exists(sqlFile): g_file.removeFile(sqlFile) if os.path.exists(resFile): g_file.removeFile(resFile) raise Exception(str(e))
def recursivePath(self, filepath): """ function: recursive path input: filepath output: NA """ fileList = os.listdir(filepath) for fileName in fileList: fileName = os.path.join(filepath, fileName) # change the owner of files g_file.changeOwner(self.context.g_opts.user, fileName) if (os.path.isfile(fileName)): # change fileName permission g_file.changeMode(DefaultValue.KEY_FILE_MODE, fileName) else: # change directory permission g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, fileName, True) self.recursivePath(fileName)
def run(self): """ function: the real interface that execute the check method input : NA output: NA """ try: outputInfo = None # check output file if self.opts.outFile != "": self.opts.outFile_tmp = os.path.join( DefaultValue.getTmpDirFromEnv(self.opts.user), (os.path.split(self.opts.outFile)[1] + "_tmp_%s" % os.getpid())) outputInfo = self.setOutFile() else: outputInfo = sys.stdout # check check item for key in self.opts.checkItem: if key == "PMK": # check PMK self.CheckPMKPerf(outputInfo) elif key == "SSD": # check SSD self.CheckSSDPerf(outputInfo) # Follow-up self.closeFile(outputInfo) except Exception as e: # close file handle if outputInfo is out file if self.opts.outFile and outputInfo: outputInfo.flush() outputInfo.close() if os.path.isfile(self.opts.outFile_tmp): g_file.removeFile(self.opts.outFile_tmp) # modify the log file's owner g_file.changeOwner(self.opts.user, self.logger.logFile) self.logger.error(str(e)) sys.exit(1)
def __decompressBinPackage(self): """ function: Install database binary file. input : NA output: NA """ if self.dws_mode: self.logger.log("Copying bin file.") bin_image_path = DefaultValue.DWS_APP_PAHT srcPath = "'%s'/*" % bin_image_path destPath = "'%s'/" % self.installPath cmd = g_file.SHELL_CMD_DICT["copyFile"] % (srcPath, destPath) self.logger.debug("Copy command: " + cmd) status, output = subprocess.getstatusoutput(cmd) if status != 0: self.logger.logExit(ErrorCode.GAUSS_502["GAUSS_50214"] % srcPath + " Error: " + output) else: self.logger.log("Decompressing bin file.") tarFile = g_OSlib.getBz2FilePath() # let bin executable g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, tarFile) cmd = "export LD_LIBRARY_PATH=$GPHOME/script/gspylib/clib:" \ "$LD_LIBRARY_PATH && " # decompress tar file. strCmd = cmd + "tar -xpf \"" + tarFile + "\" -C \"" + \ self.installPath + "\"" self.logger.log("Decompress command: " + strCmd) status, output = subprocess.getstatusoutput(strCmd) if status != 0: self.logger.logExit(ErrorCode.GAUSS_502["GAUSS_50217"] % tarFile + " Error: \n%s" % str(output)) # change owner for tar file. g_file.changeOwner(self.user, self.installPath, True) self.logger.log("Successfully decompressed bin file.")
def chmodFile(fileName, permission=FILE_MODE, user=""): # Modify the file permissions g_file.changeMode(permission, fileName) if (user): g_file.changeOwner(user, fileName)
def installToolsPhase1(self): """ function: install tools to local machine input: NA output: NA """ self.context.logger.log("Installing the tools on the local node.", "addStep") try: # Determine if the old version of the distribution package # is in the current directory oldPackName = "%s-Package-bak.tar.gz" \ % VersionInfo.PRODUCT_NAME_PACKAGE oldPackPath = os.path.join(self.context.clusterToolPath, oldPackName) if os.path.exists(self.context.clusterToolPath): versionFile = os.path.join(self.context.clusterToolPath, "version.cfg") if os.path.isfile(versionFile): version, number, commitid = VersionInfo.get_version_info( versionFile) newPackName = "%s-Package-bak_%s.tar.gz" % ( VersionInfo.PRODUCT_NAME_PACKAGE, commitid) newPackPath = os.path.join(self.context.clusterToolPath, newPackName) if os.path.isfile(oldPackPath): cmd = "(if [ -f '%s' ];then mv -f '%s' '%s';fi)" % ( oldPackPath, oldPackPath, newPackPath) self.context.logger.debug( "Command for rename bak-package: %s." % cmd) DefaultValue.execCommandWithMode( cmd, "backup bak-package files", self.context.sshTool, self.context.localMode or self.context.isSingle, self.context.mpprcFile) if (self.context.mpprcFile != ""): # check mpprc file self.checkMpprcFile() # check the package is not matches the system DefaultValue.checkPackageOS() # get the package path dirName = os.path.dirname(os.path.realpath(__file__)) packageDir = os.path.join(dirName, "./../../../../") packageDir = os.path.normpath(packageDir) # change logPath owner self.context.logger.debug("Modifying logPath owner") dirName = os.path.dirname(self.context.logFile) topDirFile = "%s/topDirPath.dat" % dirName keylist = [] if (self.context.localMode): if (os.path.exists(topDirFile)): keylist = g_file.readFile(topDirFile) if (keylist != []): for key in keylist: if (os.path.exists(key.strip())): g_file.changeOwner(self.context.user, key.strip(), True, "shell") else: self.context.logger.debug( "Warning: Can not find the " "path in topDirPath.dat.") g_file.removeFile(topDirFile) self.context.logger.debug("Successfully modified logPath owner") # Delete the old bak package in GPHOME before copy the new one. for bakPack in DefaultValue.PACKAGE_BACK_LIST: bakFile = os.path.join(self.context.clusterToolPath, bakPack) if (os.path.isfile(bakFile)): self.context.logger.debug("Remove old bak-package: %s." % bakFile) g_file.removeFile(bakFile) DefaultValue.makeCompressedToolPackage(packageDir) # check and create tool package dir global toolTopPath ownerPath = self.context.clusterToolPath clusterToolPathExistAlready = True # if clusterToolPath exist, # set the clusterToolPathExistAlready False if (not os.path.exists(ownerPath)): clusterToolPathExistAlready = False ownerPath = DefaultValue.getTopPathNotExist(ownerPath) toolTopPath = ownerPath # append clusterToolPath to self.context.needFixOwnerPaths # self.context.needFixOwnerPaths will be checked the ownet self.context.needFixOwnerPaths.append(ownerPath) # if clusterToolPath is not exist, then create it if not os.path.exists(self.context.clusterToolPath): g_file.createDirectory(self.context.clusterToolPath) g_file.changeMode(DefaultValue.MAX_DIRECTORY_MODE, self.context.clusterToolPath, True, "shell") # change the clusterToolPath permission if not clusterToolPathExistAlready: #check the localMode if self.context.localMode: #local mode,change the owner g_file.changeMode(DefaultValue.DIRECTORY_MODE, ownerPath, recursive=True, cmdType="shell") g_file.changeOwner(self.context.user, ownerPath, recursive=True, cmdType="shell") #not localMode, only change the permission else: g_file.changeMode(DefaultValue.MAX_DIRECTORY_MODE, ownerPath, recursive=True, cmdType="shell") else: g_file.changeMode(DefaultValue.DIRECTORY_MODE, ownerPath, recursive=False, cmdType="shell") # Send compressed package to local host if (packageDir != self.context.clusterToolPath): # copy the package to clusterToolPath g_file.cpFile( os.path.join(packageDir, DefaultValue.get_package_back_name()), self.context.clusterToolPath) # Decompress package on local host g_file.decompressFiles( os.path.join(self.context.clusterToolPath, DefaultValue.get_package_back_name()), self.context.clusterToolPath) # change mode of packages g_file.changeMode(DefaultValue.DIRECTORY_MODE, self.context.clusterToolPath, recursive=True, cmdType="shell") # get the top path of mpprc file need to be created on local node # this is used to fix the newly created path owner later if self.context.mpprcFile != "": ownerPath = self.context.mpprcFile if (not os.path.exists(self.context.mpprcFile)): while True: # find the top path to be created (ownerPath, dirName) = os.path.split(ownerPath) if os.path.exists(ownerPath) or dirName == "": ownerPath = os.path.join(ownerPath, dirName) break self.context.needFixOwnerPaths.append(ownerPath) # check the current storage package path is legal Current_Path = os.path.dirname(os.path.realpath(__file__)) DefaultValue.checkPathVaild(os.path.normpath(Current_Path)) # set ENV cmd = "%s -t %s -u %s -l %s -X '%s' -Q %s" % ( OMCommand.getLocalScript("Local_PreInstall"), ACTION_SET_TOOL_ENV, self.context.user, self.context.localLog, self.context.xmlFile, self.context.clusterToolPath) if self.context.mpprcFile != "": cmd += " -s '%s' " % self.context.mpprcFile #check the localmode,if mode is local then modify user group if self.context.localMode: cmd += "-g %s" % self.context.group (status, output) = subprocess.getstatusoutput(cmd) # if cmd failed, then exit if status != 0: self.context.logger.debug( "Command for setting %s tool environment variables: %s" % (VersionInfo.PRODUCT_NAME, cmd)) raise Exception(output) except Exception as e: raise Exception(str(e)) self.context.logger.log( "Successfully installed the tools on the local node.", "constant")
def doSet(self): resultStr = "" for dirName in g_chList: g_file.changeOwner(self.user, dirName, True) g_file.changeMode(DIRECTORY_MODE, dirName) self.result.val = "Set DirPermissions completely."