From ae072ef55159cbf0e01ea6b027e627573d52f0fb Mon Sep 17 00:00:00 2001 From: Patrick Oliveira <32719215+TrickOC@users.noreply.github.com> Date: Thu, 25 Feb 2021 16:00:41 -0300 Subject: [PATCH] Update xenmigrate.py Change from print "text" to print ("text") and updating exception error in 303 line. --- xenmigrate.py | 169 +++++++++++++++++++++++++------------------------- 1 file changed, 84 insertions(+), 85 deletions(-) diff --git a/xenmigrate.py b/xenmigrate.py index 7bfa7ce..7ff655e 100755 --- a/xenmigrate.py +++ b/xenmigrate.py @@ -23,7 +23,7 @@ def docmd(cmd): run a command and return the communicate PIPE """ if debug: - print 'running cmd :',cmd + print('running cmd :',cmd) execute=subprocess.Popen([cmd],shell=True,stdout=subprocess.PIPE) return execute.communicate()[0] @@ -32,7 +32,7 @@ def exportvm(vmname,lvdev,destfile,gz=False): export lvdev to dest """ if debug: - print 'exporting vm :',vmuuid + print('exporting vm :',vmuuid) # we'll need to handle difference block sizes at some point blocksize=1024*1024 notification=float(2**30) # 2**30=GB @@ -43,13 +43,13 @@ def exportvm(vmname,lvdev,destfile,gz=False): if vmstatus=='running': cmd='xe vm-shutdown -u root uuid='+vmuuid if debug: - print 'halting vm uuid :',vmuuid + print('halting vm uuid :',vmuuid) docmd(cmd) vmstatus=getvmstatus(vmuuid) if vmstatus=='halted': if not os.path.exists(destfile): try: - print '\nActivating Volume:' + print('\nActivating Volume:') cmd='lvchange -v -ay '+lvdev lvchange=docmd(cmd) source=open(lvdev,'rb') @@ -58,7 +58,7 @@ def exportvm(vmname,lvdev,destfile,gz=False): else: dest=open(destfile,'wb') noticetick=notification/(2**30) - print '\nRW notification every: '+str(noticetick)+'GB' + print('\nRW notification every: '+str(noticetick)+'GB') notification=notification/blocksize sys.stdout.write('Exporting: ') write=0 @@ -73,29 +73,29 @@ def exportvm(vmname,lvdev,destfile,gz=False): if write%notification==0: sys.stdout.write('w ') sys.stdout.flush() - print '\nSuccessful export' + print('\nSuccessful export') finally: try: source.close() dest.close() finally: - print '\nDeactivating Volume:' + print('\nDeactivating Volume:') cmd='lvchange -v -an '+lvdev docmd(cmd) else: - print 'ERROR: destination file '+destfile+' exists.' + print('ERROR: destination file '+destfile+' exists.') else: - print 'ERROR: vm status:',vmstatus,'vm needs to be halted to migrate' + print('ERROR: vm status:',vmstatus,'vm needs to be halted to migrate') def importvm(lvdest,sourcefile,vgdest,lvsize,gz=False): """ import a raw vmfile into a logical volume """ if debug: - print 'importing vm from :',sourcefile - print 'to logical volume :',lvdest - print 'on volume group :',vgdest - print 'with gz :',gz + print('importing vm from :',sourcefile) + print('to logical volume :',lvdest) + print('on volume group :',vgdest) + print('with gz :',gz) blocksize=1024*1024 notification=float(2**30) # 2**30=GB if gz: @@ -104,11 +104,11 @@ def importvm(lvdest,sourcefile,vgdest,lvsize,gz=False): lvvgs=getlvdevlist() for lvvg in lvvgs: if lvdest==lvvg[0]: - print 'ERROR: lv '+lvdest+' exists cannot import' + print('ERROR: lv '+lvdest+' exists cannot import') lvexists=1 if not lvexists: cmd='lvcreate -v -n '+lvdest+' -L '+lvsize+'G '+vgdest - print '\nCreating Logical Volume:' + print('\nCreating Logical Volume:') docmd(cmd) try: if gz: @@ -118,7 +118,7 @@ def importvm(lvdest,sourcefile,vgdest,lvsize,gz=False): destlv='/dev/'+vgdest+'/'+lvdest dest=open(destlv,'wb') noticetick=notification/(2**30) - print '\nRW notification every: '+str(noticetick)+'GB' + print('\nRW notification every: '+str(noticetick)+'GB') notification=notification/blocksize sys.stdout.write('Importing: ') write=0 @@ -133,7 +133,7 @@ def importvm(lvdest,sourcefile,vgdest,lvsize,gz=False): if write%notification==0: sys.stdout.write('w ') sys.stdout.flush() - print '\nSuccessful import' + print('\nSuccessful import') finally: try: source.close() @@ -141,16 +141,16 @@ def importvm(lvdest,sourcefile,vgdest,lvsize,gz=False): finally: print else: - print 'ERROR: logical volume '+lvdest+' exists' + print('ERROR: logical volume '+lvdest+' exists') def importxenserverdisk(sourcefile,diskuuid,vmuuid,gz=False): """ import disk from sourcefile into xenserver """ if debug: - print 'importing vm from :',sourcefile - print 'to disk uuid :',diskuuid - print 'with gz :',gz + print('importing vm from :',sourcefile) + print('to disk uuid :',diskuuid) + print('with gz :',gz) blocksize=1024*1024 notification=float(2**30) # 2**30=GB if gz: @@ -159,15 +159,15 @@ def importxenserverdisk(sourcefile,diskuuid,vmuuid,gz=False): if vmstatus=='running': cmd='xe vm-shutdown -u root uuid='+vmuuid if debug: - print 'halting vm uuid :',vmuuid + print('halting vm uuid :',vmuuid) docmd(cmd) vmstatus=getvmstatus(vmuuid) if vmstatus=='halted': if os.path.exists(sourcefile): try: lvdev=getlvdevxen(diskuuid)[0] - print 'to logical volume :',lvdev - print '\nActivating Volume:' + print('to logical volume :',lvdev) + print('\nActivating Volume:') cmd='lvchange -v -ay '+lvdev lvchange=docmd(cmd) if gz: @@ -176,7 +176,7 @@ def importxenserverdisk(sourcefile,diskuuid,vmuuid,gz=False): source=open(sourcefile,'rb') dest=open(lvdev,'wb') noticetick=notification/(2**30) - print '\nRW notification every: '+str(noticetick)+'GB' + print('\nRW notification every: '+str(noticetick)+'GB') notification=notification/blocksize sys.stdout.write('Importing: ') write=0 @@ -191,19 +191,19 @@ def importxenserverdisk(sourcefile,diskuuid,vmuuid,gz=False): if write%notification==0: sys.stdout.write('w ') sys.stdout.flush() - print '\nSuccessful import' + print('\nSuccessful import') finally: try: source.close() dest.close() finally: - print '\nDeactivating Volume:' + print('\nDeactivating Volume:') cmd='lvchange -v -an '+lvdev docmd(cmd) else: - print 'ERROR: source file '+sourcefile+' does not exist.' + print('ERROR: source file '+sourcefile+' does not exist.') else: - print 'ERROR: vm status:',vmstatus,'vm needs to be halted to import disk' + print('ERROR: vm status:',vmstatus,'vm needs to be halted to import disk') def getdiskuuidvm(diskuuid): @@ -211,7 +211,7 @@ def getdiskuuidvm(diskuuid): get vm uuid from disk uuid and return it """ if debug: - print 'vm from disk uuid :',diskuuid + print('vm from disk uuid :',diskuuid) cmd='xe vbd-list vdi-uuid='+diskuuid response=docmd(cmd).split('vm-uuid ( RO): ') vmuuid=response[1].split('\n')[0] @@ -239,7 +239,7 @@ def getlvdevxen(vmdiskuuid): take the vmdisk uuid and return the logical volume device name """ if debug: - print 'get lv from uuid :',vmdiskuuid + print('get lv from uuid :',vmdiskuuid) lvvgs=getlvdevlist() for lvvg in lvvgs: if vmdiskuuid in lvvg[0]: @@ -253,7 +253,7 @@ def getvmdiskuuid(vmuuid): return disk uuids in list """ if debug: - print 'disk from uuid :',vmuuid + print('disk from uuid :',vmuuid) diskuuid=[] cmd='xe vbd-list vm-uuid='+vmuuid response=docmd(cmd).split('vdi-uuid ( RO): ') @@ -276,7 +276,7 @@ def getvmuuid(vmname): return uuid """ if debug: - print 'uuid from name :',vmname + print('uuid from name :',vmname) try: cmd='xe vm-list name-label=\''+vmname+'\'' uuid=docmd(cmd).split(':')[1].split(' ')[1][:-1] @@ -289,9 +289,9 @@ def reftoraw(refdir,rawfile,gz=False): take the ref directory of an xva file and create a raw importable file """ if debug: - print 'ref dir :',refdir - print 'to raw file :',rawfile - print 'gzip :',gz + print('ref dir :',refdir) + print('to raw file :',rawfile) + print('gzip :',gz) blocksize=1024*1024 notification=float(2**30) # 2**30=GB if gz: @@ -300,12 +300,12 @@ def reftoraw(refdir,rawfile,gz=False): for dirobj in os.listdir(refdir): try: numfile=int(dirobj) - except ValueError, TypeError: + except (ValueError, TypeError) as e: numfile=0; if numfile>numfiles: numfiles=numfile - print 'last file :',numfiles+1 - print 'disk image size :',(numfiles+1)/1024,'GB' + print('last file :',numfiles+1) + print('disk image size :',(numfiles+1)/1024,'GB') if os.path.isdir(refdir): # This may cause problems in Windows! if refdir[-1]!='/': @@ -314,7 +314,7 @@ def reftoraw(refdir,rawfile,gz=False): try: filenum=0 noticetick=notification/(2**30) - print '\nRW notification every: '+str(noticetick)+'GB' + print('\nRW notification every: '+str(noticetick)+'GB') notification=notification/blocksize if gz: dest=gzip.GzipFile(rawfile,'wb') @@ -350,7 +350,7 @@ def reftoraw(refdir,rawfile,gz=False): sys.stdout.write('w ') sys.stdout.flush() filenum+=1 - print '\nSuccessful convert' + print('\nSuccessful convert') finally: try: dest.close() @@ -358,36 +358,36 @@ def reftoraw(refdir,rawfile,gz=False): finally: print else: - print 'ERROR: rawfile '+rawfile+' exists' + print('ERROR: rawfile '+rawfile+' exists') else: - print 'ERROR: refdir '+refdir+' does not exist' + print('ERROR: refdir '+refdir+' does not exist') def vmdktoraw(vmdkfile,rawfile,gz): """ take the ref directory of an xva file and create a raw importable file """ if debug: - print 'vmdk :',vmdkfile - print 'to raw :',rawfile - print 'gzip :',gz + print('vmdk :',vmdkfile) + print('to raw :',rawfile) + print('gzip :',gz) if (not gz and not os.path.exists(rawfile)) or ((gz and not os.path.exists(rawfile+'.gz')) and (gz and not os.path.exists(rawfile))): try: cmd='qemu-img convert '+vmdkfile+' -O raw '+rawfile - print 'Converting...' + print('Converting...') response=docmd(cmd) - print response + print(response) if gz: cmd='gzip -v '+rawfile - print 'Gzipping...' + print('Gzipping...') response=docmd(cmd) - print 'Sucessful convert' + print('Sucessful convert') except: - print 'ERROR: problem converting file (do you have qemu-img installed?)' + print('ERROR: problem converting file (do you have qemu-img installed?)') else: if gz: - print 'ERROR: rawfile '+rawfile+' or '+rawfile+'.gz exists' + print('ERROR: rawfile '+rawfile+' or '+rawfile+'.gz exists') else: - print 'ERROR: rawfile '+rawfile+' exists' + print('ERROR: rawfile '+rawfile+' exists') ## ## Main Program @@ -398,7 +398,7 @@ def vmdktoraw(vmdkfile,rawfile,gz): global debug debug=False # Hello world - print 'xenmigrate 0.7.4 -- 2011.09.13\n(c)2011 Jolokia Networks and Mark Pace -- jolokianetworks.com\n' + print('xenmigrate 0.7.4 -- 2011.09.13\n(c)2011 Jolokia Networks and Mark Pace -- jolokianetworks.com\n') # process arguments from optparse import OptionParser parser=OptionParser(usage='%prog [-cdhiltvxz] [vmname]|[exportLVdev]|[importVolGroup]|[importdiskuuid]|[converttofile]') @@ -423,27 +423,27 @@ def vmdktoraw(vmdkfile,rawfile,gz): pass else: vmuuid=getvmuuid(vmname) - print 'vm name-label :',vmname - print 'vm uuid :',vmuuid + print('vm name-label :',vmname) + print('vm uuid :',vmuuid) vmdiskuuids=getvmdiskuuid(vmuuid) for vmdiskuuid in vmdiskuuids: - print 'vm disk uuid :',vmdiskuuid[0] - print 'vm disk partid :',vmdiskuuid[1] + print('vm disk uuid :',vmdiskuuid[0]) + print('vm disk partid :',vmdiskuuid[1]) if opts.lvdev: lvdev,lvsize=getlvdevxen(vmdiskuuid[0]) if lvdev is not None: - print 'vm disk dev name :',lvdev - print 'vm disk size :',lvsize+'GB' + print('vm disk dev name :',lvdev) + print('vm disk size :',lvsize+'GB') else: - print 'vm disk dev name : not found in mounted storage repositories' + print('vm disk dev name : not found in mounted storage repositories') if opts.export and opts.doimport: - print 'ERROR: export and import cannot be run at the same time' + print('ERROR: export and import cannot be run at the same time') elif opts.export and opts.convert: - print 'ERROR: export and convert cannot be run at the same time' + print('ERROR: export and convert cannot be run at the same time') elif opts.doimport and opts.convert: - print 'ERROR: import and convert cannot be run at the same time' + print('ERROR: import and convert cannot be run at the same time') elif opts.export and opts.doimport and opts.convert: - print 'ERROR: you have got to be kidding me -- need some more options to run at the same time?' + print('ERROR: you have got to be kidding me -- need some more options to run at the same time?') elif opts.export: if '/dev' in vmname: vmdiskuuids=[vmname] @@ -466,12 +466,12 @@ def vmdktoraw(vmdkfile,rawfile,gz): exportname=exportname+'_'+vmdiskuuid[1]+'_'+lvsize if opts.gz: exportname=exportname+'.gz' - print 'export dev :',lvdev - print 'to raw file :',exportname + print('export dev :',lvdev) + print('to raw file :',exportname) if lvdev: exportvm(vmname,lvdev,exportname,opts.gz) - print 'You many need to restart your VM:' - print 'xe vm-startup -u root uuid='+vmuuid + print('You many need to restart your VM:') + print('xe vm-startup -u root uuid='+vmuuid) elif opts.doimport: importname=opts.doimport if importname[-3:]=='.gz': @@ -486,24 +486,24 @@ def vmdktoraw(vmdkfile,rawfile,gz): lvdest=lvdesttmp.split('_')[0] else: lvdest=lvdest+'_'+lvdesttmp.split('_')[index] - print 'import raw file :',opts.doimport - print 'to lv :',lvdest - print 'in vg :',args[0] - print 'lv size :',lvsize+'GB' - print 'xen config partid :',lvpartid + print('import raw file :',opts.doimport) + print('to lv :',lvdest) + print('in vg :',args[0]) + print('lv size :',lvsize+'GB') + print('xen config partid :',lvpartid) importvm(lvdest,opts.doimport,args[0],lvsize,opts.gz) elif opts.type=='xenserver': - print 'import raw file :',opts.doimport - print 'to disk uuid :',args[0] + print('import raw file :',opts.doimport) + print('to disk uuid :',args[0]) vmuuid=getdiskuuidvm(args[0]) - print 'vm uuid :',vmuuid + print('vm uuid :',vmuuid) importxenserverdisk(opts.doimport,args[0],vmuuid,opts.gz) else: - print 'ERROR: unknown Xen type for import' + print('ERROR: unknown Xen type for import') elif opts.convert: if os.path.isdir(opts.convert): - print 'convert ref dir :',opts.convert - print 'to raw file :',args[0] + print('convert ref dir :',opts.convert) + print('to raw file :',args[0]) reftoraw(opts.convert,args[0],opts.gz) elif os.path.isfile(opts.convert): if opts.convert[-5:]=='.vmdk': @@ -511,12 +511,11 @@ def vmdktoraw(vmdkfile,rawfile,gz): if filename[-3:]=='.gz': opts.gz=True filename=filename[:-3] - print 'convert vmdk file :',opts.convert - print 'to raw file :',filename + print('convert vmdk file :',opts.convert) + print('to raw file :',filename) vmdktoraw(opts.convert,filename,opts.gz) else: - print 'ERROR: unknown file convert format' + print('ERROR: unknown file convert format') else: - print 'ERROR: convert source directory or file does not exist' + print('ERROR: convert source directory or file does not exist') sys.exit(1) -