Don't delete the image that we're currently running, for safety.

This commit is contained in:
Ian Gulliver
2016-05-11 05:12:43 +00:00
parent 8b08a5cec6
commit c302ffd87a
4 changed files with 14 additions and 5 deletions

View File

@@ -86,7 +86,7 @@ class Client(threadedclient.WebSocketClient):
return int(float(fh.readline().split(' ', 1)[0])) return int(float(fh.readline().split(' ', 1)[0]))
def _NextTimestamp(self): def _NextTimestamp(self):
next_image = os.path.basename(os.readlink('/isodevice/iconograph/current')) next_image = lib.GetCurrentImage()
return int(next_image.split('.', 1)[0]) return int(next_image.split('.', 1)[0])
def _OnImageTypes(self, data): def _OnImageTypes(self, data):
@@ -106,7 +106,7 @@ class Client(threadedclient.WebSocketClient):
FLAGS.https_client_cert, FLAGS.https_client_cert,
FLAGS.https_client_key) FLAGS.https_client_key)
fetch.Fetch() fetch.Fetch()
fetch.DeleteOldImages() fetch.DeleteOldImages(skip={'%d.iso' % self._config['timestamp']})
update = update_grub.GrubUpdater( update = update_grub.GrubUpdater(
FLAGS.image_dir, FLAGS.image_dir,

View File

@@ -166,9 +166,10 @@ class Fetcher(object):
self._FetchImage(image) self._FetchImage(image)
self._SetCurrent(image) self._SetCurrent(image)
def DeleteOldImages(self, max_images=5): def DeleteOldImages(self, max_images=5, skip=None):
if not max_images: if not max_images:
return return
skip = skip or set()
images = [] images = []
for filename in os.listdir(self._image_dir): for filename in os.listdir(self._image_dir):
match = self._FILE_REGEX.match(filename) match = self._FILE_REGEX.match(filename)
@@ -177,6 +178,8 @@ class Fetcher(object):
images.append((int(match.group('timestamp')), filename)) images.append((int(match.group('timestamp')), filename))
images.sort(reverse=True) images.sort(reverse=True)
for timestamp, filename in images[max_images:]: for timestamp, filename in images[max_images:]:
if filename in skip:
continue
print('Deleting old image:', filename, flush=True) print('Deleting old image:', filename, flush=True)
path = os.path.join(self._image_dir, filename) path = os.path.join(self._image_dir, filename)
os.unlink(path) os.unlink(path)

View File

@@ -1,3 +1,4 @@
import os
import re import re
import subprocess import subprocess
@@ -13,3 +14,8 @@ def GetVolumeID(path):
]) ])
match = _VOLUME_ID_REGEX.search(isoinfo) match = _VOLUME_ID_REGEX.search(isoinfo)
return match.group('volume_id').decode('ascii') return match.group('volume_id').decode('ascii')
def GetCurrentImage(image_dir='/isodevice/iconograph'):
current_path = os.path.join(image_dir, 'current')
return os.path.basename(os.readlink(current_path))

View File

@@ -23,13 +23,13 @@ class GrubUpdater(object):
with tempfile.NamedTemporaryFile('w', dir=grub_dir, delete=False) as fh: with tempfile.NamedTemporaryFile('w', dir=grub_dir, delete=False) as fh:
try: try:
current = os.readlink(os.path.join(self._image_dir, 'current')) current = lib.GetCurrentImage(self._image_dir)
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': current,
}) })
files = [] files = []