summaryrefslogtreecommitdiffstats
path: root/ogcp
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2020-10-23 15:10:29 +0200
committerRoberto Hueso Gómez <rhueso@soleta.eu>2020-10-23 15:10:29 +0200
commit904989b175bda9c68cb9f5750ed5883b9113d834 (patch)
treef4147d8c66a27402cc8291cdb408ba8154ec36b0 /ogcp
parentc5a4ccff550ad69c72f4f8aedba4c4a8457e1dfc (diff)
Add image restore action
This action provides the image restoring functionality on a specific client.
Diffstat (limited to 'ogcp')
-rw-r--r--ogcp/forms/action_forms.py8
-rw-r--r--ogcp/templates/scopes.html2
-rw-r--r--ogcp/views.py57
3 files changed, 66 insertions, 1 deletions
diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py
index 4dfaa21..eb8d309 100644
--- a/ogcp/forms/action_forms.py
+++ b/ogcp/forms/action_forms.py
@@ -38,6 +38,14 @@ class SessionForm(FlaskForm):
os = RadioField(label=_('Session'), choices=[])
run = SubmitField(label=_('Run'))
+class ImageRestoreForm(FlaskForm):
+ ips = HiddenField()
+ partition = SelectField(label=_('Partition'), choices=[])
+ image = SelectField(label=_('Image'), choices=[])
+ method = SelectField(label=_('Method'),
+ choices=[('UNICAST', 'Unicast')])
+ restore = SubmitField(label=_('Restore'))
+
class ClientDetailsForm(FlaskForm):
name = StringField(label=_('Name'))
ip = StringField(label=_('IP'))
diff --git a/ogcp/templates/scopes.html b/ogcp/templates/scopes.html
index 27524e9..ba9d27a 100644
--- a/ogcp/templates/scopes.html
+++ b/ogcp/templates/scopes.html
@@ -44,6 +44,8 @@
formaction="{{ url_for('action_hardware') }}" formmethod="get">
<input class="dropdown-item" type="submit" value="{{ _('Sessions') }}"
formaction="{{ url_for('action_session') }}" formmethod="get">
+ <input class="dropdown-item" type="submit" value="{{ _('Restore Image') }}"
+ formaction="{{ url_for('action_image_restore') }}" formmethod="get">
<input class="dropdown-item" type="submit" value="{{ _('Setup') }}"
formaction="{{ url_for('action_setup_show') }}" formmethod="get">
<input class="dropdown-item" type="submit" value="{{ _('Details') }}"
diff --git a/ogcp/views.py b/ogcp/views.py
index e3f559d..7236d5b 100644
--- a/ogcp/views.py
+++ b/ogcp/views.py
@@ -1,6 +1,7 @@
from flask import g, render_template, url_for, request, jsonify, make_response
from ogcp.forms.action_forms import (
- WOLForm, PartitionForm, ClientDetailsForm, HardwareForm, SessionForm
+ WOLForm, PartitionForm, ClientDetailsForm, HardwareForm, SessionForm,
+ ImageRestoreForm
)
from ogcp.og_server import OGServer
from flask_babel import _
@@ -197,6 +198,60 @@ def action_setup_delete():
return make_response("200 OK", 200)
return make_response("400 Bad Request", 400)
+@app.route('/action/image/restore', methods=['GET', 'POST'])
+def action_image_restore():
+ def search_image(images_list, image_id):
+ for image in images_list:
+ if image['id'] == image_id:
+ return image
+ return False
+
+ form = ImageRestoreForm(request.form)
+ if request.method == 'POST':
+ ips = form.ips.data.split(' ')
+ disk, partition = form.partition.data.split(' ')
+ image_id = form.image.data
+ r = g.server.get('/images')
+ images_list = r.json()['images']
+ image = search_image(images_list, int(image_id))
+ if not image:
+ return make_response("400 Bad Request", 400)
+
+ payload = {'disk': disk,
+ 'partition': partition,
+ 'name': image['name'],
+ 'repository': g.server.ip,
+ 'clients': ips,
+ 'type': form.method.data,
+ 'profile': str(image['software_id']),
+ 'id': str(image['id'])}
+ g.server.post('/image/restore', payload)
+ if r.status_code == requests.codes.ok:
+ return make_response("200 OK", 200)
+ return make_response("400 Bad Request", 400)
+ else:
+ ips = parse_ips(request.args.to_dict())
+ form.ips.data = ' '.join(ips)
+ 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
+
+ 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'])
def action_hardware():
form = HardwareForm(request.form)