コード例 #1
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")
コード例 #2
0
def getOSInitFile():
    """
    function : Get the OS initialization file
    input : NA
    output : String
    """
    distname = g_Platform.dist()[0]
    systemd_system_dir = "/usr/lib/systemd/system/"
    systemd_system_file = "/usr/lib/systemd/system/gs-OS-set.service"
    # OS init file
    #     now we only support SuSE and RHEL
    initFileSuse = "/etc/init.d/boot.local"
    initFileRedhat = "/etc/rc.d/rc.local"
    # system init file
    initSystemFile = "/usr/local/gauss/script/gauss-OS-set.sh"
    dirName = os.path.dirname(os.path.realpath(__file__))
    # Get the startup file of suse or redhat os
    if (os.path.isdir(systemd_system_dir)):
        if (not os.path.exists(systemd_system_file)):
            cmd = "cp '%s'/gs-OS-set.service '%s'; chmod %s '%s'" % (
            dirName, systemd_system_file, DefaultValue.KEY_FILE_MODE,
            systemd_system_file)
            runShellCmd(cmd)
            cmd = "systemctl enable gs-OS-set.service"
            runShellCmd(cmd)
        if (not os.path.exists(initSystemFile)):
            cmd = "mkdir -p '%s'" % os.path.dirname(initSystemFile)
            runShellCmd(cmd)
            g_file.createFileInSafeMode(initSystemFile)
            with open(initSystemFile, "w") as fp:
                fp.write("#!/bin/bash\n")
        cmd = "chmod %s '%s'" % (DefaultValue.KEY_FILE_MODE, initSystemFile)
        runShellCmd(cmd)
        return initSystemFile
    if (distname == "SuSE" and os.path.isfile(initFileSuse)):
        initFile = initFileSuse
    elif (distname in (
    "redhat", "centos", "euleros", "openEuler") and os.path.isfile(
            initFileRedhat)):
        initFile = initFileRedhat
    else:
        initFile = ""
    return initFile
コード例 #3
0
    def getSshStatusOutput(self,
                           cmd,
                           hostList=None,
                           env_file="",
                           gp_path="",
                           parallel_num=300,
                           ssh_config=""):
        """
        function: Get command status and output
        input : cmd, hostList, env_file, gp_path, parallel_num
        output: resultMap, outputCollect
        """
        sshCmd = ""
        localMode = False
        resultMap = {}
        outputCollect = ""
        isTimeOut = False
        need_replace_quotes = False

        if hostList is None:
            hostList = []

        if cmd.find("[need_replace_quotes]") != -1:
            cmd = cmd.replace("[need_replace_quotes]", "")
            need_replace_quotes = True
        fp = None

        try:
            mpprcFile, userProfile, osProfile = self.getUserOSProfile(env_file)
            # clean result file
            if os.path.exists(self.__resultFile):
                os.remove(self.__resultFile)

            if gp_path == "":
                GPHOME = self.getGPHOMEPath(osProfile)
            else:
                GPHOME = gp_path.strip()
            psshpre = "python3 %s/script/gspylib/pssh/bin/pssh" % GPHOME
            if ssh_config:
                if os.path.exists(ssh_config) and os.path.isfile(ssh_config):
                    psshpre += ' -x "-F %s" ' % ssh_config

            if len(hostList) == 0:
                if os.getuid() == 0 and (mpprcFile == "" or not mpprcFile):
                    sshCmd = "source %s && %s -t %s -h %s -P -p %s -o %s -e" \
                             " %s \"source %s; %s\" 2>&1 | tee %s" \
                             % (osProfile, psshpre, self.__timeout,
                                self.__hostsFile, parallel_num,
                                self.__outputPath, self.__errorPath,
                                osProfile, cmd, self.__resultFile)
                else:
                    sshCmd = "source %s && %s -t %s -h %s -P -p %s -o %s -e" \
                             " %s \"source %s;source %s;%s\" 2>&1 | tee %s" \
                             % (osProfile, psshpre, self.__timeout,
                                self.__hostsFile, parallel_num,
                                self.__outputPath, self.__errorPath,
                                osProfile, userProfile, cmd,
                                self.__resultFile)
                hostList = self.hostNames
            else:
                if need_replace_quotes:
                    remote_cmd = cmd.replace("\"", "\\\"")
                else:
                    remote_cmd = cmd
                if os.getuid() == 0 and (mpprcFile == "" or not mpprcFile):
                    sshCmd = "source %s && %s -t %s -H %s -P -p %s -o %s -e" \
                             " %s \"source %s; %s\" 2>&1 | tee %s" \
                             % (osProfile, psshpre, self.__timeout,
                                " -H ".join(hostList), parallel_num,
                                self.__outputPath, self.__errorPath,
                                osProfile, remote_cmd, self.__resultFile)
                else:
                    sshCmd = "source %s && %s -t %s -H %s -P -p %s -o %s -e" \
                             " %s \"source %s;source %s;%s\" 2>&1 | tee %s" \
                             % (osProfile, psshpre, self.__timeout,
                                " -H ".join(hostList), parallel_num,
                                self.__outputPath, self.__errorPath,
                                osProfile, userProfile, remote_cmd,
                                self.__resultFile)

            # single cluster or execute only in local node.
            if (len(hostList) == 1
                    and hostList[0] == DefaultValue.GetHostIpOrName()):
                localMode = True
                if os.getuid() == 0 and (mpprcFile == "" or not mpprcFile):
                    sshCmd = "source %s ; %s 2>&1" % (osProfile, cmd)
                else:
                    sshCmd = "source %s ; source %s; %s 2>&1" % (
                        osProfile, userProfile, cmd)

            (status, output) = subprocess.getstatusoutput(sshCmd)
            # when the pssh is time out, kill parent and child process
            if not localMode:
                if output.find("Timed out, Killed by signal 9") > 0:
                    isTimeOut = True
                    self.timeOutClean(cmd, psshpre, hostList, env_file,
                                      parallel_num)
                    raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] %
                                    sshCmd + " Error:\n%s" % output)
                if status != 0:
                    raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] %
                                    sshCmd + " Error:\n%s" % output)

            if localMode:
                dir_permission = 0o700
                if status == 0:
                    resultMap[hostList[0]] = DefaultValue.SUCCESS
                    outputCollect = "[%s] %s:\n%s" % ("SUCCESS", hostList[0],
                                                      output)

                    if not os.path.exists(self.__outputPath):
                        os.makedirs(self.__outputPath, mode=dir_permission)
                    file_path = os.path.join(self.__outputPath, hostList[0])
                    g_file.createFileInSafeMode(file_path)
                    with open(file_path, "w") as fp:
                        fp.write(output)
                        fp.flush()
                        fp.close()
                else:
                    resultMap[hostList[0]] = DefaultValue.FAILURE
                    outputCollect = "[%s] %s:\n%s" % ("FAILURE", hostList[0],
                                                      output)

                    if not os.path.exists(self.__errorPath):
                        os.makedirs(self.__errorPath, mode=dir_permission)
                    file_path = os.path.join(self.__errorPath, hostList[0])
                    g_file.createFileInSafeMode(file_path)
                    with open(file_path, "w") as fp:
                        fp.write(output)
                        fp.flush()
                        fp.close()
            else:
                resultMap, outputCollect = self.parseSshResult(hostList)
        except Exception as e:
            if fp:
                fp.close()
            if not isTimeOut:
                self.clenSshResultFiles()
            raise Exception(str(e))

        for host in hostList:
            if resultMap.get(host) != DefaultValue.SUCCESS:
                if outputCollect.find("GAUSS-5") == -1:
                    outputCollect = ErrorCode.GAUSS_514["GAUSS_51400"] \
                                    % cmd + " Error:\n%s." % outputCollect
                    break

        return resultMap, outputCollect
コード例 #4
0
def main():
    """
    main function
    """
    try:
        (opts, args) = getopt.getopt(sys.argv[1:], "p:S:f:s:d:h", ["help"])
    except Exception as e:
        usage()
        GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % str(e))

    if (len(args) > 0):
        GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] %
                               str(args[0]))

    port = ""
    sqlfile = ""
    outputfile = ""
    database = ""
    for (key, value) in opts:
        if (key == "-h" or key == "--help"):
            usage()
            sys.exit(0)
        elif (key == "-p"):
            port = value
        elif (key == "-S"):
            sqlfile = value
        elif (key == "-f"):
            outputfile = value
        elif (key == "-s"):
            snapid = value
        elif (key == "-d"):
            database = value
        else:
            GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % key)

        Parameter.checkParaVaild(key, value)

    # check parameter
    if (port == ""):
        GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'p' + ".")
    if (sqlfile == ""):
        GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'S' + ".")
    if (outputfile == ""):
        GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'f' + ".")
    if (database == ""):
        GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'd' + ".")
    try:
        output = {}
        exesql = ""
        if os.path.exists(sqlfile):
            with open(sqlfile, "r") as fp:
                lines = fp.readlines()
                for line in lines:
                    exesql += line + "\n"
        (status, result, err_output) = \
            ClusterCommand.excuteSqlOnLocalhost(port, exesql, database)
        cmd = "rm -rf %s" % sqlfile
        if (err_output != ""):
            output["status"] = status
            output["error_output"] = err_output
            GaussLog.exitWithError(ErrorCode.GAUSS_513["GAUSS_51300"] %
                                   exesql + "Errors:%s" % err_output)
        output["status"] = status
        output["result"] = result
        output["error_output"] = err_output
        g_file.createFileInSafeMode(outputfile)
        with open(outputfile, "w") as fp_json:
            json.dump(output, fp_json)
        (status, outpout) = subprocess.getstatusoutput(cmd)
        if status != 0:
            raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] % cmd +
                            "Error:\n%s" % output)
    except Exception as e:
        GaussLog.exitWithError("Errors:%s" % str(e))
コード例 #5
0
ファイル: Backup.py プロジェクト: zoumingzhe/openGauss-server
    def doBackup(self):
        """
        function: 1.back up binary files
                  2.back up parameter files
        input : NA
        output: NA
        """
        self.logger.log("Backing up files.")

        if self.backupBin:
            self.logger.log("Backing up binary files.")

            try:
                self.logger.debug(
                    "Installation path is %s." % self.installPath)
                if (len(os.listdir(self.installPath)) == 0):
                    raise Exception(ErrorCode.GAUSS_502["GAUSS_50203"] % (
                            "installation path [%s]" % self.installPath))
                self.__tarDir(self.installPath, self.binTarName)
            except Exception as e:
                raise Exception(str(e))

            self.logger.log("Successfully backed up binary files.")

        if self.backupPara:
            self.logger.log("Backing up parameter files.")

            try:
                self.logger.debug(
                    "Creating temporary directory for all parameter files.")
                temp_dir = os.path.join(self.tmpBackupDir,
                                        "parameter_%s" % HOSTNAME)
                self.logger.debug("Temporary directory path: %s." % temp_dir)
                if (os.path.exists(temp_dir)):
                    file_list = os.listdir(temp_dir)
                    if (len(file_list) != 0):
                        self.logger.debug(
                            "The temporary directory "
                            "is not empty.\n%s\nRemove all files silently."
                            % file_list)
                        g_file.cleanDirectoryContent(temp_dir)
                else:
                    os.makedirs(temp_dir,
                                DefaultValue.KEY_DIRECTORY_PERMISSION)

                self.logger.debug("Creating hostname file.")
                hostnameFile = os.path.join(temp_dir, self.hostnameFileName)
                self.logger.debug(
                    "Register hostname file path: %s." % hostnameFile)
                g_file.createFileInSafeMode(hostnameFile)
                with open(hostnameFile, "w") as self.__hostnameFile:
                    hostName = DefaultValue.GetHostIpOrName()
                    self.__hostnameFile.write("%s" % hostName)
                    self.logger.debug("Flush hostname file.")
                    self.__hostnameFile.flush()
                self.__hostnameFile = None

                os.chmod(hostnameFile, DefaultValue.KEY_FILE_PERMISSION)

                self.logger.debug("Collecting parameter files.")
                for inst in self.dbNodeInfo.datanodes:
                    self.__collectParaFilesToTempDir(inst, temp_dir)

                self.logger.debug(
                    "Generating parameter files to be compressed.")
                self.__tarDir(temp_dir, self.paraTarName, True)

                self.logger.debug("Removing temporary directory.")
                g_file.removeDirectory(temp_dir)
            except Exception as e:
                g_file.removeDirectory(temp_dir)
                raise Exception(str(e))

            self.logger.log("Successfully backed up parameter files.")

        self.logger.log("Successfully backed up files.")