def exchange(self,vmList): m=1 key_mstr=[] mstr=RunCommand() key_slv='' ip_slv='' ip_mstr='' for vm in vmList: to_exec=vm.host+','+vm.user+','+vm.password if m==1: ip_mstr=vm.host mstr.do_add_host(to_exec) mstr.do_connect() key=mstr.do_run("cd .ssh \nssh-keygen -q -t rsa -f id_rsa -C '' -N ''") key_mstr=mstr.do_run('cat .ssh/id_rsa.pub') m=0 else: slv=RunCommand() slv.do_add_host(to_exec) slv.do_connect() print 'connected to slaves' exec_key_slv=slv.do_run("cd .ssh \nssh-keygen -q -t rsa -f id_rsa -C '' -N ''") key_slv+=slv.do_run('cat .ssh/id_rsa.pub')[0]+'\n' inject_slv=slv.do_run('echo '+key_mstr[0]+'>.ssh/authorized_keys') scan_slv=slv.do_run('ssh-keyscan '+ip_mstr+'>.ssh/known_hosts') ip_slv+=vm.host+'\n' inject_mstr=mstr.do_run("echo '"+key_slv+"'>.ssh/authorized_keys") prep_scan_mstr=mstr.do_run("echo '"+ip_slv+"'>hosts") scan_mstr=mstr.do_run("ssh-keyscan -f hosts>.ssh/known_hosts") return key_mstr[0]
def config(self,ipList,masterIp,user,pwd): cs=open('core.log', 'r') csList=cs.readlines() csList[6]=csList[6].replace('IP_ADRESS',masterIp) csCh=''.join(csList) print csCh m=1 ip_slv='\n'.join(ipList) hdp_home='/usr/local/hadoop-0.20.2' hst=masterIp+','+user+','+pwd slv=RunCommand() slv.do_add_host(hst) slv.do_connect() rt=slv.do_run("echo '"+csCh+"'>"+hdp_home+"/conf/core-site.xml") rt=slv.do_run("echo '"+ip_slv+"'>"+hdp_home+"/conf/slaves")
def config(self): keys=exchangeKeys() self.mstr_key=keys.exchange(self.listVM) cfgMstr=configMstr() cfgMstr.config(self.listSlv,self.mstr.host,self.user,self.pwd) cfgSlv=configSlv() cfgSlv.config(self.listSlv,self.mstr.host,self.user,self.pwd) # starting hadoop daemons strt=RunCommand() strt.do_add_host(self.mstr.host+','+self.user+','+self.pwd) strt.do_connect() cmd='' exec_strt=strt.do_run(cmd) strt.do_close()
def config(self, ipList, masterIp, user, pwd): cs = open("core.log", "r") csList = cs.readlines() csList[6] = csList[6].replace("IP_ADRESS", masterIp) csCh = "".join(csList) print csCh m = 1 ip_slv = "" hdp_home = "/usr/local/hadoop-0.20.2" for vm in ipList: hst = vm + "," + user + "," + pwd slv = RunCommand() slv.do_add_host(hst) slv.do_connect() rt = slv.do_run("echo '" + csCh + "'>" + hdp_home + "/conf/core-site.xml") rt = slv.do_run("echo '" + masterIp + "'>" + hdp_home + "/conf/masters")
class execution: def __init__(self): pass def prepare_exec(self): # extracting list of vms that will be used to extract values vmObj = TransformXmlToVM() vmObj.readXml("/home/khaled/listeVMsTest.xml") vms = vmObj.getVM() self.host = "%s,%s,%s" % (vms[0].host, vms[0].user, vms[0].password) print "VM:" print self.host # extracting of command from xml file: cmdCl = TransformXmlToCmd() cmdCl.readXml("/home/khaled/cmdCrTest.xml") # preapring list of indicator used listCr = [] listCr.append("disk") self.cmd = cmdCl.getcmd(listCr) # cmd is a list containing objects that contain the indicator and her command # print 'commande extraite' print self.cmd[0].cmd # preparing connexion to run commands self.cnx = RunCommand() # adding hosts extracted early self.cnx.do_add_host(self.host) self.cnx.do_connect() # executing command after its extracting and establishing connexion def execute(self, cmde): # Excuting command on remote host # the command is gotten from the previous method saved in the self vble r = self.cnx.do_run(self.cmd[0].cmd) print "affichage" print r[0] print "fin"
def exchange(self,mstr,slv,key): slv_hst=slv+','+mstr.user+','+mstr.password mstr_hst=mstr.host+','+mstr.user+','+mstr.password print mstr_hst print slv_hst key_slv='' slvC=RunCommand() slvC.do_add_host(slv_hst) slvC.do_connect() print 'connected to slave' exec_key_slv=slvC.do_run("cd .ssh \nssh-keygen -q -t rsa -f id_rsa -C '' -N ''") key_slv+=slvC.do_run('cat .ssh/id_rsa.pub')[0]+'\n' inject_slv=slvC.do_run('echo '+key+'>.ssh/authorized_keys') scan_slv=slvC.do_run('ssh-keyscan '+mstr.host+'>.ssh/known_hosts') slvC.do_close() mstrC=RunCommand() mstrC.do_add_host(mstr_hst) mstrC.do_connect() inject_mstr=mstrC.do_run("echo '"+key_slv+"'>>.ssh/authorized_keys") scan_mstr=mstrC.do_run('ssh-keyscan '+slv+'>>.ssh/known_hosts') mstrC.do_close()
def extract(self, host, cmde, cr): sumExt = 0 sumExtSqr = 0 sumCmpt = 0 sumCmptSqr = 0 nbMeasures = 3 # preparing connection to host exe = RunCommand() exe.do_add_host(host) logging.info("host added") exe.do_connect() logging.info("connection established") result = treat_result(cr) # executing commands for many times : nbMeasures for i in range(nbMeasures): # waiting and extracting value time.sleep(1) # execution of command res = exe.do_run(cmde.cmd) logging.info("command executed") # treatment of the extracted value valeur = result.return_result(res) logging.info("result of command treated") # determning of the sum of values dedicated for extension sumExt = sumExt + valeur.value sumExtSqr = sumExtSqr + math.pow(valeur.value, 2) # determining the sum of values dedicated for compacting sumCmpt = sumCmpt + valeur.complement sumCmptSqr = sumCmptSqr + math.pow(valeur.complement, 2) # determining the average and other parameters moyExt = sumExt / nbMeasures ecartTypeExt = sumExtSqr / nbMeasures - math.pow(moyExt, 2) moyCmpt = sumCmpt / nbMeasures ecartTypeCmpt = sumCmptSqr / nbMeasures - math.pow(moyCmpt, 2) # determining thresholds thresholdExt = float(cmde.tempsExt) * float(cmde.zExt) * ecartTypeExt thresholdCmpt = float(cmde.tempsCmpt) * float(cmde.zCmpt) * ecartTypeCmpt logging.info("consumption threshold determined") # extracting values to compare with thresholds res = exe.do_run(cmde.cmd) valeur = result.return_result(res) # creating the object that will be returned # this object contains value to compare and thresholds to_return = obj_cmp() to_return.cr = cr.lower() to_return.value = valeur to_return.maxExt = thresholdExt to_return.minCmpt = thresholdCmpt print "seuilExt ", thresholdExt print "seuilCmpt ", thresholdCmpt # closing connection to host exe.do_close() logging.info("connection to host closed") # returning obj to be compared return to_return