Add DNS Park as new provider.
[ddns.git] / src / ddns / __init__.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # ddns - A dynamic DNS client for IPFire #
5 # Copyright (C) 2012 IPFire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import logging
23 import logging.handlers
24 import ConfigParser
25
26 from i18n import _
27
28 logger = logging.getLogger("ddns.core")
29 logger.propagate = 1
30
31 from .providers import *
32 from .system import DDNSSystem
33
34 # Setup the logger.
35 def setup_logging():
36 rootlogger = logging.getLogger("ddns")
37 rootlogger.setLevel(logging.DEBUG)
38
39 # Setup a logger that logs to syslog.
40 #handler = logging.handlers.SysLogHandler(address="/dev/log")
41
42 handler = logging.StreamHandler()
43 rootlogger.addHandler(handler)
44
45 setup_logging()
46
47 class DDNSCore(object):
48 def __init__(self, debug=False):
49 # In debug mode, enable debug logging.
50 if debug:
51 logger.setLevel(logging.DEBUG)
52
53 # Initialize the settings array.
54 self.settings = {}
55
56 # Dict with all providers, that are supported.
57 self.providers = {}
58 self.register_all_providers()
59
60 # List of configuration entries.
61 self.entries = []
62
63 # Add the system class.
64 self.system = DDNSSystem(self)
65
66 def register_provider(self, provider):
67 """
68 Registers a new provider.
69 """
70 assert issubclass(provider, DDNSProvider)
71
72 provider_handle = provider.INFO.get("handle")
73 assert provider_handle
74
75 assert not self.providers.has_key(provider_handle), \
76 "Provider '%s' has already been registered" % provider_handle
77
78 provider_name = provider.INFO.get("name")
79 assert provider_name
80
81 logger.debug("Registered new provider: %s (%s)" % (provider_name, provider_handle))
82 self.providers[provider_handle] = provider
83
84 def register_all_providers(self):
85 """
86 Simply registers all providers.
87 """
88 for provider in (
89 DDNSProviderDHS,
90 DDNSProviderDNSpark,
91 DDNSProviderNOIP,
92 DDNSProviderLightningWireLabs,
93 DDNSProviderSelfhost,
94 ):
95 self.register_provider(provider)
96
97 def load_configuration(self, filename):
98 configs = ConfigParser.SafeConfigParser()
99 configs.read([filename,])
100
101 # First apply all global configuration settings.
102 for k, v in configs.items("config"):
103 self.settings[k] = v
104
105 for entry in configs.sections():
106 # Skip the special config section.
107 if entry == "config":
108 continue
109
110 settings = {}
111 for k, v in configs.items(entry):
112 settings[k] = v
113 settings["hostname"] = entry
114
115 # Get the name of the provider.
116 provider = settings.get("provider", None)
117 if not provider:
118 logger.warning("Entry '%s' lacks a provider setting. Skipping." % entry)
119 continue
120
121 # Try to find the provider with the wanted name.
122 try:
123 provider = self.providers[provider]
124 except KeyError:
125 logger.warning("Could not find provider '%s' for entry '%s'." % (provider, entry))
126 continue
127
128 # Create an instance of the provider object with settings from the
129 # configuration file.
130 entry = provider(self, **settings)
131
132 # Add new entry to list (if not already exists).
133 if not entry in self.entries:
134 self.entries.append(entry)
135
136 def updateall(self):
137 # If there are no entries, there is nothing to do.
138 if not self.entries:
139 logger.debug(_("Found no entries in the configuration file. Exiting."))
140 return
141
142 # Update them all.
143 for entry in self.entries:
144 self.update(entry)
145
146 def update(self, entry):
147 try:
148 entry()
149
150 except DDNSUpdateError, e:
151 logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % \
152 { "hostname" : entry.hostname, "provider" : entry.name })
153 logger.error(" %s" % e)
154
155 except Exception, e:
156 logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) throwed an unhandled exception:") % \
157 { "hostname" : entry.hostname, "provider" : entry.name })
158 logger.error(" %s" % e)
159
160 logger.info(_("Dynamic DNS update for %(hostname)s (%(provider)s) successful") % \
161 { "hostname" : entry.hostname, "provider" : entry.name })