Client-side image lifetime management.
This commit is contained in:
@@ -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"
|
--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
|
#### persistent.py
|
||||||
|
|
||||||
Mount a /persistent partition from a filesystem with LABEL=PERSISTENT. Allows
|
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%.
|
[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
|
`--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.
|
`--other-cert` specifies a chain certificate, such as your intermediate cert.
|
||||||
It may be specified more than once.
|
It may be specified more than once.
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import codecs
|
|||||||
import json
|
import json
|
||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
@@ -30,6 +31,12 @@ parser.add_argument(
|
|||||||
dest='image_dir',
|
dest='image_dir',
|
||||||
action='store',
|
action='store',
|
||||||
required=True)
|
required=True)
|
||||||
|
parser.add_argument(
|
||||||
|
'--max-images',
|
||||||
|
dest='max_images',
|
||||||
|
action='store',
|
||||||
|
type=int,
|
||||||
|
default=5)
|
||||||
FLAGS = parser.parse_args()
|
FLAGS = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
@@ -49,6 +56,7 @@ class Fetcher(object):
|
|||||||
|
|
||||||
_BUF_SIZE = 2 ** 16
|
_BUF_SIZE = 2 ** 16
|
||||||
_MAX_BP = 10000
|
_MAX_BP = 10000
|
||||||
|
_FILE_REGEX = re.compile('^(?P<timestamp>\d+)\.iso$')
|
||||||
|
|
||||||
def __init__(self, base_url, ca_cert, image_dir):
|
def __init__(self, base_url, ca_cert, image_dir):
|
||||||
self._base_url = base_url
|
self._base_url = base_url
|
||||||
@@ -158,10 +166,26 @@ class Fetcher(object):
|
|||||||
self._FetchImage(image)
|
self._FetchImage(image)
|
||||||
self._SetCurrent(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():
|
def main():
|
||||||
fetcher = Fetcher(FLAGS.base_url, FLAGS.ca_cert, FLAGS.image_dir)
|
fetcher = Fetcher(FLAGS.base_url, FLAGS.ca_cert, FLAGS.image_dir)
|
||||||
fetcher.Fetch()
|
fetcher.Fetch()
|
||||||
|
fetcher.DeleteOldImages(FLAGS.max_images)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ parser.add_argument(
|
|||||||
dest='chroot_path',
|
dest='chroot_path',
|
||||||
action='store',
|
action='store',
|
||||||
required=True)
|
required=True)
|
||||||
|
parser.add_argument(
|
||||||
|
'--max-images',
|
||||||
|
dest='max_images',
|
||||||
|
action='store',
|
||||||
|
type=int,
|
||||||
|
default=5)
|
||||||
FLAGS = parser.parse_args()
|
FLAGS = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
@@ -53,8 +59,9 @@ def main():
|
|||||||
|
|
||||||
path = os.path.join(FLAGS.chroot_path, 'iconograph', 'client', 'flags')
|
path = os.path.join(FLAGS.chroot_path, 'iconograph', 'client', 'flags')
|
||||||
with open(path, 'w') as fh:
|
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,
|
'base_url': FLAGS.base_url,
|
||||||
|
'max_images': FLAGS.max_images,
|
||||||
})
|
})
|
||||||
|
|
||||||
os.symlink(
|
os.symlink(
|
||||||
|
|||||||
Reference in New Issue
Block a user