sync ovh zone is zone.json does not exists

This commit is contained in:
Gilles Grandou 2020-10-04 15:25:16 +02:00
parent a30efabbda
commit 893327c31d
1 changed files with 22 additions and 6 deletions

View File

@ -124,7 +124,6 @@ def zone_add_entry(zone, prot, name, addr, stamp):
def populate_zone(zone, wan_hostname, wan_addr, hosts, hosts_list, hosts_nat, domain, stamp):
wan_hostname = full_name(wan_hostname, domain)
if not zone.get('A'):
@ -144,14 +143,15 @@ def populate_zone(zone, wan_hostname, wan_addr, hosts, hosts_list, hosts_nat, do
for addr in hosts.get(host, []):
zone_add_entry(zone, 'AAAA', full_name(host, domain), addr, stamp)
def process_zone(zone, stamp, grace_period):
def process_zone(zone, stamp, grace_period, sync_zone):
update = { 'add': [], 'delete': [] }
for prot in zone:
for name in zone[prot]:
add = False
for addr in zone[prot][name]:
e = zone[prot][name][addr]
if e['first'] == stamp:
if e['first'] == stamp and not sync_zone:
update['add'].append([prot, name, addr])
add = True
for addr in zone[prot][name]:
@ -160,6 +160,8 @@ def process_zone(zone, stamp, grace_period):
update['delete'].append([prot, name, addr])
elif not add and stamp - e['last'] > grace_period:
update['delete'].append([prot, name, addr])
elif sync_zone:
update['add'].append([prot, name, addr])
if not update['add'] and not update['delete']:
update = None
@ -202,10 +204,20 @@ def log_update_zone(update):
log('%5s %-20s %-6s %s' % (op, name, prot, addr))
def ovh_update_zone(domain, update):
def ovh_update_zone(domain, zone, update, sync_zone):
try:
client = ovh.Client()
if sync_zone:
for prot in zone:
for name in zone[prot]:
result = client.get('/domain/zone/{}/record'.format(domain),
fieldType=prot, subDomain=name)
for id in result:
print('OVH: delete entry #{} {} {}'.format(id, name, prot))
client.delete('/domain/zone/{}/record/{}'.format(domain, id))
for prot, name, addr in update['delete']:
result = client.get('/domain/zone/%s/record' % domain,
fieldType=prot,
@ -290,13 +302,17 @@ hosts = get_ipv6_hosts()
wan = get_wan_addr()
zone = read_zone_list(zone_filename)
sync_zone = False
if not zone:
sync_zone = True
stamp = int(time.time())
populate_zone(zone, wan_hostname, wan, hosts, hosts_list, nat_list, zone_subdomain, stamp)
update = process_zone(zone, stamp, zone_timeout)
update = process_zone(zone, stamp, zone_timeout, sync_zone)
if update:
log_update_zone(update)
success = ovh_update_zone(zone_domain, update)
success = ovh_update_zone(zone_domain, zone, update, sync_zone)
if success:
send_update_mail(mail_to, mail_from, zone_domain, update, mail_ignore_list, wan)