diff --git a/README.md b/README.md index d36d7d6..f110909 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,11 @@ Use the build_image.py flag: --module="server/modules/iconograph.py --base-url=http://yourhost/ --ca-cert=/path/to/signing/cert.pem" ``` +Optional flags: + +`--max-images` sets the number of recent images to keep. Older images are +deleted. Defaults to 5. 0 means unlimited. + #### persistent.py Mount a /persistent partition from a filesystem with LABEL=PERSISTENT. Allows @@ -109,7 +114,7 @@ defaults to zero. The units are [basis points](https://en.wikipedia.org/wiki/Basis_point); 10000 means 100%. `--max-images` sets the number of recent images to keep. Older images are -deleted. +deleted. Defaults to 0, meaning unlimited. `--other-cert` specifies a chain certificate, such as your intermediate cert. It may be specified more than once. diff --git a/client/fetcher.py b/client/fetcher.py index 4c05f34..2fed42f 100755 --- a/client/fetcher.py +++ b/client/fetcher.py @@ -5,6 +5,7 @@ import codecs import json import hashlib import os +import re import shutil import socket import struct @@ -30,6 +31,12 @@ parser.add_argument( dest='image_dir', action='store', required=True) +parser.add_argument( + '--max-images', + dest='max_images', + action='store', + type=int, + default=5) FLAGS = parser.parse_args() @@ -49,6 +56,7 @@ class Fetcher(object): _BUF_SIZE = 2 ** 16 _MAX_BP = 10000 + _FILE_REGEX = re.compile('^(?P\d+)\.iso$') def __init__(self, base_url, ca_cert, image_dir): self._base_url = base_url @@ -158,10 +166,26 @@ class Fetcher(object): self._FetchImage(image) self._SetCurrent(image) + def DeleteOldImages(self, max_images): + if not max_images: + return + images = [] + for filename in os.listdir(self._image_dir): + match = self._FILE_REGEX.match(filename) + if not match: + continue + images.append((int(match.group('timestamp')), filename)) + images.sort(reverse=True) + for timestamp, filename in images[max_images:]: + print('Deleting old image:', filename) + path = os.path.join(self._image_dir, filename) + os.unlink(path) + def main(): fetcher = Fetcher(FLAGS.base_url, FLAGS.ca_cert, FLAGS.image_dir) fetcher.Fetch() + fetcher.DeleteOldImages(FLAGS.max_images) if __name__ == '__main__': diff --git a/server/modules/iconograph.py b/server/modules/iconograph.py index a2afef4..345e5e8 100755 --- a/server/modules/iconograph.py +++ b/server/modules/iconograph.py @@ -22,6 +22,12 @@ parser.add_argument( dest='chroot_path', action='store', required=True) +parser.add_argument( + '--max-images', + dest='max_images', + action='store', + type=int, + default=5) FLAGS = parser.parse_args() @@ -53,8 +59,9 @@ def main(): path = os.path.join(FLAGS.chroot_path, 'iconograph', 'client', 'flags') with open(path, 'w') as fh: - fh.write('--base-url=%(base_url)s\n' % { + fh.write('--base-url=%(base_url)s --max-images=%(max_images)d\n' % { 'base_url': FLAGS.base_url, + 'max_images': FLAGS.max_images, }) os.symlink(