Compare commits
11 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9edb21f1c4 | ||
![]() |
7fc1031f8e | ||
![]() |
6fd56bd7ec | ||
dd5d3b7301 | |||
![]() |
1c8e1e68f8 | ||
01694fc604 | |||
0f4cd64eee | |||
![]() |
9baece2c7b | ||
![]() |
48feb825b8 | ||
![]() |
0bf05dbad3 | ||
![]() |
f007e56292 |
3 changed files with 74 additions and 15 deletions
0
generated/.gitkeep
Normal file
0
generated/.gitkeep
Normal file
87
main.py
87
main.py
|
@ -4,47 +4,106 @@ import socket
|
||||||
|
|
||||||
from re2oapi import Re2oAPIClient
|
from re2oapi import Re2oAPIClient
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
path =(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config.read('config.ini')
|
config.read(path+'/config.ini')
|
||||||
|
|
||||||
api_hostname = config.get('Re2o', 'hostname')
|
api_hostname = config.get('Re2o', 'hostname')
|
||||||
api_password = config.get('Re2o', 'password')
|
api_password = config.get('Re2o', 'password')
|
||||||
api_username = config.get('Re2o', 'username')
|
api_username = config.get('Re2o', 'username')
|
||||||
|
|
||||||
def regen_dhcp(api_client):
|
def regen_dhcp(api_client):
|
||||||
host_mac_ip = {}
|
"""Genere les fichiers de zone du DHCP, par extension et par plage d'ip"""
|
||||||
|
host_mac_ip_ext = {}
|
||||||
|
host_mac_ip_type = {}
|
||||||
|
|
||||||
for hmi in api_client.list("dhcp/hostmacip/"):
|
api_res = api_client.list("dhcp/hostmacip/")
|
||||||
if hmi['extension'] not in host_mac_ip.keys():
|
|
||||||
host_mac_ip[hmi['extension']] = []
|
build_hmi(host_mac_ip_ext, api_res, 'extension')
|
||||||
host_mac_ip[hmi['extension']].append((hmi['hostname'],
|
build_hmi(host_mac_ip_type, api_res, 'ip_type')
|
||||||
hmi['mac_address'],
|
|
||||||
hmi['ipv4']))
|
|
||||||
|
|
||||||
template = ("host {hostname}{extension} {{\n"
|
template = ("host {hostname}{extension} {{\n"
|
||||||
" hardware ethernet {mac_address};\n"
|
" hardware ethernet {mac_address};\n"
|
||||||
" fixed-address {ipv4};\n"
|
" fixed-address {ipv4};\n"
|
||||||
"}}")
|
"}}")
|
||||||
|
|
||||||
for extension, hmi_list in host_mac_ip.items():
|
generate_file(template, host_mac_ip_ext, "dhcp")
|
||||||
|
generate_file(template, host_mac_ip_type, "dhcp.ip_type.")
|
||||||
|
|
||||||
|
def build_hmi(host_mac_ip, api_res, key):
|
||||||
|
for hmi in api_res:
|
||||||
|
if hmi[key] not in host_mac_ip.keys():
|
||||||
|
host_mac_ip[hmi[key]] = []
|
||||||
|
if 'ipv4' in hmi:
|
||||||
|
host_mac_ip[hmi[key]].append((hmi['hostname'],
|
||||||
|
hmi['extension'],
|
||||||
|
hmi['mac_address'],
|
||||||
|
hmi['ipv4']))
|
||||||
|
|
||||||
|
|
||||||
|
def generate_file(template, host_mac_ip, file_prefix):
|
||||||
|
for key, hmi_list in host_mac_ip.items():
|
||||||
dhcp_leases_content = '\n\n'.join(template.format(
|
dhcp_leases_content = '\n\n'.join(template.format(
|
||||||
hostname=hostname,
|
hostname=hostname,
|
||||||
extension=extension,
|
extension=extension,
|
||||||
mac_address=mac_address,
|
mac_address=mac_address,
|
||||||
ipv4=ipv4
|
ipv4=ipv4
|
||||||
) for hostname, mac_address, ipv4 in hmi_list)
|
) for hostname, extension, mac_address, ipv4 in hmi_list)
|
||||||
|
|
||||||
filename = 'generated/dhcp{extension}.list'.format(extension=extension)
|
filename = path+'/generated/{file_prefix}{key}.list'.format(
|
||||||
|
key=key,
|
||||||
|
file_prefix=file_prefix).lower().replace(" ", "_")
|
||||||
with open(filename, 'w+') as f:
|
with open(filename, 'w+') as f:
|
||||||
f.write(dhcp_leases_content)
|
f.write(dhcp_leases_content)
|
||||||
|
|
||||||
api_client = Re2oAPIClient(api_hostname, api_username, api_password)
|
|
||||||
|
def reload_server():
|
||||||
|
"""Relance le serveur DHCP."""
|
||||||
|
try:
|
||||||
|
subprocess.check_output(
|
||||||
|
['/bin/systemctl', 'restart', 'isc-dhcp-server'],
|
||||||
|
stderr=subprocess.STDOUT)
|
||||||
|
except subprocess.CalledProcessError as err:
|
||||||
|
print(
|
||||||
|
"Erreur lors du redémarrage de isc-dhcp-server.\n"
|
||||||
|
"Code de retour: %s, Sortie:\n%s",
|
||||||
|
err.returncode, err.output.decode())
|
||||||
|
print(err)
|
||||||
|
|
||||||
|
|
||||||
|
def check_syntax():
|
||||||
|
"""Vérifie la configuration du serveur DHCP."""
|
||||||
|
try:
|
||||||
|
subprocess.check_output(
|
||||||
|
['/usr/sbin/dhcpd', '-t', '-cf', '/etc/dhcp/dhcpd.conf'],
|
||||||
|
stderr=subprocess.STDOUT)
|
||||||
|
except subprocess.CalledProcessError as err:
|
||||||
|
print(
|
||||||
|
"Erreur lors de la vérification de la configuration DHCP.\n"
|
||||||
|
"Code de retour: %s, Sortie:\n%s",
|
||||||
|
err.returncode, err.output.decode())
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
api_client = Re2oAPIClient(api_hostname, api_username, api_password, use_tls=True)
|
||||||
|
|
||||||
client_hostname = socket.gethostname().split('.', 1)[0]
|
client_hostname = socket.gethostname().split('.', 1)[0]
|
||||||
|
|
||||||
for service in api_client.list("services/regen"):
|
for arg in sys.argv:
|
||||||
|
if arg=="--force":
|
||||||
|
regen_dhcp(api_client)
|
||||||
|
|
||||||
|
for service in api_client.list("services/regen/"):
|
||||||
if service['hostname'] == client_hostname and \
|
if service['hostname'] == client_hostname and \
|
||||||
service['service_name'] == 'dhcp' and \
|
service['service_name'] == 'dhcp' and \
|
||||||
service['need_regen']:
|
service['need_regen']:
|
||||||
regen_dhcp(api_client)
|
regen_dhcp(api_client)
|
||||||
api_client.patch(service['api_url'], data={'need_regen': False})
|
if check_syntax():
|
||||||
|
api_client.patch(service['api_url'], data={'need_regen': False})
|
||||||
|
reload_server()
|
||||||
|
|
||||||
|
|
2
re2oapi
2
re2oapi
|
@ -1 +1 @@
|
||||||
Subproject commit b12df74fe73f351986ff51c8122089644218f8fe
|
Subproject commit ffaed921030deb6b6b01649709666807feb95370
|
Loading…
Add table
Add a link
Reference in a new issue