diff options
author | Javier Sánchez Parra <jsanchez@soleta.eu> | 2022-04-12 17:32:07 +0200 |
---|---|---|
committer | Javier Sánchez Parra <jsanchez@soleta.eu> | 2022-04-19 16:03:41 +0200 |
commit | 09884080c359b32576cf2c0a3128b481f5566932 (patch) | |
tree | 6e3ee5b8441e4977e09826c6747f3db25307af99 /ogcp/views.py | |
parent | 87270dc8dfca246f54c5f0ccaa3f3d3e98020a2d (diff) |
Add import clients action
Add import clients form with required inputs: room and dhcpd.conf.
This permits users to rapidly add large amounts of clients to a room
using dhcpd.conf's syntax. Users can copy full dhcpd.conf files to the
text area and the parser only matches lines with the following format as
clients:
host dummy {hardware ethernet 12:34:56:78:90:ab; fixed-address 192.168.1.55; }
Diffstat (limited to 'ogcp/views.py')
-rw-r--r-- | ogcp/views.py | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/ogcp/views.py b/ogcp/views.py index 938710d..a0d3e44 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -12,7 +12,7 @@ from ogcp.forms.action_forms import ( WOLForm, SetupForm, ClientDetailsForm, ImageDetailsForm, HardwareForm, SessionForm, ImageRestoreForm, ImageCreateForm, SoftwareForm, BootModeForm, RoomForm, DeleteRoomForm, CenterForm, DeleteCenterForm, OgliveForm, - GenericForm, SelectClientForm, ImageUpdateForm + GenericForm, SelectClientForm, ImageUpdateForm, ImportClientsForm ) from flask_login import ( current_user, LoginManager, @@ -30,6 +30,7 @@ from flask_babel import _ from ogcp import app import requests import datetime +import re FS_CODES = { 0: 'DISK', @@ -748,6 +749,66 @@ def action_client_add(): return render_template('actions/client_details.html', form=form, parent="scopes.html", scopes=scopes) +@app.route('/action/clients/import', methods=['GET']) +@login_required +def action_clients_import_get(): + form = ImportClientsForm() + r = g.server.get('/scopes') + rooms = parse_scopes_from_tree(r.json(), 'room') + rooms = [(room['id'], room['name'] + " (" + room['parent'] + ")") + for room in rooms] + form.room.choices = list(rooms) + scopes, _clients = get_scopes() + return render_template('actions/import_clients.html', form=form, + scopes=scopes) + + +OG_REGEX_DHCPD_CONF = (r'(?: *host *)' + r'([\w.-]*)' + r'(?: *{ *hardware *ethernet *)' + r'((?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2}))' + r'(?: *; *fixed-address *)' + r'(\d+\.\d+\.\d+\.\d+)' + r'(?: *; *})') +OG_CLIENT_DEFAULT_BOOT = "pxe" +OG_CLIENT_DEFAULT_LIVEDIR = "ogLive" +OG_CLIENT_DEFAULT_MAINTENANCE = False +OG_CLIENT_DEFAULT_NETDRIVER = "generic" +OG_CLIENT_DEFAULT_NETIFACE = "eth0" +OG_CLIENT_DEFAULT_NETMASK = "255.255.255.0" +OG_CLIENT_DEFAULT_REMOTE = False + + +@app.route('/action/clients/import', methods=['POST']) +@login_required +def action_clients_import_post(): + form = ImportClientsForm(request.form) + clients = re.findall(OG_REGEX_DHCPD_CONF, form.dhcpd_conf.data) + if not clients: + flash(_('No clients found. Check the dhcpd.conf file.'), + category='error') + return redirect(url_for('scopes')) + payload = {'boot': OG_CLIENT_DEFAULT_BOOT, + 'livedir': OG_CLIENT_DEFAULT_LIVEDIR, + 'maintenance': OG_CLIENT_DEFAULT_MAINTENANCE, + 'netdriver': OG_CLIENT_DEFAULT_NETDRIVER, + 'netiface': OG_CLIENT_DEFAULT_NETIFACE, + 'netmask': OG_CLIENT_DEFAULT_NETMASK, + 'remote': OG_CLIENT_DEFAULT_REMOTE, + 'room': int(form.room.data)} + for client in clients: + payload['name'] = client[0] + payload['mac'] = client[1].replace(':', '') + payload['ip'] = client[2] + resp = g.server.post('/client/add', payload) + if resp.status_code != requests.codes.ok: + flash(_('ogServer: error adding client {}').format(client[0]), + category='error') + return redirect(url_for('scopes')) + flash(_('Clients imported successfully'), category='info') + return redirect(url_for('scopes')) + + def get_selected_clients(scopes): selected_clients = dict() |