Commit 5b820fc0 by Guba Sándor

merge task rewrite

parent d383d7cd
...@@ -19,6 +19,7 @@ re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|' ...@@ -19,6 +19,7 @@ re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|'
class Disk(object): class Disk(object):
''' Storage driver DISK object. ''' Storage driver DISK object.
Handle qcow2, raw and iso images. Handle qcow2, raw and iso images.
TYPES, CREATE_TYPES, SNAPSHOT_TYPES are hand managed restrictions. TYPES, CREATE_TYPES, SNAPSHOT_TYPES are hand managed restrictions.
...@@ -139,7 +140,8 @@ class Disk(object): ...@@ -139,7 +140,8 @@ class Disk(object):
logger.info("Downloading image from %s to %s", url, disk_path) logger.info("Downloading image from %s to %s", url, disk_path)
r = requests.get(url, stream=True) r = requests.get(url, stream=True)
if r.status_code != 200: if r.status_code != 200:
raise Exception("Invalid response status code: %s" % r.status_code) raise Exception("Invalid response status code: %s at %s" %
(r.status_code, url))
class AbortException(Exception): class AbortException(Exception):
pass pass
...@@ -200,7 +202,7 @@ class Disk(object): ...@@ -200,7 +202,7 @@ class Disk(object):
if not self.check_valid_image(): if not self.check_valid_image():
os.unlink(disk_path) os.unlink(disk_path)
raise Exception("Invalid file format. Only qcow and " raise Exception("Invalid file format. Only qcow and "
"iso files are allowed.") "iso files are allowed. Image from: %s" % url)
def extract_iso_from_zip(self, disk_path): def extract_iso_from_zip(self, disk_path):
with ZipFile(disk_path, 'r') as z: with ZipFile(disk_path, 'r') as z:
...@@ -246,24 +248,65 @@ class Disk(object): ...@@ -246,24 +248,65 @@ class Disk(object):
# Call subprocess # Call subprocess
subprocess.check_output(cmdline) subprocess.check_output(cmdline)
def merge(self, new_disk): def merge(self, task, new_disk):
""" Merging a new_disk from the actual disk and its base. """ Merging a new_disk from the actual disk and its base.
""" """
from time import sleep
import threading
class AbortException(Exception):
pass
if task.is_aborted():
raise AbortException()
# Check if file already exists # Check if file already exists
if os.path.isfile(new_disk.get_path()): if os.path.isfile(new_disk.get_path()):
raise Exception('File already exists: %s' % self.get_path()) raise Exception('File already exists: %s' % self.get_path())
if self.format == "iso": if self.format == "iso":
os.symlink(self.get_path(), new_disk.get_path()) os.symlink(self.get_path(), new_disk.get_path())
elif self.base_name: elif self.base_name:
cmdline = ['qemu-img', try:
'convert', cmdline = ['qemu-img',
self.get_path(), 'convert',
'-O', new_disk.format, self.get_path(),
new_disk.get_path()] '-O', new_disk.format,
# Call subprocess new_disk.get_path()]
subprocess.check_output(cmdline) # Call subprocess
logger.debug(
"Merging %s into %s.", self.get_path(),
new_disk.get_path())
proc = subprocess.Popen(cmdline)
while True:
if proc.poll() is not None:
break
if task.is_aborted():
logger.warning(
"Merging new disk %s is aborted by user.",
new_disk.get_path())
raise AbortException
sleep(1)
except AbortException:
proc.terminate()
logger.warning("Aborted remove %s", new_disk.get_path())
os.unlink(new_disk.get_path())
else: else:
copy(self.get_path(), new_disk.get_path()) try:
t = threading.Thread(
target=copy,
args=(
self.get_path(),
new_disk.get_path()))
while True:
if not t.isAlive():
break
if task.is_aborted():
logger.warning(
"Merging new disk %s is aborted by user.",
new_disk.get_path())
raise AbortException
except AbortException:
t._Thread__stop()
logger.warning("Aborted remove %s", new_disk.get_path())
os.unlink(new_disk.get_path())
def delete(self): def delete(self):
""" Delete file. """ """ Delete file. """
......
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