From 5cbb4a7562d126dd8eaa0a304f95274edf074582 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 3 Jul 2024 15:48:10 +0200 Subject: views: skip cache space checks if already contains the image Skip checks of image_fits_in_cache() if the image data contains not 'size' or 'checksum' information. Skip checks for the clients with the image already in cache. --- ogcp/views.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/ogcp/views.py b/ogcp/views.py index 3ceffdf..1f84621 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -782,24 +782,30 @@ def get_clients_repo(server, ips): return None return repo_id -def image_fits_in_cache(server, ips, image_size): - err_report = "" - r = server.get('/cache/list', payload={'clients': ips}) - if not r: - return ogserver_down('commands') - if r.status_code != requests.codes.ok: - return ogserver_error('commands') +def image_fits_in_cache(server, clients_info, image): + image_size = int(image.get('size', '0')) + image_checksum = image.get('checksum', '') - clients_info = r.json()['clients'] + if not (image_size and image_checksum): + return True + + err_report = "" for client_info in clients_info: ip = client_info['ip'] cache_size = int(client_info['cache_size']) used_cache = 0 + has_image = False; for image_info in client_info['images']: used_cache += int(image_info['size']) + if image_info['checksum'] == image_checksum: + has_image = True + + if has_image: + continue + free_cache = cache_size - used_cache if free_cache < image_size: missing_cache = image_size - free_cache @@ -844,8 +850,15 @@ def action_image_restore(): flash(_(f'The image size is bigger than the target partition'), category='error') return redirect(url_for('commands')) - image_size = int(image['size']) - if requires_cache and image_size and not image_fits_in_cache(server, ips, image_size): + r = server.get('/cache/list', payload={'clients': ips}) + if not r: + return ogserver_down('commands') + if r.status_code != requests.codes.ok: + return ogserver_error('commands') + + clients_info = r.json()['clients'] + + if requires_cache and not image_fits_in_cache(server, clients_info, image): return redirect(url_for('commands')) try: -- cgit v1.2.3-18-g5258