190 lines
6.1 KiB
Python
Executable file
190 lines
6.1 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso-8859-15 -*-
|
|
|
|
""" Génération de la configuration pour le dhcp
|
|
|
|
Copyright (C) Frédéric Pauget
|
|
Licence : GPLv2
|
|
"""
|
|
|
|
from iptools import AddrInNet, param
|
|
from gen_confs import gen_config
|
|
from ldap_crans import hostname
|
|
|
|
class dhcp(gen_config) :
|
|
""" Génération du fichier de configuration pour dhcpd (DHCPD_CONF)
|
|
Le fichier comporte une partie par réseau servi, chaque réseau
|
|
servi doit être une clef du dictionnaire reseaux, la valeur correspondante
|
|
est une chaine décrivant les options spécifiques à ce réseau.
|
|
Les options communes sont celles de base_dhcp.
|
|
|
|
Chaque machines possède ensuite une entrée de la forme de host_template
|
|
"""
|
|
######################################PARTIE DE CONFIGURATION
|
|
# Fichier à écire
|
|
if hostname == 'ragnarok' :
|
|
DHCPD_CONF='/etc/dhcpd.conf'
|
|
else :
|
|
DHCPD_CONF = '/etc/dhcp3/dhcpd.conf'
|
|
|
|
# Options générales
|
|
if hostname == 'rouge' :
|
|
base_conf = """option option-252 code 252 = text ;
|
|
option option-119 code 119 = text ;
|
|
"""
|
|
# Hotspot ENS plus utilisé...
|
|
# elif hostname == 'ragnarok' :
|
|
# On rajoute les IP dynamiques
|
|
# base_conf = """
|
|
# subnet 10.231.144.0 netmask 255.255.248.0 {
|
|
# default-lease-time 900;
|
|
# max-lease-time 900;
|
|
# option subnet-mask 255.255.248.0;
|
|
# option broadcast-address 10.231.151.255;
|
|
# option routers 10.231.148.1;
|
|
# option domain-name-servers 10.231.148.1;
|
|
# option domain-name "ens-cachan.fr";
|
|
# option option-119 "ens-cachan.fr";
|
|
# option netbios-node-type 2;
|
|
#
|
|
# range dynamic-bootp 10.231.149.1 10.231.149.254;
|
|
# }
|
|
#"""
|
|
else :
|
|
base_conf = ''
|
|
|
|
# Réseaux servis avec leurs options spécifiques
|
|
# if hostname == 'zamok':
|
|
# reseaux = { '138.231.136.0/21' :
|
|
#"""option routers 138.231.136.4;
|
|
# option domain-name-servers 138.231.136.3, 138.231.136.9, 138.231.136.10;
|
|
# option domain-name "crans.org";
|
|
# option option-119 "crans.org wifi.crans.org";""" }
|
|
if hostname == 'ragnarok':
|
|
reseaux = { '138.231.144.0/21' :
|
|
"""option routers 138.231.148.1;
|
|
option domain-name-servers 138.231.148.1;
|
|
option domain-name "crans.org";
|
|
option option-119 "crans.org wifi.crans.org";"""
|
|
}
|
|
elif hostname == 'rouge':
|
|
reseaux = { '138.231.136.0/21' :
|
|
"""authoritative;
|
|
option routers 138.231.136.4;
|
|
option domain-name-servers 138.231.136.3, 138.231.136.9, 138.231.136.10;
|
|
option domain-name "crans.org";
|
|
option option-119 "crans.org wifi.crans.org";""" }
|
|
elif hostname == 'titanic':
|
|
reseaux = {'10.2.9.0/24' :
|
|
"""authoritative;
|
|
option routers 10.2.9.1;
|
|
option domain-name-servers 10.2.9.1;""" }
|
|
|
|
# Options communes à toutes les réseaux servis
|
|
base_dhcp="""
|
|
subnet %(network)s netmask %(netmask)s {
|
|
default-lease-time 86400;
|
|
option subnet-mask %(netmask)s;
|
|
option broadcast-address %(broadcast)s;
|
|
%(OPTIONS_RESEAU)s
|
|
option time-servers 138.231.136.3;
|
|
option ntp-servers 138.231.136.3;
|
|
option smtp-server 138.231.136.3;
|
|
option netbios-name-servers 138.231.136.1;
|
|
option netbios-dd-server 138.231.136.1;
|
|
option netbios-node-type 2;
|
|
option ip-forwarding off;
|
|
deny unknown-clients;
|
|
%(HOSTs)s
|
|
}
|
|
"""
|
|
|
|
host_template="""
|
|
host %(nom)s {
|
|
hardware ethernet %(mac)s;
|
|
fixed-address %(ip)s;
|
|
option host-name "%(nom)s";
|
|
}
|
|
"""
|
|
|
|
host_template_ltsp_i386="""
|
|
host %(nom)s {
|
|
hardware ethernet %(mac)s;
|
|
fixed-address %(ip)s;
|
|
option host-name "%(nom)s";
|
|
next-server 138.231.136.19;
|
|
filename "/ltsp/pxelinux.0";
|
|
option root-path "/opt/ltsp/i386";
|
|
}
|
|
"""
|
|
|
|
host_template_ltsp_powerpc="""
|
|
host %(nom)s {
|
|
hardware ethernet %(mac)s;
|
|
fixed-address %(ip)s;
|
|
option host-name "%(nom)s";
|
|
next-server 138.231.136.98;
|
|
filename "yaboot";
|
|
option root-path "/opt/ltsp/powerpc";
|
|
}
|
|
"""
|
|
|
|
### Verbosité
|
|
# Si =1 ralera (chaine warnings) si machines hors zone trouvée
|
|
# Si =0 ralera seulement si réseau vide
|
|
verbose = 1
|
|
|
|
# if hostname == 'zamok':
|
|
# restart_cmd = '/etc/init.d/dhcp restart'
|
|
if hostname == 'ragnarok':
|
|
restart_cmd = 'kill $(ps auxwwc | awk \'($11 == "dhcpd") {print $2}\') ; sleep 1 ; dhcpd $(grep -v \'^#\' /etc/dhcpd.interfaces | tr \'\\n\' \' \')'
|
|
elif hostname in ['rouge', 'titanic'] :
|
|
restart_cmd = '/etc/init.d/dhcp3-server restart'
|
|
else:
|
|
restart_cmd = 'true'
|
|
|
|
######################################FIN PARTIE DE CONFIGURATION
|
|
def __str__(self) :
|
|
return 'dhcp'
|
|
|
|
def _gen(self) :
|
|
warnings =''
|
|
|
|
### Construction de la partie du fichier contenant les machines
|
|
hosts={}
|
|
|
|
self.anim.iter=len(self.machines)
|
|
for machine in self.machines :
|
|
self.anim.cycle()
|
|
t = 0
|
|
for net in self.reseaux.keys() :
|
|
if AddrInNet(machine.ip(),net) :
|
|
# choix du template en fonction de la machine
|
|
if machine.nom() in ['momo.crans.org']:
|
|
host_template = self.host_template_ltsp_i386
|
|
elif machine.nom() in ['bulle3.crans.org','bulle2.crans.org','bulle10.crans.org','bulle12.crans.org']:
|
|
host_template = self.host_template_ltsp_powerpc
|
|
else:
|
|
host_template = self.host_template
|
|
# variable pour remplir le template
|
|
d = { 'nom' : machine.nom().split('.')[0] , 'mac' : machine.mac() , 'ip' : machine.ip() }
|
|
try : hosts[net] += host_template % d
|
|
except : hosts[net] = host_template % d
|
|
t = 1
|
|
|
|
### Ecriture du fichier
|
|
fd = self._open_conf(self.DHCPD_CONF,'#')
|
|
fd.write(self.base_conf)
|
|
for net, options in self.reseaux.items() :
|
|
if not hosts.has_key(net) :
|
|
warnings += u'Réseau %s ignoré : aucune machine à servir\n' % net
|
|
continue
|
|
d = param(net)
|
|
d['OPTIONS_RESEAU'] = options
|
|
d['HOSTs'] = hosts[net]
|
|
|
|
fd.write(self.base_dhcp % d)
|
|
|
|
fd.close()
|
|
|
|
return warnings
|