From 9cb3a952b4ceb3c33e8481250179023fdf5ceebb Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Thu, 16 May 2024 12:38:28 +0200 Subject: ogcp: add cache management page Implement cache management through cache/list and cache/delete API REST methods. The view corresponds to the URL action/cache and contains three main parts: - Free cache available in the client's bubbles. - Image selection form to request the deletion and view the client with that image in cache. - Cache details per client to see total, used and available cache. The main usecase for the view is the removal of cache to make room for new images in clients with slow connections. Checking clients where available cache space is not enough to hold the new image and then requesting removal of specific images. The html template receives the following structures from the view: storage_data[ip] = {'used': 223452345, 'total': 2345234523452} client_images[ip] = [f'{image_name}.{image_checksum}', ...] image_data[f'{image_name}.{image_checksum}'] = { clients: ['192.168.0.1', ...], size: 34534530850, name: image_name, } --- ogcp/forms/action_forms.py | 9 ++ ogcp/templates/actions/cache.html | 192 ++++++++++++++++++++++++++++++++++++++ ogcp/templates/commands.html | 2 + ogcp/views.py | 99 +++++++++++++++++++- 4 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 ogcp/templates/actions/cache.html (limited to 'ogcp') diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py index 46081b5..19387aa 100644 --- a/ogcp/forms/action_forms.py +++ b/ogcp/forms/action_forms.py @@ -87,6 +87,15 @@ class SessionForm(FlaskForm): ips = HiddenField() os = RadioField(label=_l('Session'), choices=[]) +class CacheImage(FlaskForm): + selected = BooleanField() + image_name = HiddenField() + clients = HiddenField() + +class CacheForm(FlaskForm): + ips = HiddenField() + images = FieldList(FormField(CacheImage)) + class ImageRestoreForm(FlaskForm): ips = HiddenField() partition = SelectField(label=_l('Partition'), choices=[]) diff --git a/ogcp/templates/actions/cache.html b/ogcp/templates/actions/cache.html new file mode 100644 index 0000000..07f5b16 --- /dev/null +++ b/ogcp/templates/actions/cache.html @@ -0,0 +1,192 @@ +{% extends 'commands.html' %} +{% import "bootstrap/wtf.html" as wtf %} + +{% set sidebar_state = 'disabled' %} +{% set btn_back = true %} + +{% block nav_client %} active{% endblock %} +{% block nav_client_cache %} active{% endblock %} +{% block content %} + +{% set ip_list = form.ips.data.split(' ') %} +{% set ip_count = ip_list | length %} +

+ {{ _('Manage Cache') }} +

+ +

{{ _('Check free cache space in the client\'s bubbles:') }}

+ +{{ macros.cmd_selected_clients(selected_clients) }} + +

{{ _('Select the images to be deleted:') }}

+ +
+ + + + + + + + + + {{ form.hidden_tag() }} + {% for image in form.images %} + + + + + {% endfor %} + +
{{ _('Image') }}{{ _('Clients') }}
+ {{ image.selected(class_="form-control") }} + {{ image.image_name() }} + {{ image.clients() }} + {{ image.image_name.data }} ({{ (images_data[image.selected.label.text]['size'] | int / 2**20)|round(3) }} MiB) + {{ ', '.join(images_data[image.selected.label.text]['clients']) }}
+ +
+ +
+ +
+
+ {{ _('Detailed cache usage') }} +
+
+ + + +
    +
  • + +
  • +
  • +

    {{ _('Images in cache:') }}

    +
    +
  • +
+
    +
  • + {{ _('Disk size') }} +
  • +
  • + {{ _('used') }} (%) +
  • +
  • + {{ _('available') }} (%) +
  • +
+
    +
  • +
  • +
  • +
+
+
+ + + + + + + +{% endblock %} diff --git a/ogcp/templates/commands.html b/ogcp/templates/commands.html index 92a1284..316eb8f 100644 --- a/ogcp/templates/commands.html +++ b/ogcp/templates/commands.html @@ -28,6 +28,8 @@ form="scopesForm" formaction="{{ url_for('action_session') }}" formmethod="get"> + diff --git a/ogcp/views.py b/ogcp/views.py index d02359b..32beb3e 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -13,7 +13,7 @@ from ogcp.forms.action_forms import ( SessionForm, ImageRestoreForm, ImageCreateForm, SoftwareForm, BootModeForm, RoomForm, DeleteRoomForm, CenterForm, DeleteCenterForm, OgliveForm, GenericForm, SelectClientForm, ImageUpdateForm, ImportClientsForm, - ServerForm, DeleteRepositoryForm, RepoForm, FolderForm + ServerForm, DeleteRepositoryForm, RepoForm, FolderForm, CacheForm ) from flask_login import ( current_user, LoginManager, @@ -1033,6 +1033,103 @@ def action_session(): selected_clients=selected_clients, scopes=scopes, os_groups=os_groups) +@app.route('/action/cache', methods=['GET', 'POST']) +@login_required +def action_client_cache(): + form = CacheForm(request.form) + if request.method == 'POST': + ips = form.ips.data.split(' ') + server = get_server_from_clients(list(ips)) + + client_list = [] + image_list = [] + for entry in form.images.entries: + if not entry.selected.data: + continue + + image_list.append(entry.image_name.data) + for client_ip in entry.clients.data.split(' '): + if not client_ip in client_list: + client_list.append(client_ip) + + if not image_list: + flash(_(f'No selected images to delete'), category='error') + return redirect(url_for('commands')) + + r = server.post('/cache/delete', + payload={'clients': client_list, + 'images': image_list}) + if r.status_code == requests.codes.ok: + flash(_('Cache delete request sent successfully'), category='info') + else: + flash(_(f'Invalid cache delete form'), category='error') + return redirect(url_for('commands')) + else: + ips = parse_elements(request.args.to_dict()) + ips_list = list(ips) + if not validate_elements(ips): + return redirect(url_for('commands')) + + server = get_server_from_clients(ips_list) + form.ips.data = ' '.join(ips_list) + + r = server.get('/cache/list', payload={'clients': ips_list}) + if not r: + return ogserver_down('commands') + if r.status_code != requests.codes.ok: + return ogserver_error('commands') + + clients_info = r.json()['clients'] + + if not clients_info: + flash(_('ogServer returned an empty client list'), category='error') + return redirect(url_for('commands')) + + storage_data = {} + images_data = {} + client_images = {} + for client_info in clients_info: + ip = client_info['ip'] + cache_size = int(client_info['cache_size']) + used_cache = 0 + for image_info in client_info['images']: + image_name = image_info['name'] + checksum = image_info['checksum'] + used_cache += int(image_info['size']) + img_identifier = f'{image_name}.{checksum}' + + if ip in client_images: + client_images[ip].append(img_identifier) + else: + client_images[ip] = [img_identifier] + + if img_identifier in images_data: + images_data[img_identifier]['clients'].append(ip) + else: + images_data[img_identifier] = { + 'clients': [ip], + 'size': int(image_info['size']), + 'name': image_name, + } + + storage_data[ip] = {'used': used_cache, + 'total': cache_size} + + for img_identifier in images_data: + image_data = images_data[img_identifier] + checkbox_entry = form.images.append_entry() + checkbox_entry.selected.label.text = img_identifier + checkbox_entry.image_name.data = image_data['name'] + checkbox_entry.clients.data = ' '.join(image_data['clients']) + + scopes, clients = get_scopes(set(ips)) + selected_clients = list(get_selected_clients(scopes['scope']).items()) + return render_template('actions/cache.html', form=form, + selected_clients=selected_clients, + scopes=scopes, images_data=images_data, + storage_data=storage_data, + client_images=client_images) + @app.route('/action/client/info', methods=['GET']) @login_required def action_client_info(): -- cgit v1.2.3-18-g5258