def authenticate(self, username): authtool = "pchangeuser" authcmd = "%s %s" % (authtool, username) fullcmd = "%s/%s 2>&1" % (self.ToolsPath, authcmd) dprint("Executing authcmd: %s\n" % fullcmd) dprint("Enter Petashare Password: "******"" while True: line = fd.readline() if not line: break output = "%s%s" % (output, line) fd.close() dprint("\n") sys.stdout.flush() if output.count("rcAuthResponse failed") > 0: lines = output.split("\n") for line in lines: if line.count("rcAuthResponse failed") > 0: dprint("Error: could not authenticate to petashare: %s" % line) return -1 return 0
def GetUsername(): if "USER" not in os.environ: fd = simenv.popen("whoami") username = fd.read().strip() fd.close() else: username = os.environ["USER"] return username
def cmd(self, cmd): fullcmd = "%s/%s 2>&1" % (self.ToolsPath, cmd) dprint("Executing petashare command: %s" % fullcmd) fd = simenv.popen(fullcmd) output = "" while True: line = fd.readline() if not line: break output = "%s%s" % (output, line) fd.close() return output
def CreateSimulationId(simulationName): (machine, machineEntry, sourceBaseDir) = simlib.GetLocalEnvironment() hostname = machineEntry.hostname # make sure fd is closed. fd = simenv.popen('whoami') user = fd.read().strip() fd.close() tt = time.localtime() timestamp = "%4d.%02d.%02d-%02d.%02d.%02d" % ( tt.tm_year, tt.tm_mon, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec) pid = os.getpid() simulation_id = "simulation-%s-%s-%s-%s-%s-%s" % ( simulationName, machine, hostname, user, timestamp, pid) return simulation_id
def GetHostNameAlias(): # first check if our hostname was specified via an option if simenv.OptionsManager.HasOption('hostname'): return simenv.OptionsManager.GetOption('hostname') # get the users home directory, check for ~/.hostname homedir = os.environ['HOME'] home_hostname = "%s%s%s" % (homedir, os.sep, ".hostname") if FileExists(home_hostname): hostname = GetFileContents(home_hostname).strip() return hostname # lets get the hostname fd = simenv.popen("hostname") hostname = fd.read().strip() fd.close() try: (name, aliases, _) = socket.gethostbyname_ex(hostname) except: # warning("could not resolve hostname %s" % hostname) return hostname # out of all our names, find one that has a dot, if possible. names = aliases names.insert(0, name) names.insert(0, hostname) names = filter(lambda n: not (re.match("localhost", n)), names) for n in names: if n.count("."): return n if len(name) > 0: return name return hostname
def list_simulations(): simlib.RequireMachine() if len(simenv.OptionsManager.args): simulations = simenv.OptionsManager.args else: simulations = simlib.GetSimulations() if len(simulations) == 0: display("There are no simulations") return simulations.sort() if simenv.VERBOSE: display("Simulations:") if simenv.OptionsManager.GetOption('name-only'): for sim in simulations: print sim return for sim in simulations: restart = simrestart.SimRestart() restart.load(sim) restartIds = restartlib.GetRestartIds(restart) activeId = restartlib.GetActiveRestartId(restart, warnOnly=True) if activeId == -2: display(" %-21s [ERROR, restart %04d]" % (sim, int(activeId))) continue active = activeId >= 0 if not active and len(restartIds) > 0: # Use the last restart if there is no active restart activeId = restartIds[len(restartIds) - 1] if activeId >= 0: ret = restart.load(sim, activeId) if ret < 0: # Something is wrong with the active restart display(" %-21s [ERROR, restart %04d]" % (sim, int(activeId))) continue job_id = -1 if activeId >= 0: job_id = restart.GetJobId() # TODO: The number of presubmitted restarts should also be # output. if active: state_map = dict() state_map['H'] = 'ACTIVE (PRESUBMITTED)' state_map['Q'] = 'ACTIVE (QUEUED)' state_map['R'] = 'ACTIVE (RUNNING)' state_map['U'] = 'ACTIVE (FINISHED)' state_map['E'] = 'ERROR' job_status = restartlib.GetJobStatus(job_id) state = state_map[job_status] if not state: error("Illegal job status '%s'" % job_status) else: state = 'INACTIVE' if not simenv.OptionsManager.GetOption('long'): display(" %-21s [%-9s, restart %04d, job id %s]" % (sim, state, int(activeId), job_id)) else: display(" %-21s= %s" % (sim, state)) # Active Restart Id display(" %-16s= %04d" % ("restart id", int(activeId))) # Job display(" %-16s= %s" % ("job id", job_id)) # Disk usage fd = simenv.popen("du -sk %s | cut -f1" % restart.SimulationDir) size_in_k = int(fd.read().strip()) fd.close() display(" %-16s= %.1f GByte" % ("Disk usage", size_in_k * 1024 / 1.0e+9)) # Properties #PrintManyLeadingSpace(restart.Properties.toString(), 8) if restart: restart.done()