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 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 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
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
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