From 6eb52268a09fa451af42b87cad03844c341a4da6 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Wed, 26 Feb 2020 22:38:28 +0100 Subject: [PATCH] initial commit --- dim_output_bind | 67 +++++++++++++++++++++++++++++++++++++++++++++ dim_output_bind.ini | 32 ++++++++++++++++++++++ requirements.txt | 1 + setup.py | 8 ++++++ 4 files changed, 108 insertions(+) create mode 100644 dim_output_bind create mode 100644 dim_output_bind.ini create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/dim_output_bind b/dim_output_bind new file mode 100644 index 0000000..ccb1302 --- /dev/null +++ b/dim_output_bind @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +import os +import sys +import subprocess +from dimclient import DimClient +# needs to be adjusted for python3 +from ConfigParser import ConfigParser + +def print_help(): + print("Usage: bind_zone_output ") + +if __name__ == "__main__": + if len(sys.argv) < 2: + print_help() + sys.exit(-1) + config = ConfigParser() + config.read(sys.argv[1]) + + if not config.has_section('default'): + print("default section not found in config file") + sys.exit(-1) + if not config.has_section('command'): + print("command section not found in config file") + sys.exit(-1) + if not config.has_option('default', 'server'): + print("setting 'server' not found in config file") + sys.exit(-1) + if not config.has_option('default', 'output'): + print("setting 'output' not found in config file") + sys.exit(-1) + if not config.has_option('default', 'zone_dir'): + print("setting 'zone_dir' not found in config file") + sys.exit(-1) + if not config.has_option('default', 'filename'): + print("setting 'filename' not found in config file") + sys.exit(-1) + + c = DimClient(config.get('default', 'server')) + if config.has_option('default', 'username'): + c.login(config.get('default', 'username'), config.get('default', 'password')) + + to_update = c.output_update_list(output=config.get('default', 'output')) + for update in to_update: + filename = config.get('default', 'filename') % (update['zone_name']) + filepath = os.path.join(config.get('default', 'zone_dir'), filename) + if update['action'] != 'delete_zone': + output = c.zone_dump(update['zone_name']) + try: + with open(filepath, 'w+', 0644) as fp: + fp.write(output) + except IOError as e: + print("could not write zone file '%s': %s" % (update['zone_name'], e)) + sys.exit(-1) + if update['action'] == 'create_zone' and config.has_option('command', 'add_zone'): + command = config.get('command', 'add_zone') % (update['zone_name']) + subprocess.call(command) + elif update['action'] != 'create_zone' and config.has_option('command', 'reload_zone'): + command = config.get('command', 'reload_zone') % (update['zone_name']) + subprocess.call(command) + elif update['action'] == 'delete_zone': + os.remove(filepath) + if config.has_option('command', 'del_zone'): + command = config.get('command', 'del_zone') % (update['zone_name']) + subprocess.call(command) + c.output_update_delete([update['id']]) + diff --git a/dim_output_bind.ini b/dim_output_bind.ini new file mode 100644 index 0000000..418aa6f --- /dev/null +++ b/dim_output_bind.ini @@ -0,0 +1,32 @@ +[default] +# server defines the server address to pull the update list from. +server = http://url.to.dim + +# Set the output name to update the specific output queue. +output = test + +# username is the user as which to connect. +# This is important as the user requires the permission to update the +# output queue. +username = myuser + +# Set a password if one was set for the user account. +password = none + +# Set the directory where all zone files are located. It should be outside +# of any other config files. +zone_dir = /tmp/zones/ + +# Set the filename format for the generated zone files. %s will be replaced +# with the zone name. +filename = %s.zone + +[command] +# Set the command to run, when a zone was added. +# add_zone = nsd-control addzone %s forward + +# Set the command to run after a zone was deleted. +# del_zone = nsd-control delzone %s + +# Set the command to reload a zone after the file was updated. +# reload_zone = nsd-control reload %s diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d5c33ba --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +dimclient>=0.4.2 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..4d1fae3 --- /dev/null +++ b/setup.py @@ -0,0 +1,8 @@ +from distutils.core import setup + +setup(name='dim_output_bind', + version='0.1', + description='A plugin to write bind files from dim output.', + scripts=['dim_output_bind'], + data_files=[('share/dim_outout_bind', ['dim_output_bind.ini'])], + install_requires=['dimclient>=0.4.2'])