Revert "Revert "- fix use_compression""

This reverts commit 3a6b93c85d.
This commit is contained in:
ssamson-tis 2015-12-23 11:43:28 +01:00
parent 3a6b93c85d
commit 3d9bd21a7b

View File

@ -18,25 +18,21 @@
# #
# ----------------------------------------------------------------------- # -----------------------------------------------------------------------
from __future__ import with_statement from __future__ import with_statement
import os
import datetime
from common import *
import XenAPI
import time
import logging import logging
import re import re
import os.path
import os import os
import datetime import datetime
import select
import urllib import urllib
import socket import socket
import tarfile import tarfile
import hashlib import hashlib
from stat import * from stat import *
import ssl import ssl
if hasattr(ssl, '_create_unverified_context'): from common import *
import XenAPI
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context ssl._create_default_https_context = ssl._create_unverified_context
@ -53,14 +49,14 @@ class backup_xva(backup_generic):
reuse_snapshot = "no" reuse_snapshot = "no"
ignore_proxies = "yes" ignore_proxies = "yes"
use_compression = "true" use_compression = "true"
if str2bool(ignore_proxies) == True: if str2bool(ignore_proxies):
os.environ['http_proxy']="" os.environ['http_proxy']=""
os.environ['https_proxy']="" os.environ['https_proxy']=""
def verify_export_xva(self,filename): def verify_export_xva(self,filename):
self.logger.debug("[%s] Verify xva export integrity",self.server_name) self.logger.debug("[%s] Verify xva export integrity",self.server_name)
tar = tarfile.open(filename) tar = tarfile.open(filename)
members = tar.getmembers() members = tar.getmembers()
for tarinfo in members: for tarinfo in members:
if re.search('^[0-9]*$',os.path.basename(tarinfo.name)): if re.search('^[0-9]*$',os.path.basename(tarinfo.name)):
@ -84,8 +80,8 @@ class backup_xva(backup_generic):
session = XenAPI.Session('https://'+xcphost) session = XenAPI.Session('https://'+xcphost)
session.login_with_password(user_xen,password_xen) session.login_with_password(user_xen,password_xen)
if not session.xenapi.VM.get_by_name_label(vdi_name): if not session.xenapi.VM.get_by_name_label(vdi_name):
return "bad VM name: %s" % vdi_name return "bad VM name: %s" % vdi_name
vm = session.xenapi.VM.get_by_name_label(vdi_name)[0] vm = session.xenapi.VM.get_by_name_label(vdi_name)[0]
status_vm = session.xenapi.VM.get_power_state(vm) status_vm = session.xenapi.VM.get_power_state(vm)
@ -99,7 +95,7 @@ class backup_xva(backup_generic):
#add snapshot option #add snapshot option
if str2bool(halt_vm) == False: if not str2bool(halt_vm):
self.logger.debug("[%s] Check if previous tisbackups snapshots exist",vdi_name) self.logger.debug("[%s] Check if previous tisbackups snapshots exist",vdi_name)
old_snapshots = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name)) old_snapshots = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))
self.logger.debug("[%s] Old snaps count %s", vdi_name, len(old_snapshots)) self.logger.debug("[%s] Old snaps count %s", vdi_name, len(old_snapshots))
@ -134,26 +130,26 @@ class backup_xva(backup_generic):
#get snapshot opaqueRef #get snapshot opaqueRef
vm = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))[0] vm = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))[0]
session.xenapi.VM.set_name_description(snapshot,"snapshot created by tisbackup on: %s"%(now.strftime("%Y-%m-%d %H:%M"))) session.xenapi.VM.set_name_description(snapshot,"snapshot created by tisbackup on: %s"%(now.strftime("%Y-%m-%d %H:%M")))
else: else:
self.logger.debug("[%s] Status of VM: %s",self.backup_name,status_vm) self.logger.debug("[%s] Status of VM: %s",self.backup_name,status_vm)
if status_vm == "Running": if status_vm == "Running":
self.logger.debug("[%s] Shudown in progress",self.backup_name) self.logger.debug("[%s] Shudown in progress",self.backup_name)
if dry_run: if dry_run:
print "session.xenapi.VM.clean_shutdown(vm)" print "session.xenapi.VM.clean_shutdown(vm)"
else: else:
session.xenapi.VM.clean_shutdown(vm) session.xenapi.VM.clean_shutdown(vm)
try: try:
try: try:
filename_temp = filename+".tmp" filename_temp = filename+".tmp"
self.logger.debug("[%s] Copy in progress",self.backup_name) self.logger.debug("[%s] Copy in progress",self.backup_name)
if not str2bool(use_compression): if not str2bool(self.use_compression):
socket.setdefaulttimeout(120) socket.setdefaulttimeout(120)
scheme = "http://" scheme = "http://"
if str2bool(enable_https) == True: if str2bool(enable_https):
scheme = "https://" scheme = "https://"
url = scheme+user_xen+":"+password_xen+"@"+self.xcphost+"/export?use_compression="+self.use_compression+"&uuid="+session.xenapi.VM.get_uuid(vm) url = scheme+user_xen+":"+password_xen+"@"+self.xcphost+"/export?use_compression="+self.use_compression+"&uuid="+session.xenapi.VM.get_uuid(vm)
urllib.urlretrieve(url, filename_temp) urllib.urlretrieve(url, filename_temp)
urllib.urlcleanup() urllib.urlcleanup()
@ -164,7 +160,7 @@ class backup_xva(backup_generic):
raise raise
finally: finally:
if str2bool(halt_vm) == False: if not str2bool(halt_vm):
self.logger.debug("[%s] Destroy snapshot",'tisbackup-%s'%(vdi_name)) self.logger.debug("[%s] Destroy snapshot",'tisbackup-%s'%(vdi_name))
try: try:
for vbd in session.xenapi.VM.get_VBDs(snapshot): for vbd in session.xenapi.VM.get_VBDs(snapshot):
@ -173,10 +169,10 @@ class backup_xva(backup_generic):
else: else:
vdi = session.xenapi.VBD.get_VDI(vbd) vdi = session.xenapi.VBD.get_VDI(vbd)
if not 'NULL' in vdi: if not 'NULL' in vdi:
session.xenapi.VDI.destroy(vdi) session.xenapi.VDI.destroy(vdi)
session.xenapi.VM.destroy(snapshot) session.xenapi.VM.destroy(snapshot)
except XenAPI.Failure, error: except XenAPI.Failure, error:
return("error when destroy snapshot %s"%(error)) return("error when destroy snapshot %s"%(error))
elif status_vm == "Running": elif status_vm == "Running":
self.logger.debug("[%s] Starting in progress",self.backup_name) self.logger.debug("[%s] Starting in progress",self.backup_name)
@ -190,7 +186,7 @@ class backup_xva(backup_generic):
if os.path.exists(filename_temp): if os.path.exists(filename_temp):
tar = tarfile.open(filename_temp) tar = tarfile.open(filename_temp)
if not tar.getnames(): if not tar.getnames():
unlink(filename_temp) os.unlink(filename_temp)
return("Tar error") return("Tar error")
tar.close() tar.close()
if str2bool(self.verify_export): if str2bool(self.verify_export):
@ -206,7 +202,7 @@ class backup_xva(backup_generic):
try: try:
dest_filename = os.path.join(self.backup_dir,"%s-%s.%s" % (self.backup_name,self.backup_start_date,'xva')) dest_filename = os.path.join(self.backup_dir,"%s-%s.%s" % (self.backup_name,self.backup_start_date,'xva'))
options = [] options = []
options_params = " ".join(options) options_params = " ".join(options)
cmd = self.export_xva( vdi_name= self.server_name,filename= dest_filename, halt_vm= self.halt_vm, enable_https=self.enable_https, dry_run= self.dry_run, reuse_snapshot=self.reuse_snapshot) cmd = self.export_xva( vdi_name= self.server_name,filename= dest_filename, halt_vm= self.halt_vm, enable_https=self.enable_https, dry_run= self.dry_run, reuse_snapshot=self.reuse_snapshot)
if os.path.exists(dest_filename): if os.path.exists(dest_filename):
@ -220,7 +216,7 @@ class backup_xva(backup_generic):
stats['backup_location'] = dest_filename stats['backup_location'] = dest_filename
if cmd == 0: if cmd == 0:
stats['log']='XVA backup from %s OK, %d bytes written' % (self.server_name,stats['written_bytes']) stats['log']='XVA backup from %s OK, %d bytes written' % (self.server_name,stats['written_bytes'])
stats['status']='OK' stats['status']='OK'
else: else:
raise Exception(cmd) raise Exception(cmd)