Add image create action

This action handles the creation of the image in the DB as well as the
'.img' file.
multi-ogserver
Roberto Hueso Gómez 2020-10-29 14:43:23 +01:00
parent 282984d0ac
commit a8d7494ab9
3 changed files with 52 additions and 1 deletions

View File

@ -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'))

View File

@ -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>

View File

@ -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())