Commits

Paul McLanahan committed 52da09c

Switched the API over to Linode.

Comments (0)

Files changed (2)

+#!/usr/bin/env python
+
+import cgi
+import logging
+import os
+import re
+import sys
+import urlparse
+
+from ConfigParser import RawConfigParser
+from urllib2 import urlopen
+
+try:
+    import simplejson as json
+except ImportError:
+    import json
+
+VERSION = (0, 1, 0)
+
+__version__ = '.'.join(map(str, VERSION))
+__author__ = 'Paul McLanahan'
+__contact__ = 'paul@mclanahan.net'
+__homepage__ = 'http://bitbucket.org/pmclanahan/linode-dynamic-dns'
+__license__ = 'BSD (3 clause)'
+
+__all__ = ['Linode']
+BASE_URL = 'https://api.linode.com/'
+LOG_FILE = '/var/log/linode_ddns.log'
+CONFIG_FILE = os.environ.get('LINODEDDNS_CONFIG', '/etc/linode_ddns.conf')
+
+
+class Linode(object):
+
+    def __init__(self, api_key, api_url=BASE_URL):
+        self.api_key = api_key
+        self.api_url = api_url
+
+    def request(self, action, **data):
+        data.update({'api_key':self.api_key, 'api_action':action})
+        urldata = urlparse(data)
+        response = urlopen(self.api_url, data=urlparse(data), timeout=5)
+        return json.loads(response.read())
+
+    def get_all_domains(self):
+        return self.request('domain.list')
+
+    def get_domain_id(self, dname):
+        data = self.get_all_domains()
+        for domain in data['DATA']:
+            if domain['DOMAIN'] == dname:
+                return domain['DOMAINID']
+
+    def set_record_value(self, dname, rid, value='[remote_addr]'):
+        rdata = {
+            'DomainID': self.get_domain_id(dname),
+            'ResourceID': rid,
+            'Target': value,
+        }
+        self.request('domain.resource.update', **rdata)
+
+
+def main(cfg_file=CONFIG_FILE):
+    logging.basicConfig(filename=LOG_FILE,
+                        level=logging.INFO,
+                        format='%(asctime)s %(levelname)s %(message)s')
+
+    def print_result(message='success'):
+        print "ContentType: text/plain\n"
+        print message
+        exit(0)
+
+    if not os.path.exists(CONFIG_FILE):
+        print_result('no config file')
+
+    config = RawConfigParser({'names':None, 'ip_check_url':IP_CHECK_URL})
+    config.read(cfg_file)
+    key = config.get('linode', 'api_key')
+    domain = config.get('linode', 'domain_name')
+    resource = config.get('linode', 'resource_id')
+    logging.debug('domain=%s, names=%r, key=%s', domain, resource, key)
+    sh = Linode(key)
+    sh.set_record_value(domain, resource)
+    logging.info('set %s to current ip', domain)
+    print_result()
+
+
+if __name__ == '__main__':
+    main()
+

slicehost.py

-#!/usr/bin/env python
-
-import cgi
-import logging
-import os
-import re
-import sys
-import urlparse
-
-from ConfigParser import RawConfigParser
-from urllib2 import urlopen
-
-from pyactiveresource.activeresource import ActiveResource
-
-
-VERSION = (0, 1, 0)
-
-__version__ = '.'.join(map(str, VERSION))
-__author__ = 'Paul McLanahan'
-__contact__ = 'paul@mclanahan.net'
-__homepage__ = 'http://bitbucket.org/pmclanahan/slicehost-dynamic-dns'
-__license__ = 'BSD (3 clause)'
-
-__all__ = ['Slicehost']
-BASE_URL = 'https://%s@api.slicehost.com/'
-LOG_FILE = '/var/log/slicehost_ddns.log'
-CONFIG_FILE = os.environ.get('SLICEHOST_CONFIG', '/etc/slicehost_ddns.conf')
-
-
-class Zone(ActiveResource):
-
-    def set_ip_for_records(self, ip_address, names=None, record_type='A'):
-        if not names:
-            names = [self.origin]
-
-        elif isinstance(names, basestring):
-            names = [names]
-
-        records = Record.find(zone_id=self.id, record_type=record_type)
-        r_count = 0
-        for record in records:
-            if record.name in names:
-                record.data = ip_address
-                record.save()
-                r_count += 1
-
-        return r_count
-
-
-class Record(ActiveResource):
-    pass
-
-
-class Slicehost(object):
-
-    def __init__(self, api_key):
-        self.api_key = api_key
-        site_url = BASE_URL % api_key
-        Zone.site = site_url
-        Record.site = site_url
-
-    def set_zone_ip(self, domain, ip_address, names=None, record_type='A'):
-        if not domain.endswith('.'):
-            domain += '.'
-        zone = Zone.find_first(origin=domain)
-        if zone is None:
-            return False
-        return zone.set_ip_for_records(ip_address, names, record_type)
-
-def main(cfg_file=CONFIG_FILE):
-    logging.basicConfig(filename=LOG_FILE,
-                        level=logging.INFO,
-                        format='%(asctime)s %(levelname)s %(message)s')
-
-    def print_result(message='success'):
-        print "ContentType: text/plain\n"
-        print message
-        exit(0)
-        
-    if not os.path.exists(CONFIG_FILE):
-        print_result('no config file')
-    
-    config = RawConfigParser({'names':None})
-    config.read(cfg_file)
-    try:
-        html = urlopen('http://checkip.dyndns.org/').read()
-    except IOError:
-        print_result('error')
-    m = re.search(r'(\d{1,3}\.){3}(\d{1,3})', html)
-    if m:
-        ip = m.group(0)
-        key = config.get('slicehost', 'api_key')
-        domain = config.get('slicehost', 'domain')
-        names = config.get('slicehost', 'names')
-        if names:
-            names = names.split()
-        logging.debug('ip=%s, domain=%s, names=%r, key=%s', ip, domain, names, key)
-        sh = Slicehost(key)
-        sh.set_zone_ip(domain, ip, names)
-        logging.info('set %s to %s', domain, ip)
-        print_result()
-    
-    print_result('error')
-
-if __name__ == '__main__':
-    main()
-