live: improve exception handling in image_create

Reduce the scope of the try except block that controls the case
of deleting the image backup in case of error. Now it only covers
the section of code after backup creation and up to image
verification. Check when the Exception is an OgError to raise
with added context.

Prevent the deletion of the target image in case of error before
the backup creation.

Bundle the backup creation on its own try except block to give
more feedback on a failed backup creation.

Enables a better error management allowing unhandled
exceptions to be reported properly.
master
Alejandro Sirgo Rica 2024-04-03 11:09:23 +02:00
parent cbe7f8d49c
commit e19437290d
1 changed files with 43 additions and 36 deletions

View File

@ -421,45 +421,48 @@ class OgLiveOperations:
if ogRest.terminated:
return
diskname = get_disks()[disk-1]
cxt = fdisk.Context(f'/dev/{diskname}', details=True)
pa = None
for i, p in enumerate(cxt.partitions):
if (p.partno + 1) == partition:
pa = cxt.partitions[i]
if pa is None:
self._restartBrowser(self._url)
raise OgError(f'Target partition /dev/{diskname} not found')
padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE)
fstype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_FSTYPE)
if not fstype:
raise OgError(f'No filesystem detected in {padev}. Aborting image creation')
if change_access(user=self._smb_user, pwd=self._smb_pass) == -1:
raise OgError('remount of /opt/opengnsys/images has failed')
if os.access(f'/opt/opengnsys/images', os.R_OK | os.W_OK) == False:
raise OgError('Cannot access /opt/opengnsys/images in read and write mode, check permissions')
if os.access(f'{image_path}', os.R_OK) == True:
logging.info(f'image file {image_path} already exists, updating.')
copy_windows_efi_bootloader(disk, partition)
if ogReduceFs(disk, partition) == -1:
raise OgError(f'Failed to shrink {fstype} filesystem in {padev}')
cmd1 = shlex.split(f'partclone.{fstype} -I -C --clone -s {padev} -O -')
cmd2 = shlex.split(f'lzop -1 -fo {image_path}')
logfile = open('/tmp/command.log', 'wb', 0)
try:
diskname = get_disks()[disk-1]
cxt = fdisk.Context(f'/dev/{diskname}', details=True)
pa = None
for i, p in enumerate(cxt.partitions):
if (p.partno + 1) == partition:
pa = cxt.partitions[i]
if pa is None:
self._restartBrowser(self._url)
raise OgError(f'Target partition /dev/{diskname} not found')
padev = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE)
fstype = cxt.partition_to_string(pa, fdisk.FDISK_FIELD_FSTYPE)
if not fstype:
raise OgError(f'No filesystem detected in {padev}. Aborting image creation')
if change_access(user=self._smb_user, pwd=self._smb_pass) == -1:
raise OgError('remount of /opt/opengnsys/images has failed')
if os.access(f'/opt/opengnsys/images', os.R_OK | os.W_OK) == False:
raise OgError('Cannot access /opt/opengnsys/images in read and write mode, check permissions')
if os.access(f'{image_path}', os.R_OK) == True:
logging.info(f'image file {image_path} already exists, updating.')
copy_windows_efi_bootloader(disk, partition)
if ogReduceFs(disk, partition) == -1:
raise OgError(f'Failed to shrink {fstype} filesystem in {padev}')
cmd1 = shlex.split(f'partclone.{fstype} -I -C --clone -s {padev} -O -')
cmd2 = shlex.split(f'lzop -1 -fo {image_path}')
logfile = open('/tmp/command.log', 'wb', 0)
if os.path.exists(image_path) and backup:
shutil.move(image_path, f'{image_path}.ant')
except OSError as e:
raise OgError(f'Cannot create backup for {image_path}: {e}') from e
try:
p1 = Popen(cmd1, stdout=PIPE, stderr=logfile)
p2 = Popen(cmd2, stdin=p1.stdout)
p1.stdout.close()
@ -493,7 +496,11 @@ class OgLiveOperations:
shutil.move(f'{image_path}.ant', image_path)
self._restartBrowser(self._url)
raise OgError(f'Failed to create image for {fstype} filesystem in device {padev}: {e}') from e
if isinstance(e, OgError):
raise OgError(f'Failed to create image for {fstype} filesystem in device {padev}: {e}') from e
else:
raise
try:
st = os.stat(image_path)