Commit bc832dcf by Carpoon

add folder option to disk export

add folder option to disk export
use finally insted of repat clenup code
parent 944ddb8b
......@@ -353,7 +353,6 @@ class Disk(object):
if 'qcow' in ftype.lower():
move(downloaded_file, self.get_path())
else:
logger.debug("Calling qemu-img with command line: %s" % str(cmdline))
cmdline = ['ionice', '-c', 'idle',
'qemu-img', 'convert',
'-m', '4', '-O', 'qcow2',
......@@ -362,36 +361,32 @@ class Disk(object):
logger.debug("Calling qemu-img with command line: %s" % str(cmdline))
subprocess.check_output(cmdline)
except AbortException:
if os.path.exists(downloaded_file):
os.unlink(downloaded_file)
if os.path.exists(self.get_path()):
os.unlink(self.get_path())
logger.info("Import of disk %s aborted" % self.name)
except:
if os.path.exists(downloaded_file):
os.unlink(downloaded_file)
if os.path.exists(self.get_path()):
os.unlink(self.get_path())
raise
else:
if os.path.exists(downloaded_file):
os.unlink(downloaded_file)
if not self.check_valid_image():
os.unlink(self.get_path())
raise Exception("Invalid file format.")
self.size = Disk.get(self.dir, self.name).size
finally:
if os.path.exists(downloaded_file):
os.unlink(downloaded_file)
def export_disk_to_datastore(self, task, disk_format, datastore):
def export_disk_to_datastore(self, task, disk_format, datastore, folder="exports"):
logger.debug("Start disk export to datastore...")
exported_path = os.path.join(datastore, "exports", os.path.basename(self.get_path()) + '.' + disk_format)
exported_path = os.path.join(datastore, folder, os.path.basename(self.get_path()) + '.' + disk_format)
cmdline = ['ionice', '-c', 'idle', 'qemu-img', 'convert']
if disk_format == 'qcow2' or disk_format == 'qcow':
cmdline.append('-c')
cmdline.extend(['-m', '4', '-O', disk_format, self.get_path(), exported_path])
logger.debug("Calling qemu-img with command line: %s" % str(cmdline))
subprocess.check_output(cmdline)
return os.path.basename(exported_path)
def export_disk(self, task, disk_format, upload_link, port=22, identity=None, mode="scp"):
logger.debug("Start disk export...")
......@@ -431,6 +426,7 @@ class Disk(object):
if proc.returncode == 0:
break
else:
sleep(2)
logger.error("Copy over ssh failed with return code: %s, will try %s more time(s)..." %
(str(proc.returncode), str(i)))
if proc.stdout is not None:
......@@ -536,7 +532,6 @@ class Disk(object):
logger.warning("Aborted merge job, removing %s",
new_disk.get_path())
os.unlink(new_disk.get_path())
except:
if proc:
proc.terminate()
......
......@@ -96,8 +96,9 @@ class export_disk_to_datastore(AbortableTask):
disk_desc = kwargs["disk_desc"]
disk_format = kwargs["disk_format"]
datastore = kwargs["datastore"]
folder = kwargs["folder"]
disk = Disk.deserialize(disk_desc)
return disk.export_disk_to_datastore(self, disk_format, datastore)
return disk.export_disk_to_datastore(self, disk_format, datastore, folder)
@celery.task()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment