def cleanScript(self): """ function: clean script """ # clean lib libPath = os.path.join(self.clusterToolPath, LIBPATH) if os.path.exists(libPath): g_file.removeDirectory(libPath) # clean om script scriptPath = os.path.join(self.clusterToolPath, SCRIPTPATH) if os.path.exists(scriptPath): g_file.removeDirectory(scriptPath) # clean root script path root_script_path = os.path.join(DefaultValue.ROOT_SCRIPTS_PATH, self.user) if os.path.exists(root_script_path): g_file.removeDirectory(root_script_path) # if /root/gauss_om has no files, delete it. if not os.listdir(DefaultValue.ROOT_SCRIPTS_PATH): g_file.removeDirectory(DefaultValue.ROOT_SCRIPTS_PATH) # clean others if os.path.exists(self.clusterToolPath): g_file.cleanDirectoryContent(self.clusterToolPath) if self.userHome != "": if os.path.exists(self.userHome): g_file.removeDirectory(self.userHome)
def ReCleanEtcdPath(self): """ function: make sure the etcd path is clean. input : NA output: NA """ # check if need delete instance if (not self.cleanInstance): self.logger.debug("No need to redelete etcd path.") return if (self.localMode): for dbnode in self.clusterInfo.dbNodes: if (dbnode.name == DefaultValue.GetHostIpOrName()): if (len(dbnode.etcds) > 0): etcdDir = dbnode.etcds[0].datadir self.logger.debug("Clean etcd path %s in node: %s." % (etcdDir, dbnode.name)) g_file.cleanDirectoryContent(etcdDir) else: for dbnode in self.clusterInfo.dbNodes: if (len(dbnode.etcds) > 0): etcdDir = dbnode.etcds[0].datadir cmd = g_file.SHELL_CMD_DICT["cleanDir4"] % etcdDir self.logger.debug("Clean etcd path %s in node: %s." % (etcdDir, dbnode.name)) (status, output) = self.sshTool.getSshStatusOutput( cmd, [dbnode.name], self.mpprcFile) if (status[dbnode.name] != DefaultValue.SUCCESS): self.logger.debug("Clean etcd failed: %s" % output)
def cleanDir(self, instDir): """ function: Clean the dirs input : instDir output: NA """ if (not os.path.exists(instDir)): return dataDir = [] dataDir = os.listdir(instDir) if (os.getuid() == 0): pglDir = '%s/pg_location' % instDir isPglDirEmpty = False if (os.path.exists(pglDir) and len(os.listdir(pglDir)) == 0): isPglDirEmpty = True if (len(dataDir) == 0 or isPglDirEmpty): g_file.cleanDirectoryContent(instDir) else: for info in dataDir: if (str(info) == "pg_location"): resultMount = [] resultFile = [] resultDir = [] pglDir = '%s/pg_location' % instDir # delete all files in the mount point cmd = "%s | %s '%s' | %s '{printf $3}'" % \ (g_Platform.getMountCmd(), g_Platform.getGrepCmd(), pglDir, g_Platform.getAwkCmd()) (status, outputMount) = subprocess.getstatusoutput(cmd) if (status != 0): raise Exception(ErrorCode.GAUSS_502["GAUSS_50207"] % instDir + " Error:\n%s." % str(outputMount) + "The cmd is %s" % cmd) else: if (len(outputMount) > 0): resultMount = str(outputMount).split() for infoMount in resultMount: g_file.cleanDirectoryContent(infoMount) else: g_file.cleanDirectoryContent(instDir) continue # delete file in the pg_location directory if (not os.path.exists(pglDir)): continue cmd = "cd '%s'" % pglDir (status, output) = subprocess.getstatusoutput(cmd) if (status != 0): raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] % cmd + " Error: \n%s " % output) outputFile = g_file.findFile(".", "f", "type") if (len(outputFile) > 0): for infoFile in outputFile: tmpinfoFile = pglDir + infoFile[1:] for infoMount in resultMount: if (tmpinfoFile.find(infoMount) < 0 and infoMount.find(tmpinfoFile) < 0): realFile = "'%s/%s'" % (pglDir, infoFile) g_file.removeFile(realFile, "shell") # delete directory in the pg_location directory cmd = "if [ -d '%s' ]; then cd '%s' && find -type d; fi" \ % \ (pglDir, pglDir) (status, outputDir) = subprocess.getstatusoutput(cmd) if (status != 0): raise Exception(ErrorCode.GAUSS_502["GAUSS_50207"] % instDir + " Error:\n%s." % str(outputDir) + "The cmd is %s" % cmd) else: resultDir = g_file.findFile(".", "d", "type") resultDir.remove(".") if (len(resultDir) > 0): for infoDir in resultDir: tmpinfoDir = pglDir + infoDir[1:] for infoMount in resultMount: if (tmpinfoDir.find(infoMount) < 0 and infoMount.find(tmpinfoDir) < 0): realPath = "'%s/%s'" % (pglDir, infoDir) g_file.removeDirectory(realPath) cmd = "if [ -d '%s' ];then cd '%s' && find . ! -name " \ "'pg_location' " \ "! -name '..' ! -name '.' -print0 |xargs -r -0 -n100 rm " \ "-rf; " \ "fi" % (instDir, instDir) (status, output) = subprocess.getstatusoutput(cmd) if (status != 0): raise Exception(ErrorCode.GAUSS_502["GAUSS_50207"] % instDir + " Error:\n%s." % str(output) + "The cmd is %s" % cmd)
def doRestore(self): """ function: restore files Restoring binary files: 1.decompress tar file 2.Check binary files 3.Create installation path 4.Restore binary files to install path Restoring parameter files: 1.decompress tar file 2.delete temporary directory 3.extract parameter files to the temporary directory 4.check hostname and parameter 5.Restore parameter files 6.Remove the temporary directory input : NA output: NA """ self.logger.log("Restoring files.") if self.restoreBin: self.logger.log("Restoring binary files.") try: # decompress tar file self.decompressTarFile("binary") # Checking binary files self.logger.debug("Checking if binary files exist.") tarName = os.path.join(self.restoreDir, self.binTarName) if (not os.path.exists(tarName)): raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % "Binary files") # Creating installation path self.logger.debug( "Creating installation path if did not exist.") if (not os.path.exists(self.installPath)): os.makedirs(self.installPath, DefaultValue.KEY_DIRECTORY_PERMISSION) # Restore binary files to install path. self.logger.debug("Restore binary files to install path.") g_file.cleanDirectoryContent(self.installPath) cmd = g_file.SHELL_CMD_DICT["decompressTarFile"] % ( self.restoreDir, tarName) cmd += " && " cmd += g_file.SHELL_CMD_DICT["copyFile"] % ( "'%s'/*" % self.binExtractName, self.installPath) self.logger.debug("Command for restoring binary files:%s." % cmd) (status, output) = subprocess.getstatusoutput(cmd) if (status != 0): raise Exception(ErrorCode.GAUSS_502["GAUSS_50220"] % ( "binary files to install path[%s]" % \ self.installPath) + " Error: \n%s" % output) g_file.removeDirectory( os.path.join(self.restoreDir, self.binExtractName)) except Exception as e: raise Exception(str(e)) self.logger.log("Successfully restored binary files.") if self.restorePara: self.logger.log("Restoring parameter files.") # Re-obtaining clusterInfo because the restoreBin succeeded if self.dbNodeInfo is None: self.clusterInfo.initFromStaticConfig(self.user, g_staticFile) hostName = DefaultValue.GetHostIpOrName() self.dbNodeInfo = self.clusterInfo.getDbNodeByName(hostName) if self.dbNodeInfo is None: self.logger.logExit(ErrorCode.GAUSS_516["GAUSS_51619"] % hostName) # Restoring parameter files. try: # decompress tar file self.decompressTarFile("parameter") # delete temporary directory self.logger.debug( "Delete temporary directory if it has existed.") temp_dir = os.path.join(self.restoreDir, "parameter_%s" % HOSTNAME) if (os.path.exists(temp_dir)): g_file.removeDirectory(temp_dir) # extract parameter files to the temporary directory self.logger.debug( "Extract parameter files to the temporary directory.") tarName = os.path.join(self.restoreDir, self.paraTarName) if (not os.path.exists(tarName)): if (g_ignoreMiss): self.logger.error(ErrorCode.GAUSS_502["GAUSS_50201"] % "parameter files") sys.exit(0) else: raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % "parameter files") cmd = g_file.SHELL_CMD_DICT["decompressTarFile"] % ( self.restoreDir, tarName) (status, output) = subprocess.getstatusoutput(cmd) if (status != 0): raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] % cmd + " Error: \n%s" % output) # check hostname self.logger.debug("Checking hostname.") self.__checkHostName("%s/%s" % (temp_dir, self.hostnameFileName)) # check parameter self.logger.debug("Checking parameter files.") paraFileList = [] self.__checkParaFiles(temp_dir, paraFileList) self.logger.debug("Restoring parameter files.") paraFileNum = len(paraFileList) for i in range(paraFileNum): tarFileName, paraFilePath = paraFileList[i].split('|') g_file.cpFile(os.path.join(temp_dir, tarFileName), paraFilePath) self.logger.debug("Remove the temporary directory.") g_file.removeDirectory(temp_dir) except Exception as e: g_file.removeDirectory(temp_dir) raise Exception(str(e)) self.logger.log("Successfully restored parameter files.") self.logger.log("Successfully restored files.")
def doBackup(self): """ function: 1.back up binary files 2.back up parameter files input : NA output: NA """ self.logger.log("Backing up files.") if self.backupBin: self.logger.log("Backing up binary files.") try: self.logger.debug( "Installation path is %s." % self.installPath) if (len(os.listdir(self.installPath)) == 0): raise Exception(ErrorCode.GAUSS_502["GAUSS_50203"] % ( "installation path [%s]" % self.installPath)) self.__tarDir(self.installPath, self.binTarName) except Exception as e: raise Exception(str(e)) self.logger.log("Successfully backed up binary files.") if self.backupPara: self.logger.log("Backing up parameter files.") try: self.logger.debug( "Creating temporary directory for all parameter files.") temp_dir = os.path.join(self.tmpBackupDir, "parameter_%s" % HOSTNAME) self.logger.debug("Temporary directory path: %s." % temp_dir) if (os.path.exists(temp_dir)): file_list = os.listdir(temp_dir) if (len(file_list) != 0): self.logger.debug( "The temporary directory " "is not empty.\n%s\nRemove all files silently." % file_list) g_file.cleanDirectoryContent(temp_dir) else: os.makedirs(temp_dir, DefaultValue.KEY_DIRECTORY_PERMISSION) self.logger.debug("Creating hostname file.") hostnameFile = os.path.join(temp_dir, self.hostnameFileName) self.logger.debug( "Register hostname file path: %s." % hostnameFile) g_file.createFileInSafeMode(hostnameFile) with open(hostnameFile, "w") as self.__hostnameFile: hostName = DefaultValue.GetHostIpOrName() self.__hostnameFile.write("%s" % hostName) self.logger.debug("Flush hostname file.") self.__hostnameFile.flush() self.__hostnameFile = None os.chmod(hostnameFile, DefaultValue.KEY_FILE_PERMISSION) self.logger.debug("Collecting parameter files.") for inst in self.dbNodeInfo.datanodes: self.__collectParaFilesToTempDir(inst, temp_dir) self.logger.debug( "Generating parameter files to be compressed.") self.__tarDir(temp_dir, self.paraTarName, True) self.logger.debug("Removing temporary directory.") g_file.removeDirectory(temp_dir) except Exception as e: g_file.removeDirectory(temp_dir) raise Exception(str(e)) self.logger.log("Successfully backed up parameter files.") self.logger.log("Successfully backed up files.")