def vm_temp_check(self): if self.temp=="": colpt.ptred("Please give vm template") #sys.exit() return "1" else: colpt.ptgreen("template check pass")
def getTemp(filename,url): a=filename md5_file=a+".md5" b=url dlcmd="wget "+b+a+" -O /datapool/"+a+" -c " dlcmdmd5="wget "+b+a+".md5 -O /datapool/"+a+".md5 -c " #print dlcmd colpt.ptred("vm images not exists,Will download it ,Please wait a moment") #sys.exit(1) #p=os.popen(dlcmd).read() #status, output = commands.getstatusoutput(dlcmd) #statusmd5, outputmd5 = commands.getstatusoutput(dlcmdmd5) os.popen(dlcmd) os.popen(dlcmdmd5) colpt.ptgreen("**********************************************") i=1 chang_md5_file(md5_file) #print str(status)+" is wget status !" #while (status != 0): # status, output = commands.getstatusoutput(dlcmd) # print str(status)+" is wget status ! and times is "+str(i) # i=i+1 # if (i > 5): # print "vm images download failed" # return 10 # break return 0
def getTemp(filename,url): a=filename # md5_file=a+".md5" b=url dlcmd="wget "+b+a+" -O /datapool/"+a+" -c " # dlcmdmd5="wget "+b+a+".md5 -O /datapool/"+a+".md5 -c " logger.debug(dlcmd) # logger.debug(dlcmdmd5) colpt.ptred("vm images not exists,Will download it ,Please wait a moment") #sys.exit(1) #p=os.popen(dlcmd).read() #status, output = commands.getstatusoutput(dlcmd) #statusmd5, outputmd5 = commands.getstatusoutput(dlcmdmd5) os.popen(dlcmd) # os.popen(dlcmdmd5) colpt.ptgreen("**********************************************") i=1 # chang_md5_file(md5_file) #print str(status)+" is wget status !" #while (status != 0): # status, output = commands.getstatusoutput(dlcmd) # print str(status)+" is wget status ! and times is "+str(i) # i=i+1 # if (i > 5): # print "vm images download failed" # return 10 # break return 0
def getopts(): helpinfo = """ -h, --help print this --vg,assige vg name,such as --vg=datavg,vg=vmVG default vg name is 'datavg' if not assige --config,assige config file name ,such as --config=vm.csv config file must in same directory and must be csv default config file name is 'vm.csv' if not assige --url,give path to download vm images,such as --url=ftp://user1:[email protected]/ """ global vg opts, args = getopt.getopt(sys.argv[1:], "hc:", ["vg=", "config=", "help", "url="]) if opts == []: colpt.ptgreen(helpinfo) colpt.ptgreen("Will run in default!") for o, a in opts: if o in ("-h", "--help"): colpt.ptgreen(helpinfo) sys.exit(12) elif o == "--vg": vg = a b = "vg name is " + a colpt.ptgreen(b) elif o == "--config": pass elif o == "--url": vmurl = a b = "url is " + a colpt.ptgreen(b) else: print "wrong argument ,pleale check again" sys.exit(11) assert False, "unhand option"
def vm_name_check(self): if self.name=="": colpt.ptred("Please give vm name") #sys.exit() return "1" else: colpt.ptgreen("name check pass")
def getopts(): helpinfo=""" -h, --help print this --vg,assige vg name,such as --vg=datavg,vg=vmVG default vg name is 'datavg' if not assige --config,assige config file name ,such as --config=vm.csv config file must in same directory and must be csv default config file name is 'vm.csv' if not assige --url,give path to download vm images,such as --url=ftp://user1:[email protected]/ """ global vg opts,args=getopt.getopt(sys.argv[1:],"hc:",["vg=","config=","help","url="]) if opts==[]: colpt.ptgreen(helpinfo) colpt.ptgreen("Will run in default!") for o,a in opts: if o in ("-h","--help"): colpt.ptgreen(helpinfo) sys.exit(12) elif o=="--vg": vg=a b="vg name is "+a colpt.ptgreen(b) elif o=="--config": pass elif o=="--url": vmurl=a b="url is "+a colpt.ptgreen(b) else: print "wrong argument ,pleale check again" sys.exit(11) assert False,"unhand option"
def vm_os_check(self): print "check vm os ......" checkos="virt-inspector /datapool/"+self.temp a=os.popen(checkos) c=a.read() b=c[0:1024] colpt.ptgreen("virt-inspector Running:") colpt.ptgreen(b[0:100]) if (string.find(b,"2003")>0): self.os="2003" elif(string.find(b,"2008")>0): self.os="2008" elif(string.find(b,"2012")>0): self.os="2008" elif(string.find(b,"centos")>=0): self.os="linux" else: #check whether os is Ubuntu deepcheckos="virt-inspector /datapool/"+self.temp t=os.popen(deepcheckos) r=t.read() print r[0:100] if (string.find(r,"ubuntu")>0): self.os="ubuntu" else: print "os is unknow" print "os is "+self.os
def vm_disk1_size_check(self): if self.disk1_size=="": colpt.ptred("Please give vm disk1 size") #sys.exit() return "1" else: colpt.ptgreen("disk1 size check pass")
def vm_xmlfile_create2(self): vmfile="/datapool/"+self.name+".xml" tmp_cpu_capabilities="virsh capabilities |grep pentium3" tmp_cpu_info=os.popen(tmp_cpu_capabilities).read() print tmp_cpu_info if (string.find(tmp_cpu_info,"pentium3")>=0): if (self.vgname=="none" or self.vgname=="cp"): tmp_xml2=" virt-install --name="+self.name+" --vcpus="+self.cpu+" --cpu host-passthrough --ram="+self.mem+" --disk path="+self.vda+\ ",bus=virtio,cache=writethrough,format=qcow2,io=native"+\ " --disk path="+self.vdb+\ ",bus=virtio,cache=writethrough,format=qcow2,io=native"+\ " --network bridge="+self.out_bridge+",model="+self.out_type+",mac="+self.out_mac+" --network bridge="+self.in_bridge+\ ",model="+self.in_type+",mac="+self.in_mac+" --vnc --vncport="+self.vnc_port+" --import --hvm --virt-type kvm "+\ " --print-xml>"+vmfile else: tmp_xml2=" virt-install --name="+self.name+" --vcpus="+self.cpu+" --cpu host-passthrough --ram="+self.mem+" --disk path="+self.vda+\ ",bus=virtio,cache=writethrough,format=raw,io=native"+\ " --disk path="+self.vdb+\ ",bus=virtio,cache=writethrough,format=raw,io=native"+\ " --network bridge="+self.out_bridge+",model="+self.out_type+",mac="+self.out_mac+" --network bridge="+self.in_bridge+\ ",model="+self.in_type+",mac="+self.in_mac+" --vnc --vncport="+self.vnc_port+" --import --hvm --virt-type kvm "+\ " --print-xml>"+vmfile else: if (self.vgname=="none" or self.vgname=="cp"): tmp_xml2=" virt-install --name="+self.name+" --vcpus="+self.cpu+" --cpu host-passthrough --ram="+self.mem+" --disk path="+self.vda+\ ",bus=virtio,cache=writethrough,format=qcow2,io=native"+\ " --disk path="+self.vdb+\ ",bus=virtio,cache=writethrough,format=qcow2,io=native"+\ " --network bridge="+self.out_bridge+",model="+self.out_type+",mac="+self.out_mac+" --network bridge="+self.in_bridge+\ ",model="+self.in_type+",mac="+self.in_mac+" --vnc --vncport="+self.vnc_port+" --import --hvm --virt-type kvm "+\ " --print-xml>"+vmfile else: tmp_xml2=" virt-install --name="+self.name+" --vcpus="+self.cpu+" --cpu host-passthrough --ram="+self.mem+" --disk path="+self.vda+\ ",bus=virtio,cache=writethrough,format=raw,io=native"+\ " --disk path="+self.vdb+\ ",bus=virtio,cache=writethrough,format=raw,io=native"+\ " --network bridge="+self.out_bridge+",model="+self.out_type+",mac="+self.out_mac+" --network bridge="+self.in_bridge+\ ",model="+self.in_type+",mac="+self.in_mac+" --vnc --vncport="+self.vnc_port+" --import --hvm --virt-type kvm "+\ " --print-xml>"+vmfile colpt.ptyellow(tmp_xml2) createxml_temp=os.popen(tmp_xml2) createxml_temp_read=createxml_temp.read() colpt.ptgreen(createxml_temp_read) xmlfilelist=[] f3=open(vmfile,"r") for line in f3: if ((line.find("clock"))>0): line="<clock offset='localtime'/>" if ((line.find("input"))>0): line="<input type='tablet' bus='usb'/>\n<input type='mouse' bus='ps2'/>" #f2.write(line) xmlfilelist.append(line) f3.close() f4=open(vmfile,"w") for line in xmlfilelist: f4.write(line) f4.close()
def checkinifile(): """check config file exist!""" global vmarray, vminfo global vminicouter colpt.ptgreen_no_enter("check config file") if os.path.isfile("vm.csv"): colpt.ptgreen("..................config file exists ok!") else: colpt.ptred("config file not exists,please check!!!") sys.exit(1)
def define_vda_vdb(self): if (self.vgname=="none"): self.vda="/datapool/"+self.name+".vda" self.vdb="/datapool/"+self.name+".vdb" elif (self.vgname=="cp"): self.vda="/datapool/"+self.name+".vda" self.vdb="/datapool/"+self.name+".vdb" else: self.vda="/dev/"+self.vgname+"/"+self.name+"-vda" self.vdb="/dev/"+self.vgname+"/"+self.name+"-vdb" colpt.ptgreen(self.vda) colpt.ptgreen(self.vdb)
def checkTempFile(filename,url1,url2): a=filename colpt.ptgreen("check vm images "+a) checkmd5="md5sum -c /datapool/"+a+".md5" #print checkmd5 if os.path.isfile("/datapool/"+a): colpt.ptgreen("vm images "+a+" exists ok!") return 0 else: colpt.ptyellow("vm images not exists ,Will download "+a) vmgeturl=geturl(url1,url2) b=getTemp(a,vmgeturl) if (b != 0): colpt.ptred("Download errors Please check !") return 11 j=1 statusCheckMd5, outputCheckMd5 = commands.getstatusoutput(checkmd5) print str(statusCheckMd5)+" md5 status is!" print "outputCheckMd5 is "+outputCheckMd5 if (string.find(outputCheckMd5,"OK")>0): statusCheckMd5=0 else: statusCheckMd5=1 while (statusCheckMd5 != 0): os.system("rm /datapool/"+a+" -f") os.system("rm /datapool/"+a+"md5 -f") getTemp(a) statusCheckMd5, outputCheckMd5 = commands.getstatusoutput(checkmd5) j=j+1 print str(statusCheckMd5)+" md5 status is!" if (j > 6): print "images sync filed" return 12 colpt.ptgreen("Temp is ok!") return 0
def checkTempFile(filename,url1,url2): a=filename colpt.ptgreen("check vm images "+a) # checkmd5="md5sum -c /datapool/"+a+".md5" #print checkmd5 if os.path.isfile("/datapool/"+a): colpt.ptgreen("vm images "+a+" exists ok!") return 0 else: colpt.ptyellow("vm images not exists ,Will download "+a) # vmgeturl=geturl(url1,url2) # b=getTemp(a,vmgeturl) b=getTemp(a,url1) if (b != 0): colpt.ptred("Download errors Please check !") return 11 ''' j=1 statusCheckMd5, outputCheckMd5 = commands.getstatusoutput(checkmd5) print str(statusCheckMd5)+" md5 status is!" print "outputCheckMd5 is "+outputCheckMd5 if (string.find(outputCheckMd5,"OK")>0): statusCheckMd5=0 else: statusCheckMd5=1 while (statusCheckMd5 != 0): os.system("rm /datapool/"+a+" -f") os.system("rm /datapool/"+a+"md5 -f") getTemp(a) statusCheckMd5, outputCheckMd5 = commands.getstatusoutput(checkmd5) j=j+1 print str(statusCheckMd5)+" md5 status is!" if (j > 6): print "images sync filed" return 12 ''' colpt.ptgreen("Temp is ok!") return 0
vmtmp.ingw=vminfo[16] if vmtmp.vm_xmlfile_exist()=="1": colpt.ptred("xml or vda vdb file exist skip vm create!") elif vmtmp.vm_host_exist()=="1": colpt.ptred("vm allready exist skip vm create!") else: vmtmp.vm_os_check() if (vmtmp.vgname=="none"): vmtmp.vm_resize_disk1() vmtmp.vm_resize_disk2() elif (vmtmp.vgname=="cp"): vmtmp.vm_cp_disk1() vmtmp.vm_resize_disk2() elif (vmtmp.vgname=="vg"): vmtmp.vm_lvm_disk1() vmtmp.vm_lvm_disk2() vmtmp.vm_xmlfile_create2() vmtmp.vm_nicinfo_create() vmtmp.vm_nicinfo_copy_in() if vmtmp.vm_define()=="1": colpt.ptred("define failed skip vm create!") else: vmtmp.vm_run() vmtmp.vm_autostart() j=j+1 getopts() checkinifile() readinifile() vm_ct() colpt.ptgreen("Done")
def vmstatus(self): a=os.popen('virsh list --all') colpt.ptgreen(a.read())
def vm_ct(): """ split vm info and assign to vm object; and create vm inst """ global vmarray, vminfo global vminicouter global vg global vmurl j = 0 vminfo = range(vminicouter) print "vm config info is:" while j < vminicouter: #print vmarray[j],j #vminfo[j]=vmarray[j].split a = vmarray[j] vminfo = string.split(a, ",") colpt.ptred("vm " + str(j) + " info") colpt.ptyellow(str(vminfo)) vmtmp = classvm.vm() ifvg = vminfo[0] if (ifvg.find("Lvm") > 0): vmtmp.vgname = "vg" elif (ifvg.find("CpOnly") > 0): vmtmp.vgname = "cp" else: vmtmp.vgname = "none" vmtmp.temp = vminfo[1] c = syncTemp.checkTempFile(vmtmp.temp, vmurl1, vmurl2) print str(c) + " is check vm iamges error status" if (c != 0): colpt.ptred(" vm images errors Please check!!!") sys.exit(5) colpt.ptgreen(" vm images check is ok!") vmtmp.name = vminfo[2] vmtmp.define_vda_vdb() vmtmp.disk1_size = vminfo[3] vmtmp.disk2_size = vminfo[4] vmtmp.mem = vminfo[5] vmtmp.cpu = vminfo[6] vmtmp.out_type = vminfo[7] vmtmp.out_bridge = vminfo[8] vmtmp.in_bridge = vminfo[9] vmtmp.vnc_port = vminfo[10] vmtmp.outip = vminfo[11] vmtmp.outmask = vminfo[12] vmtmp.outgw = vminfo[13] vmtmp.in_type = vminfo[7] vmtmp.inip = vminfo[14] vmtmp.inmask = vminfo[15] vmtmp.ingw = vminfo[16] if vmtmp.vm_xmlfile_exist() == "1": colpt.ptred("xml or vda vdb file exist skip vm create!") elif vmtmp.vm_host_exist() == "1": colpt.ptred("vm allready exist skip vm create!") else: vmtmp.vm_os_check() if (vmtmp.vgname == "none"): vmtmp.vm_resize_disk1() vmtmp.vm_resize_disk2() elif (vmtmp.vgname == "cp"): vmtmp.vm_cp_disk1() vmtmp.vm_resize_disk2() elif (vmtmp.vgname == "vg"): vmtmp.vm_lvm_disk1() vmtmp.vm_lvm_disk2() vmtmp.vm_xmlfile_create2() vmtmp.vm_nicinfo_create() vmtmp.vm_nicinfo_copy_in() if vmtmp.vm_define() == "1": colpt.ptred("define failed skip vm create!") else: vmtmp.vm_run() vmtmp.vm_autostart() j = j + 1
colpt.ptred("xml or vda vdb file exist skip vm create!") elif vmtmp.vm_host_exist() == "1": colpt.ptred("vm allready exist skip vm create!") else: vmtmp.vm_os_check() if (vmtmp.vgname == "none"): vmtmp.vm_resize_disk1() vmtmp.vm_resize_disk2() elif (vmtmp.vgname == "cp"): vmtmp.vm_cp_disk1() vmtmp.vm_resize_disk2() elif (vmtmp.vgname == "vg"): vmtmp.vm_lvm_disk1() vmtmp.vm_lvm_disk2() vmtmp.vm_xmlfile_create2() vmtmp.vm_nicinfo_create() vmtmp.vm_nicinfo_copy_in() if vmtmp.vm_define() == "1": colpt.ptred("define failed skip vm create!") else: vmtmp.vm_run() vmtmp.vm_autostart() j = j + 1 getopts() checkinifile() readinifile() vm_ct() colpt.ptgreen("Done")
def vm_ct(): """ split vm info and assign to vm object; and create vm inst """ global vmarray, vminfo global vminicouter global vg global vmurl j=0 vminfo=range(vminicouter) print "vm config info is:" while j < vminicouter: #print vmarray[j],j #vminfo[j]=vmarray[j].split a=vmarray[j] vminfo=string.split(a,",") colpt.ptred("vm "+str(j)+" info") colpt.ptyellow(str(vminfo)) vmtmp=classvm.vm() ifvg=vminfo[0] if (ifvg.find("Lvm")>0): vmtmp.vgname="vg" elif (ifvg.find("CpOnly")>0): vmtmp.vgname="cp" else: vmtmp.vgname="none" vmtmp.temp=vminfo[1] c=syncTemp.checkTempFile(vmtmp.temp,vmurl1,vmurl2) print str(c)+" is check vm iamges error status" if (c != 0 ): colpt.ptred(" vm images errors Please check!!!") sys.exit(5) colpt.ptgreen(" vm images check is ok!") vmtmp.name=vminfo[2] vmtmp.define_vda_vdb() vmtmp.disk1_size=vminfo[3] vmtmp.disk2_size=vminfo[4] vmtmp.mem=vminfo[5] vmtmp.cpu=vminfo[6] vmtmp.out_type=vminfo[7] vmtmp.out_bridge=vminfo[8] vmtmp.in_bridge=vminfo[9] vmtmp.vnc_port=vminfo[10] vmtmp.outip=vminfo[11] vmtmp.outmask=vminfo[12] vmtmp.outgw=vminfo[13] vmtmp.in_type=vminfo[7] vmtmp.inip=vminfo[14] vmtmp.inmask=vminfo[15] vmtmp.ingw=vminfo[16] if vmtmp.vm_xmlfile_exist()=="1": colpt.ptred("xml or vda vdb file exist skip vm create!") elif vmtmp.vm_host_exist()=="1": colpt.ptred("vm allready exist skip vm create!") else: vmtmp.vm_os_check() if (vmtmp.vgname=="none"): vmtmp.vm_resize_disk1() vmtmp.vm_resize_disk2() elif (vmtmp.vgname=="cp"): vmtmp.vm_cp_disk1() vmtmp.vm_resize_disk2() elif (vmtmp.vgname=="vg"): vmtmp.vm_lvm_disk1() vmtmp.vm_lvm_disk2() vmtmp.vm_xmlfile_create2() vmtmp.vm_nicinfo_create() vmtmp.vm_nicinfo_copy_in() if vmtmp.vm_define()=="1": colpt.ptred("define failed skip vm create!") else: vmtmp.vm_run() vmtmp.vm_autostart() j=j+1