From 7815d1fac7392757f057efa25ceee2047403c86c Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Mon, 14 Sep 2020 10:56:58 +0200 Subject: Add partitions setup forms This provides a menu to setup the internal partitions setup of each machine. Delete partitions is still just a placeholder. --- ogcp/forms/action_forms.py | 21 ++++++++++++++++- ogcp/templates/scopes.html | 3 ++- ogcp/views.py | 57 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 3 deletions(-) (limited to 'ogcp') diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py index d6416bd..6479b48 100644 --- a/ogcp/forms/action_forms.py +++ b/ogcp/forms/action_forms.py @@ -1,4 +1,6 @@ -from wtforms import Form, SubmitField, HiddenField, SelectField +from wtforms import ( + Form, SubmitField, HiddenField, SelectField, BooleanField, IntegerField +) from flask_wtf import FlaskForm from flask_babel import _ @@ -8,3 +10,20 @@ class WOLForm(FlaskForm): choices=[('broadcast', 'Broadcast'), ('unicast', 'Unicast')]) submit = SubmitField(label=_('Submit')) + +class PartitionForm(FlaskForm): + ips = HiddenField() + disk = HiddenField() + partition = HiddenField() + part_type = SelectField(label=_('Type'), + choices=[('LINUX', 'Linux'), + ('NTFS', 'NTFS'), + ('EMPTY', 'Empty')]) + fs = SelectField(label=_('Filesystem'), + choices=[('EXT4', 'EXT4'), + ('NTFS', 'NTFS'), + ('EMPTY', 'Empty')]) + size = IntegerField(label=_('Size (KB)')) + format_partition = BooleanField(label=_('Format')) + modify = SubmitField(label=_('Modify')) + delete = SubmitField(label=_('Delete')) diff --git a/ogcp/templates/scopes.html b/ogcp/templates/scopes.html index 81124dd..376df67 100644 --- a/ogcp/templates/scopes.html +++ b/ogcp/templates/scopes.html @@ -40,7 +40,8 @@ formaction="{{ url_for('action_reboot') }}" formmethod="post"> - + diff --git a/ogcp/views.py b/ogcp/views.py index 2bb4713..22ce379 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -1,8 +1,9 @@ from flask import g, render_template, url_for, request, jsonify, make_response -from ogcp.forms.action_forms import WOLForm +from ogcp.forms.action_forms import WOLForm, PartitionForm from ogcp.og_server import OGServer from flask_babel import _ from ogcp import app +import requests def parse_ips(checkboxes_dict): ips = set() @@ -73,6 +74,60 @@ def action_wol(): form.ips.data = " ".join(ips) return render_template('actions/wol.html', form=form) +@app.route('/action/setup', methods=['GET']) +def action_setup_show(): + ips = parse_ips(request.args.to_dict()) + payload = {'client': list(ips)} + r = g.server.get('/client/setup', payload) + db_partitions = r.json()['partitions'] + forms = [PartitionForm() for _ in db_partitions] + forms = list(forms) + for form, db_part in zip(forms, db_partitions): + form.ips.data = " ".join(ips) + form.disk.data = db_part['disk'] + form.partition.data = db_part['partition'] + form.size.data = db_part['size'] + form.modify.render_kw = {"formaction": url_for('action_setup_modify')} + form.delete.render_kw = {"formaction": url_for('action_setup_delete')} + return render_template('actions/setup.html', forms=forms) + +@app.route('/action/setup/modify', methods=['POST']) +def action_setup_modify(): + form = PartitionForm(request.form) + if form.validate(): + ips = form.ips.data.split(' ') + r = g.server.get('/client/setup', payload={'client': ips}) + + payload = {'clients': ips, + 'disk': str(form.disk.data), + 'cache': str(0), + 'cache_size': str(0), + 'partition_setup': []} + + for i in range(1,5): + partition_placeholder = {'partition': str(i), + 'filesystem': 'EMPTY', + 'code': 'EMPTY', + 'size': str(0), + 'format': str(int(False))} + payload['partition_setup'].append(partition_placeholder) + + modified_part = payload['partition_setup'][int(form.partition.data) - 1] + modified_part['partition'] = str(form.partition.data) + modified_part['filesystem'] = str(form.fs.data) + modified_part['code'] = str(form.part_type.data) + modified_part['size'] = str(form.size.data) + modified_part['format'] = str(int(form.format_partition.data)) + + r = g.server.post('/setup', payload=payload) + if r.status_code == requests.codes.ok: + return make_response("200 OK", 200) + return make_response("400 Bad Request", 400) + +@app.route('/action/setup/delete', methods=['POST']) +def action_setup_delete(): + return make_response("200 OK", 200) + @app.route('/action/reboot', methods=['POST']) def action_reboot(): ips = parse_ips(request.form.to_dict()) -- cgit v1.2.3-18-g5258