def new(self, geometry='', queue='', sessionname='', subnet='', vncpassword='', vncpassword_crypted='', vnc_id='', choices_string=''): logger.debug("new") if choices_string: print("choices string:", choices_string, file=sys.stderr) choices = json.loads(choices_string) res = self.rcm_server.pconfig.gui_composer.substitute(choices) for k, v in res.items(): print(k, ":::>", file=sys.stderr) print(v, file=sys.stderr) dummy_session = rcm.rcm_session() return dummy_session print("create new vnc display session") if (subnet): self.rcm_server.subnet = subnet if (queue): self.rcm_server.queue = queue if (sessionname): self.rcm_server.sessionname = sessionname if (vncpassword): self.rcm_server.vncpassword = vncpassword if (vncpassword_crypted): self.rcm_server.vncpassword_crypted = vncpassword_crypted self.rcm_server.substitutions['RCM_GEOMETRY'] = geometry self.rcm_server.set_vnc_setup(vnc_id) new_session = self.rcm_server.execute_new() new_session.write()
def get_queue(self, testJobScriptDict=None): logger.debug("get_queue") group = os.environ.get('ACCOUNT_NUMBER', os.path.basename(os.environ.get('WORK', ''))) logger.debug("account : " + group) self.substitutions['RCM_QSUBPAR_A'] = self.groupSubstitution( group, '-A $RCM_GROUP') #get list of possible queue (named "visual") queueList = [] if (not testJobScriptDict): testJobScriptDict = self.pconfig.get_testjobs() for key, value in testJobScriptDict.iteritems(): #print value #print key args = shlex.split( string.Template(value).safe_substitute(self.substitutions)) p1 = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p1.communicate() if len(stderr) == 0: queueList.append(key) self.kill_job(stdout.strip().split('.')[0]) return queueList
def vnc_subst(self, vnc_id): logger.debug("vnc_subst") subst = dict() for s in self.sections: if s.startswith('vnc_'): subst[s] = self.vnc_attribute(vnc_id, s) return subst
def cprex(self,cmd): logger.debug("cprex") (r,o,e)=self.prex(cmd) if (r != 0): print e raise Exception("command %s failed with error: %s" % ([cmd[0],cmd[-1]],e)) return (r,o,e)
def timeleft_string(self, sid): logger.debug("timeleft_string") if (self.no_timeleft): return self.notimeleft_string try: walltime_py24 = time.strptime(self.sessions[sid].hash['walltime'], "%H:%M:%S") endtime_py24 = time.strptime(self.sessions[sid].hash['created'], "%Y%m%d-%H:%M:%S") walltime = datetime.datetime(*walltime_py24[0:6]) endtime = datetime.datetime(*endtime_py24[0:6]) endtime = endtime + datetime.timedelta(hours=walltime.hour, minutes=walltime.minute, seconds=walltime.second) timedelta = endtime - datetime.datetime.now() #check if timedelta is positive if timedelta <= datetime.timedelta(0): timedelta = datetime.timedelta(0) timeleft = (((datetime.datetime.min + timedelta).time())).strftime("%H:%M:%S") return timeleft except Exception, inst: #sys.stderr.write("session->%s RCM:EXCEPTION %s: %s " % (sid,inst, traceback.format_exc())) #print("exception in getting time:",type(inst),inst.args,file=sys.stderr) return self.notimeleft_string
def get_queue(self,testJobScriptDict=None): logger.debug("get_queue") queueList = [] #tutti gli utenti possono sottomettere nella cosa serial?? #cineca deployment dependencies queueList.append("visual") return queueList
def handle(self, args=None): logger.debug("handle") if (args): (o, a) = CommandParser.parser.parse_args(args) else: (o, a) = CommandParser.parser.parse_args() options = o.__dict__ # print "options-->",options # options=o.Values fname = options['command'] if (fname): if (fname == ''): sys.stdout.write("please specify a command-->" + str(options)) return if (fname in self.functions.keys()): myfunc = self.functions[fname][0] myargs = dict() #collect all the relevant parameters for the function and pass it for p in self.functions[fname][1]: a = options.get(p, '') if (a != ''): myargs[p] = a # sys.stdout.write("calling "+fname+"-->" + str(myargs)) #debug_print myfile=open('/plx/userinternal/lcalori0/RCM/log.txt',"a") #debug_print myfile.write("calling "+fname+"-->" + str(myargs)+"\n") #debug_print myfile.close() myfunc(self.protocol, **myargs) else: sys.stdout.write("command " + fname + " undefined-->" + str(options)) else: sys.stderr.write("missing command option " + str(options))
def new_sid(self): self.to_delete() logger.debug("new_sid") n_err = len(self.sids['err']) n_run = len(self.sids['run']) n_end = len(self.sids['end']) n_ini = len(self.sids['ini']) n_loc = n_err + n_run + n_ini #locked: can't reuse these sid n_all = n_loc + n_end if (n_loc >= self.max_user_session): raise Exception( "ERROR: max %d sessions: no more available (%d running, %d errored, %d initializing).\n" % (self.max_user_session, n_run, n_err, n_ini)) else: if (n_all >= self.max_user_session): #need to recycle a sid, the oldest res = sorted( self.sids['end'], key=lambda sid: self.sessions[sid].hash['created'])[0] else: #pick an unused sid all = self.sids['err'] | self.sids['run'] | self.sids[ 'end'] | self.sids['ini'] for i in range(1, self.max_user_session + 1): sid = self.id2sid(i) if (sid not in all): res = sid break return res
def kill_job(self, jid): logger.debug("kill_job") #cprex(['qdel',jid]) #cprex(['kill '+ jid]) try: os.kill(int(jid), 9) except: raise Exception("Can not kill Xvnc process with pid: %s." % (jid))
def import_scheduler(self): logger.debug("import_scheduler") #sys.stderr.write("####### importing scheduler####-->"+self.scheduler_name) if (self.default_scheduler_name == self.scheduler_name): self.session_tag = self.hostname else: self.session_tag = self.scheduler_name exec("import rcm_server_" + self.scheduler_name + " as rcm_scheduler") self.scheduler = rcm_scheduler
def __init__(self): logger.debug("platform config init") baseconfig.__init__(self) self.confdict[('platform', 'nodepostfix')] = '' self.filename = 'platform.cfg' self.default_scheduler_name = 'ssh' self.parse() self.find_scheduler() self.import_scheduler()
def get_rcmdirs(self, U=False): logger.debug("get_rcmdirs") if (U): #cineca deployment dependencies udirs = glob.glob("/plx/user*/*/.rcm") else: #cineca deployment dependencies udirs = [os.path.expanduser("~%s/.rcm" % (self.username))] return (udirs)
def get_login(self, subnet=''): logger.debug("get_login") nodelogin = '' if (subnet): nodelogin = enumerate_interfaces.external_name(subnet) if (not nodelogin): nodelogin = socket.getfqdn() nodelogin = self.hack_login(subnet, nodelogin) return nodelogin
def external_name(subnet): logger.debug("external_name") ifs = all_interfaces() for i in ifs: ip = format_ip(i[1]) if (re.match('^' + subnet, ip)): name = socket.gethostbyaddr(ip)[0] if (name): return name return None
def list(self, subnet=''): logger.debug("list") self.rcm_server.subnet = subnet self.rcm_server.load_sessions() s = rcm.rcm_sessions() logger.debug("list run session ") for sid in self.rcm_server.sids['run']: s.add_session(self.rcm_server.sessions[sid]) s.write()
def get_checksum(build_platform): logger.debug("get_checksum") import ConfigParser config = ConfigParser.RawConfigParser() myPath = os.path.dirname(os.path.abspath(__file__)) config.read(os.path.join(myPath, 'versionRCM.cfg')) checksum = config.get('checksum', build_platform) downloadurl = config.get('url', build_platform) return ((checksum, downloadurl))
def clean_files(self, sid): logger.debug("clean_files sid:" + str(sid)) for d in self.get_rcmdirs(): logger.debug("clean_files:" + d) if (not os.path.isdir(d)): os.mkdir(d) os.chmod(d, 0755) self.desktop_setup() for f in glob.glob("%s/%s.*" % (d, sid)): os.remove(f)
def list(self, subnet=''): logger.debug("list") self.rcm_server.subnet = subnet self.rcm_server.load_sessions() s = rcm.rcm_sessions() for sid in self.rcm_server.sids['run']: s.array.append(self.rcm_server.sessions[sid]) sys.stdout.write(rcm.serverOutputString) sys.stdout.write(s.get_string()) sys.stdout.flush()
def prex(self,cmd): logger.debug("prex") cmdstring=cmd[0] for p in cmd[1:]: cmdstring+=" '%s'" % (p) #sys.stderr.write("Executing: %s\n" % (cmdstring) ) logger.debug("Popen cmd"+str(cmd)) myprocess = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) stdout,stderr = myprocess.communicate() myprocess.wait() return (myprocess.returncode,stdout,stderr)
def loginlist(self, subnet=''): logger.debug("loginlist") # import socket self.rcm_server.subnet = subnet # fullhostname = socket.getfqdn() self.rcm_server.fill_sessions_hash() s = rcm.rcm_sessions() for sid, ses in list(self.rcm_server.sessions.items()): s.add_session(ses) #s.array.append(self.rcm_server.sessions[sid]) s.write()
def getQueueGroup(self,queue): logger.debug("getQueueGroup") self.accountList = self.getUserAccounts() if len(self.accountList) == 0: return '' else: #cineca deployment dependencies if( 'cin' in self.username): group="cinstaff" else: group="cin_visual" return group
def loginlist(self, subnet=''): logger.debug("loginlist") # import socket self.rcm_server.subnet = subnet # fullhostname = socket.getfqdn() self.rcm_server.fill_sessions_hash() s = rcm.rcm_sessions() for sid, ses in list(self.rcm_server.sessions.items()): s.array.append(self.rcm_server.sessions[sid]) sys.stdout.write(rcm.serverOutputString) sys.stdout.write(s.get_string()) sys.stdout.flush()
def vnc_attrib_subst(self, vnc_id, section, subst=dict()): logger.debug("vnc_attrib_subst") if self.sections.has_key(section): v_subst = self.vnc_subst(vnc_id) found = v_subst.get(section, None) if (found): f1 = string.Template(found).safe_substitute(subst) us = dict() for s in v_subst: us[s] = string.Template(v_subst[s]).safe_substitute(subst) ret = string.Template(f1).safe_substitute(us) return ret return ''
def config(self, build_platform=''): logger.debug("config") conf = rcm.rcm_config() if (build_platform): (check, url) = self.rcm_server.get_checksum(build_platform) conf.set_version(check, url) queueList = self.rcm_server.get_queue() for q in queueList: conf.add_queue(q) for vnc_id, menu_entry in self.rcm_server.pconfig.get_vnc_menu().items( ): conf.add_vnc(vnc_id, menu_entry) conf.serialize()
def submit_job(self, sid, rcm_dirs, jobScript): logger.debug("submit_job") #cineca deployment dependencies self.ssh_template = jobScript #""" ##!/bin/bash # #. /cineca/prod/environment/module/3.1.6/none/init/bash #module purge #module load profile/advanced #module load turbovnc # #$RCM_CLEANPIDS # #echo -e $RCM_VNCPASSWD | vncpasswd -f > $RCM_JOBLOG.pwd # #$RCM_VNCSERVER -otp -rfbauth $RCM_JOBLOG.pwd > $RCM_JOBLOG.vnc 2>&1 #cat `ls -tr ~/.vnc/*.pid | tail -1` #""" s = string.Template(self.ssh_template) print s otp = '%s/%s.otp' % (rcm_dirs[0], sid) if (os.path.isfile(otp)): os.remove(otp) file = '%s/%s.job' % (rcm_dirs[0], sid) fileout = '%s/%s.joblog' % (rcm_dirs[0], sid) self.substitutions['RCM_JOBLOG'] = fileout self.substitutions['RCM_VNCSERVER'] = string.Template( self.substitutions['RCM_VNCSERVER']).safe_substitute( self.substitutions) self.substitutions['RCM_CLEANPIDS'] = self.clean_pids_string self.substitutions['RCM_VNCPASSWD'] = self.vncpassword batch = s.safe_substitute(self.substitutions) print "batch------------------------\n", batch # batch=s.safe_substitute(RCM_MODULE_SETUP=self.vnc_setup,RCM_JOBLOG=fileout,RCM_VNCSERVER=self.vncserver_string,RCM_CLEANPIDS=self.clean_pids_string,RCM_VNCPASSWD=self.vncpassword) f = open(file, 'w') f.write(batch) f.close() os.chmod(file, stat.S_IRWXU) (res, out, err) = self.cprex([file]) if (res != 0): sys.write.stderr(err) raise Exception('Creation of remote display failed: ' + str(err)) else: return out.rstrip() #out is the pid of Xvnc
def get_vnc_menu(self): logger.debug("get_vnc_menu") menu = dict() for vnc_id in self.sections.get('vnc_menu', []): menu_item_string = self.confdict[('vnc_menu', vnc_id)] m = menu_item_string.split('|', 1) if (len(m) > 1): item = m[0] info = m[1] else: item = vnc_id info = menu_item_string menu[vnc_id] = (item, info) return (menu)
def kill(self, session_id=''): logger.debug("kill") self.rcm_server.load_sessions() if (session_id): if session_id in self.rcm_server.sids['run']: jid = self.rcm_server.sessions[session_id].hash['jobid'] self.rcm_server.kill_job(jid) file = '%s/%s.session' % (self.rcm_server.get_rcmdirs()[0], session_id) c = rcm.rcm_session(fromfile=file) c.hash['state'] = 'killing' c.serialize(file) else: sys.stderr.write("Not running session: %s\n" % (session_id)) sys.stderr.flush() sys.exit(1)
def fill_sessions_hash(self, U=False): logger.debug("fill_sessions_hash") udirs = self.get_rcmdirs(U) if (U): ure = '\w+' else: ure = self.username #read sessions files r = re.compile(r'(?P<sid>(?P<user>%s)-(?P<tag>\S+)-\d+)\.session$' % ure) self.sessions = {} for d in udirs: if os.path.isdir(d) and os.access(d, os.R_OK): for f in os.listdir(d): ro = r.match(f) if ro: file = d + '/' + f user = ro.group(2) sid = ro.group(1) tag = ro.group(3) logger.debug("session file-- " + file) try: logger.debug("prima di fromfile:" + file) self.sessions[sid] = rcm.rcm_session(fromfile=file) logger.debug("dopo di fromfile:" + file) logger.debug("file:" + self.sessions[sid].hash['file']) #need the following lines to map nodes with different hostname from different subnet originalNodeLogin = self.sessions[sid].hash.get( 'nodelogin', '') if (self.subnet != '' and originalNodeLogin != ''): #originalNodeLogin = self.sessions[sid].hash['nodelogin'] self.sessions[sid].hash[ 'nodelogin'] = self.pconfig.confdict.get( (self.subnet, originalNodeLogin), originalNodeLogin) self.sessions[sid].hash[ 'timeleft'] = self.timeleft_string(sid) except Exception, inst: #print("WARNING: not valid session file %s: %s\n" % (file, e),type(inst),inst.args,file=sys.stderr) sys.stderr.write("%s: %s RCM:EXCEPTION" % (inst, traceback.format_exc()))
def config(self, build_platform=''): logger.debug("config") conf = rcm.rcm_config() if (build_platform): (check, url) = self.rcm_server.get_checksum(build_platform) conf.set_version(check, url) queueList = self.rcm_server.get_queue() for q in queueList: conf.add_queue(q) for vnc_id, menu_entry in self.rcm_server.pconfig.get_vnc_menu().items( ): conf.add_vnc(vnc_id, menu_entry) # ########## new interface ############ jobscript_json_menu = self.rcm_server.pconfig.get_jobscript_json_menu() if jobscript_json_menu: conf.config['jobscript_json_menu'] = jobscript_json_menu # ##################################### conf.serialize()
def getUserAccounts(self): logger.debug("getUserAccounts") #cineca deployment dependencies try: p1 = subprocess.Popen(["saldo", "-nb"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p1.communicate() if 'not existing' in stdout: #old type user return [] else: #now return a fixed group for experimentation #cineca deployment dependencies return ['cin_visual'] except Exception, inst: #sys.stderr.write("session->%s RCM:EXCEPTION %s: %s " % (sid,inst, traceback.format_exc())) return []