Commit b171f0a4 by Bálint Máhonfai

Fix indentation errors

parent 63579fd2
...@@ -299,121 +299,74 @@ class Disk(object): ...@@ -299,121 +299,74 @@ class Disk(object):
finally: finally:
os.unlink(exported_path) os.unlink(exported_path)
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: isos = z.namelist()
isos = z.namelist() if len(isos) != 1:
if len(isos) != 1: isos = [i for i in isos
isos = [i for i in isos if i.lower().endswith('.iso')]
if i.lower().endswith('.iso')] if len(isos) == 1:
if len(isos) == 1: logger.info('Unzipping %s started.', disk_path)
logger.info('Unzipping %s started.', disk_path) f = open(disk_path + '~', 'wb')
f = open(disk_path + '~', 'wb') zf = z.open(isos[0])
zf = z.open(isos[0]) with zf, f:
with zf, f: copyfileobj(zf, f)
copyfileobj(zf, f) f.flush()
f.flush() move(disk_path + '~', disk_path)
move(disk_path + '~', disk_path) else:
logger.info("Extracting %s failed, keeping original.",
disk_path)
def snapshot(self):
""" Creating qcow2 snapshot with base image.
"""
# Check if snapshot type and qcow2 format matchmatch
if self.type != 'snapshot':
raise Exception('Invalid type: %s' % self.type)
# Check if file already exists
if os.path.isfile(self.get_path()):
raise Exception('File already exists: %s' % self.get_path())
# Check if base file exist
if not os.path.isfile(self.get_base()):
raise Exception('Image Base does not exists: %s' % self.get_base())
# Build list of Strings as command parameters
if self.format == 'iso':
os.symlink(self.get_base(), self.get_path())
elif self.format == 'raw':
raise NotImplemented()
else: else:
logger.info("Extracting %s failed, keeping original.", cmdline = ['qemu-img',
disk_path) 'create',
'-b', self.get_base(),
'-f', self.format,
def snapshot(self): self.get_path()]
""" Creating qcow2 snapshot with base image. # Call subprocess
""" subprocess.check_output(cmdline)
# Check if snapshot type and qcow2 format matchmatch
if self.type != 'snapshot': def merge_disk_with_base(self, task, new_disk, parent_id=None):
raise Exception('Invalid type: %s' % self.type) proc = None
# Check if file already exists try:
if os.path.isfile(self.get_path()): cmdline = [
raise Exception('File already exists: %s' % self.get_path()) 'qemu-img', 'convert', self.get_path(),
# Check if base file exist '-O', new_disk.format, new_disk.get_path()]
if not os.path.isfile(self.get_base()): # Call subprocess
raise Exception('Image Base does not exists: %s' % self.get_base()) logger.debug(
# Build list of Strings as command parameters "Merging %s into %s.", self.get_path(),
if self.format == 'iso': new_disk.get_path())
os.symlink(self.get_base(), self.get_path()) percent = 0
elif self.format == 'raw': diff_disk = Disk.get(self.dir, self.name)
raise NotImplemented() base_disk = Disk.get(self.dir, self.base_name)
else: clen = min(base_disk.actual_size + diff_disk.actual_size,
cmdline = ['qemu-img', diff_disk.size)
'create', output = new_disk.get_path()
'-b', self.get_base(), proc = subprocess.Popen(cmdline)
'-f', self.format,
self.get_path()]
# Call subprocess
subprocess.check_output(cmdline)
def merge_disk_with_base(self, task, new_disk, parent_id=None):
proc = None
try:
cmdline = [
'qemu-img', 'convert', self.get_path(),
'-O', new_disk.format, new_disk.get_path()]
# Call subprocess
logger.debug(
"Merging %s into %s.", self.get_path(),
new_disk.get_path())
percent = 0
diff_disk = Disk.get(self.dir, self.name)
base_disk = Disk.get(self.dir, self.base_name)
clen = min(base_disk.actual_size + diff_disk.actual_size,
diff_disk.size)
output = new_disk.get_path()
proc = subprocess.Popen(cmdline)
while True:
if proc.poll() is not None:
break
try:
actsize = os.path.getsize(output)
except OSError:
actsize = 0
new_percent = min(100, round(actsize * 100.0 / clen))
if new_percent > percent:
percent = new_percent
if not task.is_aborted():
task.update_state(
task_id=parent_id,
state=task.AsyncResult(parent_id).state,
meta={'size': actsize, 'percent': percent})
else:
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 merge job, removing %s",
new_disk.get_path())
os.unlink(new_disk.get_path())
except:
if proc:
proc.terminate()
logger.exception("Unknown error occured, removing %s ",
new_disk.get_path())
os.unlink(new_disk.get_path())
raise
def merge_disk_without_base(self, task, new_disk, parent_id=None,
length=1024 * 1024):
try:
fsrc = open(self.get_path(), 'rb')
fdst = open(new_disk.get_path(), 'wb')
clen = self.size
actsize = 0
percent = 0
with fsrc, fdst:
while True: while True:
buf = fsrc.read(length) if proc.poll() is not None:
if not buf:
break break
fdst.write(buf) try:
actsize += len(buf) actsize = os.path.getsize(output)
except OSError:
actsize = 0
new_percent = min(100, round(actsize * 100.0 / clen)) new_percent = min(100, round(actsize * 100.0 / clen))
if new_percent > percent: if new_percent > percent:
percent = new_percent percent = new_percent
...@@ -427,42 +380,82 @@ def merge_disk_without_base(self, task, new_disk, parent_id=None, ...@@ -427,42 +380,82 @@ def merge_disk_without_base(self, task, new_disk, parent_id=None,
"Merging new disk %s is aborted by user.", "Merging new disk %s is aborted by user.",
new_disk.get_path()) new_disk.get_path())
raise AbortException() raise AbortException()
except AbortException: sleep(1)
logger.warning("Aborted remove %s", new_disk.get_path()) except AbortException:
os.unlink(new_disk.get_path()) proc.terminate()
except: logger.warning("Aborted merge job, removing %s",
logger.exception("Unknown error occured removing %s ", new_disk.get_path())
new_disk.get_path()) os.unlink(new_disk.get_path())
os.unlink(new_disk.get_path())
raise
def merge(self, task, new_disk, parent_id=None):
""" Merging a new_disk from the actual disk and its base.
"""
if task.is_aborted(): except:
raise AbortException() if proc:
proc.terminate()
logger.exception("Unknown error occured, removing %s ",
new_disk.get_path())
os.unlink(new_disk.get_path())
raise
def merge_disk_without_base(self, task, new_disk, parent_id=None,
length=1024 * 1024):
try:
fsrc = open(self.get_path(), 'rb')
fdst = open(new_disk.get_path(), 'wb')
clen = self.size
actsize = 0
percent = 0
with fsrc, fdst:
while True:
buf = fsrc.read(length)
if not buf:
break
fdst.write(buf)
actsize += len(buf)
new_percent = min(100, round(actsize * 100.0 / clen))
if new_percent > percent:
percent = new_percent
if not task.is_aborted():
task.update_state(
task_id=parent_id,
state=task.AsyncResult(parent_id).state,
meta={'size': actsize, 'percent': percent})
else:
logger.warning(
"Merging new disk %s is aborted by user.",
new_disk.get_path())
raise AbortException()
except AbortException:
logger.warning("Aborted remove %s", new_disk.get_path())
os.unlink(new_disk.get_path())
except:
logger.exception("Unknown error occured removing %s ",
new_disk.get_path())
os.unlink(new_disk.get_path())
raise
# Check if file already exists def merge(self, task, new_disk, parent_id=None):
if os.path.isfile(new_disk.get_path()): """ Merging a new_disk from the actual disk and its base.
raise Exception('File already exists: %s' % self.get_path()) """
if self.format == "iso": if task.is_aborted():
os.symlink(self.get_path(), new_disk.get_path()) raise AbortException()
elif self.base_name:
self.merge_disk_with_base(task, new_disk, parent_id)
else:
self.merge_disk_without_base(task, new_disk, parent_id)
# Check if file already exists
if os.path.isfile(new_disk.get_path()):
raise Exception('File already exists: %s' % self.get_path())
def delete(self): if self.format == "iso":
""" Delete file. """ os.symlink(self.get_path(), new_disk.get_path())
if os.path.isfile(self.get_path()): elif self.base_name:
os.unlink(self.get_path()) self.merge_disk_with_base(task, new_disk, parent_id)
else:
self.merge_disk_without_base(task, new_disk, parent_id)
def delete(self):
""" Delete file. """
if os.path.isfile(self.get_path()):
os.unlink(self.get_path())
@classmethod @classmethod
def list(cls, dir): def list(cls, dir):
""" List all files in <dir> directory.""" """ List all files in <dir> directory."""
return [cls.get(dir, file) for file in os.listdir(dir)] return [cls.get(dir, file) for file in os.listdir(dir)]
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