From 2b761064753d03f26593efe33797e9d2e9c676b3 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Mon, 22 Feb 2021 12:12:36 +0100 Subject: Add multiple ip handling to image restore form This patch enables a set of ips to be specified as a target of an image restore action. The set of selected computers must have the same partition setup in order for the form to be rendered sucessfully, if different partition setups are detected a redirection is made and an error flash message is shown. --- ogcp/views.py | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'ogcp') diff --git a/ogcp/views.py b/ogcp/views.py index 37c59db..339e335 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -273,25 +273,43 @@ def action_image_restore(): return make_response("400 Bad Request", 400) else: ips = parse_ips(request.args.to_dict()) + if not validate_ips(ips): + return redirect(url_for('scopes')) form.ips.data = ' '.join(ips) + + part_choices = [] + r = g.server.get('/images') for image in r.json()['images']: form.image.choices.append((image['id'], image['name'])) - r = g.server.get('/client/setup', payload={'client': list(ips)}) - for partition in r.json()['partitions']: - disk_id = partition['disk'] - part_id = partition['partition'] - fs_id = partition['filesystem'] - if part_id == 0: - # This is the disk data, not a partition. - continue + for ip in ips: + r = g.server.get('/client/setup', payload={'client': [ip]}) + if r.status_code == requests.codes.ok: + partitions = r.json()['partitions'] + parts = [] + for partition in partitions: + disk_id = partition['disk'] + part_id = partition['partition'] + if part_id == 0: # This is the disk data, not a partition. + continue + + choice_value = (disk_id, part_id) + parts.append(choice_value) + + if not part_choices: # Use first computer as reference part setup conf + part_choices = [part for part in parts] + elif part_choices != parts: + flash(_(f'Computers have different partition setup'), category='error') + return redirect(url_for("scopes")) + + else: + flash(_(f'ogServer was unable to obtain setup of selected computer {ip}'), category='error') + return redirect(url_for("scopes")) + + form.partition.choices = [ (f'{disk_id} {part_id}', _(f'Disk: {disk_id} | Part: {part_id}')) + for disk_id, part_id in part_choices ] - choice_value = f"{disk_id} {part_id}" - choice_name = (f"{_('Disk')} {disk_id} - " - f"{_('Partition')} {part_id} - " - f"{_('FS')} {FS_CODES[fs_id]}") - form.partition.choices.append((choice_value, choice_name)) return render_template('actions/image_restore.html', form=form) @app.route('/action/hardware', methods=['GET', 'POST']) -- cgit v1.2.3-18-g5258