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; }
multi-ogserver
Javier Sánchez Parra 2022-04-12 17:32:07 +02:00
parent 87270dc8df
commit 09884080c3
4 changed files with 89 additions and 2 deletions

View File

@ -7,7 +7,7 @@
from wtforms import (
Form, SubmitField, HiddenField, SelectField, BooleanField, IntegerField,
StringField, RadioField, FormField, FieldList, DecimalField
StringField, RadioField, FormField, FieldList, DecimalField, TextAreaField
)
from wtforms.validators import InputRequired
from flask_wtf import FlaskForm
@ -119,6 +119,11 @@ class ClientDetailsForm(FlaskForm):
boot = SelectField(label=_l('Boot Mode'))
create = SubmitField(label=_l('Create'))
class ImportClientsForm(FlaskForm):
room = SelectField(label=_l('Room'))
dhcpd_conf = TextAreaField(label=_l('dhcpd configuration'))
import_btn = SubmitField(label=_l('Import'))
class BootModeForm(FlaskForm):
ips = HiddenField()
boot = SelectField(label=_l('Boot mode'))

View File

@ -0,0 +1,19 @@
{% extends 'scopes.html' %}
{% import "bootstrap/wtf.html" as wtf %}
{% set sidebar_state = 'disabled' %}
{% set btn_back = true %}
{% block nav_client %}active{% endblock %}
{% block nav_clients_import %}active{% endblock %}
{% block content %}
<h1 class="m-5">{{_('Import clients')}}</h1>
{{ wtf.quick_form(form,
action=url_for('action_clients_import_post'),
method='post',
button_map={'submit': 'primary'},
extra_classes="mx-5") }}
{% endblock %}

View File

@ -23,6 +23,8 @@
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<input class="btn btn-light dropdown-item {% block nav_client_add %}{% endblock %}" type="submit" value="{{ _('Add client') }}"
form="scopesForm" formaction="{{ url_for('action_client_add') }}" formmethod="get">
<input class="btn btn-light dropdown-item {% block nav_clients_import %}{% endblock %}" type="submit" value="{{ _('Import clients') }}"
form="scopesForm" formaction="{{ url_for('action_clients_import_get') }}" formmethod="get">
<input class="btn btn-light dropdown-item {% block nav_client_delete %}{% endblock %}" type="submit" value="{{ _('Delete client') }}"
form="scopesForm" formaction="{{ url_for('action_client_delete') }}" formmethod="get">
</div>

View File

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