dim_output_bind/dim_output_bind

69 lines
3.2 KiB
Python

#!/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 <config_file>")
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)
fp.write("\n")
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.split(' '))
elif update['action'] != 'create_zone' and config.has_option('command', 'reload_zone'):
command = config.get('command', 'reload_zone') % (update['zone_name'])
subprocess.call(command.split(' '))
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.split(' '))
c.output_update_delete([update['id']])