def checkPath(self, key, value): """ function: Check some path parameters input : key, value output: path """ # Check that the path parameter is a file try: if (not value): GaussLog.exitWithError( ErrorCode.GAUSS_500["GAUSS_50004"] % key[1:] + "Error:\noption %s requires argument" % key) path = str(value) g_file.checkFilePermission(path, True) return path except Exception as e: GaussLog.exitWithError(str(e))
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))