]>
Commit | Line | Data |
---|---|---|
f22ab085 | 1 | #!/usr/bin/python |
3fdcb9d1 MT |
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 | ############################################################################### | |
f22ab085 MT |
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") | |
4bba8ae2 | 37 | rootlogger.setLevel(logging.INFO) |
f22ab085 MT |
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) | |
4bba8ae2 | 52 | logger.debug(_("Debugging mode enabled")) |
f22ab085 MT |
53 | |
54 | # Initialize the settings array. | |
55 | self.settings = {} | |
56 | ||
57 | # Dict with all providers, that are supported. | |
58 | self.providers = {} | |
59 | self.register_all_providers() | |
60 | ||
61 | # List of configuration entries. | |
62 | self.entries = [] | |
63 | ||
64 | # Add the system class. | |
65 | self.system = DDNSSystem(self) | |
66 | ||
67 | def register_provider(self, provider): | |
68 | """ | |
69 | Registers a new provider. | |
70 | """ | |
71 | assert issubclass(provider, DDNSProvider) | |
72 | ||
73 | provider_handle = provider.INFO.get("handle") | |
74 | assert provider_handle | |
75 | ||
76 | assert not self.providers.has_key(provider_handle), \ | |
77 | "Provider '%s' has already been registered" % provider_handle | |
78 | ||
79 | provider_name = provider.INFO.get("name") | |
80 | assert provider_name | |
81 | ||
82 | logger.debug("Registered new provider: %s (%s)" % (provider_name, provider_handle)) | |
83 | self.providers[provider_handle] = provider | |
84 | ||
85 | def register_all_providers(self): | |
86 | """ | |
87 | Simply registers all providers. | |
88 | """ | |
89 | for provider in ( | |
f3cf1f70 | 90 | DDNSProviderDHS, |
39301272 | 91 | DDNSProviderDNSpark, |
f22ab085 | 92 | DDNSProviderNOIP, |
a36d8df8 | 93 | DDNSProviderLightningWireLabs, |
f22ab085 MT |
94 | DDNSProviderSelfhost, |
95 | ): | |
96 | self.register_provider(provider) | |
97 | ||
2dae4713 MT |
98 | def get_provider_names(self): |
99 | """ | |
100 | Returns a list of names of all registered providers. | |
101 | """ | |
102 | return sorted(self.providers.keys()) | |
103 | ||
f22ab085 MT |
104 | def load_configuration(self, filename): |
105 | configs = ConfigParser.SafeConfigParser() | |
106 | configs.read([filename,]) | |
107 | ||
108 | # First apply all global configuration settings. | |
109 | for k, v in configs.items("config"): | |
110 | self.settings[k] = v | |
111 | ||
112 | for entry in configs.sections(): | |
113 | # Skip the special config section. | |
114 | if entry == "config": | |
115 | continue | |
116 | ||
117 | settings = {} | |
118 | for k, v in configs.items(entry): | |
119 | settings[k] = v | |
120 | settings["hostname"] = entry | |
121 | ||
122 | # Get the name of the provider. | |
123 | provider = settings.get("provider", None) | |
124 | if not provider: | |
125 | logger.warning("Entry '%s' lacks a provider setting. Skipping." % entry) | |
126 | continue | |
127 | ||
128 | # Try to find the provider with the wanted name. | |
129 | try: | |
130 | provider = self.providers[provider] | |
131 | except KeyError: | |
132 | logger.warning("Could not find provider '%s' for entry '%s'." % (provider, entry)) | |
133 | continue | |
134 | ||
135 | # Create an instance of the provider object with settings from the | |
136 | # configuration file. | |
137 | entry = provider(self, **settings) | |
138 | ||
139 | # Add new entry to list (if not already exists). | |
140 | if not entry in self.entries: | |
141 | self.entries.append(entry) | |
142 | ||
1b8c6925 MT |
143 | def updateone(self, hostname, **kwargs): |
144 | for entry in self.entries: | |
145 | if not entry.hostname == hostname: | |
146 | continue | |
147 | ||
148 | return self._update(entry, **kwargs) | |
149 | ||
150 | raise DDNSHostNotFoundError(hostname) | |
151 | ||
152 | def updateall(self, **kwargs): | |
153 | """ | |
154 | Update all configured entries. | |
155 | """ | |
f22ab085 MT |
156 | # If there are no entries, there is nothing to do. |
157 | if not self.entries: | |
158 | logger.debug(_("Found no entries in the configuration file. Exiting.")) | |
159 | return | |
160 | ||
f22ab085 | 161 | for entry in self.entries: |
1b8c6925 | 162 | self._update(entry, **kwargs) |
f22ab085 | 163 | |
1b8c6925 | 164 | def _update(self, entry, force=False): |
f22ab085 | 165 | try: |
9da3e685 | 166 | entry(force=force) |
f22ab085 MT |
167 | |
168 | except DDNSUpdateError, e: | |
169 | logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % \ | |
170 | { "hostname" : entry.hostname, "provider" : entry.name }) | |
171 | logger.error(" %s" % e) | |
172 | ||
173 | except Exception, e: | |
174 | logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) throwed an unhandled exception:") % \ | |
175 | { "hostname" : entry.hostname, "provider" : entry.name }) | |
176 | logger.error(" %s" % e) | |
177 | ||
178 | logger.info(_("Dynamic DNS update for %(hostname)s (%(provider)s) successful") % \ | |
179 | { "hostname" : entry.hostname, "provider" : entry.name }) |