Example #1
0
    def __saveUpgradeVerionInfo(self):
        """
        function: save upgrade version info
        input: NA
        output: NA
        """
        if self.dws_mode:
            versionCfgFile = "%s/version.cfg" % DefaultValue.DWS_PACKAGE_PATH
            upgradeVersionFile = "%s/bin/upgrade_version" % self.installPath
        else:
            dirName = os.path.dirname(os.path.realpath(__file__))
            versionCfgFile = "%s/../../version.cfg" % dirName
            upgradeVersionFile = "%s/bin/upgrade_version" % self.installPath

        if not os.path.exists(versionCfgFile):
            self.logger.logExit(ErrorCode.GAUSS_502["GAUSS_50201"]
                                % versionCfgFile)
        if not os.path.isfile(versionCfgFile):
            self.logger.logExit(ErrorCode.GAUSS_502["GAUSS_50210"]
                                % versionCfgFile)

        try:
            # read version info from version.cfg file
            (newClusterVersion, newClusterNumber, commitId) = \
                VersionInfo.get_version_info(versionCfgFile)
            # save version info to upgrade_version file
            if os.path.isfile(upgradeVersionFile):
                os.remove(upgradeVersionFile)

            g_file.createFile(upgradeVersionFile)
            g_file.writeFile(upgradeVersionFile,
                             [newClusterVersion, newClusterNumber, commitId])
            g_file.changeMode(DefaultValue.KEY_FILE_MODE, upgradeVersionFile)
        except Exception as e:
            self.logger.logExit(str(e))
Example #2
0
    def __fixInstallPathPermission(self):
        """
        function: fix the whole install path's permission
        input : NA
        output: NA
        """
        installPathFileTypeDict = {}
        try:
            # get files type
            installPathFileTypeDict = g_file.getFilesType(self.installPath)
        except Exception as e:
            self.logger.logExit(str(e))

        for key in installPathFileTypeDict:
            if not os.path.exists(key):
                self.logger.debug("[%s] does not exist. Please skip it."
                                  % key)
                continue
            if os.path.islink(key):
                self.logger.debug("[%s] is a link file. Please skip it."
                                  % key)
                continue
            # skip DbClusterInfo.pyc
            if os.path.basename(key) == "DbClusterInfo.pyc":
                continue
            if (installPathFileTypeDict[key].find("executable") >= 0 or
                    installPathFileTypeDict[key].find("ELF") >= 0 or
                    installPathFileTypeDict[key].find("directory") >= 0):
                g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, key, True)
            else:
                g_file.changeMode(DefaultValue.KEY_FILE_MODE, key)
    def writeOperateStep(self, stepName, nodes=None):
        """
        function: write operate step signal 
        input : step
        output: NA
        """
        if nodes is None:
            nodes = []
        try:
            # write the step into INSTALL_STEP
            # open the INSTALL_STEP
            with open(self.operateStepFile, "w") as g_DB:
                # write the INSTALL_STEP
                g_DB.write(stepName)
                g_DB.write(os.linesep)
                g_DB.flush()
            # change the INSTALL_STEP permissions
            g_file.changeMode(DefaultValue.KEY_FILE_MODE, self.operateStepFile)

            # distribute file to all nodes
            cmd = "mkdir -p -m %s '%s'" % (DefaultValue.KEY_DIRECTORY_MODE,
                                           self.operateStepDir)
            DefaultValue.execCommandWithMode(
                cmd, "create backup directory "
                "on all nodes", self.sshTool, self.localMode or self.isSingle,
                "", nodes)

            if not self.localMode and not self.isSingle:
                self.sshTool.scpFiles(self.operateStepFile,
                                      self.operateStepDir, nodes)
        except Exception as e:
            # failed to write the step into INSTALL_STEP
            raise Exception(str(e))
Example #4
0
 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)
Example #5
0
    def __writeHostFiles(self):
        """
        function: Write all hostname to a file
        input : NA
        output: NA
        """
        try:
            g_file.createFileInSafeMode(self.__hostsFile)
            with open(self.__hostsFile, "w") as fp:
                for host in self.hostNames:
                    fp.write("%s\n" % host)
                fp.flush()
            subprocess.getstatusoutput(
                "chmod %s '%s'" % (DefaultValue.FILE_MODE, self.__hostsFile))
        except Exception as e:
            g_file.removeFile(self.__hostsFile)
            raise Exception(ErrorCode.GAUSS_502["GAUSS_50205"] % "host file" +
                            " Error: \n%s" % str(e))

        # change the mode
        # if it created by root user,and permission is 640, then
        # install user will have no permission to read it, so we should set
        # its permission 644.
        g_file.changeMode(DefaultValue.KEY_HOSTS_FILE, self.__hostsFile, False,
                          "python")
Example #6
0
    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.")
Example #7
0
 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 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.")
Example #9
0
 def __makeDirForDBUser(self, path, desc):
     """
     function: Create a dir for DBUser:
               1.create a dir for DB user
               2.Check if target directory is writeable for user
     input : path, desc
     output: NA
     """
     self.logger.debug("Making %s directory[%s] for database node user." %
                       (desc, path))
     g_file.createDirectory(path)
     g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, path)
     if (not g_file.checkDirWriteable(path)):
         self.logger.logExit(ErrorCode.GAUSS_501["GAUSS_50102"] %
                             (path, self.user))
 def checkFilePermission(self, filename):
     """
     Function : check file: 1.exist 2. isfile 3. permission
     Note     : 1.You must check that the file exist and is a file.
                2.You can choose whether to check the file's
                 permission:executable.
     """
     # Check if the file exists
     if (not os.path.exists(filename)):
         raise Exception("The file %s does not exist." % filename)
     # Check whether the file
     if (not os.path.isfile(filename)):
         raise Exception("%s is not file." % filename)
     # Check the file permissions
     # Modify the file permissions
     if (not os.access(filename, os.X_OK)):
         g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, filename)
Example #11
0
 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)
Example #12
0
    def __checkPgsqlDir(self):
        """
        function: 1.Check pgsql directory
                  2.change permission
                  3.Check if target directory is writeable for user
        input : NA
        output: NA
        """
        tmpDir = DefaultValue.getTmpDirFromEnv()
        self.logger.log("Checking directory [%s]." % tmpDir)
        if (not os.path.exists(tmpDir)):
            self.logger.logExit(ErrorCode.GAUSS_502["GAUSS_50201"] % tmpDir +
                                " Please create it first.")

        self.__pgsqlFiles = os.listdir(tmpDir)

        g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, tmpDir)
        if (not g_file.checkDirWriteable(tmpDir)):
            self.logger.logExit(ErrorCode.GAUSS_501["GAUSS_50102"] %
                                (tmpDir, self.user))
Example #13
0
    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.")
Example #14
0
    def distributeDNCert(self, certList, dnDict=None):
        """
        function: distribute ssl cert files on single_inst cluster
        input: certList:     cert files list
               dnDict:       dictionary
        output: NA
        """
        tempDir = "tempCertDir"
        gphost = DefaultValue.getTmpDirFromEnv()
        if dnDict is None:
            dnDict = {}
        dnName = dnDict.keys()
        certPathList = []
        self.logger.debug(certList)

        for num in iter(certList):
            sslPath = os.path.join(os.path.join(gphost, tempDir), num)
            certPathList.append(sslPath)
        # local mode
        if self.context.g_opts.localMode:
            localDnDir = dnDict[DefaultValue.GetHostIpOrName()]
            for num in range(len(certList)):
                # distribute gsql SSL cert
                if (os.path.isfile(os.path.join(localDnDir, certList[num]))):
                    os.remove(os.path.join(localDnDir, certList[num]))
                if (os.path.isfile(certPathList[num])):
                    g_file.cpFile(certPathList[num],
                                  os.path.join(localDnDir, certList[num]))
                    g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                                      os.path.join(localDnDir, certList[num]))

                    # remove 'sslcrl-file.crl' file
            if (DefaultValue.SSL_CRL_FILE not in certList and os.path.isfile(
                    os.path.join(localDnDir, DefaultValue.SSL_CRL_FILE))):
                os.remove(os.path.join(localDnDir, DefaultValue.SSL_CRL_FILE))

                # config 'sslcrl-file.crl' option in 'postgresql.conf'
            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)
                (status, output) = subprocess.getstatusoutput(cmd)
                if (status != 0):
                    raise Exception((ErrorCode.GAUSS_514["GAUSS_51400"] %
                                     cmd) +
                                    "Failed set 'ssl_crl_file' option." +
                                    "Error: \n%s" % output)
            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) +
                                    "Failed set 'ssl_crl_file' option." +
                                    "Error: \n%s" % output)
                    # remove backup flag file 'certFlag'
            if (os.path.isfile(os.path.join(localDnDir, 'certFlag'))):
                os.remove(os.path.join(localDnDir, 'certFlag'))
            self.logger.log(
                "Replace SSL cert files with local mode successfully.")
            return
        # not local mode
        for node in dnName:
            for num in range(len(certList)):
                sshcmd = g_file.SHELL_CMD_DICT["deleteFile"] % (os.path.join(
                    dnDict[node],
                    certList[num]), os.path.join(dnDict[node], certList[num]))
                self.sshTool.executeCommand(sshcmd,
                                            "Delete read only cert file.",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)

                if (os.path.exists(certPathList[num])):
                    self.sshTool.scpFiles(certPathList[num], dnDict[node],
                                          [node])

                # change permission of cert file 600,
                # there no need to is exists file,
                # because the files must be exist.
                sshcmd = g_file.SHELL_CMD_DICT["changeMode"] % (
                    DefaultValue.KEY_FILE_MODE,
                    os.path.join(dnDict[node], certList[num]))
                self.sshTool.executeCommand(sshcmd,
                                            "Change file permisstion.'",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)

            if (DefaultValue.SSL_CRL_FILE in certList):
                if (node == DefaultValue.GetHostIpOrName()):
                    sshcmd = "gs_guc set " \
                             "-D %s -c \"ssl_crl_file='%s'\"" \
                             % (dnDict[node], DefaultValue.SSL_CRL_FILE)
                else:
                    sshcmd = "gs_guc set " \
                             " -D %s -c \"ssl_crl_file=\\\\\\'%s\\\\\\'\"" \
                             % (dnDict[node], DefaultValue.SSL_CRL_FILE)
                self.sshTool.executeCommand(sshcmd, "Find 'ssl_crl_file'",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)
            else:
                # no ssl cert file there will delete old cert file,
                # and config option ssl_crl_file = ''
                sshcmd = g_file.SHELL_CMD_DICT["deleteFile"] % (
                    os.path.join(dnDict[node], DefaultValue.SSL_CRL_FILE),
                    os.path.join(dnDict[node], DefaultValue.SSL_CRL_FILE))
                self.sshTool.executeCommand(sshcmd, "Find 'ssl_crl_file'",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)
                if (node == DefaultValue.GetHostIpOrName()):
                    sshcmd = "gs_guc set " \
                             "-D %s -c \"ssl_crl_file=\'\'\"" % (dnDict[node])
                else:
                    sshcmd = \
                        "gs_guc set " \
                        "-D %s " \
                        "-c \"ssl_crl_file=\\\\\\'\\\\\\'\"" % (dnDict[node])
                self.sshTool.executeCommand(sshcmd, "Find 'ssl_crl_file'",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)
                # remove file 'sslcrl-file.crl'
                sshcmd = g_file.SHELL_CMD_DICT["deleteFile"] % (
                    os.path.join(dnDict[node], DefaultValue.SSL_CRL_FILE),
                    os.path.join(dnDict[node], DefaultValue.SSL_CRL_FILE))
                self.sshTool.executeCommand(sshcmd,
                                            "Delete read only cert file.",
                                            DefaultValue.SUCCESS, [node],
                                            self.context.g_opts.mpprcFile)
            # remove backup flag file 'certFlag'
            sshcmd = g_file.SHELL_CMD_DICT["deleteFile"] % (os.path.join(
                dnDict[node],
                "certFlag"), os.path.join(dnDict[node], "certFlag"))
            self.sshTool.executeCommand(sshcmd, "Delete backup flag file.",
                                        DefaultValue.SUCCESS, [node],
                                        self.context.g_opts.mpprcFile)
            self.logger.log("%s replace SSL cert files successfully." % node)
def checkLimitsParameter(limitPara, isSet):
    """
    function: check and set the limit parameter
    input: limitPara, isSet
    output: NA
    """

    # utility class for this function only
    class limitsconf_data:
        """
        Class: limitsconf_data
        """

        def __init__(self, expected):
            """
            function: constructor
            """
            self.domain = None
            self.value_found = None
            self.value_expected = expected

    # check the limit parameter
    table = dict()

    for key in list(limitPara.keys()):
        cmd = "ulimit -a |  grep -F '%s' 2>/dev/null" % key
        (status, output) = subprocess.getstatusoutput(cmd)
        if (status == 0):
            resLines = output.split('\n')
            resList = resLines[0].split(' ')
            limitValue = resList[-1].strip()
            if (limitPara[key] == 'unlimited'):
                resultList.append(2)
                if limitValue != 'unlimited':
                    g_logger.log("        Warning reason: variable '%s'"
                                 " RealValue '%s' ExpectedValue '%s'"
                                 % (key, limitValue, limitPara[key]))
                if (key == 'virtual memory'):
                    table[('soft', 'as')] = limitsconf_data(limitPara[key])
                    table[('hard', 'as')] = limitsconf_data(limitPara[key])
                if (key == 'max user processes'):
                    table[('soft', 'nproc')] = limitsconf_data(limitPara[key])
                    table[('hard', 'nproc')] = limitsconf_data(limitPara[key])

            elif (limitPara[key] != 'unlimited'):
                if (limitValue == 'unlimited'):
                    continue
                if (int(limitValue) < int(limitPara[key])):
                    if (key == "stack size"):
                        resultList.append(1)
                        g_logger.log("        Abnormal reason: variable '%s'"
                                     " RealValue '%s' ExpectedValue '%s'"
                                     % (key, limitValue, limitPara[key]))
                    else:
                        resultList.append(2)
                        g_logger.log("        Warning reason: variable '%s'"
                                     " RealValue '%s' ExpectedValue '%s'"
                                     % (key, limitValue, limitPara[key]))
                    if (key == 'stack size'):
                        table[('soft',
                               'stack')] = limitsconf_data(limitPara[key])
                        table[('hard',
                               'stack')] = limitsconf_data(limitPara[key])
                if (key == 'open files'):
                    table[('soft',
                           'nofile')] = limitsconf_data(limitPara[key])
                    table[('hard',
                           'nofile')] = limitsconf_data(limitPara[key])
        else:
            resultList.append(1)
            g_logger.debug("The cmd is %s " % cmd)
            g_logger.log("        Failed to obtain '%s'. Error: \n%s"
                         % (key, output))

    # set the open file numbers
    if isSet and len(list(table.keys())):
        for key in list(table.keys()):
            if (key[1] == "nofile" or key[1] == "nproc"):
                limitPath = '/etc/security/limits.d/'
                nofiles = glob.glob("/etc/security/limits.d/*.conf")
                for conf in nofiles:
                    g_file.changeMode(DefaultValue.HOSTS_FILE, conf)
                    SetLimitsConf(key[0], key[1],
                                  table[key].value_expected, conf)
                if os.path.isfile(os.path.join(limitPath, '91-nofile.conf')):
                    limitFile = '91-nofile.conf'
                else:
                    limitFile = '90-nofile.conf'
            if (key[1] == "stack" or key[1] == "as" or key[1] == "nproc"):
                limitPath = '/etc/security/'
                limitFile = 'limits.conf'
            if (checkLimitFile(limitPath, limitFile) != 0):
                return

            SetLimitsConf(key[0], key[1], table[key].value_expected,
                          limitPath + limitFile)
            g_logger.log("        Set variable '%s %s' to '%s'"
                         % (key[0], key[1], table[key].value_expected))
Example #16
0
    def __fixFilePermission(self):
        """
        function: modify permission for app path
        input: NA
        ouput: NA
        """
        self.logger.log("Fixing file permission.")
        binPath = "'%s'/bin" % self.installPath
        g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, binPath, True)
        libPath = "'%s'/lib" % self.installPath
        g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, libPath, True)
        sharePath = "'%s'/share" % self.installPath
        g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, sharePath, True)
        etcPath = "'%s'/etc" % self.installPath
        g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, etcPath, True)
        includePath = "'%s'/include" % self.installPath
        g_file.changeMode(DefaultValue.KEY_DIRECTORY_MODE, includePath, True)

        tarFile = "'%s'/bin/'%s'" % (self.installPath,
                                     DefaultValue.get_package_back_name())
        if (os.path.isfile(tarFile)):
            g_file.changeMode(DefaultValue.KEY_FILE_MODE, tarFile)

        # ./script/util/*.conf *.service
        g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                          "'%s'/bin/script/gspylib/etc/conf/check_list.conf"
                          % self.installPath)
        g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                          "'%s'/bin/script/gspylib/etc/conf/"
                          "check_list_dws.conf" % self.installPath)
        g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                          "'%s'/bin/script/gspylib/etc/conf/gs-OS-set.service"
                          % self.installPath)
        # bin config file
        g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                          "'%s'/bin/alarmItem.conf" % self.installPath)
        g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                          "'%s'/bin/cluster_guc.conf" % self.installPath)
        g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                          "'%s'/bin/upgrade_version" % self.installPath)
        g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                          "'%s'/bin/retry_errcodes.conf" % self.installPath)
        g_file.changeMode(DefaultValue.KEY_FILE_MODE,
                          "'%s'/bin/cluster_static_config" % self.installPath)

        # ./script/local/*.sql
        cmd = "find '%s'/bin/script -type f -name \"*.sql\" -exec" \
              " chmod 600 {} \\;" % self.installPath
        # ./lib files
        cmd += " && find '%s'/lib/ -type f -exec chmod 600 {} \\;" \
               % self.installPath
        # ./share files
        cmd += " && find '%s'/share/ -type f -exec chmod 600 {} \\;" \
               % self.installPath
        self.logger.debug("Command: %s" % cmd)
        (status, output) = subprocess.getstatusoutput(cmd)
        if status != 0:
            self.logger.log(output)
            self.logger.logExit(ErrorCode.GAUSS_501["GAUSS_50107"] % "app.")
Example #17
0
 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."
    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")
Example #19
0
def chmodFile(fileName, permission=FILE_MODE, user=""):
    # Modify the file permissions
    g_file.changeMode(permission, fileName)
    if (user):
        g_file.changeOwner(user, fileName)
Example #20
0
    def createTrust(self,
                    user,
                    pwd,
                    ips=None,
                    mpprcFile="",
                    skipHostnameSet=False,
                    preMode=False):
        """
        function: create trust for specified user with both ip and hostname,
                  when using N9000 tool create trust failed
                  do not support using a normal user to create trust for
                  another user.
        input : user, pwd, ips, mpprcFile, skipHostnameSet
        output: NA
        """
        tmp_hosts = "/tmp/tmp_hosts_%d" % self.__pid
        cnt = 0
        status = 0
        output = ""
        if ips is None:
            ips = []
        try:
            g_file.removeFile(tmp_hosts)
            # 1.prepare hosts file
            for ip in ips:
                cmd = "echo %s >> %s 2>/dev/null" % (ip, tmp_hosts)
                (status, output) = subprocess.getstatusoutput(cmd)
                if status != 0:
                    raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] %
                                    tmp_hosts + " Error:\n%s." % output +
                                    "The cmd is %s" % cmd)
            g_file.changeMode(DefaultValue.KEY_HOSTS_FILE, tmp_hosts, False,
                              "python")

            # 2.call createtrust script
            create_trust_file = "gs_sshexkey"
            if pwd is None or len(str(pwd)) == 0:
                GaussLog.printMessage("Please enter password for current"
                                      " user[%s]." % user)
                pwd = getpass.getpass()

            if (mpprcFile != ""
                    and g_file.checkFilePermission(mpprcFile, True)
                    and self.checkMpprcfile(user, mpprcFile)):
                cmd = "source %s; %s -f %s -l '%s'" % (
                    mpprcFile, create_trust_file, tmp_hosts, self.__logFile)
            elif (mpprcFile == ""
                  and g_file.checkFilePermission('/etc/profile', True)):
                cmd = "source /etc/profile;" \
                      " %s -f %s -l '%s'" % (create_trust_file,
                                             tmp_hosts, self.__logFile)

            if skipHostnameSet:
                cmd += " --skip-hostname-set"
            cmd += " 2>&1"

            tempcmd = ["su", "-", user, "-c"]
            tempcmd.append(cmd)
            cmd = tempcmd

            p = subprocess.Popen(cmd,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            if os.getuid() != 0:
                time.sleep(5)
                p.stdin.write((pwd + "\n").encode(encoding="utf-8"))
            time.sleep(10)
            p.stdin.write((pwd + "\n").encode(encoding="utf-8"))
            (output, err) = p.communicate()
            # 3.delete hosts file
            g_file.removeFile(tmp_hosts)
            if output is not None:
                output = str(output, encoding='utf-8')
                if re.search("\[GAUSS\-", output):
                    if re.search("Please enter password", output):
                        GaussLog.printMessage(
                            ErrorCode.GAUSS_503["GAUSS_50306"] % user)
                    else:
                        GaussLog.printMessage(output.strip())
                    sys.exit(1)
                else:
                    GaussLog.printMessage(output.strip())
            else:
                sys.exit(1)
        except Exception as e:
            g_file.removeFile(tmp_hosts)
            raise Exception(str(e))