Skip to content

Commit

Permalink
Merge branch 'release/v0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
eternalharvest committed Mar 25, 2015
2 parents 263bb5e + 50e14d5 commit 9ca6226
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bash_completion.d/r53update
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/bash
#
# R53Update Dynamic DNS Updater v0.3.0
# R53Update Dynamic DNS Updater v0.4.0
# (C)2014 Takuya Sawada All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
43 changes: 20 additions & 23 deletions r53update
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# R53Update Dynamic DNS Updater v0.3.0
# R53Update Dynamic DNS Updater v0.4.0
# (C)2014 Takuya Sawada All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -175,15 +175,10 @@ class R53UpdateApp(App):
self._hostname = hostname
self._resolvername = resolvername

def __resolveServerName(self, servername):
def resolveGlobalIP(self, ns=False):
resolver = dns.resolver.Resolver()
resolver.nameservers = self._app._opts.dns
return map(lambda x: x.to_text(), resolver.query(servername, 'A'))

def resolveGlobalIP(self):
resolver = dns.resolver.Resolver()
resolver.nameservers = self.__resolveServerName(self._resolvername)
return resolver.query(self._hostname, 'A')[0].to_text()
resolver.nameservers = self._app._opts.dns if ns else self.resolveGlobalIP(True)
return map(lambda x: x.to_text(), resolver.query(self._resolvername if ns else self._hostname, 'A'))

class NETIFACES_GlobalIP_Resolver(GlobalIP_Resolver):
def __init__(self, app):
Expand All @@ -195,7 +190,7 @@ class R53UpdateApp(App):
except Exception as e:
raise Exception("%s: no inet address found" % self._app._opts.iface)

return inet[0]['addr']
return map(lambda x: x['addr'], inet)

def _pre_init(self):
super(R53UpdateApp, self)._pre_init()
Expand Down Expand Up @@ -264,7 +259,8 @@ class R53UpdateApp(App):

def __get_global_ip(self):
self.logger.debug('resolving global ip adreess with \'%s\'', self._opts.resolver)
return self._gipresolvers[self._opts.resolver].resolveGlobalIP()
gips = self._gipresolvers[self._opts.resolver].resolveGlobalIP()
return gips if type(gips) is list else [gips]

def __get_records_from_host(self, fqdn):
resolver = dns.resolver.Resolver()
Expand All @@ -285,7 +281,7 @@ class R53UpdateApp(App):

##
# Ref) https://gist.github.com/mariocesar/4142563
def __update_r53_record(self, zone_name, host_name, ip):
def __update_r53_record(self, zone_name, host_name, gips):
fqdn = '%s.%s' % (host_name, zone_name)

conn = self.ctx.getR53Connection()
Expand All @@ -297,7 +293,10 @@ class R53UpdateApp(App):

changes = ResourceRecordSets(conn, zone.id, '')
change = changes.add_change('UPSERT', fqdn, 'A', self._opts.ttl)
change.add_value(ip)

for gip in gips:
change.add_value(ip)

changes.commit()

self.logger.info('update A records of \'%s\' with \'%s\'' % (fqdn, ip))
Expand All @@ -306,19 +305,19 @@ class R53UpdateApp(App):
fqdn = '%s.%s' % (self._opts.host, self._opts.zone)
self.logger.debug('fqdn: %s' % fqdn)

global_ip = self.__get_global_ip()
self.logger.debug('global ip: %s' % global_ip)
gips = self.__get_global_ip()
self.logger.debug('global ips: %s' % str(gips))

recs = self.__get_records_from_host(fqdn)
self.logger.debug('current a records: %s' % str(recs))
arecs = self.__get_records_from_host(fqdn)
self.logger.debug('current a records: %s' % str(arecs))

if not global_ip in recs or len(recs) > 1 or self._opts.force:
if gips != arecs or self._opts.force:
if not self._opts.dry:
self.logger.debug('updating route53 zone info')
self.__update_r53_record(
self._opts.zone,
self._opts.host,
global_ip
gips
)
else:
self.logger.debug('updating route53 zone info (dry-run)')
Expand All @@ -327,11 +326,9 @@ class R53UpdateApp(App):

def show_version(self):
print >>sys.stderr, "Copyrights (c)2014 Takuya Sawada All rights reserved."
print >>sys.stderr, "Route53Update Dynamic DNS Updater 0.3.0"
print >>sys.stderr, "Route53Update Dynamic DNS Updater 0.4.0"


##
# DOCUMENT
#
if __name__ == '__main__':
try:
R53UpdateApp(sys.argv)()
Expand Down

0 comments on commit 9ca6226

Please sign in to comment.