[dhcpd_new,ldap_crans] Mise à jour dynamique du dhcp à l'édition, la création ou la suppression d'une machine via ldap_crans
Ignore-this: 96d21c9e9d12fc10faff670c8c1b2560 darcs-hash:20130120030947-3a55a-5e038fafa3d29f9e5772f808aa35a817e43b5b4d.gz
This commit is contained in:
parent
b844853894
commit
7141c2c7da
2 changed files with 90 additions and 3 deletions
|
@ -6,12 +6,62 @@
|
||||||
Copyright (C) Frédéric Pauget
|
Copyright (C) Frédéric Pauget
|
||||||
Licence : GPLv2
|
Licence : GPLv2
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
from iptools import AddrInNet
|
from iptools import AddrInNet
|
||||||
from gen_confs import gen_config
|
from gen_confs import gen_config
|
||||||
from ldap_crans import hostname
|
from ldap_crans import hostname
|
||||||
from config import NETs
|
from config import NETs
|
||||||
|
|
||||||
|
from pypureomapi import pack_ip, pack_mac, OMAPI_OP_UPDATE
|
||||||
|
from pypureomapi import Omapi, OmapiMessage, OmapiError, OmapiErrorNotFound
|
||||||
|
import struct
|
||||||
|
|
||||||
|
class dydhcp:
|
||||||
|
def __init__(self):
|
||||||
|
import sys
|
||||||
|
sys.path.append('/usr/scripts/gestion/secrets')
|
||||||
|
from secrets import dhcp_omapi_keyname,dhcp_omapi_key
|
||||||
|
self.dhcp_omapi_keyname=dhcp_omapi_keyname
|
||||||
|
self.dhcp_omapi_key=dhcp_omapi_key
|
||||||
|
def add_host(self, ip, mac,name=None):
|
||||||
|
"""
|
||||||
|
@type ip: str
|
||||||
|
@type mac: str
|
||||||
|
@type name: str
|
||||||
|
@raises ValueError:
|
||||||
|
@raises OmapiError:
|
||||||
|
@raises socket.error:
|
||||||
|
"""
|
||||||
|
msg = OmapiMessage.open(b"host")
|
||||||
|
msg.message.append((b"create", struct.pack("!I", 1)))
|
||||||
|
msg.message.append((b"exclusive", struct.pack("!I", 1)))
|
||||||
|
msg.obj.append((b"hardware-address", pack_mac(mac)))
|
||||||
|
msg.obj.append((b"hardware-type", struct.pack("!I", 1)))
|
||||||
|
msg.obj.append((b"ip-address", pack_ip(ip)))
|
||||||
|
if name:
|
||||||
|
msg.obj.append((b"name", bytes(name)))
|
||||||
|
conn=Omapi('dhcp.adm.crans.org',9991,self.dhcp_omapi_keyname, self.dhcp_omapi_key)
|
||||||
|
response = conn.query_server(msg)
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def del_host(self, ip,mac):
|
||||||
|
"""
|
||||||
|
@type ip: str
|
||||||
|
@type mac: str
|
||||||
|
@raises ValueError:
|
||||||
|
@raises OmapiError:
|
||||||
|
@raises socket.error:
|
||||||
|
"""
|
||||||
|
msg = OmapiMessage.open(b"host")
|
||||||
|
msg.obj.append((b"hardware-address", pack_mac(mac)))
|
||||||
|
msg.obj.append((b"hardware-type", struct.pack("!I", 1)))
|
||||||
|
msg.obj.append((b"ip-address", pack_ip(ip)))
|
||||||
|
conn=Omapi('dhcp.adm.crans.org',9991,self.dhcp_omapi_keyname, self.dhcp_omapi_key)
|
||||||
|
response = conn.query_server(msg)
|
||||||
|
if response.opcode == OMAPI_OP_UPDATE:
|
||||||
|
response = conn.query_server(OmapiMessage.delete(response.handle))
|
||||||
|
conn.close()
|
||||||
|
|
||||||
class dhcp(gen_config) :
|
class dhcp(gen_config) :
|
||||||
""" Génération du fichier de déclaration des hosts.
|
""" Génération du fichier de déclaration des hosts.
|
||||||
Chaque réseau servi doit être une clef du dictionnaire reseaux,
|
Chaque réseau servi doit être une clef du dictionnaire reseaux,
|
||||||
|
@ -38,6 +88,7 @@ class dhcp(gen_config) :
|
||||||
'10.42.0.0/16' : '/etc/dhcp3/generated/gratuit.liste',
|
'10.42.0.0/16' : '/etc/dhcp3/generated/gratuit.liste',
|
||||||
'10.2.9.0/24' : '/etc/dhcp3/generated/appartements.liste',
|
'10.2.9.0/24' : '/etc/dhcp3/generated/appartements.liste',
|
||||||
'138.231.144.0/21' : '/etc/dhcp3/generated/wifi.liste' }
|
'138.231.144.0/21' : '/etc/dhcp3/generated/wifi.liste' }
|
||||||
|
dhcplease='/var/lib/dhcp/dhcpd.leases'
|
||||||
|
|
||||||
|
|
||||||
host_template = """
|
host_template = """
|
||||||
|
@ -68,6 +119,27 @@ class dhcp(gen_config) :
|
||||||
def __str__(self) :
|
def __str__(self) :
|
||||||
return 'dhcp'
|
return 'dhcp'
|
||||||
|
|
||||||
|
def lease_clean(self):
|
||||||
|
f=open(self.dhcplease)
|
||||||
|
w=open(self.dhcplease+'.new','w')
|
||||||
|
config=""
|
||||||
|
line=f.readline()
|
||||||
|
write=True
|
||||||
|
while line:
|
||||||
|
if line.strip().startswith('host'):
|
||||||
|
write=False
|
||||||
|
if write:
|
||||||
|
w.write(line)
|
||||||
|
if not write and line.strip().endswith('}'):
|
||||||
|
write=True
|
||||||
|
line=f.readline()
|
||||||
|
f.close()
|
||||||
|
w.close()
|
||||||
|
os.rename(self.dhcplease+'.new',self.dhcplease)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _gen(self) :
|
def _gen(self) :
|
||||||
"""Construction de la liste des machines appartenant à un réseau
|
"""Construction de la liste des machines appartenant à un réseau
|
||||||
"""
|
"""
|
||||||
|
@ -96,5 +168,9 @@ class dhcp(gen_config) :
|
||||||
if hosts.has_key(net):
|
if hosts.has_key(net):
|
||||||
fd0.write(hosts[net])
|
fd0.write(hosts[net])
|
||||||
fd0.close()
|
fd0.close()
|
||||||
|
try:
|
||||||
|
self.lease_clean()
|
||||||
|
except:
|
||||||
|
print("An error append during cleaning of dhcp lease")
|
||||||
|
|
||||||
return warnings
|
return warnings
|
||||||
|
|
|
@ -3185,6 +3185,12 @@ Contactez nounou si la MAC est bien celle d'une carte.""", 3)
|
||||||
# On vire les doublons dans reconf_ip
|
# On vire les doublons dans reconf_ip
|
||||||
reconf_ip = list(dict(zip(reconf_ip, [None]*len(reconf_ip))))
|
reconf_ip = list(dict(zip(reconf_ip, [None]*len(reconf_ip))))
|
||||||
|
|
||||||
|
# Si la configuration ip à changer on met dynamiquement à jour le dhcp
|
||||||
|
if reconf_ip:
|
||||||
|
from gen_confs.dhcpd_new import dydhcp
|
||||||
|
dhcp=dydhcp()
|
||||||
|
dhcp.del_host(self._init_data.get('ipHostNumber',[self.ip()])[0],self._init_data.get('macAddress',[self.mac()])[0])
|
||||||
|
dhcp.add_host(self._data.get('ipHostNumber',[self.ip()])[0],self._data.get('macAddress',[self.mac()])[0],self.nom())
|
||||||
# Enregistrement
|
# Enregistrement
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
|
@ -3268,6 +3274,11 @@ Contactez nounou si la MAC est bien celle d'une carte.""", 3)
|
||||||
self.__proprietaire = None # On oublie le propriétaire
|
self.__proprietaire = None # On oublie le propriétaire
|
||||||
self._delete(self.dn, comment)
|
self._delete(self.dn, comment)
|
||||||
|
|
||||||
|
# On supprime la machine du dhcp
|
||||||
|
from gen_confs.dhcpd_new import dydhcp
|
||||||
|
dhcp=dydhcp()
|
||||||
|
dhcp.del_host(self.ip(),self.mac())
|
||||||
|
|
||||||
# Services à redémarrer
|
# Services à redémarrer
|
||||||
self.services_to_restart('dhcp-dhcp')
|
self.services_to_restart('dhcp-dhcp')
|
||||||
if isinstance(self, MachineWifi):
|
if isinstance(self, MachineWifi):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue