Compare commits

..

2 commits

Author SHA1 Message Date
5aba565dd4
Add dockerignore. 2024-06-05 23:46:58 +02:00
11846f1b4f
Allow multiple subdomains. 2024-06-05 23:46:04 +02:00
2 changed files with 47 additions and 31 deletions

4
.dockerignore Normal file
View file

@ -0,0 +1,4 @@
ci
README.md
requirements.txt
*.yaml

74
main.py
View file

@ -3,6 +3,7 @@
import itertools import itertools
import os import os
import sys import sys
import typing
import logging import logging
import yaml import yaml
@ -15,6 +16,12 @@ from schema import And, Optional, Use, Schema
logger = logging.getLogger('ovhdns') 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({ config_schema = Schema({
'ovh': { 'ovh': {
'application_key': Use(str), 'application_key': Use(str),
@ -23,7 +30,7 @@ config_schema = Schema({
'endpoint': Use(str), 'endpoint': Use(str),
}, },
'zone': 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('field_type', default='A'): And(Use(str), Use(str.upper), dns.rdatatype.RdataType.make),
Optional('logging', default={'level': ':WARNING,ovhdns:INFO'}): { Optional('logging', default={'level': ':WARNING,ovhdns:INFO'}): {
Optional('level', default=':WARNING,ovhdns:INFO'): Use(str), Optional('level', default=':WARNING,ovhdns:INFO'): Use(str),
@ -108,44 +115,49 @@ def main(config, level, force_refresh):
logger.debug('Found records %d:', len(records)) logger.debug('Found records %d:', len(records))
for r in records: for r in records:
logger.debug(' - %s', repr(r)) logger.debug(' - %s', repr(r))
records = [ logging.info('matching against subdomains %s', config['subdomain'])
r records_dict = {}
for r in records for subdomain in config['subdomain']:
if r['fieldType'] == config['field_type'] records_dict[subdomain] = [
and r['zone'] == config['zone'] r
and r['subDomain'] == config['subdomain'] for r in records
] if r['fieldType'] == config['field_type']
logger.info('found matching records: %s', records) and r['zone'] == config['zone']
if len(records) > 1: and r['subDomain'] == subdomain
logger.error('found more than one record, don\'t know which to pick') ]
sys.exit(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() my_ips = get_my_ips()
need_refresh = False need_refresh = False
if len(records) == 1: for subdomain, records in records_dict.items():
logger.info('record already present, updating if needed') if len(records) == 1:
if my_ips[0] == records[0]['target']: logger.info('record already present, updating if needed')
logger.info('ip is up-to-date, skipping update') if my_ips[0] == records[0]['target']:
logger.info('ip is up-to-date, skipping update')
else:
need_refresh = True
ovh_client.put(
f'/domain/zone/{config["zone"]}/record/{records[0]["id"]}',
target=my_ips[0],
ttl=config['ttl'],
)
logger.info('updated record, new ip is %s', my_ips[0])
else: else:
logger.info('record not already present, creating new one')
need_refresh = True need_refresh = True
ovh_client.put( ovh_client.post(
f'/domain/zone/{config["zone"]}/record/{records[0]["id"]}', f'/domain/zone/{config["zone"]}/record/',
subDomain=subdomain,
target=my_ips[0], target=my_ips[0],
fieldType=config['field_type'],
ttl=config['ttl'], ttl=config['ttl'],
) )
logger.info('updated record, new ip is %s', my_ips[0]) logger.info('created new record, ip is %s', my_ips[0])
else:
logger.info('record not already present, creating new one')
need_refresh = True
ovh_client.post(
f'/domain/zone/{config["zone"]}/record/',
subDomain=config['subdomain'],
target=my_ips[0],
fieldType=config['field_type'],
ttl=config['ttl'],
)
logger.info('created new record, ip is %s', my_ips[0])
if need_refresh or force_refresh: if need_refresh or force_refresh:
ovh_client.post(f'/domain/zone/{config["zone"]}/refresh') ovh_client.post(f'/domain/zone/{config["zone"]}/refresh')