Display reservations. Can't yet change them

This commit is contained in:
Andy Teijelo Pérez 2015-11-06 12:34:40 -05:00
parent ca70df5736
commit 5fe6c56601
5 changed files with 76 additions and 33 deletions

View file

@ -95,12 +95,13 @@ ff:ff:ff:ff:ff:fe,10.6.122.93
ff:ff:ff:ff:ff:fe,10.6.122.94
ff:ff:ff:ff:ff:fe,10.6.122.95
# { "reserved-by": "Andy" }
00:23:54:8c:65:13,10.6.122.96
00:23:54:8c:65:13,10.6.122.96,set:client0023548c6513
ff:ff:ff:ff:ff:fe,10.6.122.97
ff:ff:ff:ff:ff:fe,10.6.122.98
ff:ff:ff:ff:ff:fe,10.6.122.99
ff:ff:ff:ff:ff:fe,10.6.122.100
ff:ff:ff:ff:ff:fe,10.6.122.101
# { "reserved-by": "Andy" }
aa:bb:cc:dd:ee:ff,10.6.122.101,set:clientaabbccddeeff
ff:ff:ff:ff:ff:fe,10.6.122.102
ff:ff:ff:ff:ff:fe,10.6.122.103
ff:ff:ff:ff:ff:fe,10.6.122.104

View file

@ -1 +1,3 @@
tag:client5cf9ffffffff,option:router,10.0.0.1
tag:clientaabbccddeeff,option:router,10.6.122.96
tag:client0023548c6513,option:router,10.6.122.2

49
main.py
View file

@ -23,6 +23,7 @@ app.secret_key = "6ab77f3c45447429c2ae163c260a626029519a66450e474c"
users_file = "/etc/freeradius/users.dpto2"
dhcp_hosts_file = "/etc/dnsmasq.d/dpto2/dhcp-hosts"
dhcp_opts_file = "/etc/dnsmasq.d/dpto2/dhcp-opts"
impossible_mac = "ff:ff:ff:ff:ff:fe"
class Lock:
def __init__(self, lockfile):
@ -191,19 +192,21 @@ def list_users():
return render_template("users.html", users=lines)
mac_re = re.compile("(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}")
ip_re = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
ip_re_str = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
ip_re = re.compile(ip_re_str)
DhcpHostLine = namedtuple("DhcpHostLine","macs ip tag")
def parse_dchp_host(line):
macs = []
ip = ""
tag = ""
tag = None
parts = line.split(",")
for p in parts:
p = p.strip()
m = mac_re.match(p)
if m:
macs.append(m.group(0))
if m.group(0) != impossible_mac:
macs.append(m.group(0))
continue
m = ip_re.match(p)
if m:
@ -213,14 +216,40 @@ def parse_dchp_host(line):
tag = p[4:]
return DhcpHostLine(macs, ip, tag)
Ip = namedtuple("Ip","reserved_by dhcp locked")
Ip = namedtuple("Ip","reserved_by dhcp_pool locked macs gw")
def load_ips():
ipmap = {}
for i in range(1,255):
ipmap[i] = Ip("",True,False)
ipmap[0] = ipmap[255] = Ip("", False, False)
ipmap[i] = Ip(
reserved_by="",
dhcp_pool=True,
locked=False,
macs=[],
gw=None
)
ipmap[0] = ipmap[255] = Ip(
reserved_by="",
dhcp_pool=False,
locked=False,
macs=[],
gw=None
)
prefix = None
# Read custom gateways
gws = {}
with open(dhcp_opts_file) as f:
for line in f:
line = line.strip()
if line.startswith("#"): continue
m = re.match(
"tag:(client[0-9a-f]{12}),option:router,(" + ip_re_str + ")",
line
)
if m:
gws[m.group(1)] = m.group(2)
with open(dhcp_hosts_file) as f:
meta = {}
for line in f:
@ -241,8 +270,10 @@ def load_ips():
prefix = ".".join(r.ip.split(".")[:-1])
ipmap[ip] = Ip(
reserved_by=meta.get("reserved-by",""),
dhcp=False,
locked=meta.get("locked",False)
dhcp_pool=False,
locked=meta.get("locked",False),
macs=r.macs,
gw=gws.get(r.tag, None)
)
meta = {}
ipmap["prefix"] = prefix
@ -253,7 +284,7 @@ def ips():
ipmap = load_ips()
return render_template("ips.html", ipmap=ipmap)
@app.route("/ip/<int:last_byte>")
@app.route("/ip/<int:last_byte>", methods=['GET', 'POST'])
def ip(last_byte):
ipmap = load_ips()
addr = "{}.{}".format(ipmap["prefix"], last_byte)

View file

@ -11,45 +11,54 @@
<h3>Asignación de {{ addr }}</h3>
</div>
{{ meta }}
{% if meta.locked %}
<div class="alert alert-warning"><i class="fa fa-lock"></i> Esta dirección está reservada por <i>Ilúvatar</i></div>
{% endif %}
<div class="row">
<form class="form-horizontal" method="POST">
{% if meta.locked %}
<fieldset disabled>
{% endif %}
<div class="form-group">
<div class="col-xs-6">
<input type="checkbox" id="reserved" name="reserved"
{% if meta.reserved_by -%}
checked="checked"
{%- endif -%}>
<label for="reserved">Reservado a:</label>
<input type="text" class="form-control"
id="reserved-by" name="reserved-by"
<div class="form-group">
<div class="col-xs-6">
<input type="checkbox" id="reserved" name="reserved"
{% if meta.reserved_by -%}
value="{{ meta.reserved_by }}"
{%- else -%}
disabled
{%- endif -%}
>
</div> </div>
checked="checked"
{%- endif -%}>
<label for="reserved">Reservado a:</label>
<input type="text" class="form-control"
id="reserved-by" name="reserved-by"
{% if meta.reserved_by -%}
value="{{ meta.reserved_by }}"
{%- else -%}
disabled
{%- endif -%}
>
</div>
</div>
<div class="form-group">
<div class="col-xs-11">
<input type="checkbox" id="dhcp" name="dhcp"></input>
<input type="checkbox" id="dhcp" name="dhcp"
{% if meta.dhcp_pool or meta.macs %}checked="checked"{% endif %}/>
<label for="dhcp">Ofrecer por DHCP</label>
<fieldset id="dhcp-fieldset" disabled>
<div class="form-group">
<div class="col-sm-offset-1">
<input type="radio" id="dhcp-client-any" name="dhcp-client" value="any">
<input type="radio" id="dhcp-client-any" name="dhcp-client" value="any"
{% if meta.dhcp_pool %}checked="checked"{% endif %}/>
<label for="dhcp-client-any">A cualquier dispositivo</label>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1">
<input type="radio" id="dhcp-client-specific" name="dhcp-client" value="mac">
<input type="radio" id="dhcp-client-specific" name="dhcp-client" value="mac"
{% if meta.macs %}checked="checked"{% endif %}/>
<label for="dhcp-client-specific">A un dispositivo específico</label>
<div class="form-group">
<fieldset id="dhcp-specific-fieldset" disabled>
@ -57,7 +66,7 @@
<label for="dhcp-client-mac" class="col-sm-4 control-label">MAC:</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="dhcp-client-mac"
placeholder="aa:bb:cc:dd:ee:ff"/>
value="{{ meta.macs|join(",") }}"/>
</div>
</div>
<div>
@ -66,7 +75,7 @@
</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="dhcp-client-mac"
placeholder="10.6.122.1"/>
value="{{ meta.gw if meta.gw }}"/>
</div>
</div>
</fieldset>

View file

@ -23,11 +23,11 @@
{%- if col == 7 %} ips-ver-half{% endif %}
{%- if row == 7 %} ips-hor-half{% endif %}
{% if unusable %} unusable{% endif %}
{%- if ipmap[ip].dhcp %} dhcp-pool{% endif -%}
{%- if ipmap[ip].dhcp_pool %} dhcp-pool{% endif -%}
">
<div style="display: inline-block; max-height: 52px;">
<div class="name">
{% if ipmap[ip].dhcp %}
{% if ipmap[ip].dhcp_pool %}
DHCP
{% else %}
{{ ipmap[ip].reserved_by }}