Permalink
Browse files

Fix zone/subzone limit

  • Loading branch information...
1 parent 5dc1e98 commit 7fa4e77f756207d3abe260e4d905e7df2df04393 @slikts committed Apr 26, 2012
Showing with 40 additions and 17 deletions.
  1. +9 −3 config/server.py
  2. +31 −14 server.py
View
@@ -1,21 +1,27 @@
-logger = None
ttl = 90
+logger = None
wan_ip = None
+zone = None
+subzone = None
names = {}
def _update_config():
import logging
import json
+ from sys import argv
from os import getenv
- global logger, wan_ip, names
+ global logger, wan_ip, names, zone, subzone
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
wan_ip = getenv('SSH_CONNECTION').split(' ')[0]
-
names = json.loads(getenv('SSH_ORIGINAL_COMMAND'))
+ zone = argv[1]
+ if len(argv) > 2:
+ subzone = argv[2]
+
_update_config()
View
@@ -11,9 +11,9 @@ class DDNS(object):
max_group_len = 63
group_sep = '.'
subgroup_sep = '-'
- subgroup_pattern = re.compile(r'^[\w\d]+$')
+ subgroup_pattern = re.compile(r'^[*\w\d]+$')
config = None
-
+
class NameTooLong(Exception):
pass
@@ -36,27 +36,44 @@ def validate_name(cls, name):
raise cls.GroupTooLong
for subgroup in group.split(cls.subgroup_sep):
if not cls.subgroup_pattern.match(subgroup):
+ print subgroup
+ exit()
raise cls.InvalidCharacters
def __init__(self, config):
self.config = config
+ config.logger.info('Zone: %s' % config.zone)
+ config.logger.info('Subzone: %s' % config.subzone)
def update(self):
commands = []
- for name, value in self.config.names.iteritems():
- name = self.clean_name(name)
+ params = {
+ 'wan_ip': self.config.wan_ip
+ }
+ for name, (value, record_type) in self.config.names.iteritems():
+ config = self.config
+ name_zone = config.zone
+ if config.subzone:
+ name_zone = '%s.%s' % (config.subzone, name_zone)
+ name = '%s%s' % (self.clean_name(name), name_zone)
self.validate_name(name)
- value = value % self.config
- commands += ['update delete %s' % name,
- 'update add %s. %d A' (name, config.ttl)]
- tmp = tempfile.mkstemp(text=True)
- tmp.write('\n'.join(commands))
- tmp.close()
- logger.debug('Input file: %s')
+ value = value % params
+ commands += ['update delete %s %s' % (name, record_type),
+ 'update add %s. %d %s %s' % (name, config.ttl, record_type, value)]
+ self._run(commands)
+
+ def _run(self, commands):
+ commands = ['zone %s' % (self.config.zone)] \
+ + commands + ['send\n']
+ handle, pathname = tempfile.mkstemp(text=True)
+ descriptor = os.fdopen(handle, 'w')
+ descriptor.write('\n'.join(commands))
+ descriptor.close()
+ self.config.logger.info('Input file: %s' % pathname)
for command in commands:
- logger.info('Command: %s' % command)
- subprocess.call(['/usr/bin/env', 'nsupdate', f.name])
- os.unlink(tmp.name)
+ self.config.logger.info('Command: %s' % command)
+ subprocess.call(['/usr/bin/env', 'nsupdate', '-l', pathname])
+ os.unlink(pathname)
def main():

0 comments on commit 7fa4e77

Please sign in to comment.