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 vm_lvm_disk2(self): mycwd=os.getcwd() w2k3vdbvirtpath=mycwd+"/virtvdb/win2k3.vdb" w2k3vdbdatapath="/datapool/win2k3.vdb" copyw2k3vdb="\cp -f "+w2k3vdbvirtpath+" /datapool/" w2k8vdbvirtpath=mycwd+"/virtvdb/win2k8.vdb" w2k8vdbdatapath=mycwd+"/datapool/win2k8.vdb" copyw2k8vdb="\cp -f "+w2k8vdbvirtpath+" /datapool/" if os.path.isfile(w2k3vdbdatapath): pass else: os.popen(copyw2k3vdb) if os.path.isfile(w2k8vdbdatapath): pass else: os.popen(copyw2k8vdb) checkos=self.os create_lvm_vdb="lvcreate -L "+self.disk2_size+" -n "+self.name+"-vdb "+self.vgname print create_lvm_vdb c=os.popen(create_lvm_vdb) d=c.read() print d if (checkos=="2003"): vm_disk2_resize="virt-resize --expand /dev/vda1 /datapool/"+self.temp+" "+self.vdb elif (checkos=="2008"): vm_disk2_resize="virt-resize --expand /dev/vda1 /datapool/"+self.temp+" "+self.vdb elif (checkos=="linux"): colpt.ptred("Linux disk expand only test image by centos5.6 ") #virt-resize --lvexpand /dev/vmvg/root centos56x64_10G_vda.qcow2 centostest.vda vm_disk2_resize="virt-resize --expand /dev/vda2 --lvexpand /dev/vmvg/root /datapool/"+self.temp+" "+self.vdb else: vmiprun="ls /datapool" print "RRRR" print vm_disk2_resize os.system(vm_disk2_resize)
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 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 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 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 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 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 vm_xmlfile_exist(self): vmfile="/datapool/"+self.name+".xml" vmvda=self.vda vmvdb=self.vdb if os.path.isfile(vmfile): #print vmfile," xml file exists skip this vm create!" #continue a="xml file"+vmfile+" is exist skip!" colpt.ptred(a) return "1" elif os.path.isfile(vmvda): a="vda file"+vmvda+" is exist skip!" colpt.ptred(a) return "1" elif os.path.isfile(vmvdb): a="vdb file"+vmvdb+" is exist skip!" colpt.ptred(a) return "1" else: lvscan="lvs" a=os.popen(lvscan) b=a.read() if (b.find(self.name)>=0): c="Lvm "+self.name+" exist!" colpt.ptred(c) return "1" else: print "+_+"
def vm_resize_disk2(self): mycwd=os.getcwd() w2k3vdbvirtpath=mycwd+"/virtvdb/win2k3.vdb" w2k3vdbdatapath="/datapool/win2k3.vdb" copyw2k3vdb="\cp -f "+w2k3vdbvirtpath+" /datapool/" w2k8vdbvirtpath=mycwd+"/virtvdb/win2k8.vdb" w2k8vdbdatapath=mycwd+"/datapool/win2k8.vdb" copyw2k8vdb="\cp -f "+w2k8vdbvirtpath+" /datapool/" if os.path.isfile(w2k3vdbdatapath): pass else: os.popen(copyw2k3vdb) if os.path.isfile(w2k8vdbdatapath): pass else: os.popen(copyw2k8vdb) checkos=self.os vmdisk2="qemu-img create -f qcow2 "+self.vdb+" "+self.disk2_size #vm_disk1_resize="virt-resize --expand /dev/vda1 "+size+" "+self.disk1_size os.popen(vmdisk2) if (checkos=="2003"): vm_disk2_resize="virt-resize --expand /dev/vda1 /datapool/win2k3.vdb "+self.vdb elif (checkos=="2008"): vm_disk2_resize="virt-resize --expand /dev/vda1 /datapool/win2k8.vdb "+self.vdb elif (checkos=="linux"): colpt.ptred("Linux disk expand only test image by centos5.6 ") #virt-resize --lvexpand /dev/vmvg/root centos56x64_10G_vda.qcow2 centostest.vda #vm_disk2_resize="virt-resize --expand /dev/vda2 --lvexpand /dev/vmvg/root /datapool/"+self.temp+" /datapool/"+self.vda vm_disk2_resize=""" echo "linux not expand disk2" """ elif (checkos=="ubuntu"): colpt.ptred("Linux disk expand only test image by ubuntu ") #virt-resize --lvexpand /dev/vmvg/root centos56x64_10G_vda.qcow2 centostest.vda #vm_disk2_resize="virt-resize --expand /dev/vda2 --lvexpand /dev/vmvg/root /datapool/"+self.temp+" /datapool/"+self.vda vm_disk2_resize=""" echo "linux not expand disk2" """ else: vmiprun="ls /datapool" print "RRRR" #vm_disk1_resize="virt-resize --lveexpand /dev/vda1 /datapool/"+self.temp+" /datapool/"+self.vda print vm_disk2_resize os.system(vm_disk2_resize)
def vm_xmlfile_create(self): tmp_xml=mycwd+"/virtxml/win03.xml" f=open(tmp_xml,"r") vmfile="/datapool/"+self.name+".xml" if os.path.isfile(vmfile): print vmfile," file exists skip this vm create!" #continue a="xml file is exist skip!" colpt.ptred(a) return "1" f2=open(vmfile,"w") for line in f: if ((line.find("vmname"))>0): line=string.replace(line,"vmname",self.name) elif (line.find("vmmem")>=0): line=string.replace(line,"vmmem",self.mem) elif (line.find("vmcpu")>=0): line=string.replace(line,"vmcpu",self.cpu) elif (line.find("vmvda")>=0): line=string.replace(line,"vmvda",self.vda) elif (line.find("vmvdb")>=0): line=string.replace(line,"vmvdb",self.vdb) elif (line.find("vmbr1type")>=0): line=string.replace(line,"vmbr1type",self.out_type) elif (line.find("vmbr2type")>=0): line=string.replace(line,"vmbr2type",self.in_type) elif (line.find("vmbr1")>=0): line=string.replace(line,"vmbr1",self.out_bridge) elif (line.find("mac1")>=0): line=string.replace(line,"mac1",self.out_mac) elif (line.find("vmbr2")>=0): line=string.replace(line,"vmbr2",self.in_bridge) elif (line.find("mac2")>=0): line=string.replace(line,"mac2",self.in_mac) elif (line.find("vncport")>=0): line=string.replace(line,"vncport",self.vnc_port) f2.write(line) f.close() f2.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_lvm_disk1(self): vgname=self.vgname checkos=self.os create_lvm_vda="lvcreate -L "+self.disk1_size+" -n "+self.name+"-vda "+self.vgname print create_lvm_vda c=os.popen(create_lvm_vda) d=c.read() print d if (checkos=="2003"): vm_disk1_resize="virt-resize --expand /dev/vda1 /datapool/"+self.temp+" "+self.vda elif(checkos=="2008"): vm_disk1_resize="virt-resize --expand /dev/vda1 /datapool/"+self.temp+" "+self.vda elif(checkos=="linux"): colpt.ptred("Linux disk expand only test image by centos5.6 ") #virt-resize --lvexpand /dev/vmvg/root centos56x64_10G_vda.qcow2 centostest.vda vm_disk1_resize="virt-resize --expand /dev/vda2 --lvexpand /dev/vmvg/root /datapool/"+self.temp+" "+self.vda else: vm_disk1_resize="ls /datapool" print "RRRR" print vm_disk1_resize #os.popen(vmdisk1) os.system(vm_disk1_resize)
def vm_resize_disk1(self): checkos=self.os vmdisk1="qemu-img create -f qcow2 "+self.vda+" "+self.disk1_size if (checkos=="2003"): vm_disk1_resize="virt-resize --expand /dev/vda1 /datapool/"+self.temp+" "+self.vda elif (checkos=="2008"): vm_disk1_resize="virt-resize --expand /dev/vda1 /datapool/"+self.temp+" "+self.vda elif (checkos=="linux"): colpt.ptred("Linux disk expand only test image by centos5.6 ") #virt-resize --lvexpand /dev/vmvg/root centos56x64_10G_vda.qcow2 centostest.vda vm_disk1_resize="virt-resize --expand /dev/vda2 --lvexpand /dev/vmvg/root /datapool/"+self.temp+" "+self.vda elif (checkos=="ubuntu"): colpt.ptred("Linux disk expand only test image by ubuntu ") #virt-resize --lvexpand /dev/vmvg/root centos56x64_10G_vda.qcow2 centostest.vda vm_disk1_resize="virt-resize --expand /dev/sda2 --lvexpand /dev/vmvg/root /datapool/"+self.temp+" "+self.vda else: vmiprun="ls /datapool" print "RRRR" print vmdisk1 #vm_disk1_resize="virt-resize --lveexpand /dev/vda1 /datapool/"+self.temp+" /datapool/"+self.vda print vm_disk1_resize os.popen(vmdisk1) os.system(vm_disk1_resize)
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
def vm_nicinfo_copy_in(self): checkos=self.os if (checkos=="2003"): nic_file=mycwd+"/virttmp/nicinfo.ini" vmiprun="virt-copy-in -a "+self.vda+" "+nic_file+" /WINDOWS" elif (checkos=="2008"): nic_file=mycwd+"/virttmp/nicinfo.ini" vmiprun="virt-copy-in -a "+self.vda+" "+nic_file+" /Windows" elif (checkos=="linux"): colpt.ptred("Linux system ip config only test image by centos5.6 ") tmp_setipbymac=mycwd+"/virtscript/setipbymac.sh" tmp_rc_local_out="virt-copy-out -a "+self.vda+" /etc/rc.d/rc.local "+mycwd+"/virttmp/" tmp_rc_local_in="virt-copy-in -a "+self.vda+" "+mycwd+"/virttmp/rc.local /etc/rc.d/" tmp_rc_local_in_setipscript="virt-copy-in -a "+self.vda+" "+mycwd+"/virtscript/setipbymac.sh /bin/" tmp_setip_cmd="setipbymac.sh "+self.name+" "+self.outgw+" "+self.outip+" "+self.outmask+" "+self.out_mac.upper()+\ " "+self.inip+" "+self.inmask+" "+self.in_mac.upper() tmp_setip_echo="echo sh /bin/"+tmp_setip_cmd+" >>"+mycwd+"/virttmp/rc.local" tmp_chmod_rc_local="chmod 711 "+mycwd+"/virttmp/rc.local" print "show copy out "+tmp_rc_local_out print "show copy in "+tmp_rc_local_in print "show setip echo "+tmp_setip_echo print "show copy in setipscript "+tmp_rc_local_in_setipscript print tmp_setip_cmd os.system(tmp_rc_local_out) os.system(tmp_setip_echo) os.system(tmp_chmod_rc_local) os.system(tmp_rc_local_in) os.system(tmp_rc_local_in_setipscript) vmiprun="echo 'linux ^_^'" elif (checkos=="ubuntu"): colpt.ptred("Linux system ip config only test image by ubuntu1104*64 ") tmp_setipbymac=mycwd+"/virtscript/setipbymac.sh" tmp_rc_local_out="virt-copy-out -a "+self.vda+" /etc/rc.local "+mycwd+"/virttmp/" tmp_rc_local_in="virt-copy-in -a "+self.vda+" "+mycwd+"/virttmp/rc.local /etc/" tmp_rc_local_in_setipscript="virt-copy-in -a "+self.vda+" "+mycwd+"/virtscript/setipbymac-ubuntu.sh /bin/" tmp_setip_cmd="setipbymac-ubuntu.sh "+self.name+" "+self.outgw+" "+self.outip+" "+self.outmask+" "+self.out_mac.upper()+\ " "+self.inip+" "+self.inmask+" "+self.in_mac.upper() tmp_start_rc_local="sed -i \'s/#!\/bin\/sh -e/#!\/bin\/sh/g\' "+mycwd+"/virttmp/rc.local" tmp_del_exit_rc_local="sed -i /exit/d "+mycwd+"/virttmp/rc.local" tmp_setip_echo="echo /bin/bash /bin/"+tmp_setip_cmd+" >>"+mycwd+"/virttmp/rc.local" tmp_chmod_rc_local="chmod 711 "+mycwd+"/virttmp/rc.local" print "show copy out "+tmp_rc_local_out print "show copy in "+tmp_rc_local_in print "show setip echo "+tmp_setip_echo print "show copy in setipscript "+tmp_rc_local_in_setipscript print tmp_setip_cmd os.system(tmp_rc_local_out) os.system(tmp_start_rc_local) os.system(tmp_del_exit_rc_local) os.system(tmp_setip_echo) os.system(tmp_chmod_rc_local) os.system(tmp_rc_local_in) os.system(tmp_rc_local_in_setipscript) vmiprun="echo 'ubuntu ^_^'" else: vmiprun="ls /datapool" print "RRRR" c=os.popen(vmiprun) d=c.read() print c