diff options
-rw-r--r-- | ogcp/views.py | 33 |
1 files 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: |