Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
[RFR][NOTEST]Unzip rework (#8233)
Browse files Browse the repository at this point in the history
* GCE upload uses archived file so extraction removed

* Reworked try/except in download_image
  • Loading branch information
mmojzis authored and mshriver committed Dec 4, 2018
1 parent 904eff2 commit 71a2db4
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions cfme/utils/template/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import re
import tarfile


from contextlib import closing
Expand Down Expand Up @@ -241,12 +240,13 @@ def deploy_template(self):

@log_wrap("download image locally")
def download_image(self):
ARCHIVE_TYPES = ['zip']
suffix = re.compile(
r'^.*?[.](?P<ext>tar\.gz|tar\.bz2|\w+)$').match(self.image_name).group('ext')
# Check if file exists already:
if path.isfile(self.local_file_path):
if path.isfile(self.image_name):
logger.info('Local image found, skipping download: %s', self.local_file_path)
if suffix not in ['zip', 'tar.gz']:
if suffix not in ARCHIVE_TYPES:
return True
else:
# Download file to cli-tool-client
Expand All @@ -260,27 +260,36 @@ def download_image(self):
# For EC2 and SCVMM images is zip used and for GCE is tar.gz used.

archive_path = self.image_name
try:
if suffix == 'zip':
archive = ZipFile(archive_path)
zipinfo = archive.infolist()
self._unzipped_file = zipinfo[0].filename
elif suffix == 'tar.gz':
archive = tarfile.open(archive_path, "r:gz")
self._unzipped_file = archive.firstmember.name
else:
return True
if path.isfile(self.image_name):
os.remove(self.image_name)
logger.info('Image archived - unzipping as : %s', self._unzipped_file)
archive.extractall()
archive.close()
# remove the archive
os.remove(archive_path)
if suffix not in ARCHIVE_TYPES:
return True
except Exception:
logger.exception("{} archive unzip failed.".format(suffix))
return False
else:
if suffix == 'zip':
try:
archive = ZipFile(archive_path)
zipinfo = archive.infolist()
self._unzipped_file = zipinfo[0].filename
except Exception:
logger.exception("Getting information of {} archive failed.".format(
self.image_name))
return False

if path.isfile(self.image_name):
try:
os.remove(self.image_name)
except Exception:
logger.exception("Deleting previously unpacked file {} failed.".format(
self.image_name))
return False
logger.info("Image archived - unpacking as : {}".format(self._unzipped_file))
try:
archive.extractall()
archive.close()
# remove the archive
os.remove(archive_path)
return True
except Exception:
logger.exception("{} archive unpacked failed.".format(suffix))
return False

@log_wrap('add template to glance')
def glance_upload(self):
Expand Down

0 comments on commit 71a2db4

Please sign in to comment.