def run_qemu_convert(cmd): out = subprocess.call(cmd, shell=True) if int(out) == 0: #print "Se creo correctamente la imagen" print printf.OK("qcow2 file creation success") else: print printf.ERROR("qcow2 file creation failed")
def delete_snap(idvm, snapid): snap_service = connection.service("vms/" + idvm + "/snapshots/" + snapid) snap_service.remove() status = get_snap_status(idvm, snapid) while str(status) != "None": time.sleep(30) printf.INFO("Waiting until snapshot deletion ends") status = get_snap_status(idvm, snapid) printf.OK("Snapshot deleted")
def backup_vms_with_tag(self, tag): self.start() for vm in self.get_vms_with_tag(tag): try: printf.OK("Running backup of vm %s with tag %s" % (vm, tag)) b = backup_vm(self.conf_file, vm) b.main() except Exception as ex: printf.ERROR("Backup %s failed!" % vm)
def start(self): try: # Create a connection to the server: self.connection = sdk.Connection(url=self.url, username=self.user, password=self.password, ca_file=self.ca_file) printf.OK("Connection to oVIrt API success %s" % self.url) except Exception as ex: print ex printf.ERROR("Connection to oVirt API has failed")
def create_snap(idvm): vm_service = connection.service("vms") snapshots_service = vm_service.vm_service(idvm).snapshots_service() snapshots_service.add(types.Snapshot(description=snapname, persist_memorystate=False)) snapid = get_snap_id(vmid) status = get_snap_status(idvm,snapid) printf.INFO("Trying to create snapshot of VM: " + idvm) while str(status) == "locked": time.sleep(10) printf.INFO("Waiting until snapshot creation ends") status = get_snap_status(idvm,snapid) printf.OK("Snapshot created")
def create_snap(self,vmid,snapname,my_disk): vm_service = self.connection.service("vms") snapshots_service = vm_service.vm_service(vmid).snapshots_service() snapshots_service.add(types.Snapshot(description=snapname,persist_memorystate=False, disk_attachments=[ types.DiskAttachment( disk=types.Disk( id=my_disk)) ])) snapid = self.get_snap_id(vmid) status = self.get_snap_status(vmid,snapid) printf.INFO("Trying to create snapshot of VM: " + vmid) while str(status) == "locked": time.sleep(10) printf.INFO("Waiting until snapshot creation ends") status = self.get_snap_status(vmid,snapid) printf.OK("Snapshot created")
def run_qemu_convert(self,cmd): out = subprocess.call(cmd, shell=True) qcowfile = cmd.split()[-1] if int(out) == 0: #print "Se creo correctamente la imagen" print printf.OK("qcow2 file creation success") else: print printf.ERROR("qcow2 file creation failed") printf.ERROR("Deleting file %s" % qcowfile) subprocess.call("rm -rf %s" % qcowfile , shell=True)
def progress_bar_qcow(self,qcowfile): time.sleep(3) seconds=time.time() try: firstvalue=0 endvalue=self.get_qcow_size(qcowfile) pid=self.pid_qcow_convert(qcowfile) while os.path.isfile("/proc/"+str(pid)+"/io"): value=self.get_pid_read(pid) speed=str(round(float(value-firstvalue)/1024/1024,2)) + " MB/s..." firstvalue=value self.progress_bar(value,endvalue,speed) time.sleep(1) seconds=time.time()-seconds avg=round((endvalue/seconds/1024/1024),2) printf.OK("Summary: Total Size MB="+str(endvalue/1024/1024)+", Avg Speed MB/s="+str(avg)+", Took="+str(round(seconds,0))+" seconds") except: print "ERROR to get process status"
ca_file = cfg.get('bkp', 'ca_file') bckdir = cfg.get('bkp', 'bckdir') bkpvm = cfg.get('bkp', 'bkpvm') vmname = sys.argv[2] date = str((time.strftime("%Y-%m-%d-%H"))) vmid = "" snapname = "BACKUP" + "_" + date + "h" try: # Create a connection to the server: connection = sdk.Connection(url=url, username=user, password=password, ca_file=ca_file) printf.OK("Connection to oVIrt API success %s" % url) except Exception as ex: printf.ERROR("Connection to oVirt API has failed") #print "Unexpected error: %s" % ex # Funcion para obtener el id de la vm buscandola por el nombre # Function to get VM_ID from VM name def get_id_vm(vmname): vm_service = connection.service("vms") vms = vm_service.list() for vm in vms: if vm.name == vmname: return vm.id