Allow multiple subdomains.

This commit is contained in:
redxef 2024-06-05 23:46:04 +02:00
parent 1654cf5d76
commit 11846f1b4f
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921

24
main.py
View file

@ -3,6 +3,7 @@
import itertools
import os
import sys
import typing
import logging
import yaml
@ -15,6 +16,12 @@ from schema import And, Optional, Use, Schema
logger = logging.getLogger('ovhdns')
def str_or_list(i: typing.Union[str, typing.List[str]]) -> typing.List[str]:
if isinstance(i, str):
return [i]
return i
config_schema = Schema({
'ovh': {
'application_key': Use(str),
@ -23,7 +30,7 @@ config_schema = Schema({
'endpoint': Use(str),
},
'zone': Use(str),
Optional('subdomain', default=''): Use(str),
Optional('subdomain', default=''): Use(str_or_list),
Optional('field_type', default='A'): And(Use(str), Use(str.upper), dns.rdatatype.RdataType.make),
Optional('logging', default={'level': ':WARNING,ovhdns:INFO'}): {
Optional('level', default=':WARNING,ovhdns:INFO'): Use(str),
@ -108,20 +115,25 @@ def main(config, level, force_refresh):
logger.debug('Found records %d:', len(records))
for r in records:
logger.debug(' - %s', repr(r))
records = [
logging.info('matching against subdomains %s', config['subdomain'])
records_dict = {}
for subdomain in config['subdomain']:
records_dict[subdomain] = [
r
for r in records
if r['fieldType'] == config['field_type']
and r['zone'] == config['zone']
and r['subDomain'] == config['subdomain']
and r['subDomain'] == subdomain
]
logger.info('found matching records: %s', records)
if len(records) > 1:
logger.info('found matching records: %s', records_dict)
for v in records_dict.values():
if len(v) > 1:
logger.error('found more than one record, don\'t know which to pick')
sys.exit(1)
my_ips = get_my_ips()
need_refresh = False
for subdomain, records in records_dict.items():
if len(records) == 1:
logger.info('record already present, updating if needed')
if my_ips[0] == records[0]['target']:
@ -140,7 +152,7 @@ def main(config, level, force_refresh):
need_refresh = True
ovh_client.post(
f'/domain/zone/{config["zone"]}/record/',
subDomain=config['subdomain'],
subDomain=subdomain,
target=my_ips[0],
fieldType=config['field_type'],
ttl=config['ttl'],