summaryrefslogtreecommitdiffstats
path: root/ogcp/views.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-08-07 16:13:50 +0200
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-08-09 10:01:23 +0200
commit844bc1e7c677908063002b525f7996f0b0ea58f4 (patch)
tree2e31914f075e51daba1d01149b5e218a6e1ba349 /ogcp/views.py
parentace0c5bb46ee78b0fe6533b5d50a2dd8c04198a4 (diff)
ogcp: Add /action/cache/fetch endpoint
Add view to request the download of images into the client's cache.
Diffstat (limited to 'ogcp/views.py')
-rw-r--r--ogcp/views.py91
1 files changed, 90 insertions, 1 deletions
diff --git a/ogcp/views.py b/ogcp/views.py
index 3725899..f964c1e 100644
--- a/ogcp/views.py
+++ b/ogcp/views.py
@@ -14,7 +14,7 @@ from ogcp.forms.action_forms import (
RoomForm, DeleteRoomForm, CenterForm, DeleteCenterForm, OgliveForm,
GenericForm, SelectClientForm, ImageUpdateForm, ImportClientsForm,
ServerForm, DeleteRepositoryForm, RepoForm, FolderForm, CacheForm,
- ClientMoveForm, RunScriptForm, ImageConfigForm
+ ClientMoveForm, RunScriptForm, ImageConfigForm, ImageFetchForm
)
from flask_login import (
current_user, LoginManager,
@@ -1307,6 +1307,95 @@ def action_client_cache():
storage_data=storage_data,
client_images=client_images)
+@app.route('/action/cache/fetch', methods=['GET', 'POST'])
+@login_required
+def action_image_fetch():
+ form = ImageFetchForm(request.form)
+ if request.method == 'POST':
+ ips = form.ips.data.split(' ')
+ server = get_server_from_clients(list(ips))
+
+ image_id = form.image.data
+ server = get_server_from_clients(ips)
+ r = server.get('/images')
+ if not r:
+ return ogserver_down('commands')
+ if r.status_code != requests.codes.ok:
+ return ogserver_error('commands')
+
+ images_list = r.json()['images']
+ image = search_image(images_list, int(image_id))
+ if not image:
+ flash(_(f'Image to fetch was not found'), category='error')
+ return redirect(url_for('commands'))
+
+ try:
+ repository = get_repository(image['repo_id'], server)
+ except ServerError:
+ return ogserver_down('commands')
+ except ServerErrorCode:
+ return ogserver_error('commands')
+
+ payload = {'clients': ips,
+ 'repository': repository['ip'],
+ 'type': form.method.data,
+ 'image': image['name']}
+
+ r = server.post('/cache/fetch', payload=payload)
+ if not r:
+ return ogserver_down('commands')
+ if r.status_code == requests.codes.ok:
+ flash(_('Cache fetch request sent successfully'), category='info')
+ else:
+ flash(_(f'Invalid cache fetch form'), category='error')
+ return redirect(url_for('commands'))
+ else:
+ params = request.args.to_dict()
+ ips = parse_elements(params)
+ center_id = int(params.get('scope-center'))
+
+ if not validate_elements(ips):
+ return redirect(url_for('commands'))
+
+ ips_list = list(ips)
+ form.ips.data = ' '.join(ips_list)
+
+ server = get_server_from_clients(ips)
+
+ try:
+ repo_id = get_clients_repo(server, ips)
+ except ServerError:
+ return ogserver_down('commands')
+ except ServerErrorCode:
+ return ogserver_error('commands')
+
+ if repo_id is None:
+ flash(_(f'Computers have different repos assigned'), category='error')
+ return redirect(url_for('commands'))
+ try:
+ images = get_images_from_repo(server, repo_id)
+
+ if not images:
+ flash(_(f'Computer(s) assigned to a repo with no images'), category='error')
+ return redirect(url_for('commands'))
+
+ images = filter_images_allowed_in_center(server, images, center_id)
+ except ServerError:
+ return ogserver_down('commands')
+ except ServerErrorCode:
+ return ogserver_error('commands')
+
+ sort_images(images)
+
+ for image in images:
+ form.image.choices.append((image['id'], image['name']))
+
+ scopes, clients = get_scopes(set(ips))
+ selected_clients = list(get_selected_clients(scopes['scope']).items())
+ return render_template('actions/cache_fetch.html', form=form,
+ selected_clients=selected_clients,
+ scopes=scopes)
+
@app.route('/action/client/info', methods=['GET'])
@login_required
def action_client_info():