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_name_check(self): if self.name == "": colpt.ptred("Please give vm name") #sys.exit() return "1" else: colpt.ptgreen("name check pass")
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_resize_disk1(self): checkos = self.os vmdisk1 = "qemu-img create -f qcow2 " + self.vda + " " + self.disk1_size logger.debug(vmdisk1) # print vmdisk1 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 --lv-expand /dev/vmvg/root /datapool/" + self.temp + " " + self.vda elif (checkos == "ubuntu" or "ubuntu-cloud"): 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/sda1 /datapool/" + self.temp + " " + self.vda logger.debug(vm_disk1_resize) # print vm_disk1_resize else: vmiprun = "ls /datapool" print "RRRR" os.popen(vmdisk1) os.system(vm_disk1_resize)
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 --lv-expand /dev/vmvg/root /datapool/" + self.temp + " " + self.vdb else: vmiprun = "ls /datapool" print "RRRR" print vm_disk2_resize os.system(vm_disk2_resize)
def checkinifile(): """check config file exist!""" # global vmarray, vminfo # global vminicouter colpt.ptgreen_no_enter("check config file") if os.path.isfile("config/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" or "ubuntu-cloud"): 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 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 --lv-expand /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_ct(): ''' split vm info and assign to vm object and create vm inst ''' global vmarray, vminfo global vminicouter global vg j = 0 vminfo = range(vminicouter) print "vm config info is:" while j < vminicouter: a = vmarray[j] vminfo = string.split(a, ",") colpt.ptred("vm " + str(j) + " info") colpt.ptyellow(str(vminfo)) vmtmp = classvm.vm() ifvg = vminfo[0] logger.debug(ifvg) if (ifvg.find('Lvm') > 0): vmtmp.vgname = 'vg' elif (ifvg.find('CpOnly') > 0): vmtmp.vgname = 'none' vmtmp.temp = vminfo[1] logger.debug(vmtmp.temp) c = syncTemp.checkTempFile(vmtmp.temp, vmurl1, vmurl1) print str(c) + " is check vm iamges error status" if (c != 0): colpt.ptred('vm images erros Please check!!!') sys.exit(5) colpt.ptgreen('vm images check is ok!') vmtmp.name = vminfo[2] vmtmp.define_vda_vdb() vmtmp.pwd = vminfo[3] vmtmp.disk1_size = vminfo[4] vmtmp.disk2_size = vminfo[5] vmtmp.mem = vminfo[6] vmtmp.cpu = vminfo[7] vmtmp.out_type = vminfo[8] vmtmp.out_bridge = vminfo[9] vmtmp.in_bridge = vminfo[10] vmtmp.vnc_port = vminfo[11] vmtmp.outip = vminfo[12] vmtmp.outmask = vminfo[13] vmtmp.outgw = vminfo[14] vmtmp.in_type = vminfo[8] vmtmp.inip = vminfo[15] vmtmp.inmask = vminfo[16] vmtmp.ingw = vminfo[17] 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 already 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_cp_disk1() vmtmp.vm_resize_disk1() vmtmp.vm_resize_disk2() vmtmp.vm_xmlfile_create2() vmtmp.vm_nicinfo_copy_in() if vmtmp.pwd == 'none': pass else: vmtmp.vm_set_passwd() 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