def CleanTmpFiles(self): """ function: clean temp files input : NA output: NA """ self.logger.debug("Deleting temporary files.", "addStep") try: # copy record_app_directory file tmpDir = DefaultValue.getTmpDirFromEnv(self.user) if tmpDir == "": raise Exception(ErrorCode.GAUSS_518["GAUSS_51800"] % "$PGHOST") upgradeBackupPath = os.path.join(tmpDir, "binary_upgrade") copyPath = os.path.join(upgradeBackupPath, "record_app_directory") appPath = DefaultValue.getInstallDir(self.user) if appPath == "": raise Exception(ErrorCode.GAUSS_518["GAUSS_51800"] % "$PGHOST") if copyPath != "": copyCmd = "(if [ -f '%s' ];then cp -f -p '%s' '%s/';fi)" % ( copyPath, copyPath, appPath) DefaultValue.execCommandWithMode( copyCmd, "copy record_app_directory file", self.sshTool, self.localMode, self.mpprcFile) cmd = g_file.SHELL_CMD_DICT["cleanDir"] % ( self.tmpDir, self.tmpDir, self.tmpDir) # clean dir of PGHOST DefaultValue.execCommandWithMode(cmd, "delete temporary files", self.sshTool, self.localMode, self.mpprcFile) except Exception as e: self.logger.logExit(str(e)) self.logger.debug("Successfully deleted temporary files.", "constant")
def copyAndModCertFiles(self): """ function : copy and chage permission cert files input : NA output : NA """ user = g_OSlib.getUserInfo()["name"] appPath = DefaultValue.getInstallDir(user) caPath = os.path.join(appPath, "share/sslcert/om") # cp cert files g_file.cpFile("%s/server.crt" % caPath, "%s/" % self.instInfo.datadir) g_file.cpFile("%s/server.key" % caPath, "%s/" % self.instInfo.datadir) g_file.cpFile("%s/cacert.pem" % caPath, "%s/" % self.instInfo.datadir) g_file.cpFile("%s/server.key.cipher" % caPath, "%s/" % self.instInfo.datadir) g_file.cpFile("%s/server.key.rand" % caPath, "%s/" % self.instInfo.datadir) # change mode g_file.changeMode(DefaultValue.KEY_FILE_MODE, "%s/server.crt" % self.instInfo.datadir) g_file.changeMode(DefaultValue.KEY_FILE_MODE, "%s/server.key" % self.instInfo.datadir) g_file.changeMode(DefaultValue.KEY_FILE_MODE, "%s/cacert.pem" % self.instInfo.datadir) g_file.changeMode(DefaultValue.KEY_FILE_MODE, "%s/server.key.cipher" % self.instInfo.datadir) g_file.changeMode(DefaultValue.KEY_FILE_MODE, "%s/server.key.rand" % self.instInfo.datadir)
def doDeploy(self): """ function: Deploy Application input : NA output: NA """ # read the install setp from INSTALL_STEP self.context.logger.debug("Installing application") # compare xmlconfigInfo with staticConfigInfo gaussHome = DefaultValue.getInstallDir(self.context.user) commonStaticConfigFile = "%s/bin/cluster_static_config" % gaussHome if os.path.exists(commonStaticConfigFile): self.context.oldClusterInfo = dbClusterInfo() self.context.oldClusterInfo.initFromStaticConfig( self.context.user, commonStaticConfigFile) sameFlag = self.compareOldNewClusterConfigInfo( self.context.clusterInfo, self.context.oldClusterInfo) else: sameFlag = True step = self.context.readOperateStep() # if step is STEP_INSTALL if (step == STEP_INSTALL) or (step == STEP_CONFIG and not sameFlag): # rollback the install self.rollbackInstall() # write the install step self.context.writeOperateStep(STEP_INIT) # read the install step from INSTALL_STEP step = self.context.readOperateStep() # if step is STEP_INIT if step == STEP_INIT: # write the install step STEP_INSTALL into INSTALL_STEP self.context.writeOperateStep(STEP_INSTALL) # install Gauss200 DB self.doInstall() # write the install step STEP_CONFIG into INSTALL_STEP self.context.writeOperateStep(STEP_CONFIG) # read the install step from INSTALL_STEP step = self.context.readOperateStep() # if step is STEP_CONFIG if step == STEP_CONFIG: # config Gauss200 DB self.doConfig() # write the install step STEP_CONFIG into STEP_START self.context.writeOperateStep(STEP_START) # read the install step from INSTALL_STEP step = self.context.readOperateStep() # if step is STEP_START if step == STEP_START: # start Gauss200 DB self.doStart() # change pg_log file mode in pg_log path (only AP) self.checkPgLogFileMode() # clear the backup directory. self.context.managerOperateStepDir("delete") self.context.logger.log("Successfully installed application.")
def parseClusterInfoFromStaticFile(self): """ function: 1.init the clusterInfo 2.get clusterInfo from static config file input : NA output: NA """ try: self.readConfigInfo() except Exception as e: self.logger.debug(str(e)) gaussHome = DefaultValue.getInstallDir(self.user) try: g_oldVersionModules = OldVersionModules() if (os.path.exists( "%s/bin/script/util/DbClusterInfo.py" % gaussHome)): sys.path.append( os.path.dirname("%s/bin/script/util/" % gaussHome)) else: sys.path.append(os.path.dirname( "%s/bin/script/gspylib/common/" % gaussHome)) g_oldVersionModules.oldDbClusterInfoModule = __import__( 'DbClusterInfo') self.clusterInfo = \ g_oldVersionModules.oldDbClusterInfoModule.dbClusterInfo() self.clusterInfo.initFromStaticConfig(self.user) except Exception as e: self.logger.debug(str(e)) try: self.clusterInfo = dbClusterInfo() self.clusterInfo.initFromStaticConfig(self.user) except Exception as e: self.logger.logExit(str(e))
def __changeuserEnv(self): """ function: Change user GAUSS_ENV input : NA output: NA """ # clean os user environment variable self.logger.log("Modifying user's environmental variable $GAUSS_ENV.") userProfile = self.mpprcFile DefaultValue.updateUserEnvVariable(userProfile, "GAUSS_ENV", "1") if "HOST_IP" in os.environ.keys(): g_file.deleteLine(userProfile, "^\\s*export\\s*WHITELIST_ENV=.*$") self.logger.log("Successfully modified user's environmental" " variable GAUSS_ENV.") self.logger.debug("Deleting symbolic link to $GAUSSHOME if exists.") gaussHome = DefaultValue.getInstallDir(self.user) if gaussHome == "": raise Exception(ErrorCode.GAUSS_518["GAUSS_51800"] % "$GAUSSHOME") if os.path.islink(gaussHome): self.installPath = os.path.realpath(gaussHome) os.remove(gaussHome) else: self.logger.debug("symbolic link does not exists.") self.logger.debug("Deleting bin file in installation path.") g_file.removeDirectory("%s/bin" % self.installPath) self.logger.debug("Successfully deleting bin file in" " installation path.")
def createServerCa(self, hostList=None): """ function: create grpc ca file input : NA output: NA """ self.logger.debug("Generating CA files.") if hostList is None: hostList = [] appPath = DefaultValue.getInstallDir(self.user) caPath = os.path.join(appPath, "share/sslcert/om") self.logger.debug("The ca file dir is: %s." % caPath) if (len(hostList) == 0): for dbNode in self.clusterInfo.dbNodes: hostList.append(dbNode.name) # Create CA dir and prepare files for using. self.logger.debug("Create CA file directory.") try: DefaultValue.createCADir(self.sshTool, caPath, hostList) self.logger.debug("Add hostname to config file.") DefaultValue.createServerCA(DefaultValue.SERVER_CA, caPath, self.logger) # Clean useless files, and change permission of ca file to 600. DefaultValue.cleanServerCaDir(caPath) self.logger.debug("Scp CA files to all nodes.") except Exception as e: certFile = caPath + "/demoCA/cacert.pem" if os.path.exists(certFile): g_file.removeFile(certFile) DefaultValue.cleanServerCaDir(caPath) raise Exception(str(e)) for certFile in DefaultValue.SERVER_CERT_LIST: scpFile = os.path.join(caPath, "%s" % certFile) self.sshTool.scpFiles(scpFile, caPath, hostList) self.logger.debug("Successfully generated server CA files.")
def genCipherAndRandFile(self, hostList=None, initPwd=None): self.logger.debug("Encrypting cipher and rand files.") if hostList is None: hostList = [] appPath = DefaultValue.getInstallDir(self.user) binPath = os.path.join(appPath, "bin") retry = 0 while True: if not initPwd: sshpwd = getpass.getpass("Please enter password for database:") sshpwd_check = getpass.getpass("Please repeat for database:") else: sshpwd = sshpwd_check = initPwd if sshpwd_check != sshpwd: sshpwd = "" sshpwd_check = "" self.logger.error(ErrorCode.GAUSS_503["GAUSS_50306"] % "database" + "The two passwords are different, " "please enter password again.") else: cmd = "%s/gs_guc encrypt -M server -K %s -D %s " % ( binPath, sshpwd, binPath) (status, output) = subprocess.getstatusoutput(cmd) sshpwd = "" sshpwd_check = "" initPwd = "" if status != 0: self.logger.error(ErrorCode.GAUSS_503["GAUSS_50322"] % "database" + "Error:\n %s" % output) else: break if retry >= 2: raise Exception(ErrorCode.GAUSS_503["GAUSS_50322"] % "database") retry += 1 g_file.changeMode(DefaultValue.KEY_FILE_MODE, "'%s'/server.key.cipher" % binPath) g_file.changeMode(DefaultValue.KEY_FILE_MODE, "'%s'/server.key.rand" % binPath) if len(hostList) == 0: for dbNode in self.clusterInfo.dbNodes: hostList.append(dbNode.name) if not self.isSingle: # localhost no need scp files for certFile in DefaultValue.BIN_CERT_LIST: scpFile = os.path.join(binPath, "%s" % certFile) self.sshTool.scpFiles(scpFile, binPath, hostList) self.logger.debug("Successfully encrypted cipher and rand files.")
def checkVersion(): """ function: check version information input: NA output: NA """ g_logger.debug("Checking version information.") gaussHome = DefaultValue.getInstallDir(g_opts.user) if gaussHome == "": raise Exception(ErrorCode.GAUSS_518["GAUSS_51800"] % "$GAUSSHOME") localPostgresVersion = \ DefaultValue.getAppBVersion(os.path.realpath(gaussHome)) if (localPostgresVersion.find(g_opts.upgrade_version) > 0): g_logger.debug("Successfully checked version information.") else: raise Exception(ErrorCode.GAUSS_529["GAUSS_52935"])
def initClusterInfo(self, refreshCN=True): """ function: Init cluster info input : NA output: NA """ try: self.clusterInfo = dbClusterInfo() if (refreshCN): static_config_file = "%s/bin/cluster_static_config" % \ DefaultValue.getInstallDir(self.user) self.clusterInfo.initFromXml(self.xmlFile, static_config_file) else: self.clusterInfo.initFromXml(self.xmlFile) except Exception as e: raise Exception(str(e)) self.logger.debug("Instance information of cluster:\n%s." % str(self.clusterInfo))
def createLinkToApp(): """ function: create link to app input : NA output : NA """ if g_opts.upgrade: g_opts.logger.log("Under upgrade process," " no need to create symbolic link.") return g_opts.logger.debug("Created symbolic link to $GAUSSHOME with commitid.") gaussHome = DefaultValue.getInstallDir(g_opts.user) if gaussHome == "": raise Exception(ErrorCode.GAUSS_518["GAUSS_51800"] % "$GAUSSHOME") versionFile = VersionInfo.get_version_file() commitid = VersionInfo.get_version_info(versionFile)[2] actualPath = gaussHome + "_" + commitid if os.path.exists(gaussHome): if not os.path.islink(gaussHome): raise Exception(ErrorCode.GAUSS_502["GAUSS_50200"] % gaussHome + " Cannot create symbolic link," " please rename or delete it.") else: if os.path.realpath(gaussHome) == actualPath: g_opts.logger.log("$GAUSSHOME points to %s, no need to create" " symbolic link." % actualPath) return cmd = "ln -snf %s %s" % (actualPath, gaussHome) g_opts.logger.log("Command for creating symbolic link: %s." % cmd) (status, output) = subprocess.getstatusoutput(cmd) if status != 0: g_opts.logger.log(output) g_opts.logger.logExit(ErrorCode.GAUSS_501["GAUSS_50107"] % "app.") g_opts.logger.debug("Successfully created symbolic link to" " $GAUSSHOME with commitid.")
def readConfigInfoByXML(self): """ function: Read config from xml config file input : NA output: NA """ try: if (self.clusterConfig is None): self.logger.logExit(ErrorCode.GAUSS_502["GAUSS_50201"] % "XML configuration file") static_config_file = "%s/bin/cluster_static_config" % \ DefaultValue.getInstallDir(self.user) self.clusterInfo = dbClusterInfo() self.clusterInfo.initFromXml(self.clusterConfig, static_config_file) hostName = DefaultValue.GetHostIpOrName() self.dbNodeInfo = self.clusterInfo.getDbNodeByName(hostName) if (self.dbNodeInfo is None): self.logger.logExit(ErrorCode.GAUSS_516["GAUSS_51619"] % hostName) except Exception as e: self.logger.logExit(str(e)) self.logger.debug("Instance information on local node:\n%s" % str(self.dbNodeInfo))