diff --git a/script/base_utils/os/env_util.py b/script/base_utils/os/env_util.py index e54ed657161b3a6d898fa39cc466675d84629c0a..0e5ec63708eddaa8c63252b4dbc7a72026fd12d9 100644 --- a/script/base_utils/os/env_util.py +++ b/script/base_utils/os/env_util.py @@ -82,14 +82,17 @@ class EnvUtil(object): return EnvUtil.getEnvironmentParameterValue("PGHOST", user) @staticmethod - def getTempDir(dir_name): + def getTempDir(dir_name, user = ""): """ function: create temp directory in PGHOST input: dir_name output: pathName """ - tmp_path = EnvUtil.getTmpDirFromEnv() + if user == "": + tmp_path = EnvUtil.getTmpDirFromEnv() + else: + tmp_path = EnvUtil.getTmpDirFromEnv(user) return os.path.join(tmp_path, dir_name) @staticmethod diff --git a/script/domain_utils/cluster_file/package_info.py b/script/domain_utils/cluster_file/package_info.py index 68644f5fb9d6cd8c4cd7282294f7feb1973e521b..62a1db9bf81ef9d3ca6876d4540c478ee64db9f8 100644 --- a/script/domain_utils/cluster_file/package_info.py +++ b/script/domain_utils/cluster_file/package_info.py @@ -157,6 +157,8 @@ class PackageInfo(object): integrity_file_name = PackageInfo.getSHA256FilePath() cm_package = "%s-cm.tar.gz" % PackageInfo.getPackageFile( "bz2File").replace(".tar.bz2", "") + om_package = "%s-om.tar.gz" % PackageInfo.getPackageFile( + "bz2File").replace(".tar.bz2", "") tar_lists = SingleInstDiff.get_package_tar_lists(is_single_inst, os.path.normpath(package_path)) @@ -171,8 +173,8 @@ class PackageInfo(object): # do not tar *.log files cmd += CompressUtil.getCompressFilesCmd(PackageInfo.get_package_back_name(), tar_lists) - cmd += " %s %s " % (os.path.basename(bz2_file_name), - os.path.basename(integrity_file_name)) + cmd += " %s %s %s " % (os.path.basename(bz2_file_name), + os.path.basename(integrity_file_name), os.path.basename(om_package)) # add CM package to bak package if os.path.isfile(os.path.realpath(os.path.join(package_path, cm_package))): cmd += "%s " % os.path.basename(cm_package) diff --git a/script/domain_utils/cluster_file/profile_file.py b/script/domain_utils/cluster_file/profile_file.py index a44929d4a61f52d0f9476a2d9ececbefd89695a5..c995ad919f6046e8b6bf4fd3d112eecdcdb1a04b 100644 --- a/script/domain_utils/cluster_file/profile_file.py +++ b/script/domain_utils/cluster_file/profile_file.py @@ -167,7 +167,7 @@ class ProfileFile: if not status: return False, output - if user and os.getuid() == 0: + if user: execute_cmd = "%s '%s' && %s '%s'" % (CmdUtil.SOURCE_CMD, ClusterConstants.ETC_PROFILE, CmdUtil.SOURCE_CMD, @@ -208,9 +208,4 @@ class ProfileFile: input : username output : NA """ - cmd = "su - %s -c \"echo ~\" 2>/dev/null" % username - (status, output) = subprocess.getstatusoutput(cmd) - if status == 0: - return output + "/.bashrc" - else: - return ClusterConstants.HOME_USER_BASHRC % username + return ClusterConstants.HOME_USER_BASHRC % username \ No newline at end of file diff --git a/script/domain_utils/domain_common/cluster_constants.py b/script/domain_utils/domain_common/cluster_constants.py index 7f4e84d9e40b6381b9012c1dad0bc33bdb1d8fa3..43baef94ba18c6e1cf3736a09da0eab11e07ec82 100644 --- a/script/domain_utils/domain_common/cluster_constants.py +++ b/script/domain_utils/domain_common/cluster_constants.py @@ -42,4 +42,5 @@ class ClusterConstants: LCCTL_LOG_FILE = "gs_lcctl.log" RESIZE_LOG_FILE = "gs_resize.log" HOTPATCH_LOG_FILE = "gs_hotpatch.log" + GS_CHECKOS_LOG_FILE = "gs_checkos.log" diff --git a/script/gs_checkos b/script/gs_checkos index 5f9522754cbd82aec2343e4e8c2cf41d6c58b9a4..f4743a58b1ad9be5de797f9660d14c2f2e913514 100644 --- a/script/gs_checkos +++ b/script/gs_checkos @@ -37,6 +37,8 @@ from base_utils.os.file_util import FileUtil from base_utils.os.net_util import NetUtil from domain_utils.domain_common.cluster_constants import ClusterConstants from base_utils.common.constantsbase import ConstantsBase +from base_utils.os.user_util import UserUtil +from domain_utils.cluster_file.cluster_config_file import ClusterConfigFile ############################################################################# @@ -134,6 +136,7 @@ ACTION_SET_IO_CONFIGURE = "Set_IO_Configure" ACTION_SET_IO_REQUEST = "Set_IO_REQUEST" ACTION_SET_ASYNCHRONOUS_IO_REQUEST = "Set_Asynchronous_IO_Request" +current_user_root = False ####################################################### class CmdOptions(): @@ -157,10 +160,17 @@ class CmdOptions(): self.localMode = False self.skipOSCheck = [] self.skip_OS_Check = {} + self.skip_cgroup_set = False ######################################################### +def check_current_user(): + if os.getuid() == 0: + current_user_root = True + else: + current_user_root = False + # Init global log ######################################################### def initGlobals(): @@ -331,6 +341,15 @@ def parseCommandLine(): if (ParaDict.__contains__("skipOSCheck")): g_opts.skipOSCheck = ParaDict.get("skipOSCheck") +def get_user_info(): + """ + function: get user + input: NA + output: NA + """ + user_info = UserUtil.getUserInfo() + g_opts.user = user_info.get("name") + DefaultValue.checkPathVaild(g_opts.user) def readHostFile(hostfile): """ @@ -373,19 +392,38 @@ def checkConfigFile(): GaussLog.exitWithError( ErrorCode.GAUSS_502["GAUSS_50201"] % g_opts.confFile) +def get_checkos_log_path(log_name, xml): + """ + function: get the checkos log path + input: log_name, xml + output: fullLogPath + """ + try: + gs_checkos_log_path = "" + # get the log path + configedLogPath = ClusterConfigFile.getOneClusterConfigItem("gaussdbLogPath", xml) + DefaultValue.checkPathVaild(configedLogPath) + # check gaussdbLogPath is not null + if configedLogPath == "": + gs_checkos_log_path = "%s/%s/om/%s" % ( + ClusterConstants.GAUSSDB_DIR, g_opts.user, log_name) + else: + gs_checkos_log_path = "%s/%s/om/%s" % ( + os.path.normpath(configedLogPath), g_opts.user, log_name) + UserUtil.check_path_owner(gs_checkos_log_path) + return gs_checkos_log_path + except Exception as e: + GaussLog.exitWithError(str(e)) def setLogFile(): """ """ if (g_opts.logFile == ""): - cmd = "(if [ ! -d %s ]; then mkdir -p %s -m %s; fi)" % ( - LOG_DIR, LOG_DIR, DefaultValue.KEY_DIRECTORY_MODE) - (status, output) = subprocess.getstatusoutput(cmd) - if (status != 0): - GaussLog.exitWithError(ErrorCode.GAUSS_502["GAUSS_50208"] - % "log of gs_checkos" + " Error: \n%s." - % output + "The cmd is %s" % cmd) - g_opts.logFile = os.path.join(LOG_DIR, "gs_checkos.log") + g_opts.logFile = get_checkos_log_path(ClusterConstants.GS_CHECKOS_LOG_FILE, g_opts.confFile) + if (not os.path.isabs(g_opts.logFile)): + GaussLog.exitWithError(ErrorCode.GAUSS_502["GAUSS_50213"] + % g_opts.logFile) + UserUtil.check_path_owner(g_opts.logFile) def checkItems(): @@ -409,6 +447,7 @@ def checkParameter(): """ Check parameter from command line """ + get_user_info() ############################################ # check hostlist info ########################################### @@ -482,6 +521,8 @@ def doCheckOS(itemNumber): elif (itemNumber == 'A8' and g_opts.skip_OS_Check["A8"]): checkDiskConfigure() elif (itemNumber == 'A9' and g_opts.skip_OS_Check["A9"]): + if not current_user_root: + return checkBlockDevConfigure() checkLogicalBlock() elif (itemNumber == 'A10' and g_opts.skip_OS_Check["A10"]): @@ -489,6 +530,8 @@ def doCheckOS(itemNumber): checkMaxAsyIOrequests() checkIOConfigure() elif (itemNumber == 'A11' and g_opts.skip_OS_Check["A11"]): + if not current_user_root: + return checkNetworkConfigure() elif (itemNumber == 'A12' and g_opts.skip_OS_Check["A12"]): checkTimeConsistency() @@ -1511,8 +1554,7 @@ def main(): """ main function """ - if (os.getuid() != 0): - GaussLog.exitWithError(ErrorCode.GAUSS_501["GAUSS_50104"]) + check_current_user() global Local_CheckOs global Local_Check @@ -1565,10 +1607,10 @@ def main(): % g_opts.outputfile) for item in itemList: - if (g_opts.set == False): - doCheckOS(item) - else: + if (current_user_root and g_opts.set): doSetOS(item) + else: + doCheckOS(item) DisplayResultInformation(item, output) if (fp): diff --git a/script/gs_expansion b/script/gs_expansion index 1fe22d617d7f019aac8138758e20f9250e50a09a..47c2906f3c037eeb03a0cc8b1c456b6bc300b1a4 100644 --- a/script/gs_expansion +++ b/script/gs_expansion @@ -47,6 +47,7 @@ from impl.expansion.expansion_impl_with_cm_local import ExpansionImplWithCmLocal from domain_utils.cluster_file.cluster_config_file import ClusterConfigFile from domain_utils.cluster_file.cluster_log import ClusterLog from base_utils.os.env_util import EnvUtil +from base_utils.os.user_util import UserUtil ENV_LIST = ["MPPDB_ENV_SEPARATE_PATH", "GPHOME", "PATH", "LD_LIBRARY_PATH", "PYTHONPATH", "GAUSS_WARNING_TYPE", @@ -113,6 +114,15 @@ General options: """ print(self.usage.__doc__) + def check_current_user(self): + user_info = UserUtil.getUserInfo() + if user_info['uid'] == 0: + self.current_user_root = True + else: + self.current_user_root = False + self.user = user_info['name'] + self.group = user_info['g_name'] + def parseCommandLine(self): """ parse parameter from command line @@ -124,6 +134,8 @@ General options: if (ParaDict.__contains__("helpFlag")): self.usage() sys.exit(0) + # check no root parameter + self.check_no_root_parameter(ParaDict) # Resolves command line arguments # parameter -U if (ParaDict.__contains__("user")): @@ -149,6 +161,18 @@ General options: if (ParaDict.__contains__("time_out")): self.time_out = ParaDict.get("time_out") + def check_no_root_parameter(self, para_dict): + """ + function: Check no root user paramter + input: NA + output: NA + """ + if not self.current_user_root: + if (para_dict.__contains__("user") and (para_dict.get("user") != self.user)): + GaussLog.exitWithError(ErrorCode.GAUSS_503["GAUSS_50324"]) + if (para_dict.__contains__("group") and (para_dict.get("group") != self.group)): + GaussLog.exitWithError(ErrorCode.GAUSS_503["GAUSS_50324"]) + def checkParameters(self): """ function: Check parameter from command line @@ -157,10 +181,11 @@ General options: """ # check user | group | xmlfile | node - if len(self.user) == 0: - GaussLog.exitWithError(ErrorCode.GAUSS_357["GAUSS_35701"] % "-U") - if len(self.group) == 0: - GaussLog.exitWithError(ErrorCode.GAUSS_357["GAUSS_35701"] % "-G") + if self.current_user_root: + if len(self.user) == 0: + GaussLog.exitWithError(ErrorCode.GAUSS_357["GAUSS_35701"] % "-U") + if len(self.group) == 0: + GaussLog.exitWithError(ErrorCode.GAUSS_357["GAUSS_35701"] % "-G") if len(self.xmlFile) == 0: GaussLog.exitWithError(ErrorCode.GAUSS_357["GAUSS_35701"] % "-X") if len(self.newHostList) == 0: @@ -287,13 +312,6 @@ General options: self.backIpNameMap[backip] = \ self.clusterInfo.getNodeNameByBackIp(backip) - def checkExecutingUser(self): - """ - check whether current user executing this command is root - """ - if os.getuid() != 0: - GaussLog.exitWithError(ErrorCode.GAUSS_501["GAUSS_50104"]) - def checkExecutingHost(self): """ check whether current host is primary host @@ -322,17 +340,19 @@ General options: rootSSHExceptionHosts = [] individualSSHExceptionHosts = [] for host in hostList: - # check root's trust - checkRootTrustCmd = "%s -s -H %s 'pwd'" % (psshPath, host) - (status, output) = subprocess.getstatusoutput(checkRootTrustCmd) - if status != 0: - rootSSHExceptionHosts.append(host) - # check individual user's trust - checkUserTrustCmd = "su - %s -c '%s -s -H %s pwd'" % ( - self.user, psshPath, host) - (status, output) = subprocess.getstatusoutput(checkUserTrustCmd) - if status != 0: - individualSSHExceptionHosts.append(host) + if self.current_user_root: + # check individual user's trust + checkUserTrustCmd = "su - %s -c '%s -s -H %s pwd'" % ( + self.user, psshPath, host) + (status, output) = subprocess.getstatusoutput(checkUserTrustCmd) + if status != 0: + individualSSHExceptionHosts.append(host) + else: + # check current user's trust, if user is root or non root + checkRootTrustCmd = "%s -s -H %s 'pwd'" % (psshPath, host) + (status, output) = subprocess.getstatusoutput(checkRootTrustCmd) + if status != 0: + rootSSHExceptionHosts.append(host) # output ssh exception info if ssh connect failed if rootSSHExceptionHosts or individualSSHExceptionHosts: sshExceptionInfo = "" @@ -353,6 +373,12 @@ General options: check whether info in XML is consistent with environment variable """ self.logger.debug("Checking environment variable.") + if not self.envFile: + self.envFile = "/home/%s/.bashrc" % self.user + cmd = "source %s" % self.envFile + (status, output) = subprocess.getstatusoutput(cmd) + if status != 0: + raise Expansion("not found envfile.") if not EnvUtil.getEnv("GPHOME"): GaussLog.exitWithError(ErrorCode.GAUSS_518["GAUSS_51802"] % ( "\"GPHOME\", please import environment variable")) @@ -408,7 +434,7 @@ General options: expand_cluster_info.compare_cluster_info(static_cluster_info, xml_cluster_info) - def expand_run(self): + def expand_run(self, expansion): """ This is expansion frame start """ @@ -579,7 +605,7 @@ if __name__ == "__main__": """ """ expansion = Expansion() - expansion.checkExecutingUser() + expansion.check_current_user() expansion.parseCommandLine() expansion.checkParameters() expansion.initLogs() @@ -588,4 +614,4 @@ if __name__ == "__main__": expansion.checkXmlIncludeNewHost() expansion.checkExecutingHost() expansion.checkTrust() - expansion.expand_run() + expansion.expand_run(expansion) diff --git a/script/gs_preinstall b/script/gs_preinstall index 83985fbec460b837ad7d04f6e8b47ff137fc843e..8ae9d594b9e09fd9d45ec03454135b9d3abe5259 100644 --- a/script/gs_preinstall +++ b/script/gs_preinstall @@ -88,7 +88,7 @@ class Preinstall(ParallelBaseOM): self.envParams = [] self.rootUser = "" self.rootPasswd = "" - self.createUserSshTrust = True + self.create_user_ssh_trust = True self.clusterToolPath = "" self.needFixOwnerPaths = [] self.preMode = False @@ -106,6 +106,7 @@ class Preinstall(ParallelBaseOM): self.dss_vg_info = "" self.dss_vgname = "" self.enable_perf_config = False + self.skip_cgroup_set = False def usage(self): """ @@ -133,6 +134,8 @@ General options: --sep-env-file=ENVFILE Path of the MPP environment file. --skip-hostname-set Whether to skip hostname setting. (The default value is set.) + --skip-cgroup-set Whether to skip cgroup setting. + (The default value is set.) -l Path of log file. -?, --help Show help information for this utility, and exit the command line mode. @@ -168,6 +171,8 @@ General options: self.usage() sys.exit(0) + # check no root user paramters + self.check_no_root_paramter(ParaDict) # Resolves command line arguments # parameter -U if (ParaDict.__contains__("user")): @@ -195,6 +200,9 @@ General options: # parameter --skip-hostname-set if (ParaDict.__contains__("skipHostnameSet")): self.skipHostnameSet = ParaDict.get("skipHostnameSet") + # parameter --skip-cgroup-set + if (ParaDict.__contains__("skip_cgroup_set")): + self.skip_cgroup_set = ParaDict.get("skip_cgroup_set") # parameter --skip-os-set if (ParaDict.__contains__("skipOSSet")): self.skipOSSet = ParaDict.get("skipOSSet") @@ -211,6 +219,21 @@ General options: if (ParaDict.__contains__("enable_perf_config")): self.enable_perf_config = True + def check_no_root_paramter(self, para_dict): + """ + function: Check no root user paramter + input: NA + output: NA + """ + if not self.current_user_root: + if (para_dict.__contains__("user") and (para_dict.get("user") != self.user)): + GaussLog.exitWithError(ErrorCode.GAUSS_503["GAUSS_50324"]) + if (para_dict.__contains__("group") and (para_dict.get("group") != self.group)): + GaussLog.exitWithError(ErrorCode.GAUSS_503["GAUSS_50324"]) + if not self.skipOSSet: + GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50028"] % 'os') + if not self.skip_cgroup_set: + GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50028"] % 'cgroup') def checkUserParameter(self): """ @@ -250,6 +273,16 @@ General options: + "User:Group[%s:%s]" % (self.user, self.group)) + def check_local_node_info(self): + """ + check local node info + """ + hostName = NetUtil.GetHostIpOrName() + g_nodeInfo = self.clusterInfo.getDbNodeByName(hostName) + if (g_nodeInfo is None): + GaussLog.exitWithError(ErrorCode.GAUSS_516["GAUSS_51620"] % "local" + + " It is not a host name %s." % hostName) + def check_config_content(self, g_nodeInfo): UserUtil.check_path_owner(ClusterConfigFile.getOneClusterConfigItem("gaussdbAppPath", self.xmlFile)) UserUtil.check_path_owner(ClusterConfigFile.getOneClusterConfigItem("gaussdbToolPath", self.xmlFile)) @@ -355,44 +388,50 @@ General options: GaussLog.exitWithError(ErrorCode.GAUSS_518["GAUSS_51808"] % \ checkoutput + "Please check %s." % envfile) - def checkParameter(self): + def delete_host_ip(self): """ - function: Check parameter from command line + function: delete host_ip env input: NA output: NA """ - ClusterUser.checkGroupParameter(self.user, self.group) - # remove HOST_IP info with /etc/profile and environ - cmd = "sed -i '/^export[ ]*HOST_IP=/d' /etc/profile" - (status, output) = subprocess.getstatusoutput(cmd) - if status != 0: - GaussLog.exitWithError(ErrorCode.GAUSS_502["GAUSS_50205"] - % ClusterConstants.ETC_PROFILE + "The cmd is %s" % cmd) + if self.current_user_root: + cmd = "sed -i '/^export[ ]*HOST_IP=/d' /etc/profile" + (status, output) = subprocess.getstatusoutput(cmd) + if status != 0: + GaussLog.exitWithError(ErrorCode.GAUSS_502["GAUSS_50205"] + % ClusterConstants.ETC_PROFILE + "The cmd is %s" % cmd) + if "HOST_IP" in os.environ.keys(): os.environ.pop("HOST_IP") + def checkParameter(self): + """ + function: Check parameter from command line + input: NA + output: NA + """ + if self.current_user_root: + # check -G parameter + ClusterUser.checkGroupParameter(self.user, self.group) + + # remove HOST_IP info with /etc/profile and environ + self.delete_host_ip() # check config file ClusterConfigFile.checkConfigFile(self.xmlFile) # check user info self.checkUserParameter() # check user group match self.checkUserAndGroup() + # init cluster info self.initClusterInfo() - # check config content - hostName = NetUtil.GetHostIpOrName() - g_nodeInfo = self.clusterInfo.getDbNodeByName(hostName) - if (g_nodeInfo is None): - GaussLog.exitWithError(ErrorCode.GAUSS_516["GAUSS_51620"] % "local" + - " It is not a host name %s." % hostName) - + # check local node info + self.check_local_node_info() # check env-val self.checkEnvValueParameter() # check mpprc file self.checkMpprcFile() - # check log file self.checkLogFile() - # skip OS check self.checkskipOS() @@ -577,25 +616,43 @@ General options: if subdir_path.startswith(parentdir_path): GaussLog.exitWithError(ErrorCode.GAUSS_502["GAUSS_50240"] % dss_home_path) -def clearHistTimeFormat(): - cmd = "sed -i '/HISTTIMEFORMAT=/d' /etc/profile" - (status, output) = subprocess.getstatusoutput(cmd) - if status != 0: - GaussLog.exitWithError("Clear HISTTIMEFORMAT from /etc/profile " - "failed.\nError: %s\nThe cmd is: %s\n" % - (output,cmd)) + def clear_hist_time_format(self): + if self.current_user_root: + cmd = "sed -i '/HISTTIMEFORMAT=/d' /etc/profile" + (status, output) = subprocess.getstatusoutput(cmd) + if status != 0: + GaussLog.exitWithError("Clear HISTTIMEFORMAT from /etc/profile " + "failed.\nError: %s\nThe cmd is: %s\n" % + (output,cmd)) + def check_current_user(self): + user_info = UserUtil.getUserInfo() + if user_info.get("uid") == 0: + self.current_user_root = True + else: + self.current_user_root = False + self.user = user_info.get("name") + self.group = user_info.get("g_name") + self.skipOSSet = True + self.skip_cgroup_set = True + +def remove_mpp_env(): + mpprc = os.environ.get('MPPDB_ENV_SEPARATE_PATH') + if mpprc: + os.environ.pop('MPPDB_ENV_SEPARATE_PATH') if __name__ == '__main__': """ main function """ - # check if user is root - if os.getuid() != 0: - GaussLog.exitWithError(ErrorCode.GAUSS_501["GAUSS_50104"]) - clearHistTimeFormat() try: + # if not remove mpprc env, it will affect preinstall init + remove_mpp_env() # Objectize class preinstall = Preinstall() + # check if user is root + preinstall.check_current_user() + # remove HISTTIMEFORMAT with /etc/profile for root + preinstall.clear_hist_time_format() # set LD_LIBRARY_PATH preinstall.setLibPath() # parse cmd lines diff --git a/script/gspylib/common/Common.py b/script/gspylib/common/Common.py index 18022432f82f010a830f82e28396ca8fde4f65ad..0e36f723a2a407540ec76ac48ded2e114649014c 100644 --- a/script/gspylib/common/Common.py +++ b/script/gspylib/common/Common.py @@ -128,6 +128,7 @@ from gspylib.common.Constants import Constants from domain_utils.cluster_file.profile_file import ProfileFile from domain_utils.domain_common.cluster_constants import ClusterConstants from gspylib.common.aes_cbc_util import AesCbcUtil +from base_utils.os.user_util import UserUtil noPassIPs = [] g_lock = thread.allocate_lock() @@ -856,7 +857,7 @@ class DefaultValue(): dirName = os.path.dirname(os.path.realpath(__file__)) # Get the startup file of suse or redhat os - if (os.path.isdir(systemDir)): + if (os.path.isdir(systemDir) and os.getpid() == 0): # Judge if cgroup para 'Delegate=yes' is written in systemFile cgroup_gate = False cgroup_gate_para = "Delegate=yes" @@ -2280,10 +2281,12 @@ class DefaultValue(): input : module output: NAself """ - if os.getuid() == 0: + user_info = UserUtil.getUserInfo() + if user_info['uid'] == 0: return + user_name = user_info.get('name') # Get the temporary directory from PGHOST - tmpDir = EnvUtil.getTmpDirFromEnv() + tmpDir = EnvUtil.getTmpDirFromEnv(user_name) if not tmpDir: raise Exception(ErrorCode.GAUSS_518["GAUSS_51802"] % "PGHOST") # check if tmp dir exists diff --git a/script/gspylib/common/ErrorCode.py b/script/gspylib/common/ErrorCode.py index a4e4cc95f4528a6e6fd851d7442f7e7b2da02538..558471eba9b84c06dc8cea0a19a3d10f2a8eae5c 100644 --- a/script/gspylib/common/ErrorCode.py +++ b/script/gspylib/common/ErrorCode.py @@ -108,7 +108,7 @@ class ErrorCode(): 'GAUSS_50025': "[GAUSS-50025] : There is illegal character '%s' in parameter %s.", 'GAUSS_50026': "[GAUSS-50026] : Failed to check %s parameters in the XML file.", 'GAUSS_50027': "[GAUSS-50027] : Parameter '%s' format error.", - + 'GAUSS_50028': "[GAUSS-50028] : Non root users do not support setting %s parameters." } ########################################################################### @@ -234,7 +234,8 @@ class ErrorCode(): "from /etc/ssh/sshd_config.", 'GAUSS_50322': "[GAUSS-50322] : Failed to encrypt the password for %s", 'GAUSS_50323': "[GAUSS-50323] : The user %s is not the cluster " - "installation user " + "installation user ", + 'GAUSS_50324': "[GAUSS-50324] : Non root user, -U -G parameter must be the current user and group." } ########################################################################### diff --git a/script/gspylib/common/OMCommand.py b/script/gspylib/common/OMCommand.py index 337d08f575522a0f9da1fc23c7e9ad719bf3996e..14291ff97a678222ea6bb3c0a6c517d9c9563e7c 100644 --- a/script/gspylib/common/OMCommand.py +++ b/script/gspylib/common/OMCommand.py @@ -234,14 +234,15 @@ class OMCommand(): retry += 1 time.sleep(1) - hostnameCmd = "pssh -s -H %s 'cat /etc/hostname'" % (nodename) - (status, output) = subprocess.getstatusoutput(hostnameCmd) - if status == 0 and output.strip() == nodename: - pass - else: - raise Exception(ErrorCode.GAUSS_512["GAUSS_51248"] % nodename - + " Command: \"%s\". Error: \n%s" - % (hostnameCmd, output)) + if os.getuid() == 0: + hostnameCmd = "pssh -s -H %s 'cat /etc/hostname'" % (nodename) + (status, output) = subprocess.getstatusoutput(hostnameCmd) + if status == 0 and output.strip() == nodename: + pass + else: + raise Exception(ErrorCode.GAUSS_512["GAUSS_51248"] % nodename + + " Command: \"%s\". Error: \n%s" + % (hostnameCmd, output)) except Exception as e: raise Exception(str(e)) diff --git a/script/gspylib/common/ParallelBaseOM.py b/script/gspylib/common/ParallelBaseOM.py index 4f2a9539970895ee94cc5a3269c80f83008c09ff..82ac859b931d8c7027a45ae4602f1ba8ce006f8c 100644 --- a/script/gspylib/common/ParallelBaseOM.py +++ b/script/gspylib/common/ParallelBaseOM.py @@ -18,6 +18,7 @@ import os import socket import sys import getpass +import pwd from subprocess import PIPE sys.path.append(sys.path[0] + "/../../") @@ -67,7 +68,7 @@ class ParallelBaseOM(object): self.mpprcFile = "" # Temporary catalog for install self.operateStepDir = EnvUtil.getTempDir( - "%s_step" % self.__class__.__name__.lower()) + "%s_step" % self.__class__.__name__.lower(), pwd.getpwuid(os.getuid()).pw_name) # Temporary files for install step self.operateStepFile = "%s/%s_step.dat" % ( self.operateStepDir, self.__class__.__name__.lower()) @@ -113,6 +114,8 @@ class ParallelBaseOM(object): # Adapt to 200 and 300 self.productVersion = None + # current user root + self.current_user_root = True def initComponent(self): """ diff --git a/script/gspylib/common/ParameterParsecheck.py b/script/gspylib/common/ParameterParsecheck.py index 27422e4fadb468f39c60d3f1b957a1d36791b3ea..b796e9e3ced23435d96bfa39d4f2baebfd2c9a94 100644 --- a/script/gspylib/common/ParameterParsecheck.py +++ b/script/gspylib/common/ParameterParsecheck.py @@ -64,7 +64,7 @@ VALUE_CHECK_LIST = ["|", ";", "&", "$", "<", ">", "`", "\\", "'", "\"", "{", # no child branch gs_preinstall = ["-?", "--help", "-V", "--version", "-U:", "-G:", "-L", "--skip-os-set", "-X:", "--skip-os-check", - "--env-var=", "--sep-env-file=", "--skip-hostname-set", + "--env-var=", "--sep-env-file=", "--skip-hostname-set", "--skip-cgroup-set", "-l:", "--non-interactive", "--delete-root-trust", "--unused-third-party", "--enable-perf-config"] gs_install = ["-?", "--help", "-V", "--version", "-X:", "-l:", @@ -351,6 +351,7 @@ class Parameter(): "--non-interactive": "preMode", "--skip-os-set": "skipOSSet", "--skip-hostname-set": "skipHostnameSet", + "--skip-cgroup-set": "skip_cgroup_set", "--reset": "reset", "--parameter": "isParameter", "--binary": "isBinary", diff --git a/script/gspylib/threads/SshTool.py b/script/gspylib/threads/SshTool.py index d00c9cd0fc711456853c1393037f84246c92206f..d6441b3972d32f6d161a7807780022b60137a0c5 100644 --- a/script/gspylib/threads/SshTool.py +++ b/script/gspylib/threads/SshTool.py @@ -233,7 +233,10 @@ class SshTool(): output: True/False """ ownerPath = os.path.split(filePath)[0] - cmd = "su - %s -c 'cd %s'" % (username, ownerPath) + if os.getuid() == 0: + cmd = "su - %s -c 'cd %s'" % (username, ownerPath) + else: + cmd = "cd %s" % ownerPath (status, output) = subprocess.getstatusoutput(cmd) if status != 0: raise Exception(ErrorCode.GAUSS_500["GAUSS_50004"] diff --git a/script/impl/expansion/ExpansionImpl.py b/script/impl/expansion/ExpansionImpl.py index 6e627c66386c84ce1516f6506111d2fe2fc2b213..7aa4bca7db60e74af2de8524380e8d18e2cc9177 100644 --- a/script/impl/expansion/ExpansionImpl.py +++ b/script/impl/expansion/ExpansionImpl.py @@ -147,7 +147,7 @@ class ExpansionImpl(): self.logger.log("Failed to rollback wal_keep_segments, please manually " "set it to original value %s." % self.walKeepSegments) else: - self.reloadPrimaryConf(self.user) + self.reloadPrimaryConf() def final(self): """ @@ -546,7 +546,7 @@ class ExpansionImpl(): else: command = "source /etc/profile;source %s;"\ "gs_om -t status --detail" % self.envFile - if isRootUser: + if isRootUser and self.context.current_user_root: command = "su - %s -c '%s'" % (self.user, command) self.logger.debug(command) sshTool = SshTool([primaryHost]) @@ -753,7 +753,7 @@ gs_guc set -D {dn} -c "available_zone='{azName}'" primaryHost = self.getPrimaryHostName() dataNode = self.context.clusterInfoDict[primaryHost]["dataNode"] command = "" - if user: + if self.context.current_user_root: command = "su - %s -c 'source %s;gs_ctl reload -D %s'" % \ (user, self.envFile, dataNode) else: @@ -1343,12 +1343,11 @@ remoteservice={remoteservice}'" self.logger.debug("Checking the consistence of datanodes.") primaryName = self.getPrimaryHostName() cmd = "" - if EnvUtil.getEnv("MPPDB_ENV_SEPARATE_PATH"): - cmd = "su - %s -c 'source %s;gs_om -t status --detail'" % \ + if self.context.current_user_root: + cmd = "su - %s -c 'source /etc/profile;source %s;gs_om -t status --detail'" % \ (self.user, self.envFile) else: - cmd = "su - %s -c 'source /etc/profile;source %s;"\ - "gs_om -t status --detail'" % (self.user, self.envFile) + cmd = "source %s;gs_om -t status --detail" % (self.envFile) sshTool = SshTool([primaryName]) resultMap, outputCollect = sshTool.getSshStatusOutput(cmd, [primaryName], self.envFile) @@ -1392,14 +1391,14 @@ remoteservice={remoteservice}'" if hostName == primary: continue dataNode = clusterInfoDict[hostName]["dataNode"] - if EnvUtil.getEnv("MPPDB_ENV_SEPARATE_PATH"): - cmd = "su - %s -c 'source %s;" \ - "gs_guc check -D %s -c \"available_zone\"'" % \ - (self.user, self.envFile, dataNode) - else: + if self.context.current_user_root: cmd = "su - %s -c 'source /etc/profile;source %s;" \ "gs_guc check -D %s -c \"available_zone\"'" % \ (self.user, self.envFile, dataNode) + else: + cmd = "source /etc/profile;source %s;" \ + "gs_guc check -D %s -c \"available_zone\"" % \ + (self.envFile, dataNode) sshTool = SshTool([hostIp]) resultMap, output = sshTool.getSshStatusOutput(cmd, [hostIp], self.envFile) @@ -1424,12 +1423,11 @@ remoteservice={remoteservice}'" curHostName = socket.gethostname() command = "" - if EnvUtil.getEnv("MPPDB_ENV_SEPARATE_PATH"): - command = "su - %s -c 'source %s;gs_om -t status --detail'" % \ - (self.user, self.envFile) - else: - command = "su - %s -c 'source /etc/profile;source %s;"\ + if self.context.current_user_root: + command = "su - %s -c 'source %s;"\ "gs_om -t status --detail'" % (self.user, self.envFile) + else: + command = "source %s; gs_om -t status --detail" % (self.envFile) sshTool = SshTool([curHostName]) resultMap, outputCollect = sshTool.getSshStatusOutput(command, [curHostName], self.envFile) @@ -1501,11 +1499,14 @@ remoteservice={remoteservice}'" if (fstat[stat.ST_UID] == uid and (mode & stat.S_IRUSR > 0)) or \ (fstat[stat.ST_GID] == gid and (mode & stat.S_IRGRP > 0)): pass - else: + elif self.context.current_user_root: self.logger.debug(ErrorCode.GAUSS_501["GAUSS_50100"] % (xmlFile, self.user)) os.chown(xmlFile, uid, gid) os.chmod(xmlFile, stat.S_IRUSR) + else: + GaussLog.exitWithError(ErrorCode.GAUSS_501["GAUSS_50100"] + % (xmlFile, self.user)) def checkUserAndGroupExists(self): """ @@ -1632,11 +1633,15 @@ remoteservice={remoteservice}'" self.logger.debug("[%s] rollbackPg_hbaCmd:%s" % (host, rollbackPg_hbaCmd)) sshTool.getSshStatusOutput(rollbackPg_hbaCmd, [host]) - reloadGUCCommand = "su - %s -c 'source %s; gs_ctl reload " \ - "-D %s'" % (self.user, self.envFile, dataNode) - self.logger.debug(reloadGUCCommand) + if self.context.current_user_root: + reload_guc_command = "su - %s -c 'source %s; gs_ctl reload " \ + "-D %s'" % (self.user, self.envFile, dataNode) + else: + reload_guc_command = "'source %s; gs_ctl reload " \ + "-D %s'" % (self.envFile, dataNode) + self.logger.debug(reload_guc_command) resultMap, outputCollect = sshTool.getSshStatusOutput( - reloadGUCCommand, [host], self.envFile) + reload_guc_command, [host], self.envFile) self.logger.debug(resultMap) self.logger.debug(outputCollect) self.cleanSshToolFile(sshTool) @@ -1762,7 +1767,7 @@ class GsCtlCommon: """ value = "" command = "" - if user: + if os.getuid() == 0 and user: command = "su - %s -c 'source %s; gs_guc check -D %s -c \"%s\"'" % \ (user, env, datanode, para) else: diff --git a/script/impl/expansion/expansion_impl_with_cm.py b/script/impl/expansion/expansion_impl_with_cm.py index 8f7f0089338bb370d8f4d0cda8f2792c32e1bc4c..a8fb2a0f4fac65f868e7a67d35ba6240590d1db1 100644 --- a/script/impl/expansion/expansion_impl_with_cm.py +++ b/script/impl/expansion/expansion_impl_with_cm.py @@ -359,7 +359,10 @@ class ExpansionImplWithCm(ExpansionImpl): "\\\"%s='%s'\\\"" % (self.envFile, guc_path, "pgxc_node_name", self._get_pgxc_node_name_for_single_inst()) - su_cmd = """su - {0} -c "{1}" """.format(self.user, cmd) + if self.context.current_root_user: + su_cmd = """su - {0} -c "{1}" """.format(self.user, cmd) + else: + su_cmd = cmd self.logger.debug("Set guc parameter command: {0}".format(su_cmd)) status, output = subprocess.getstatusoutput(su_cmd) if status == 0: diff --git a/script/impl/preinstall/OLAP/PreinstallImplOLAP.py b/script/impl/preinstall/OLAP/PreinstallImplOLAP.py index 5c9e05d18b3c0307ba984a7421107577612f5774..4fcae5b3a368c8e9755f406bcc50d001550b37d1 100644 --- a/script/impl/preinstall/OLAP/PreinstallImplOLAP.py +++ b/script/impl/preinstall/OLAP/PreinstallImplOLAP.py @@ -314,7 +314,7 @@ class PreinstallImplOLAP(PreinstallImpl): """ self.context.clusterInfo.corePath = \ self.context.clusterInfo.readClustercorePath(self.context.xmlFile) - if not self.context.clusterInfo.corePath: + if not self.context.clusterInfo.corePath or not self.context.current_user_root: return self.context.logger.log("Setting Core file", "addStep") try: @@ -362,7 +362,8 @@ class PreinstallImplOLAP(PreinstallImpl): psshlib_path = os.path.join( os.path.dirname(__file__), "../../../gspylib/pssh/bin/TaskPool.py") - dest_path = "/usr/bin/" + # 将pssh放到om tools的bin目录下 + dest_path = os.path.join(self.context.clusterToolPath, "bin") secbox_path = "/var/chroot/usr/bin/" cmd = "cp %s %s %s %s" % ( pssh_path, pscp_path, psshlib_path, dest_path) @@ -399,13 +400,15 @@ class PreinstallImplOLAP(PreinstallImpl): self.context.logger.log("Setting host ip env", "addStep") try: # remove HOST_IP info with /etc/profile and environ - cmd = "sed -i '/^export[ ]*HOST_IP=/d' /etc/profile" - CmdExecutor.execCommandWithMode( - cmd, - self.context.sshTool, - self.context.localMode or self.context.isSingle) - if "HOST_IP" in os.environ.keys(): - os.environ.pop("HOST_IP") + if self.context.current_user_root: + # remove HOST_IP info with /etc/profile and environ + cmd = "sed -i '/^export[ ]*HOST_IP=/d' /etc/profile" + CmdExecutor.execCommandWithMode( + cmd, + self.context.sshTool, + self.context.localMode or self.context.isSingle) + if "HOST_IP" in os.environ.keys(): + os.environ.pop("HOST_IP") except Exception as e: raise Exception(str(e)) self.context.logger.log("Successfully set host ip env.", "constant") @@ -416,6 +419,8 @@ class PreinstallImplOLAP(PreinstallImpl): input: NA output: NA """ + if self.context.skip_cgroup_set or not self.context.current_user_root: + return self.context.logger.log("Setting Cgroup.", "addStep") try: # set the cgroup @@ -483,6 +488,8 @@ class PreinstallImplOLAP(PreinstallImpl): input: NA output: NA """ + if not self.context.current_user_root: + return # the flag for whether the virtual IP exists flag = 0 # all virtual IPs list diff --git a/script/impl/preinstall/PreinstallImpl.py b/script/impl/preinstall/PreinstallImpl.py index cbf3a8dac490378bc921ae28d520fad1ada42ea0..84ec292bdc855cf47a82e1c7ad39fabf34898931 100644 --- a/script/impl/preinstall/PreinstallImpl.py +++ b/script/impl/preinstall/PreinstallImpl.py @@ -171,7 +171,7 @@ class PreinstallImpl: try: # save the sshIps Ips = [] - # create trust for root + # # create trust for root or current user # get the user name username = pwd.getpwuid(os.getuid()).pw_name # get the user sshIps @@ -193,6 +193,12 @@ class PreinstallImpl: """ if self.context.preMode: return + if not self.context.current_user_root: + # Create user mutual trust, and after execution, no mutual trust will be created + self.context.create_user_ssh_trust = True + self.createTrustForCommonUser() + self.context.create_user_ssh_trust = False + return # Ask to create trust for root flag = input("Are you sure you want to create trust for root (yes/no)?") while True: @@ -235,7 +241,7 @@ class PreinstallImpl: return if self.context.preMode or not self.context.root_ssh_agent_flag: return - if not self.context.root_delete_flag: + if not self.context.root_delete_flag or not self.context.current_user_root: return self.context.logger.debug("Start Delete root mutual trust") @@ -342,8 +348,6 @@ class PreinstallImpl: self.context.logger.log("Distributing package.", "addStep") try: - PackageInfo.makeCompressedToolPackage(self.context.clusterToolPath) - # get the all node names in xml file hosts = self.context.clusterInfo.getClusterNodeNames() # remove the local node name @@ -554,7 +558,10 @@ class PreinstallImpl: cmd_type="python") # check enter permission - cmd = "su - %s -c 'cd '%s''" % (self.context.user, packageDir) + if self.context.current_user_root: + cmd = "su - %s -c 'cd '%s''" % (self.context.user, packageDir) + else: + cmd = "cd '%s'" % packageDir (status, output) = subprocess.getstatusoutput(cmd) # if cmd failed, then exit if status != 0: @@ -574,7 +581,10 @@ class PreinstallImpl: retry_time=15, waite_time=1, link=True) # check enter permission - cmd = "su - %s -c 'cd '%s''" % (self.context.user, user_dir) + if self.context.current_user_root: + cmd = "su - %s -c 'cd '%s''" % (self.context.user, user_dir) + else: + cmd = "cd '%s'" % user_dir (status, output) = subprocess.getstatusoutput(cmd) # if cmd failed, then exit if status != 0: @@ -590,7 +600,10 @@ class PreinstallImpl: # check enter permission log_file_dir = os.path.dirname(self.context.logger.logFile) - cmd = "su - %s -c 'cd '%s''" % (self.context.user, log_file_dir) + if self.context.current_user_root: + cmd = "su - %s -c 'cd '%s''" % (self.context.user, log_file_dir) + else: + cmd = "cd '%s'" % log_file_dir (status, output) = subprocess.getstatusoutput(cmd) # if cmd failed, then exit if status != 0: @@ -703,6 +716,8 @@ class PreinstallImpl: if createTrustFlag: return + if not self.context.current_user_root and not self.context.create_user_ssh_trust: + return self.context.logger.log( "Creating SSH trust for [%s] user." % self.context.user) try: @@ -763,11 +778,10 @@ class PreinstallImpl: output:NA hiden:NA """ + if self.context.localMode or not self.context.current_user_root: + return # single cluster also need to create user without local mode self.context.logger.debug("Creating OS user and create trust for user") - if self.context.localMode: - return - global createTrustFlag try: # check the interactive mode @@ -995,7 +1009,7 @@ class PreinstallImpl: namelist = ",".join(NodeNames) # check skip-os-set parameter - if self.context.skipOSSet: + if self.context.skipOSSet or not self.context.current_user_root: # check the OS parameters self.checkOSParameter(namelist) else: @@ -1088,15 +1102,15 @@ class PreinstallImpl: raise Exception( ErrorCode.GAUSS_524["GAUSS_52400"] + "\nPlease get more details by \"%s " - "-i A -h %s --detail\"." - % (gs_checkos_path, namelist)) + "-i A -h %s %s --detail\"." + % (gs_checkos_path, namelist, self.context.xmlFile)) if warning_num > 0: self.context.logger.log( "Warning: Installation environment " "contains some warning messages." + \ "\nPlease get more details by \"%s " - "-i A -h %s --detail\"." - % (gs_checkos_path, namelist)) + "-i A -h %s %s --detail\"." + % (gs_checkos_path, namelist, self.context.xmlFile)) except Exception as e: raise Exception(str(e)) @@ -1191,6 +1205,14 @@ class PreinstallImpl: self.context.logger.log("Successfully set the dynamic link library.", "constant") + def setArmOptimization(self): + """ + function: set arm optimization + input: NA + output: NA + """ + pass + def setCgroup(self): """ function: setting Cgroup @@ -1271,7 +1293,10 @@ class PreinstallImpl: os.path.join(dirName, "./../../../")) + "/" # check enter permission - cmd = "su - %s -c 'cd '%s''" % (self.context.user, packageDir) + if self.context.current_user_root: + cmd = "su - %s -c 'cd '%s''" % (self.context.user, packageDir) + else: + cmd = "cd '%s'" % packageDir (status, output) = subprocess.getstatusoutput(cmd) # if cmd failed, then exit if status != 0: @@ -1285,7 +1310,10 @@ class PreinstallImpl: # so we need check its exists if os.path.exists(user_dir): # check enter permission - cmd = "su - %s -c 'cd '%s''" % (self.context.user, user_dir) + if self.context.current_user_root: + cmd = "su - %s -c 'cd '%s''" % (self.context.user, user_dir) + else: + cmd = "cd '%s'" % user_dir (status, output) = subprocess.getstatusoutput(cmd) # if cmd failed, then exit if status != 0: @@ -1295,7 +1323,10 @@ class PreinstallImpl: # check enter permission log_file_dir = os.path.dirname(self.context.logger.logFile) - cmd = "su - %s -c 'cd '%s''" % (self.context.user, log_file_dir) + if self.context.current_user_root: + cmd = "su - %s -c 'cd '%s''" % (self.context.user, log_file_dir) + else: + cmd = "cd '%s'" % log_file_dir (status, output) = subprocess.getstatusoutput(cmd) # if cmd failed, then exit if status != 0: @@ -1383,7 +1414,8 @@ class PreinstallImpl: with open(source_file, 'r') as f: env_list = f.readlines() new_env_list = [] - if not self.context.mpprcFile: + # mpprcFile not exist + if not self.context.mpprcFile and self.context.current_user_root: with open(os.path.join("/etc", "profile"), "r") as etc_file: gp_home_env = etc_file.readlines() gphome_env_list = [env.replace('\n', '') for env in gp_home_env] @@ -1553,19 +1585,19 @@ class PreinstallImpl: """ :return: """ - if not self.context.user_ssh_agent_flag: - return self.context.logger.debug("Start set cron for %s" %self.context.user) tmp_path = ClusterConfigFile.readClusterTmpMppdbPath( self.context.user, self.context.xmlFile) gaussdb_tool_path = ClusterDir.getPreClusterToolPath(self.context.xmlFile) cron_file = "%s/gauss_cron_%s" % (tmp_path, self.context.user) - set_cron_cmd = "crontab -u %s -l > %s && " % (self.context.user, cron_file) + if self.context.current_user_root: + set_cron_cmd = "crontab -u %s -l > %s && " % (self.context.user, cron_file) + else: + set_cron_cmd = "crontab -l > %s && " % cron_file set_cron_cmd += "sed -i '/CheckSshAgent.py/d' %s;" % cron_file set_cron_cmd += "echo '*/1 * * * * source ~/.bashrc;python3 %s/script/local/CheckSshAgent.py >>/dev/null 2>&1 &' >> %s;" % (gaussdb_tool_path, cron_file) - - set_cron_cmd += "crontab -u %s %s;service cron restart;" % (self.context.user, cron_file) set_cron_cmd += "rm -f '%s'" % cron_file + set_cron_cmd += "crontab %s " % cron_file self.context.logger.debug("Command for setting CRON: %s" % set_cron_cmd) CmdExecutor.execCommandWithMode(set_cron_cmd, self.context.sshTool, @@ -1649,14 +1681,12 @@ class PreinstallImpl: self.setPssh() # set cgroup self.setCgroup() - + # set arm optimization self.setArmOptimization() # fix server package mode self.fixServerPackageOwner() - # unreg the disk of the dss and about self.dss_init() - # set user cron self.set_user_crontab() # set user env and a flag, diff --git a/script/local/PreInstallUtility.py b/script/local/PreInstallUtility.py index 69e59c32c3a73f8888d82c2a6dac381c0b1faf8a..91e5491a09e3ffd7f7c6da2a716d8a5de6873c6a 100644 --- a/script/local/PreInstallUtility.py +++ b/script/local/PreInstallUtility.py @@ -134,6 +134,15 @@ class PreInstall(LocalBaseOM): self.clusterAppPath = "" self.white_list = {} self.logger = None + self.current_user_root = False + + def get_current_user(self): + """ + get current user + """ + user_info = UserUtil.getUserInfo() + if user_info['uid'] == 0: + self.current_user_root = True def initGlobals(self): """ @@ -305,7 +314,7 @@ Common options: if self.logFile == "": self.logFile = ClusterLog.getOMLogPath( - ClusterConstants.LOCAL_LOG_FILE, self.user, "") + ClusterConstants.LOCAL_LOG_FILE, self.user, "", self.clusterConfig) def prepareMpprcFile(self): """ @@ -576,7 +585,10 @@ Common options: # report permisson error about GPHOME FileUtil.checkPathandChangeOwner(originalPath, self.user, DefaultValue.KEY_DIRECTORY_MODE) - cmd = "su - %s -c \"cd '%s'\"" % (username, originalPath) + if self.current_user_root: + cmd = "su - %s -c \"cd '%s'\"" % (username, originalPath) + else: + cmd = "cd '%s'" % originalPath status = subprocess.getstatusoutput(cmd)[0] if status != 0: return False @@ -585,14 +597,21 @@ Common options: return True testFile = os.path.join(originalPath, "touch.tst") - cmd = "su - %s -c 'touch %s && chmod %s %s' >/dev/null 2>&1" % ( - username, testFile, DefaultValue.KEY_FILE_MODE, testFile) + if self.current_user_root: + cmd = "su - %s -c 'touch %s && chmod %s %s' >/dev/null 2>&1" % ( + username, testFile, DefaultValue.KEY_FILE_MODE, testFile) + else: + cmd = "touch %s && chmod %s %s >/dev/null 2>&1" % ( + testFile, DefaultValue.KEY_FILE_MODE, testFile) status = subprocess.getstatusoutput(cmd)[0] if status != 0: return False - cmd = "su - %s -c 'echo aaa > %s' >/dev/null 2>&1" \ - % (username, testFile) + if self.current_user_root: + cmd = "su - %s -c 'echo test > %s' >/dev/null 2>&1" \ + % (username, testFile) + else: + cmd = "echo test > %s >/dev/null 2>&1" % testFile (status, output) = subprocess.getstatusoutput(cmd) if status != 0: cmd = "rm -f '%s' >/dev/null 2>&1" % testFile @@ -612,6 +631,8 @@ Common options: input : NA output: NA """ + if not self.current_user_root: + return self.logger.debug("Checking hostname mapping.") try: self.logger.debug("Change file[/etc/hosts] mode.") @@ -851,6 +872,7 @@ Common options: needCheckEmpty = True self.initNodeInfo() + self.prepare_om_tools_bin_path() self.prepareGaussLogPath() self.prepareInstallPath(needCheckEmpty) self.prepareTmpPath(needCheckEmpty) @@ -971,6 +993,25 @@ Common options: break self.logger.debug("Successfully created dss disk link.") + def prepare_om_tools_bin_path(self): + """ + function: Prepare om tools bin path + input : NA + output: NA + """ + self.logger.debug("Creating om tools bin path.") + bin_path = os.path.join(self.clusterToolPath, "bin") + # check bin path + self.logger.debug("Checking %s directory [%s]." % (VersionInfo.PRODUCT_NAME, bin_path)) + if not os.path.exists(bin_path): + self.makeDirsInRetryMode(bin_path, DefaultValue.MAX_DIRECTORY_MODE) + try: + FileUtil.checkLink(bin_path) + FileUtil.changeMode(DefaultValue.MAX_DIRECTORY_MODE, bin_path, False, "shell") + FileUtil.changeOwner(self.user, bin_path, False, "shell", link=True) + except Exception as e: + raise Exception(str(e)) + def prepareGaussLogPath(self): """ function: Prepare Gausslog Path @@ -1128,8 +1169,11 @@ Common options: if not needCheckEmpty: return upperDir = os.path.dirname(installPath) - cmd = "su - %s -c \"if [ -w %s ];then echo 1; else echo 0;fi\"" % ( - self.user, upperDir) + if self.current_user_root: + cmd = "su - %s -c \"if [ -w %s ];then echo 1; else echo 0;fi\"" % ( + self.user, upperDir) + else: + cmd = "if [ -w %s ];then echo 1; else echo 0;fi" % upperDir self.logger.debug( "Command to check if we have write permission for upper path:" " %s" % cmd) @@ -1168,6 +1212,8 @@ Common options: input : NA output: NA """ + if not self.current_user_root: + return self.logger.debug("Preparing user cron service.") ##1.set crontab file permission crontabFile = "/usr/bin/crontab" @@ -1226,6 +1272,8 @@ Common options: input : NA output: NA """ + if not self.current_user_root: + return self.logger.debug("Preparing user SSHD service.") sshd_config_file = "/etc/ssh/sshd_config" paramName = "MaxStartups" @@ -1387,8 +1435,10 @@ Common options: FileUtil.changeOwner(self.user, omLogPath, True, "shell", retry_flag=True, retry_time=15, waite_time=1, link=True) self.logger.debug("Checking the permission of GPHOME: %s." % user_dir) - cmd = g_file.SHELL_CMD_DICT["checkUserPermission"] % ( - self.user, user_dir) + if self.current_user_root: + cmd = "su - %s -c 'cd %s'" % (self.user, user_dir) + else: + cmd = "cd %s" % user_dir self.logger.debug("The command of check permission is: %s." % cmd) (status, output) = subprocess.getstatusoutput(cmd) if status != 0: @@ -1399,8 +1449,11 @@ Common options: # get the value of GAUSS_ENV self.logger.debug("Setting finish flag.") - cmd = "su - %s -c 'source %s;echo $GAUSS_ENV' 2>/dev/null" % ( - self.user, userProfile) + if self.current_user_root: + cmd = "su - %s -c 'source %s;echo $GAUSS_ENV' 2>/dev/null" % ( + self.user, userProfile) + else: + cmd = "source %s;echo $GAUSS_ENV 2>/dev/null" % userProfile (status, output) = subprocess.getstatusoutput(cmd) if status != 0: raise Exception(ErrorCode.GAUSS_514[ @@ -1421,11 +1474,17 @@ Common options: output: True/False """ if self.mpprcFile != "": - cmd = "su - root -c 'source %s;echo $GAUSS_ENV' 2>/dev/null" \ - % self.mpprcFile + if self.current_user_root: + cmd = "su - %s -c 'source %s;echo $GAUSS_ENV' 2>/dev/null" \ + % (self.user, self.mpprcFile) + else: + cmd = "source %s;echo $GAUSS_ENV 2>/dev/null" % self.mpprcFile else: - cmd = "su - %s -c 'source ~/.bashrc;echo $GAUSS_ENV' 2>/dev/null" \ - % self.user + if self.current_user_root: + cmd = "su - %s -c 'source ~/.bashrc;echo $GAUSS_ENV' 2>/dev/null" \ + % self.user + else: + cmd = "source ~/.bashrc;echo $GAUSS_ENV 2>/dev/null" status, output = subprocess.getstatusoutput(cmd) if status != 0: self.logger.debug( @@ -1503,16 +1562,16 @@ Common options: if self.mpprcFile != "": # have check its exists when check parameters, # so it should exist here - userProfile = self.mpprcFile + user_profile = self.mpprcFile else: # check if os profile exist - userProfile = ClusterConstants.ETC_PROFILE - if not os.path.exists(userProfile): + user_profile = ProfileFile.get_user_bashrc(self.user) + if not os.path.exists(user_profile): self.logger.debug( - "Profile does not exist. Please create %s." % userProfile) - FileUtil.createFile(userProfile) - FileUtil.changeMode(DefaultValue.DIRECTORY_MODE, userProfile) - return userProfile + "Profile does not exist. Please create %s." % user_profile) + FileUtil.createFile(user_profile) + FileUtil.changeMode(DefaultValue.DIRECTORY_MODE, user_profile) + return user_profile def setDBUerProfile(self): """ @@ -1556,7 +1615,7 @@ Common options: # clean GPHOME FileUtil.deleteLine(userProfile, "^\\s*export\\s*GPHOME=.*$") - # clean GPHOME + # clean UNPACKPATH FileUtil.deleteLine(userProfile, "^\\s*export\\s*UNPACKPATH=.*$") self.logger.debug( "Deleting crash GPHOME in user environment variables.") @@ -1673,6 +1732,8 @@ Common options: input : NA output: NA """ + if not self.current_user_root: + return self.logger.debug("Setting Library.") config_file_dir = "/etc/ld.so.conf" alreadySet = False @@ -2588,7 +2649,7 @@ Common options: """ package_path = get_package_path() om_root_path = os.path.dirname(package_path) - if om_root_path == DefaultValue.ROOT_SCRIPTS_PATH: + if om_root_path == DefaultValue.ROOT_SCRIPTS_PATH or not self.current_user_root: return self.logger.log("Separate om root scripts.") @@ -2674,15 +2735,15 @@ Common options: toolPath = self.clusterToolPath self.logger.log("change '%s' files permission and owner." % toolPath) FileUtil.changeOwner(self.user, toolPath, recursive=True, link=True) - FileUtil.changeMode(DefaultValue.KEY_DIRECTORY_MODE, + FileUtil.changeMode(DefaultValue.MAX_DIRECTORY_MODE, toolPath, recursive=True) FileUtil.changeMode(DefaultValue.SPE_FILE_MODE, "%s/script/gs_*" % toolPath) - FileUtil.changeMode(DefaultValue.MIN_FILE_MODE, "%s/*.sha256" % toolPath) - FileUtil.changeMode(DefaultValue.MIN_FILE_MODE, "%s/*.tar.gz" % toolPath) - FileUtil.changeMode(DefaultValue.MIN_FILE_MODE, "%s/*.tar.bz2" % + FileUtil.changeMode(DefaultValue.BIN_FILE_MODE, "%s/*.sha256" % toolPath) + FileUtil.changeMode(DefaultValue.BIN_FILE_MODE, "%s/*.tar.gz" % toolPath) + FileUtil.changeMode(DefaultValue.BIN_FILE_MODE, "%s/*.tar.bz2" % toolPath) - FileUtil.changeMode(DefaultValue.MIN_FILE_MODE, "%s/version.cfg" % + FileUtil.changeMode(DefaultValue.BIN_FILE_MODE, "%s/version.cfg" % toolPath) def fixop_package_path(self): @@ -2695,17 +2756,17 @@ Common options: gsom_path = os.path.dirname(package_path) if gsom_path != DefaultValue.ROOT_SCRIPTS_PATH: self.logger.log("Change file mode in path %s" % package_path) - FileUtil.changeOwner("root", package_path, recursive=True, link=True) + FileUtil.changeOwner(self.user, package_path, recursive=True, link=True) FileUtil.changeMode(DefaultValue.MAX_DIRECTORY_MODE, package_path) FileUtil.changeMode(DefaultValue.KEY_DIRECTORY_MODE, "%s/script" % package_path, recursive=True) - FileUtil.changeMode(DefaultValue.MIN_FILE_MODE, "%s/*.sha256" % + FileUtil.changeMode(DefaultValue.BIN_FILE_MODE, "%s/*.sha256" % package_path) - FileUtil.changeMode(DefaultValue.MIN_FILE_MODE, "%s/*.tar.gz" % + FileUtil.changeMode(DefaultValue.BIN_FILE_MODE, "%s/*.tar.gz" % package_path) - FileUtil.changeMode(DefaultValue.MIN_FILE_MODE, "%s/*.tar.bz2" % + FileUtil.changeMode(DefaultValue.BIN_FILE_MODE, "%s/*.tar.bz2" % package_path) - FileUtil.changeMode(DefaultValue.MIN_FILE_MODE, "%s/version.cfg" % + FileUtil.changeMode(DefaultValue.BIN_FILE_MODE, "%s/version.cfg" % package_path) def fix_dss_cap_permission(self): @@ -2941,6 +3002,7 @@ Common options: output : NA """ try: + self.get_current_user() self.parseCommandLine() self.checkParameter() self.initGlobals()