1
0
mirror of https://github.com/github/octodns.git synced 2024-05-11 05:55:00 +00:00

Convert Manager.config['zones'] to IdnaDict

- print Zone.decoded_name to logs for better readability
- Prefer decoded name in YamlProvider
This commit is contained in:
Ross McFarland
2022-08-17 10:28:17 -07:00
parent 67c75ff146
commit c1ef45e0fd
6 changed files with 39 additions and 22 deletions

View File

@@ -17,6 +17,7 @@ from sys import stdout
import logging import logging
from . import __VERSION__ from . import __VERSION__
from .idna import IdnaDict, idna_decode
from .provider.base import BaseProvider from .provider.base import BaseProvider
from .provider.plan import Plan from .provider.plan import Plan
from .provider.yaml import SplitYamlProvider, YamlProvider from .provider.yaml import SplitYamlProvider, YamlProvider
@@ -114,6 +115,8 @@ class Manager(object):
# Read our config file # Read our config file
with open(config_file, 'r') as fh: with open(config_file, 'r') as fh:
self.config = safe_load(fh, enforce_order=False) self.config = safe_load(fh, enforce_order=False)
# convert the zones portion of things into an IdnaDict
self.config['zones'] = IdnaDict(self.config['zones'])
manager_config = self.config.get('manager', {}) manager_config = self.config.get('manager', {})
max_workers = ( max_workers = (
@@ -341,10 +344,12 @@ class Manager(object):
lenient=False, lenient=False,
): ):
self.log.debug(
'sync: populating, zone=%s, lenient=%s', zone_name, lenient
)
zone = Zone(zone_name, sub_zones=self.configured_sub_zones(zone_name)) zone = Zone(zone_name, sub_zones=self.configured_sub_zones(zone_name))
self.log.debug(
'sync: populating, zone=%s, lenient=%s',
zone.decoded_name,
lenient,
)
if desired: if desired:
# This is an alias zone, rather than populate it we'll copy the # This is an alias zone, rather than populate it we'll copy the
@@ -368,7 +373,7 @@ class Manager(object):
for processor in processors: for processor in processors:
zone = processor.process_source_zone(zone, sources=sources) zone = processor.process_source_zone(zone, sources=sources)
self.log.debug('sync: planning, zone=%s', zone_name) self.log.debug('sync: planning, zone=%s', zone.decoded_name)
plans = [] plans = []
for target in targets: for target in targets:
@@ -431,7 +436,7 @@ class Manager(object):
aliased_zones = {} aliased_zones = {}
futures = [] futures = []
for zone_name, config in zones: for zone_name, config in zones:
self.log.info('sync: zone=%s', zone_name) self.log.info('sync: zone=%s', idna_decode(zone_name))
if 'alias' in config: if 'alias' in config:
source_zone = config['alias'] source_zone = config['alias']
@@ -602,7 +607,7 @@ class Manager(object):
self.log.debug('sync: applying') self.log.debug('sync: applying')
zones = self.config['zones'] zones = self.config['zones']
for target, plan in plans: for target, plan in plans:
zone_name = plan.existing.name zone_name = plan.existing.decoded_name
if zones[zone_name].get('always-dry-run', False): if zones[zone_name].get('always-dry-run', False):
self.log.info( self.log.info(
'sync: zone=%s skipping always-dry-run', zone_name 'sync: zone=%s skipping always-dry-run', zone_name

View File

@@ -114,7 +114,7 @@ class BaseProvider(BaseSource):
self.log.warning( self.log.warning(
'root NS record supported, but no record ' 'root NS record supported, but no record '
'is configured for %s', 'is configured for %s',
desired.name, desired.decoded_name,
) )
else: else:
if record: if record:
@@ -179,7 +179,7 @@ class BaseProvider(BaseSource):
self.log.warning('%s; %s', msg, fallback) self.log.warning('%s; %s', msg, fallback)
def plan(self, desired, processors=[]): def plan(self, desired, processors=[]):
self.log.info('plan: desired=%s', desired.name) self.log.info('plan: desired=%s', desired.decoded_name)
existing = Zone(desired.name, desired.sub_zones) existing = Zone(desired.name, desired.sub_zones)
exists = self.populate(existing, target=True, lenient=True) exists = self.populate(existing, target=True, lenient=True)
@@ -247,7 +247,7 @@ class BaseProvider(BaseSource):
self.log.info('apply: disabled') self.log.info('apply: disabled')
return 0 return 0
zone_name = plan.desired.name zone_name = plan.desired.decoded_name
num_changes = len(plan.changes) num_changes = len(plan.changes)
self.log.info('apply: making %d changes to %s', num_changes, zone_name) self.log.info('apply: making %d changes to %s', num_changes, zone_name)
self._apply(plan) self._apply(plan)

View File

@@ -165,8 +165,8 @@ class PlanLogger(_PlanOutput):
if plans: if plans:
current_zone = None current_zone = None
for target, plan in plans: for target, plan in plans:
if plan.desired.name != current_zone: if plan.desired.decoded_name != current_zone:
current_zone = plan.desired.name current_zone = plan.desired.decoded_name
buf.write(hr) buf.write(hr)
buf.write('* ') buf.write('* ')
buf.write(current_zone) buf.write(current_zone)
@@ -215,8 +215,8 @@ class PlanMarkdown(_PlanOutput):
if plans: if plans:
current_zone = None current_zone = None
for target, plan in plans: for target, plan in plans:
if plan.desired.name != current_zone: if plan.desired.decoded_name != current_zone:
current_zone = plan.desired.name current_zone = plan.desired.decoded_name
fh.write('## ') fh.write('## ')
fh.write(current_zone) fh.write(current_zone)
fh.write('\n\n') fh.write('\n\n')
@@ -276,8 +276,8 @@ class PlanHtml(_PlanOutput):
if plans: if plans:
current_zone = None current_zone = None
for target, plan in plans: for target, plan in plans:
if plan.desired.name != current_zone: if plan.desired.decoded_name != current_zone:
current_zone = plan.desired.name current_zone = plan.desired.decoded_name
fh.write('<h2>') fh.write('<h2>')
fh.write(current_zone) fh.write(current_zone)
fh.write('</h2>\n') fh.write('</h2>\n')

View File

@@ -147,7 +147,7 @@ class YamlProvider(BaseProvider):
self.log = logging.getLogger(f'{klass}[{id}]') self.log = logging.getLogger(f'{klass}[{id}]')
self.log.debug( self.log.debug(
'__init__: id=%s, directory=%s, default_ttl=%d, ' '__init__: id=%s, directory=%s, default_ttl=%d, '
'enforce_order=%d, populate_should_replace=%d', 'nforce_order=%d, populate_should_replace=%d',
id, id,
directory, directory,
default_ttl, default_ttl,
@@ -196,7 +196,7 @@ class YamlProvider(BaseProvider):
def populate(self, zone, target=False, lenient=False): def populate(self, zone, target=False, lenient=False):
self.log.debug( self.log.debug(
'populate: name=%s, target=%s, lenient=%s', 'populate: name=%s, target=%s, lenient=%s',
zone.name, zone.decoded_name,
target, target,
lenient, lenient,
) )
@@ -207,7 +207,15 @@ class YamlProvider(BaseProvider):
return False return False
before = len(zone.records) before = len(zone.records)
filename = join(self.directory, f'{zone.name}yaml') filename = join(self.directory, f'{zone.decoded_name}yaml')
if not isfile(filename):
idna_filename = join(self.directory, f'{zone.name}yaml')
self.log.warning(
'populate: "%s" does not exist, falling back to idna version "%s"',
filename,
idna_filename,
)
filename = idna_filename
self._populate_from_file(filename, zone, lenient) self._populate_from_file(filename, zone, lenient)
self.log.info( self.log.info(
@@ -220,7 +228,9 @@ class YamlProvider(BaseProvider):
desired = plan.desired desired = plan.desired
changes = plan.changes changes = plan.changes
self.log.debug( self.log.debug(
'_apply: zone=%s, len(changes)=%d', desired.name, len(changes) '_apply: zone=%s, len(changes)=%d',
desired.decoded_name,
len(changes),
) )
# Since we don't have existing we'll only see creates # Since we don't have existing we'll only see creates
records = [c.new for c in changes] records = [c.new for c in changes]
@@ -248,7 +258,7 @@ class YamlProvider(BaseProvider):
self._do_apply(desired, data) self._do_apply(desired, data)
def _do_apply(self, desired, data): def _do_apply(self, desired, data):
filename = join(self.directory, f'{desired.name}yaml') filename = join(self.directory, f'{desired.decoded_name}yaml')
self.log.debug('_apply: writing filename=%s', filename) self.log.debug('_apply: writing filename=%s', filename)
with open(filename, 'w') as fh: with open(filename, 'w') as fh:
safe_dump(dict(data), fh) safe_dump(dict(data), fh)

View File

@@ -164,7 +164,7 @@ class Record(EqualityTupleMixin):
def __init__(self, zone, name, data, source=None): def __init__(self, zone, name, data, source=None):
self.log.debug( self.log.debug(
'__init__: zone.name=%s, type=%11s, name=%s', '__init__: zone.name=%s, type=%11s, name=%s',
zone.name, zone.decoded_name,
self.__class__.__name__, self.__class__.__name__,
name, name,
) )

View File

@@ -13,6 +13,7 @@ from collections import defaultdict
from logging import getLogger from logging import getLogger
import re import re
from .idna import idna_decode
from .record import Create, Delete from .record import Create, Delete
@@ -36,6 +37,7 @@ class Zone(object):
raise Exception(f'Invalid zone name {name}, missing ending dot') raise Exception(f'Invalid zone name {name}, missing ending dot')
# Force everything to lowercase just to be safe # Force everything to lowercase just to be safe
self.name = str(name).lower() if name else name self.name = str(name).lower() if name else name
self.decoded_name = idna_decode(self.name)
self.sub_zones = sub_zones self.sub_zones = sub_zones
# We're grouping by node, it allows us to efficiently search for # We're grouping by node, it allows us to efficiently search for
# duplicates and detect when CNAMEs co-exist with other records # duplicates and detect when CNAMEs co-exist with other records
@@ -283,4 +285,4 @@ class Zone(object):
return copy return copy
def __repr__(self): def __repr__(self):
return f'Zone<{self.name}>' return f'Zone<{self.decoded_name}>'