Commit ff6dde66 authored by ale's avatar ale

autogenerate a named.conf snippet

parent 7393336e
......@@ -11,6 +11,10 @@ def main():
parser.add_option(
'-c', '--config', dest='config', metavar='FILE',
help='Load global configuration from FILE (in YAML format)')
parser.add_option(
'--named-conf', dest='named_conf', metavar='FILE',
help='Location of the autogenerated named.conf snippet '
'(the file will not be created if the option is unset)')
parser.add_option(
'-o', '--output-dir', dest='output_dir',
default='.', metavar='DIR',
......@@ -61,6 +65,7 @@ def main():
# Render the zone data to 'output_dir'.
zw = zone.ZoneWriter(opts.output_dir,
opts.named_conf,
delete=opts.delete,
dry_run=opts.dry_run)
changed, removed = zw.write(zp.render(), postproc=pproc)
......
......@@ -155,8 +155,9 @@ def readyaml(filenames):
class ZoneWriter(object):
def __init__(self, output_dir, delete=False, dry_run=False):
def __init__(self, output_dir, named_conf, delete=False, dry_run=False):
self.output_dir = output_dir
self.named_conf = named_conf
self.dry_run = dry_run
self.delete_old_files = delete
......@@ -170,11 +171,21 @@ class ZoneWriter(object):
fd.write(data)
return True
def config_snippet(self, zone_name, zone_file):
return '''
zone "%s" {
type master;
file "%s";
allow-query { any; };
};
''' % (zone_name, zone_file)
def write(self, zones, postproc=None):
# Compare the current contents of 'output_dir' with the
# generated output to detect zones that were removed.
existing = set(os.listdir(self.output_dir))
changed = set()
config = []
for zone_name, zone_attrs, zone_data in zones:
outfile = zone_name
zone_changed = False
......@@ -187,12 +198,21 @@ class ZoneWriter(object):
outfile = postproc(
self.output_dir, outfile, zone_name, zone_attrs, zone_changed)
existing.discard(outfile)
# Generate config snippet.
config.append(self.config_snippet(
zone_name, os.path.join(self.output_dir, outfile)))
# Wipe what shouldn't be there.
if not self.dry_run and self.delete_old_files:
for filename in existing:
os.remove(os.path.join(self.output_dir, filename))
# Write the autogenerated named.conf.
if not self.dry_run and self.named_conf:
with open(self.named_conf, 'w') as fd:
for c in config:
fd.write(c)
return changed, existing
......
......@@ -58,7 +58,10 @@ class ZoneWriterTestBase(unittest.TestCase):
def setUp(self):
self.zp = ZoneParser(TEST_CONFIG)
self.tmpdir = tempfile.mkdtemp()
self.zw = ZoneWriter(self.tmpdir)
zonedir = os.path.join(self.tmpdir, 'zones')
os.mkdir(zonedir)
self.zw = ZoneWriter(zonedir,
os.path.join(self.tmpdir, 'named.conf'))
def tearDown(self):
shutil.rmtree(self.tmpdir)
......@@ -71,6 +74,8 @@ class ZoneWriterTest(ZoneWriterTestBase):
changed, removed = self.zw.write(self.zp.render())
self.assertEquals(set(['autistici.org']), changed)
self.assertEquals(set(), removed)
self.assertTrue(
os.path.exists(os.path.join(self.tmpdir, 'named.conf')))
# Writing a second time, should see no changes.
changed, removed = self.zw.write(self.zp.render())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment