More reliable cleanup

This commit is contained in:
Ian Gulliver
2016-05-10 20:55:50 +00:00
parent 927b851b06
commit f43e9913e9
2 changed files with 33 additions and 30 deletions

View File

@@ -130,17 +130,17 @@ class Fetcher(object):
resp = self._session.get(url, stream=True) resp = self._session.get(url, stream=True)
hash_obj = hashlib.sha256() hash_obj = hashlib.sha256()
try: with tempfile.NamedTemporaryFile(dir=self._image_dir, delete=False) as fh:
fh = tempfile.NamedTemporaryFile(dir=self._image_dir, delete=False) try:
for data in resp.iter_content(self._BUF_SIZE): for data in resp.iter_content(self._BUF_SIZE):
hash_obj.update(data) hash_obj.update(data)
fh.write(data) fh.write(data)
if hash_obj.hexdigest() != image['hash']: if hash_obj.hexdigest() != image['hash']:
raise InvalidHash raise InvalidHash
os.rename(fh.name, path) os.rename(fh.name, path)
except: except:
os.unlink(fh.name) os.unlink(fh.name)
raise raise
def _SetCurrent(self, image): def _SetCurrent(self, image):
filename = '%d.iso' % (image['timestamp']) filename = '%d.iso' % (image['timestamp'])

View File

@@ -32,25 +32,25 @@ class GrubUpdater(object):
def Update(self): def Update(self):
grub_dir = os.path.join(self._boot_dir, 'grub') grub_dir = os.path.join(self._boot_dir, 'grub')
# TODO: clean up if we fail here
with tempfile.NamedTemporaryFile('w', dir=grub_dir, delete=False) as fh: with tempfile.NamedTemporaryFile('w', dir=grub_dir, delete=False) as fh:
current = os.readlink(os.path.join(self._image_dir, 'current')) try:
current = os.readlink(os.path.join(self._image_dir, 'current'))
fh.write(""" fh.write("""
set timeout=5 set timeout=5
set default=%(default_image_filename)s set default=%(default_image_filename)s
""" % { """ % {
'default_image_filename': os.path.basename(current), 'default_image_filename': os.path.basename(current),
}) })
files = [] files = []
for filename in os.listdir(self._image_dir): for filename in os.listdir(self._image_dir):
if not filename.endswith('.iso'): if not filename.endswith('.iso'):
continue continue
files.append(filename) files.append(filename)
for i, filename in enumerate(sorted(files, reverse=True)): for i, filename in enumerate(sorted(files, reverse=True)):
fh.write(""" fh.write("""
menuentry "%(image_filename)s (%(volume_id)s)" --hotkey=%(hotkey)s { menuentry "%(image_filename)s (%(volume_id)s)" --hotkey=%(hotkey)s {
search --no-floppy --file --set=root %(image_path)s/%(image_filename)s search --no-floppy --file --set=root %(image_path)s/%(image_filename)s
iso_path="%(image_path)s/%(image_filename)s" iso_path="%(image_path)s/%(image_filename)s"
@@ -60,11 +60,14 @@ menuentry "%(image_filename)s (%(volume_id)s)" --hotkey=%(hotkey)s {
configfile /boot/grub/loopback.cfg configfile /boot/grub/loopback.cfg
} }
""" % { """ % {
'image_filename': filename, 'image_filename': filename,
'image_path': self._image_path, 'image_path': self._image_path,
'hotkey': self._HOTKEYS[i], 'hotkey': self._HOTKEYS[i],
'volume_id': self._GetVolumeID(os.path.join(self._image_dir, filename)), 'volume_id': self._GetVolumeID(os.path.join(self._image_dir, filename)),
}) })
fh.flush() fh.flush()
os.rename(fh.name, os.path.join(grub_dir, 'grub.cfg')) os.rename(fh.name, os.path.join(grub_dir, 'grub.cfg'))
except:
os.unlink(fh.name)
raise