📄 mpdboot.py
字号:
ips = gethostbyname_ex(oldhost)[2] # may fail if invalid host except: print 'unable to obtain IP for host:', oldhost continue uips = {} # unique ips for ip in ips: uips[ip] = 1 keep = 1 for ip in uips.keys(): if cachedIPs.has_key(ip): keep = 0 break if keep: hostsAndInfo.append(hostAndInfo) cachedIPs.update(uips) if len(hostsAndInfo) < totalnumToStart: # one is local print 'totalnum=%d numhosts=%d' % (totalnumToStart,len(hostsAndInfo)) print 'there are not enough hosts on which to start all processes' exit(-1) if chkupIndicator: hostsToCheck = [ hai['host'] for hai in hostsAndInfo[1:totalnumToStart] ] (upList,dnList) = chkupdn(hostsToCheck) if dnList: print "these hosts are down; exiting" print dnList exit(-1) print "there are %d hosts up (counting local)" % (len(upList)+1) if chkupIndicator == 2: # do the chkup and quit exit(0) try: # stop current (if any) mpds; ignore the output getoutput('%s/mpdallexit.py' % (fullDirName)) if verbose or debug: print 'running mpdallexit on %s' % (myHost) except: pass if myIfhn: ifhn = '--ifhn=%s' % (myIfhn) else: ifhn = '' hostsAndInfo[0]['entry_host'] = '' hostsAndInfo[0]['entry_port'] = '' mpdArgs = '%s %s --ncpus=%d' % (localConArg,ifhn,myNcpus) (mpdPID,mpdFD) = launch_one_mpd(0,0,mpdArgs,hostsAndInfo) fd2idx = {mpdFD : 0} handle_mpd_output(mpdFD,fd2idx,hostsAndInfo) try: from os import sysconf maxfds = sysconf('SC_OPEN_MAX') except: maxfds = 1024 maxAtOnce = min(128,maxfds-8) # -8 for stdeout, etc. + a few more for padding hostsSeen = { myHost : 1 } fdsToSelect = [] numStarted = 1 # local already going numStarting = 0 numUnderCurrRoot = 0 possRoots = [] currRoot = 0 idxToStart = 1 # local mpd already going while numStarted < totalnumToStart: if numStarting < maxAtOnce and idxToStart < totalnumToStart: if numUnderCurrRoot < maxUnderOneRoot: entryHost = hostsAndInfo[currRoot]['host'] entryPort = hostsAndInfo[currRoot]['list_port'] hostsAndInfo[idxToStart]['entry_host'] = entryHost hostsAndInfo[idxToStart]['entry_port'] = entryPort if hostsSeen.has_key(hostsAndInfo[idxToStart]['host']): remoteConArg = '-n' myNcpus = hostsAndInfo[idxToStart]['ncpus'] ifhn = hostsAndInfo[idxToStart]['ifhn'] if ifhn: ifhn = '--ifhn=%s' % (ifhn) mpdArgs = '%s -h %s -p %s %s --ncpus=%d' % (remoteConArg,entryHost,entryPort,ifhn,myNcpus) (mpdPID,mpdFD) = launch_one_mpd(idxToStart,currRoot,mpdArgs,hostsAndInfo) numStarting += 1 numUnderCurrRoot += 1 hostsAndInfo[idxToStart]['pid'] = mpdPID hostsSeen[hostsAndInfo[idxToStart]['host']] = 1 fd2idx[mpdFD] = idxToStart fdsToSelect.append(mpdFD) idxToStart += 1 else: if possRoots: currRoot = possRoots.pop() numUnderCurrRoot = 0 selectTime = 0.01 else: selectTime = 0.1 try: (readyFDs,unused1,unused2) = select(fdsToSelect,[],[],selectTime) except error, errmsg: mpd_print(1,'mpdboot: select failed: errmsg=:%s:' % (errmsg) ) exit(-1) for fd in readyFDs: handle_mpd_output(fd,fd2idx,hostsAndInfo) numStarted += 1 numStarting -= 1 possRoots.append(fd2idx[fd]) fdsToSelect.remove(fd) fd.close()def launch_one_mpd(idxToStart,currRoot,mpdArgs,hostsAndInfo): global myHost, fullDirName, rshCmd, user, mpdCmd, debug, verbose mpdHost = hostsAndInfo[idxToStart]['host'] if idxToStart == 0: cmd = '%s %s -e -d' % (mpdCmd,mpdArgs) else: if rshCmd == 'ssh': rshArgs = '-x -n -q' else: rshArgs = '-n' mpdHost = hostsAndInfo[idxToStart]['host'] cmd = "%s %s %s '%s %s -e -d' " % \ (rshCmd,rshArgs,mpdHost,mpdCmd,mpdArgs) if verbose: entryHost = hostsAndInfo[idxToStart]['entry_host'] entryPort = hostsAndInfo[idxToStart]['entry_port'] # print "LAUNCHED mpd on %s via %s %s" % (mpdHost,entryHost,str(entryPort)) print "LAUNCHED mpd on %s via %s" % (mpdHost,entryHost) if debug: print "debug: launch cmd=", cmd mpd = Popen4(cmd,0) mpdFD = mpd.fromchild mpdPID = mpd.pid return (mpdPID,mpdFD)def handle_mpd_output(fd,fd2idx,hostsAndInfo): global myHost, fullDirName, rshCmd, user, mpdCmd, debug, verbose idx = fd2idx[fd] host = hostsAndInfo[idx]['host'] # port = fd.readline().strip() port = 'no_port' for line in fd.readlines(): # handle output from shells that echo stuff line = line.strip() splitLine = line.split('=') if splitLine[0] == 'mpd_port': port = splitLine[1] break if debug: print "debug: mpd on %s on port %s" % (host,port) if port.isdigit(): hostsAndInfo[idx]['list_port'] = int(port) tempSock = MPDSock(name='temp_to_mpd') try: tempSock.connect((host,int(port))) except: tempSock.close() tempSock = 0 if tempSock: msgToSend = { 'cmd' : 'ping', 'ifhn' : 'dummy', 'port' : 0} tempSock.send_dict_msg(msgToSend) msg = tempSock.recv_dict_msg() # RMB: WITH TIMEOUT ?? if not msg or not msg.has_key('cmd') or msg['cmd'] != 'challenge': mpd_print(1,'failed to handshake with mpd on %s; recvd output=%s' % \ (host,msg) ) tempOut = tempSock.recv(1000) print tempOut try: getoutput('%s/mpdallexit.py' % (fullDirName)) except: pass exit(-1) tempSock.close() else: mpd_print(1,'failed to connect to mpd on %s' % (host) ) try: getoutput('%s/mpdallexit.py' % (fullDirName)) except: pass exit(-1) else: mpd_print(1,'from mpd on %s, invalid port info:' % (host) ) print port print fd.read() try: getoutput('%s/mpdallexit.py' % (fullDirName)) except: pass exit(-1) if verbose: print "RUNNING: mpd on", hostsAndInfo[fd2idx[fd]]['host'] if debug: print "debug: info for running mpd:", hostsAndInfo[fd2idx[fd]]def chkupdn(hostList): upList = [] dnList = [] for hostname in hostList: print 'checking', hostname if rshCmd == 'ssh': rshArgs = '-x -n' else: rshArgs = '-n' cmd = "%s %s %s /bin/echo hello" % (rshCmd,rshArgs,hostname) runner = Popen3(cmd,1,0) runout = runner.fromchild runerr = runner.childerr runin = runner.tochild runpid = runner.pid up = 0 try: # (readyFDs,unused1,unused2) = select([runout,runerr],[],[],9) (readyFDs,unused1,unused2) = select([runout],[],[],9) except: print 'select failed' readyFDs = [] for fd in readyFDs: # may have runout and runerr sometimes line = fd.readline() if line and line.startswith('hello'): up = 1 else: pass if up: upList.append(hostname) else: dnList.append(hostname) try: kill(runpid,SIGKILL) except: pass return(upList,dnList)def usage(): print __doc__ stdout.flush() exit(-1) if __name__ == '__main__': mpdboot()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -