maj argparse
This commit is contained in:
parent
c3ad5c3ac4
commit
208b4d4653
1 changed files with 167 additions and 141 deletions
56
main.py
56
main.py
|
@ -1,14 +1,15 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import argparse
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
import socket
|
|
||||||
import datetime
|
import datetime
|
||||||
|
from multiprocessing import Pool
|
||||||
import netaddr
|
import netaddr
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
|
||||||
from re2oapi import Re2oAPIClient
|
from re2oapi import Re2oAPIClient
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
path = os.path.dirname(os.path.abspath(__file__))
|
path = os.path.dirname(os.path.abspath(__file__))
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
|
@ -77,8 +78,8 @@ template_reverse = (
|
||||||
"{ptr_records}\n"
|
"{ptr_records}\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
def write_dns_files(api_client, processes):
|
|
||||||
for zone in api_client.list("dns/zones"):
|
def write_dns_file(zone):
|
||||||
zone_name = zone['name'][1:]
|
zone_name = zone['name'][1:]
|
||||||
|
|
||||||
now = datetime.datetime.now(datetime.timezone.utc)
|
now = datetime.datetime.now(datetime.timezone.utc)
|
||||||
|
@ -220,6 +221,15 @@ def write_dns_files(api_client, processes):
|
||||||
f.write(zone_file_content)
|
f.write(zone_file_content)
|
||||||
|
|
||||||
|
|
||||||
|
def write_dns_files(api_client, processes):
|
||||||
|
if processes:
|
||||||
|
with Pool(processes) as pool:
|
||||||
|
pool.map(write_dns_file, api_client.list("dns/zones"))
|
||||||
|
else:
|
||||||
|
for zone in api_client.list("dns/zones"):
|
||||||
|
write_dns_file(zone)
|
||||||
|
|
||||||
|
|
||||||
def get_ip_reverse(ip, prefix_length):
|
def get_ip_reverse(ip, prefix_length):
|
||||||
""" Truncate an ip address given a prefix length """
|
""" Truncate an ip address given a prefix length """
|
||||||
ip = netaddr.IPAddress(ip)
|
ip = netaddr.IPAddress(ip)
|
||||||
|
@ -375,21 +385,37 @@ def write_dns_reverse_file(api_client):
|
||||||
f.write(zone_file_content)
|
f.write(zone_file_content)
|
||||||
zone_v6.append(zone6_name)
|
zone_v6.append(zone6_name)
|
||||||
|
|
||||||
api_client = Re2oAPIClient(api_hostname, api_username, api_password, use_tls=False)
|
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 arg in sys.argv:
|
if __name__ == '__main__':
|
||||||
if arg=="--force":
|
parser = argparse.ArgumentParser(description="Générer les fichiers de zone du DNS.")
|
||||||
write_dns_files(api_client)
|
parser.add_argument('-f', '--force', '--forced', help="Forcer la régénaration des fichiers de zone.", action='store_true')
|
||||||
|
parser.add_argument('-k', '--keep', help="Ne pas changer le statut du service.", action='store_true')
|
||||||
|
parser.add_argument('-p', '--processes', help="Regénérer en utilisant n processus en parallèle (par défaut ne pas parallèliser).", metavar='n', nargs=1, type=int, default=0)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
for service in api_client.list("services/regen/"):
|
if args.force:
|
||||||
|
write_dns_files(api_client, args.processes[0])
|
||||||
|
write_dns_reverse_file(api_client)
|
||||||
|
if not args.keep:
|
||||||
|
for service in api_client.list("services/regen/"):
|
||||||
if service['hostname'] == client_hostname and \
|
if service['hostname'] == client_hostname and \
|
||||||
service['service_name'] == 'dns' and \
|
service['service_name'] == 'dns' and \
|
||||||
service['need_regen']:
|
service['need_regen']:
|
||||||
write_dns_files(api_client)
|
|
||||||
write_dns_reverse_file(api_client)
|
|
||||||
api_client.patch(service['api_url'], data={'need_regen': False})
|
api_client.patch(service['api_url'], data={'need_regen': False})
|
||||||
ok = os.system('/usr/sbin/knotc zone-reload >/dev/null 2>&1')
|
else:
|
||||||
if not ok:
|
for service in api_client.list("services/regen/"):
|
||||||
|
if service['hostname'] == client_hostname and \
|
||||||
|
service['service_name'] == 'dns' and \
|
||||||
|
service['need_regen']:
|
||||||
|
write_dns_files(api_client, args.processes[0])
|
||||||
|
write_dns_reverse_file(api_client)
|
||||||
|
if not args.keep:
|
||||||
|
api_client.patch(service['api_url'], data={'need_regen': False})
|
||||||
|
|
||||||
|
error = os.system('/usr/sbin/knotc zone-reload >/dev/null 2>&1')
|
||||||
|
if error:
|
||||||
|
# reload again and display the error message
|
||||||
os.system('/usr/sbin/knotc zone-reload')
|
os.system('/usr/sbin/knotc zone-reload')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue