Add DHS 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 DDNSProviderNOIP,
91 DDNSProviderLightningWireLabs,
92 DDNSProviderSelfhost,
93 ):
94 self.register_provider(provider)
95
96 def load_configuration(self, filename):
97 configs = ConfigParser.SafeConfigParser()
98 configs.read([filename,])
99
100 # First apply all global configuration settings.
101 for k, v in configs.items("config"):
102 self.settings[k] = v
103
104 for entry in configs.sections():
105 # Skip the special config section.
106 if entry == "config":
107 continue
108
109 settings = {}
110 for k, v in configs.items(entry):
111 settings[k] = v
112 settings["hostname"] = entry
113
114 # Get the name of the provider.
115 provider = settings.get("provider", None)
116 if not provider:
117 logger.warning("Entry '%s' lacks a provider setting. Skipping." % entry)
118 continue
119
120 # Try to find the provider with the wanted name.
121 try:
122 provider = self.providers[provider]
123 except KeyError:
124 logger.warning("Could not find provider '%s' for entry '%s'." % (provider, entry))
125 continue
126
127 # Create an instance of the provider object with settings from the
128 # configuration file.
129 entry = provider(self, **settings)
130
131 # Add new entry to list (if not already exists).
132 if not entry in self.entries:
133 self.entries.append(entry)
134
135 def updateall(self):
136 # If there are no entries, there is nothing to do.
137 if not self.entries:
138 logger.debug(_("Found no entries in the configuration file. Exiting."))
139 return
140
141 # Update them all.
142 for entry in self.entries:
143 self.update(entry)
144
145 def update(self, entry):
146 try:
147 entry()
148
149 except DDNSUpdateError, e:
150 logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % \
151 { "hostname" : entry.hostname, "provider" : entry.name })
152 logger.error(" %s" % e)
153
154 except Exception, e:
155 logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) throwed an unhandled exception:") % \
156 { "hostname" : entry.hostname, "provider" : entry.name })
157 logger.error(" %s" % e)
158
159 logger.info(_("Dynamic DNS update for %(hostname)s (%(provider)s) successful") % \
160 { "hostname" : entry.hostname, "provider" : entry.name })