def main(): """ function: main function: 1.parse parameter 2.check $GAUSS_ENV input : NA output: NA """ try: (opts, args) = getopt.getopt(sys.argv[1:], "U:h:t:", ["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])) DBUser = "" checkInstall = "preinstall" for (key, value) in opts: if (key == "-h" or key == "--help"): usage() sys.exit(0) elif (key == "-U"): DBUser = value elif (key == "-t"): checkInstall = value else: GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % key) # check para vaild Parameter.checkParaVaild(key, value) # check user if (DBUser == ""): GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'U' + ".") try: execUser = pwd.getpwuid(os.getuid()).pw_name if (execUser != DBUser): GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50004"] % "U") # Check if user exists and if is the right user DefaultValue.checkUser(DBUser, False) except Exception as e: GaussLog.exitWithError(str(e)) # check if have done preinstall for this user cmd = "echo $GAUSS_ENV 2>/dev/null" (status, output) = subprocess.getstatusoutput(cmd) if (status != 0): GaussLog.exitWithError(ErrorCode.GAUSS_518["GAUSS_51802"] % "GAUSS_ENV") if checkInstall == "preinstall": if (output.strip() == PREINSTALL_FLAG or output.strip() == INSTALL_FLAG): GaussLog.printMessage("Successfully checked GAUSS_ENV.") sys.exit(0) else: GaussLog.exitWithError(ErrorCode.GAUSS_518["GAUSS_51805"] % "GAUSS_ENV") elif checkInstall == "install" and output.strip() == INSTALL_FLAG: GaussLog.exitWithError(ErrorCode.GAUSS_518["GAUSS_51806"])
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))