def startNodes(ami, inst_size, keyName, maxPrice, nodecnt): GF.log("... starting " + str(nodecnt) + " node(s)", 1); local=[] try: res = GF.run("ec2-request-spot-instances " + ami + " -p " + str(maxPrice) + " -instance-type " + inst_size + " -n " + str(nodecnt) + " --type one-time" + " --key " + keyName) lines=res.split("\n") for i in range(0,len(res.split("\n"))): line=lines[i] print "res: ",i,line if line.find("SPOTINSTANCEREQUEST")>=0: inst=line.split("\t") local.append(CLnode.CLnode( '' ,'slave' ,inst[5], '' , '' , '' ,inst[6] ,inst[0], '' ,False,inst[1],False)) #instID,instName,status , ami , key , size , date , ntype , url ,master,sir,deployed): if res.find("timeout")>=0: print "TIMEOUT: ", res sys.exit() if res.find("InvalidAMIID")>=0: print "INVALID AMI ID: ", res sys.exit() except Exception as x: print x, "\n", res sys.exit() GF.addNewNodes(local)
def launchCluster(ami, inst_size, keyName, maxPrice, nodes): GF.log("Maximum Price: "+str(maxPrice), 1); curPrice=curSpotCost(inst_size) if curPrice == -1: print "Error: Failed to get current spot price." sys.exit(-1) if curPrice > maxPrice: print "Error: Current spot price too high." sys.exit(-2) GF.log("Launching "+str(nodes)+" nodes.", 1); startNodes(ami, inst_size, keyName, maxPrice, nodes)
def buildBundle(payload, payloadDir): try: #RM old bundle res=GF.run("rm "+payload) GF.log("rm "+payload,1) #make new bundle res=GF.run("tar cvf "+payload+' '+payloadDir+"/*") GF.log("tar cvf "+payload+' '+payloadDir+"/*",1) except Exception as x: print x, "\n", res sys.exit()
def getRunningInstances(): try: res = GF.run("ec2-describe-instances") if res.find("timeout")>=0: print "TIMEOUT: ", res sys.exit() for line in res.split("\n"): if line.find("INSTANCE")>=0: inst=line.split("\t") if inst[5] != "terminated": GF.nodes.append(CLnode.CLnode(inst[1],inst[1],inst[5],inst[2],inst[6],inst[9],inst[10],inst[0],inst[3],'',inst[22])) else: GF.log("found terminated"+line,2) except Exception as x: print x, "\n", res sys.exit()
def curSpotCost(inst_size): lt = time.localtime(time.time()) curdate = str(lt[0])+"-"+str(lt[1])+"-"+str(lt[2])+"T"+str(lt[3])+":"+str(lt[4])+":"+str(lt[5])+":"+str(lt[6])+"-0000" try: res = GF.run("ec2-describe-spot-price-history -d Linux/UNIX --region us-east-1 --instance-type "+inst_size+" -s "+curdate) if res.find("timeout")>=0: print "TIMEOUT: ", res sys.exit() cost=0 for i in res.split("\n"): cost += float((i.split("\t"))[1]); cost = cost/len(res.split("\n")) except Exception as x: print x, "\n", res sys.exit() GF.log("Current Instance Cost: "+str(cost), 1); return cost
def launchMaster(ami, inst_size, keyName): GF.log("Launching Master node..",1) local=[] try: res = GF.run("ec2-run-instances " + ami + " -k " + keyName + " -t " + size) if res.find("InvalidAMIID")>=0: print "INVALID AMI ID: ", res sys.exit() print res i=0 lines=res.split("\n") master=CLnode.CLnode() for l in lines: inst=l.split("\t") if inst[0]=="INSTANCE": master = CLnode.CLnode(inst[1],"MASTER",inst[5],inst[2],inst[6],inst[9],inst[10],inst[0],'',True) master.desc_detail() local.append(master) GF.addNewNodes(local) except Exception as x: print x, "\n", res sys.exit()
output = None verbose = False loadState() for o, a in opts: if o in ("-d", "--debug"): GF.logLevel=2 elif o in ("-i", "--info"): GF.logLevel=1 elif o in ("-l", "--list"): cnt=0 for node in GF.nodes: if node.running() is True: cnt+=1 node.desc() GF.log("There are a total of "+str(cnt)+" instances running.",0) saveState() sys.exit() elif o in ("--listblock"): cnt=0 for node in GF.nodes: if node.running() is True: cnt+=1 node.desc_detail() GF.log("There are a totoal of "+str(cnt)+" instances running.",0) saveState() sys.exit() elif o in ("--listspots"): getSpotRequests() runcnt=0 ocnt=0