summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2020-10-29 14:43:23 +0100
committerRoberto Hueso Gómez <rhueso@soleta.eu>2020-10-29 14:43:23 +0100
commita8d7494ab9601d2ea4fb2065018580fb2a1c95b3 (patch)
tree22bf3ec610b696b5ff12a49762bb2393e3da2c78
parent282984d0ac17236bee19375607fa9a413d0b4057 (diff)
Add image create action
This action handles the creation of the image in the DB as well as the '.img' file.
-rw-r--r--ogcp/forms/action_forms.py7
-rw-r--r--ogcp/templates/scopes.html2
-rw-r--r--ogcp/views.py44
3 files changed, 52 insertions, 1 deletions
diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py
index eb8d309..154e9f0 100644
--- a/ogcp/forms/action_forms.py
+++ b/ogcp/forms/action_forms.py
@@ -67,3 +67,10 @@ class ClientDetailsForm(FlaskForm):
room = SelectField(label=_('Room'))
boot = SelectField(label=_('Boot Mode'))
create = SubmitField(label=_('Create'))
+
+class ImageCreateForm(FlaskForm):
+ ip = HiddenField()
+ os = SelectField(label=_('OS'), choices=[])
+ name = StringField(label=_('Image name'))
+ description = StringField(label=_('Description'))
+ create = SubmitField(label=_('Create'))
diff --git a/ogcp/templates/scopes.html b/ogcp/templates/scopes.html
index ba9d27a..19ce9d8 100644
--- a/ogcp/templates/scopes.html
+++ b/ogcp/templates/scopes.html
@@ -52,6 +52,8 @@
formaction="{{ url_for('action_client_info') }}" formmethod="get">
<input class="dropdown-item" type="submit" value="{{ _('Add client') }}"
formaction="{{ url_for('action_client_add') }}" formmethod="get">
+ <input class="dropdown-item" type="submit" value="{{ _('Create image') }}"
+ formaction="{{ url_for('action_image_create') }}" formmethod="get">
</div>
</div>
</form>
diff --git a/ogcp/views.py b/ogcp/views.py
index 7236d5b..7aaf60d 100644
--- a/ogcp/views.py
+++ b/ogcp/views.py
@@ -1,7 +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,
- ImageRestoreForm
+ ImageRestoreForm, ImageCreateForm
)
from ogcp.og_server import OGServer
from flask_babel import _
@@ -361,6 +361,48 @@ def action_client_add():
form.create.render_kw = {"formaction": url_for('action_client_add')}
return render_template('actions/client_details.html', form=form)
+@app.route('/action/image/create', methods=['GET', 'POST'])
+def action_image_create():
+ form = ImageCreateForm(request.form)
+ if request.method == 'POST':
+ ip = form.ip.data
+ r = g.server.get('/client/info', payload={"client": [ip]})
+ disk, partition, code = form.os.data.split(' ')
+ payload = {"clients": [ip],
+ "disk": disk,
+ "partition": partition,
+ "code": code,
+ "name": form.name.data,
+ "repository": g.server.ip,
+ "id": "0", # This is ignored by the server.
+ "description": form.description.data,
+ "group_id": 0, # Default group.
+ "center_id": r.json()["center"]}
+ r = g.server.post('/image/create', 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.ip.data = " ".join(ips)
+ 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']
+ code = partition['code']
+
+ if part_id == 0:
+ # This is the disk data, not a partition.
+ continue
+
+ choice_value = f"{disk_id} {part_id} {code}"
+ choice_name = (f"{_('Disk')} {disk_id} | "
+ f"{_('Partition')} {part_id} | "
+ f"{_('FS')} {FS_CODES[fs_id]}")
+ form.os.choices.append((choice_value, choice_name))
+ return render_template('actions/image_create.html', form=form)
+
@app.route('/action/reboot', methods=['POST'])
def action_reboot():
ips = parse_ips(request.form.to_dict())