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.")
Example #2
0
 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))
Example #3
0
def initGlobalInfos():
    """
    function: init global infos
    input: NA
    output: NA
    """
    global g_logger
    global g_clusterInfo
    g_logger = GaussLog(g_opts.logFile, "CheckUpgrade")
    try:
        if g_opts.action in [Const.ACTION_CHECK_VERSION]:
            g_logger.log("No need to init cluster info under action %s" %
                         g_opts.action)
            return
        g_clusterInfo = dbClusterInfo()
        if g_opts.xmlFile == "" or not os.path.exists(g_opts.xmlFile):
            if g_opts.appPath == "" or not os.path.exists(g_opts.appPath):
                raise Exception(ErrorCode.GAUSS_500["GAUSS_50001"] % "R")
            staticConfigFile = "%s/bin/cluster_static_config" % g_opts.appPath
            g_clusterInfo.initFromStaticConfig(g_opts.user, staticConfigFile)
        else:
            g_clusterInfo.initFromXml(g_opts.xmlFile)
    except Exception as e:
        g_logger.log(traceback.format_exc())
        g_logger.logExit(str(e))
Example #4
0
    def parseConfigFile(self):
        """
        function: parse the configuration file:
                  1.get local installation path for restoration
                  2.Obtain user and group for restoration
                  3.Obtain the local node information for restoration
        input : NA
        output: NA
        """
        self.logger.log("Parsing the configuration file.")

        try:
            self.clusterInfo = dbClusterInfo()
            gaussHome = os.getenv("GAUSSHOME")
            if g_forceRestore and self.restoreBin:
                self.clusterInfo.appPath = gaussHome
            else:
                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)
            # Getting local installation path for restoration.
            self.logger.log("Getting local installation path for restoration.")
            self.installPath = os.path.realpath(self.clusterInfo.appPath)
            self.binExtractName = self.installPath.split("/")[-1]
            self.logger.debug("Local installation path: %s." %
                              self.installPath)
        except Exception as e:
            raise Exception(str(e))

        self.logger.log("Successfully parsed the configuration file.")
 def __init__(self, logFile, user, clusterConf, dwsMode=False):
     """
     function: Constructor
     input : logFile, user, clusterConf, dwsMode
     output: NA
     """
     LocalBaseOM.__init__(self, logFile, user, clusterConf, dwsMode)
     if (self.clusterConfig == ""):
         # Read config from static config file
         self.readConfigInfo()
     else:
         self.clusterInfo = dbClusterInfo()
         self.clusterInfo.initFromXml(self.clusterConfig)
         hostName = DefaultValue.GetHostIpOrName()
         self.dbNodeInfo = self.clusterInfo.getDbNodeByName(hostName)
         if (self.dbNodeInfo is None):
             self.logger.logExit(ErrorCode.GAUSS_516["GAUSS_51619"] %
                                 hostName)
     # get user info
     self.getUserInfo()
     if (user != "" and self.user != user.strip()):
         self.logger.debug("User parameter : %s." % user)
         self.logger.logExit(ErrorCode.GAUSS_503["GAUSS_50315"] %
                             (self.user, self.clusterInfo.appPath))
     # init every component
     self.initComponent()
Example #6
0
    def readConfigInfo(self):
        """
        function: Read config from static config file
        input : NA
        output: NA
        """
        try:
            self.clusterInfo = dbClusterInfo()
            hostName = DefaultValue.GetHostIpOrName()
            dynamicFileExist = False
            if self.__class__.__name__ == "Start":
                dynamicFileExist = \
                    self.clusterInfo.dynamicConfigExists(self.user)
            if dynamicFileExist:
                self.clusterInfo.readDynamicConfig(self.user)
                self.dbNodeInfo = self.clusterInfo.getDbNodeByName(hostName)
            else:
                self.clusterInfo.initFromStaticConfig(self.user)
                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))
def initGlobals():
    """
    function: Init global log
    input : NA
    output: NA
    """
    # state global variable
    global g_logger
    global g_clusterInfo
    # Init the log file
    g_logger = GaussLog(g_opts.logFile, "gaussdb_localcheck")
    if os.path.exists(g_opts.confFile):
        g_clusterInfo = dbClusterInfo()
        g_clusterInfo.initFromXml(g_opts.confFile)
 def initClusterInfoFromStaticFile(self, user, flag=True):
     """
     function: Function to init clusterInfo from static file
     input : user
     output: NA
     """
     try:
         self.clusterInfo = dbClusterInfo()
         self.clusterInfo.initFromStaticConfig(user)
     except Exception as e:
         raise Exception(str(e))
     if flag:
         self.logger.debug("Instance information of cluster:\n%s." %
                           str(self.clusterInfo))
 def storageDbClusterInfo(self, dbclusterInfo):
     """
     function: covert to comp cluster
     input : dbclusterInfo
     output: midClusterInfo
     """
     # init dbcluster class
     midClusterInfo = dbClusterInfo()
     # get cluster name
     midClusterInfo.name = dbclusterInfo.name
     for dbnode in dbclusterInfo.dbNodes:
         compNodeInfo = dbNodeInfo()
         compNodeInfo.azName = dbnode.azName
         compNodeInfo.name = dbnode.name
         midClusterInfo.dbNodes.append(compNodeInfo)
     return midClusterInfo
Example #10
0
    def __init__(self,
                 logFile,
                 user,
                 clusterConf,
                 dwsMode=False,
                 mpprcFile="",
                 installPath="",
                 alarmComponent="",
                 upgrade=False):
        """
        function: Constructor
        input : logFile, user, clusterConf, dwsMode, mpprcFile, installPath
                alarmComponent, upgrade
        output: NA
        """
        LocalBaseOM.__init__(self, logFile, user, clusterConf, dwsMode)

        if self.clusterConfig == "":
            # Read config from static config file
            self.readConfigInfo()
        else:
            self.clusterInfo = dbClusterInfo()
            self.clusterInfo.initFromXml(self.clusterConfig,
                                         g_opts.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)
        # get user info
        self.getUserInfo()
        if user != "" and self.user != user.strip():
            self.logger.debug("User parameter : %s." % user)
            self.logger.logExit(ErrorCode.GAUSS_503["GAUSS_50315"] %
                                (self.user, self.clusterInfo.appPath))
        # init every component
        self.initComponent()

        self.mpprcFile = mpprcFile
        self.installPath = installPath
        self.alarmComponent = alarmComponent
        self.upgrade = upgrade
        # This script will be not validating the parameters.
        # Because this should be detected by which instance call
        #  this local script.
        self.productVersion = None
        self.time_out = None
Example #11
0
 def getInstanceNodeName(self):
     """
     function: Get Instance Node Name
     input : NA
     output: instance node name
     """
     user = g_OSlib.getUserInfo()["name"]
     clusterInfo = dbClusterInfo()
     clusterInfo.initFromStaticConfig(user)
     peerInsts = clusterInfo.getPeerInstance(self.instInfo)
     nodename = "dn_%d" % self.instInfo.instanceId
     if len(peerInsts) == 0:
         return nodename
     nodename = ClusterInstanceConfig. \
         setReplConninfoForSinglePrimaryMultiStandbyCluster(
         self.instInfo, peerInsts, clusterInfo)[1]
     return nodename
 def checkLogicCluster(self):
     clusterInfo = dbClusterInfo()
     staticConfigDir = os.path.join(self.cluster.appPath, "bin")
     cmd = "find %s -name *.cluster_static_config" % staticConfigDir
     output = SharedFuncs.runShellCmd(cmd)
     if output:
         for staticConfigFile in output.splitlines():
             clusterInfo.initFromStaticConfig(self.user, staticConfigFile,
                                              True)
             lcName = os.path.splitext(
                 os.path.basename(staticConfigFile))[0]
             for dbnode in clusterInfo.dbNodes:
                 if (dbnode.name == DefaultValue.GetHostIpOrName()):
                     return [lcName, dbnode]
         return ["", None]
     else:
         return ["", None]
 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 checkNetWorkMTU():
    """
    function: gs_check check NetWork card MTU parameters
    input: NA
    output: int
    """
    try:
        # Init cluster info
        DbClusterInfo = dbClusterInfo()
        DbClusterInfo.initFromStaticConfig(g_opts.user)
        localHost = DefaultValue.GetHostIpOrName()
        nodeIp = None
        for dbnode in DbClusterInfo.dbNodes:
            if (dbnode.name == localHost):
                nodeIp = dbnode.backIps[0]
                break
        networkCardNum = DefaultValue.CheckNetWorkBonding(nodeIp, False)
        # check NetWork card MTU parameters
        valueStr = DefaultValue.checkNetWorkMTU(nodeIp, False)
        if (not str(valueStr).isdigit()):
            g_logger.log("Abnormal reason: Failed to obtain network"
                         " card MTU value." + " Error: \n%s" % valueStr)
            return 1
        netParameter = DefaultValue.getConfigFilePara(configFile,
                                                      '/sbin/ifconfig')
        if (int(valueStr) != int(g_opts.mtuValue)):
            g_logger.log("        Abnormal: network '%s' 'mtu' value[%s:%s]"
                         " is different from the other node [%s:%s]"
                         % (networkCardNum, localHost, valueStr,
                            g_opts.hostname, g_opts.mtuValue))
            return 1
        elif (int(valueStr) != int(netParameter["mtu"])):
            g_logger.log("        Warning reason: variable 'MTU' RealValue "
                         "'%s' ExpectedValue '%s'." % (valueStr,
                                                       netParameter["mtu"]))
            return 2
        else:
            return 0

    except Exception as e:
        g_logger.log("        Abnormal reason: Failed to obtain the"
                     " networkCard parameter [MTU]. Error: \n        %s"
                     % str(e))
        return 1
Example #15
0
 def getClusterDbNodeInfo(clusterUser, xmlFile=""):
     """
     function: get cluster and database node info from static config file
     input : clusterUser, xmlFile
     output: NA
     """
     try:
         clusterInfo = dbClusterInfo()
         if (os.getuid() == 0):
             clusterInfo.initFromXml(xmlFile)
         else:
             clusterInfo.initFromStaticConfig(clusterUser)
         hostName = DefaultValue.GetHostIpOrName()
         dbNodeInfo = clusterInfo.getDbNodeByName(hostName)
         if (dbNodeInfo is None):
             raise Exception(ErrorCode.GAUSS_516["GAUSS_51619"] % hostName)
         return clusterInfo, dbNodeInfo
     except Exception as e:
         raise Exception(str(e))
Example #16
0
    def setPrimaryStandyConnInfo(self, peerInsts):
        """
        function: Modify replconninfo for datanode
        input : peerInsts
        output: NA
        """
        connInfo1 = None
        connInfo2 = None
        dummyStandbyInst = None
        nodename = None
        user = g_OSlib.getUserInfo()["name"]
        clusterInfo = dbClusterInfo()
        clusterInfo.initFromStaticConfig(user)
        if (self.clusterType ==
                DefaultValue.CLUSTER_TYPE_SINGLE_PRIMARY_MULTI_STANDBY or
                self.clusterType == DefaultValue.CLUSTER_TYPE_SINGLE_INST):
            (connInfo1, nodename) = ClusterInstanceConfig. \
                setReplConninfoForSinglePrimaryMultiStandbyCluster(
                self.instInfo, peerInsts, clusterInfo)
            for i in range(len(connInfo1)):
                connInfo = "replconninfo" + "%d" % (i + 1)
                tmpDict1 = {}
                tmpDict1[connInfo] = "'%s'" % connInfo1[i]
                self.setGucConfig(tmpDict1)
                if "availablezone" in tmpDict1[connInfo]:
                    tempazname = tmpDict1[connInfo].split("=")[-1].strip("'")
            #if "availablezone" in str(connInfo1):
            self.setGucConfig({"available_zone": "'%s'" %
                                                 self.instInfo.azName})
        else:
            (connInfo1, connInfo2, dummyStandbyInst, nodename) = \
                ClusterInstanceConfig.setReplConninfo(self.instInfo,
                                                      peerInsts, clusterInfo)
            connInfo = "replconninfo1"
            tmpDict1 = {}
            tmpDict1[connInfo] = "'%s'" % connInfo1
            self.setGucConfig(tmpDict1)

        if (dummyStandbyInst is not None):
            tmpDict2 = {}
            tmpDict2["replconninfo2"] = "'%s'" % connInfo2
            self.setGucConfig(tmpDict2)
    def getDnPeerInstance(self, user):
        """  
        function :  Get the Peer instance of DN
        input : user
        output : Idlist
        """
        global g_clusterInfo
        global g_instanceInfo
        global g_clusterInfoInitialized
        if not g_clusterInfoInitialized:
            DefaultValue.checkUser(user)
            g_clusterInfo = dbClusterInfo()
            g_clusterInfo.initFromStaticConfig(user)
            g_clusterInfoInitialized = True
        dbNode = g_clusterInfo.getDbNodeByName(self.name)
        Idlist = {}
        for dnInst in self.datanodes:
            # get the instance info
            g_instanceInfo = None
            for instInfo in dbNode.datanodes:
                if instInfo.instanceId == dnInst.instanceId:
                    g_instanceInfo = instInfo
                    break
            if not g_instanceInfo:
                raise Exception(ErrorCode.GAUSS_516["GAUSS_51620"] % "DN")

            # construct the instance name
            peerInsts = g_clusterInfo.getPeerInstance(g_instanceInfo)
            if (len(peerInsts) != 2 and len(peerInsts) != 1):
                raise Exception(ErrorCode.GAUSS_516["GAUSS_51603"] %
                                g_instanceInfo.datadir)

            dnMasterInst = None
            dnStandbyInst = None
            if (g_instanceInfo.instanceType == MASTER_INSTANCE):
                dnMasterInst = g_instanceInfo
                for instIndex in range(len(peerInsts)):
                    if (peerInsts[instIndex].instanceType == STANDBY_INSTANCE):
                        dnStandbyInst = peerInsts[instIndex]
                        Idlist[dnMasterInst.instanceId] = \
                            dnStandbyInst.instanceId
        return Idlist
Example #18
0
 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))
Example #19
0
    def doRebuildConf(self):
        """
        generating static configuration files for all nodes
        input:NA
        output:NA
        """
        try:
            self.logger.log(
                "Generating static configuration files for all nodes.")
            # Initialize the cluster information according to the XML file
            self.context.clusterInfo = dbClusterInfo()
            self.context.clusterInfo.initFromXml(self.context.g_opts.confFile)

            # 1.create a tmp dir
            self.logger.log(
                "Creating temp directory to store static configuration files.")
            dirName = os.path.dirname(os.path.realpath(__file__))
            tmpDirName = os.path.realpath("%s/../../static_config_files" %
                                          dirName)
            cmd = "mkdir -p -m %s '%s'" % (DefaultValue.KEY_DIRECTORY_MODE,
                                           tmpDirName)
            (status, output) = subprocess.getstatusoutput(cmd)
            if (status != 0):
                raise Exception(ErrorCode.GAUSS_502["GAUSS_50208"] %
                                "temporary directory" +
                                "\nCommand:%s\nError: %s" % (cmd, output))
            self.logger.log("Successfully created the temp directory.")

            # create static files
            self.logger.log("Generating static configuration files.")
            for dbNode in self.context.clusterInfo.dbNodes:
                staticConfigPath = "%s/cluster_static_config_%s" % (
                    tmpDirName, dbNode.name)
                self.context.clusterInfo.saveToStaticConfig(
                    staticConfigPath, dbNode.id)
            self.logger.log(
                "Successfully generated static configuration files.")
            self.logger.log(
                "Static configuration files for all nodes are saved in %s." %
                tmpDirName)

            # check if need send static config files
            if not self.context.g_opts.distribute:
                self.logger.debug(
                    "No need to distribute static configuration files "
                    "to installation directory.")
                return

            # distribute static config file
            self.logger.log(
                "Distributing static configuration files to all nodes.")
            for dbNode in self.context.clusterInfo.dbNodes:
                if (dbNode.name != DefaultValue.GetHostIpOrName()):
                    cmd = \
                        "pscp -H %s '%s'/cluster_static_config_%s '%s'" \
                        "/bin/cluster_static_config" % (
                            dbNode.name, tmpDirName,
                            dbNode.name, self.context.clusterInfo.appPath)
                else:
                    cmd = \
                        "cp '%s'/cluster_static_config_%s '%s'" \
                        "/bin/cluster_static_config" % (
                            tmpDirName,
                            dbNode.name, self.context.clusterInfo.appPath)
                (status, output) = subprocess.getstatusoutput(cmd)
                if (status != 0):
                    raise Exception(ErrorCode.GAUSS_502["GAUSS_50216"] %
                                    "static configuration file" +
                                    "Node: %s.\nCommand: \n%s\nError: \n%s" %
                                    (dbNode.name, cmd, output))
            self.logger.log(
                "Successfully distributed static configuration files.")

        except Exception as e:
            g_file.removeDirectory(tmpDirName)
            raise Exception(str(e))
    def outputNodeStatus(self, stdout, user, showDetail=False):
        """  
        function :  output the status of node
        input : stdout, user
        output : NA
        """
        global g_clusterInfo
        global g_instanceInfo
        global g_clusterInfoInitialized
        if not g_clusterInfoInitialized:
            DefaultValue.checkUser(user)
            g_clusterInfo = dbClusterInfo()
            g_clusterInfo.initFromStaticConfig(user)
            g_clusterInfoInitialized = True
        dbNode = g_clusterInfo.getDbNodeByName(self.name)
        instName = ""
        # print node information
        print("%-20s: %s" % ("node", str(self.id)), file=stdout)
        print("%-20s: %s" % ("node_name", self.name), file=stdout)
        if (self.isNodeHealthy()):
            print("%-20s: %s\n" %
                  ("node_state", DbClusterStatus.OM_NODE_STATUS_NORMAL),
                  file=stdout)
        else:
            print("%-20s: %s\n" %
                  ("node_state", DbClusterStatus.OM_NODE_STATUS_ABNORMAL),
                  file=stdout)

        if (not showDetail):
            return

        # coordinator status
        for inst in self.coordinators:
            # get the instance info
            g_instanceInfo = None
            for instInfo in dbNode.coordinators:
                if instInfo.instanceId == inst.instanceId:
                    g_instanceInfo = instInfo
                    break
            if not g_instanceInfo:
                raise Exception(ErrorCode.GAUSS_516["GAUSS_51620"] % "CN")
            # construct the instance name
            instName = "cn_%s" % g_instanceInfo.instanceId
            # print CN instance information
            print("Coordinator", file=stdout)
            print("%-20s: %d" % ("    node", inst.nodeId), file=stdout)
            print("%-20s: %s" % ("    instance_name", instName), file=stdout)
            print("%-20s: %s" % ("    listen_IP", g_instanceInfo.listenIps),
                  file=stdout)
            print("%-20s: %d" % ("    port", g_instanceInfo.port), file=stdout)
            print("%-20s: %s" % ("    data_path", inst.datadir), file=stdout)
            print("%-20s: %s" % ("    instance_state", inst.status),
                  file=stdout)
            print("", file=stdout)

        for inst in self.gtms:
            # get the instance info
            g_instanceInfo = None
            for instInfo in dbNode.gtms:
                if instInfo.instanceId == inst.instanceId:
                    g_instanceInfo = instInfo
                    break
            if not g_instanceInfo:
                raise Exception(ErrorCode.GAUSS_516["GAUSS_51620"] % "GTM")
            # construct the instance name
            instName = "gtm_%s" % g_instanceInfo.instanceId
            # print gtm instance information
            print("GTM", file=stdout)
            print("%-20s: %d" % ("    node", inst.nodeId), file=stdout)
            print("%-20s: %s" % ("    instance_name", instName), file=stdout)
            print("%-20s: %s" % ("    listen_IP", g_instanceInfo.listenIps),
                  file=stdout)
            print("%-20s: %d" % ("    port", g_instanceInfo.port), file=stdout)
            print("%-20s: %s" % ("    data_path", inst.datadir), file=stdout)
            print("%-20s: %s" % ("    instance_state", inst.status),
                  file=stdout)
            print("%-20s: %s" % ("    conn_state", inst.connStatus),
                  file=stdout)
            print("%-20s: %s" % ("    reason", inst.reason), file=stdout)
            print("", file=stdout)

        i = 1
        for inst in self.datanodes:
            # get the instance info
            g_instanceInfo = None
            for instInfo in dbNode.datanodes:
                if instInfo.instanceId == inst.instanceId:
                    g_instanceInfo = instInfo
                    break
            if not g_instanceInfo:
                raise Exception(ErrorCode.GAUSS_516["GAUSS_51620"] % "DN")
            # construct the instance name
            peerInsts = g_clusterInfo.getPeerInstance(g_instanceInfo)
            instName = \
                ClusterInstanceConfig. \
                    setReplConninfoForSinglePrimaryMultiStandbyCluster(
                    g_instanceInfo, peerInsts, g_clusterInfo)[1]

            # print DB instance information
            print("Datanode%d" % i, file=stdout)
            print("%-20s: %d" % ("    node", inst.nodeId), file=stdout)
            print("%-20s: %s" % ("    instance_name", instName), file=stdout)
            print("%-20s: %s" % ("    listen_IP", g_instanceInfo.listenIps),
                  file=stdout)
            print("%-20s: %s" % ("    HA_IP", g_instanceInfo.haIps),
                  file=stdout)
            print("%-20s: %d" % ("    port", g_instanceInfo.port), file=stdout)
            print("%-20s: %s" % ("    data_path", inst.datadir), file=stdout)
            print("%-20s: %s" % ("    instance_state", inst.status),
                  file=stdout)
            print("%-20s: %s" % ("    HA_state", inst.haStatus), file=stdout)
            print("%-20s: %s" % ("    reason", inst.reason), file=stdout)
            print("", file=stdout)

            i += 1
        # print fenced UDF status
        for inst in self.fencedUDFs:
            print("Fenced UDF", file=stdout)
            print("%-20s: %d" % ("    node", inst.nodeId), file=stdout)
            print("%-20s: %s" % ("    listen_IP", dbNode.backIps[0]),
                  file=stdout)
            print("%-20s: %s" % ("    instance_state", inst.status),
                  file=stdout)
Example #21
0
    def doReplaceSSLCert(self):
        """
        function: replace ssl cert files
        input: NA
        output: NA
        """
        try:
            # Initialize the cluster information according to the xml file
            self.context.clusterInfo = dbClusterInfo()
            self.context.clusterInfo.initFromStaticConfig(
                g_OSlib.getPathOwner(self.context.g_opts.certFile)[0])
            self.sshTool = SshTool(
                self.context.clusterInfo.getClusterNodeNames(),
                self.logger.logFile)
        except Exception as e:
            raise Exception(str(e))

        try:
            self.logger.log("Starting ssl cert files replace.", "addStep")
            tempDir = os.path.join(DefaultValue.getTmpDirFromEnv(),
                                   "tempCertDir")

            # unzip files to temp directory
            if (os.path.exists(tempDir)):
                g_file.removeDirectory(tempDir)
            g_file.createDirectory(tempDir, True,
                                   DefaultValue.KEY_DIRECTORY_MODE)
            g_file.decompressZipFiles(self.context.g_opts.certFile, tempDir)

            realCertList = DefaultValue.CERT_FILES_LIST
            clientCertList = DefaultValue.CLIENT_CERT_LIST
            # check file exists
            for clientCert in clientCertList:
                sslFile = os.path.join(tempDir, clientCert)
                if (not os.path.isfile(sslFile)):
                    raise Exception(
                        (ErrorCode.GAUSS_502["GAUSS_50201"] % sslFile) + \
                        "Missing SSL client cert file in ZIP file.")

            certList = []
            dnDict = self.getDnNodeDict()
            for cert in realCertList:
                sslFile = os.path.join(tempDir, cert)

                if (not os.path.isfile(sslFile)
                        and cert != DefaultValue.SSL_CRL_FILE):
                    raise Exception(
                        (ErrorCode.GAUSS_502["GAUSS_50201"] % sslFile) + \
                        "Missing SSL server cert file in ZIP file.")
                if (os.path.isfile(sslFile)):
                    certList.append(cert)

            # distribute cert files to datanodes
            self.doDNBackup()
            self.distributeDNCert(certList, dnDict)

            # clear temp directory
            g_file.removeDirectory(tempDir)
            if (not self.context.g_opts.localMode):
                self.logger.log(
                    "Successfully distributed cert files on all nodes.")
        except Exception as e:
            g_file.removeDirectory(tempDir)
            raise Exception(str(e))
Example #22
0
    def doDNSSLCertRollback(self):
        """
        function: rollback SSL cert file in DN instance directory
        input:  NA
        output: NA
        """
        self.context.clusterInfo = dbClusterInfo()
        self.context.clusterInfo.initFromStaticConfig(
            pwd.getpwuid(os.getuid()).pw_name)
        self.sshTool = SshTool(self.context.clusterInfo.getClusterNodeNames(),
                               self.logger.logFile)
        backupList = DefaultValue.CERT_FILES_LIST[:]

        allDnNodeDict = self.getDnNodeDict()
        noBackupList = []

        temp = "tempDir"
        if self.context.g_opts.localMode:
            if ((DefaultValue.GetHostIpOrName() in allDnNodeDict.keys())
                    and os.path.isfile(
                        os.path.join(
                            allDnNodeDict[DefaultValue.GetHostIpOrName()],
                            DefaultValue.CERT_BACKUP_FILE))):

                localDnDir = allDnNodeDict[DefaultValue.GetHostIpOrName()]
                tempDir = os.path.join(localDnDir, temp)
                if (os.path.exists(tempDir)):
                    g_file.removeDirectory(tempDir)
                os.mkdir(tempDir, DefaultValue.KEY_DIRECTORY_PERMISSION)

                for certFile in backupList:
                    realCertFile = os.path.join(localDnDir, certFile)
                    if (os.path.exists(realCertFile)):
                        g_file.moveFile(realCertFile, tempDir)

                cmd = "cd '%s' && if [ -f '%s' ];then tar -zxvf %s;fi" % \
                      (localDnDir, DefaultValue.CERT_BACKUP_FILE,
                       DefaultValue.CERT_BACKUP_FILE)
                (status, output) = subprocess.getstatusoutput(cmd)
                if (status != 0):
                    cmd = "cp '%s'/* '%s' && rm -rf '%s'" % (
                        tempDir, localDnDir, tempDir)
                    (status, output) = subprocess.getstatusoutput(cmd)
                    raise Exception((ErrorCode.GAUSS_514["GAUSS_51400"] %
                                     cmd) +
                                    "Failed uncompression SSL backup file." +
                                    "Error: \n%s" % output)

                # remove temp directory
                if (os.path.exists(tempDir)):
                    g_file.removeDirectory(tempDir)

                # set guc option
                if (os.path.isfile(
                        os.path.join(localDnDir, DefaultValue.SSL_CRL_FILE))):
                    cmd = \
                        "gs_guc set -D %s " \
                        "-c \"ssl_crl_file=\'%s\'\"" \
                        % (localDnDir, DefaultValue.SSL_CRL_FILE)
                else:
                    cmd = \
                        "gs_guc set -D %s " \
                        "-c \"ssl_crl_file=\'\'\"" % localDnDir
                (status, output) = subprocess.getstatusoutput(cmd)
                if (status != 0):
                    raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] % cmd +
                                    "Error: \n%s" % output)

                if (os.path.isfile(os.path.join(localDnDir, EMPTY_CERT))):
                    os.remove(os.path.join(localDnDir, EMPTY_CERT))

                self.logger.log(
                    "Successfully rollback SSL cert files with local mode.")
                return
            else:
                self.logger.log("There is not exists backup files.")
                return
                # 1.check backup file "gsql_cert_backup.tar.gz" on all dbnodes.
        for node in allDnNodeDict.keys():
            backupGzFile = os.path.join(allDnNodeDict[node],
                                        DefaultValue.CERT_BACKUP_FILE)
            status = self.sshTool.checkRemoteFileExist(
                node, backupGzFile, self.context.g_opts.mpprcFile)
            if not status:
                noBackupList.append(node)
        if (len(noBackupList) > 0):
            raise Exception((ErrorCode.GAUSS_502["GAUSS_50201"] %
                             DefaultValue.CERT_BACKUP_FILE) +
                            "Can't rollback SSL cert files on %s." %
                            noBackupList)

        # 2.perform rollback on all dbnodes.
        for node in allDnNodeDict.keys():
            backupGzFile = os.path.join(allDnNodeDict[node],
                                        DefaultValue.CERT_BACKUP_FILE)
            # 2-1.move SSL cert files in dn directory to temp directory.
            sshcmd = "cd '%s' && if [ -d '%s' ];then rm -rf '%s'" \
                     " && mkdir '%s';else mkdir '%s';fi" % \
                     (allDnNodeDict[node], temp, temp, temp, temp)
            self.sshTool.executeCommand(sshcmd, "Make temp directory.",
                                        DefaultValue.SUCCESS, \
                                        [node], self.context.g_opts.mpprcFile)
            for certFile in backupList:
                realCertFile = os.path.join(allDnNodeDict[node], certFile)
                sshcmd = " %s && " % g_Platform.getCdCmd(
                    os.path.join(allDnNodeDict[node], temp))
                sshcmd += g_file.SHELL_CMD_DICT["renameFile"] % (
                    realCertFile, realCertFile, "./")
                self.sshTool.executeCommand(
                    sshcmd, "Backup cert files to temp directory.",
                    DefaultValue.SUCCESS, [node],
                    self.context.g_opts.mpprcFile)

            # 2-2.uncompression "gsql_cert_backup.tar.gz" file
            sshcmd = "cd '%s' && if [ -f '%s' ];then tar -zxvf %s;fi" % \
                     (allDnNodeDict[node], DefaultValue.CERT_BACKUP_FILE,
                      DefaultValue.CERT_BACKUP_FILE)
            self.sshTool.executeCommand(sshcmd, "Unzip backup file.",
                                        DefaultValue.SUCCESS, [node],
                                        self.context.g_opts.mpprcFile)

            # 2-3.clear temp directory
            sshcmd = " %s && " % g_Platform.getCdCmd(allDnNodeDict[node])
            sshcmd += g_file.SHELL_CMD_DICT["deleteDir"] % (temp, temp)
            self.sshTool.executeCommand(sshcmd, "Clear backup cert files.",
                                        DefaultValue.SUCCESS, [node],
                                        self.context.g_opts.mpprcFile)

            # 2-4.is have "sslcrl-file.crl",config 'ssl_crl_file' option
            status = self.sshTool.checkRemoteFileExist(
                node,
                os.path.join(allDnNodeDict[node], DefaultValue.SSL_CRL_FILE),
                self.context.g_opts.mpprcFile)
            # exists 'sslcrl-file.crl' file ,config option of 'postgresql.conf'
            if (status):
                if node == DefaultValue.GetHostIpOrName():
                    sshcmd = \
                        "gs_guc set -D %s " \
                        "-c \"ssl_crl_file='%s'\"" \
                        % (allDnNodeDict[node], DefaultValue.SSL_CRL_FILE)
                else:
                    sshcmd = "gs_guc set -D %s " \
                             "-c \"ssl_crl_file=\\\\\\'%s\\\\\\'\"" \
                             % (allDnNodeDict[node], DefaultValue.SSL_CRL_FILE)
                self.sshTool.executeCommand(sshcmd, "Exist 'ssl_crl_file'",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)
            else:
                if (node == DefaultValue.GetHostIpOrName()):
                    sshcmd = "gs_guc set " \
                             "-D %s -c \"ssl_crl_file=''\"" % (
                                 allDnNodeDict[node])
                else:
                    sshcmd = "gs_guc set " \
                             "-D %s -c \"ssl_crl_file=\\\\\\'\\\\\\'\"" \
                             % (allDnNodeDict[node])
                self.sshTool.executeCommand(sshcmd, "No exist 'ssl_crl_file'",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)

            # Clear empty file.
            if (self.isDnEmpty(node)):
                sshcmd = g_file.SHELL_CMD_DICT["deleteFile"] % (os.path.join(
                    allDnNodeDict[node],
                    EMPTY_CERT), os.path.join(allDnNodeDict[node], EMPTY_CERT))
                self.sshTool.executeCommand(sshcmd, "Clear empty file.",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)
            self.logger.log("Successfully rollback SSL cert files on [%s]." %
                            node)
    def getClusterStatusMap(self, user):
        """
        Get the cluster status information required for reporting.
        """
        repClusterSts = ReportClusterStatus()
        repClusterSts.status = self.clusterStatusDetail
        repClusterSts.balanced = self.balanced
        repClusterSts.redistributing = self.redistributing
        clusterInfo = dbClusterInfo()
        clusterInfo.initFromStaticConfig(user)
        masterInstList = []
        for dbNodeInfo in clusterInfo.dbNodes:
            insts = dbNodeInfo.etcds + dbNodeInfo.cmservers + \
                    dbNodeInfo.datanodes + dbNodeInfo.gtms
            for inst in insts:
                if inst.instanceType == 0:
                    masterInstList.append(inst.instanceId)
        dnMirrorDict = {}
        etcdStatus = EtcdGroupStatus()
        gtmStatus = NodeGroupStatus()
        cmsStatus = NodeGroupStatus()
        for dbNode in self.dbNodes:
            # get cn instance status info
            for inst in dbNode.coordinators:
                cnRepSts = self.getReportInstanceStatus(inst)
                repClusterSts.cns.append(cnRepSts)
            # get etcds instance status info
            for inst in dbNode.etcds:
                if (inst.instanceId in masterInstList):
                    etcdStatus.leader = self.getReportInstanceStatus(inst)
                else:
                    etcdInstStatus = self.getReportInstanceStatus(inst)
                    etcdStatus.follower.append(etcdInstStatus)
            # get gtm instance status info
            for inst in dbNode.gtms:
                if (inst.instanceId in masterInstList):
                    gtmStatus.primary = self.getReportInstanceStatus(inst)
                else:
                    gtmInstStatus = self.getReportInstanceStatus(inst)
                    gtmStatus.standby.append(gtmInstStatus)
            # get cms instance status info
            for inst in dbNode.cmservers:
                if (inst.instanceId in masterInstList):
                    cmsStatus.primary = self.getReportInstanceStatus(inst)
                else:
                    cmsInstStatus = self.getReportInstanceStatus(inst)

                    cmsStatus.standby.append(cmsInstStatus)

            for dnInst in dbNode.datanodes:
                dnNode = clusterInfo.getDbNodeByID(dnInst.nodeId)
                clusterDnInst = None
                for dnInstInfo in dnNode.datanodes:
                    if (dnInst.instanceId == dnInstInfo.instanceId):
                        clusterDnInst = dnInstInfo
                if clusterDnInst.mirrorId not in dnMirrorDict.keys():
                    dnMirrorDict[clusterDnInst.mirrorId] = [dnInst]
                else:
                    dnMirrorDict[clusterDnInst.mirrorId].append(dnInst)
        # get datanodes instance status info
        for mirrorDNs in dnMirrorDict.keys():
            dnStatus = NodeGroupStatus()
            for dnInst in dnMirrorDict[mirrorDNs]:
                if dnInst.instanceId in masterInstList:
                    primaryDnSts = self.getDnInstanceStatus(dnInst)
                    dnStatus.primary = \
                        self.getReportInstanceStatus(primaryDnSts)
                else:
                    peerInstSts = self.getDnInstanceStatus(dnInst)
                    peerInstStatus = self.getReportInstanceStatus(peerInstSts)
                    dnStatus.standby.append(peerInstStatus)
            repClusterSts.dns.append(dnStatus.__dict__)

        repClusterSts.etcds = etcdStatus.__dict__
        repClusterSts.gtms = gtmStatus.__dict__
        repClusterSts.cms = cmsStatus.__dict__

        return repClusterSts.__dict__