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.94
ff:ff:ff:ff:ff:fe,10.6.122.95 ff:ff:ff:ff:ff:fe,10.6.122.95
# { "reserved-by": "Andy" } # { "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.97
ff:ff:ff:ff:ff:fe,10.6.122.98 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.99
ff:ff:ff:ff:ff:fe,10.6.122.100 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.102
ff:ff:ff:ff:ff:fe,10.6.122.103 ff:ff:ff:ff:ff:fe,10.6.122.103
ff:ff:ff:ff:ff:fe,10.6.122.104 ff:ff:ff:ff:ff:fe,10.6.122.104

View file

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

47
main.py
View file

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

View file

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

View file

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