]>
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 | ||
37e24fbf | 31 | import database |
adfe6272 MT |
32 | import providers |
33 | ||
6367cd92 | 34 | from .errors import * |
f22ab085 MT |
35 | from .system import DDNSSystem |
36 | ||
37 | # Setup the logger. | |
38 | def setup_logging(): | |
39 | rootlogger = logging.getLogger("ddns") | |
4bba8ae2 | 40 | rootlogger.setLevel(logging.INFO) |
f22ab085 MT |
41 | |
42 | # Setup a logger that logs to syslog. | |
5ad24917 MT |
43 | handler = logging.handlers.SysLogHandler(address="/dev/log", |
44 | facility=logging.handlers.SysLogHandler.LOG_DAEMON | |
45 | ) | |
21fd4b8d MT |
46 | formatter = logging.Formatter("ddns[%(process)d]: %(message)s") |
47 | handler.setFormatter(formatter) | |
5ad24917 MT |
48 | handler.setLevel(logging.INFO) |
49 | rootlogger.addHandler(handler) | |
f22ab085 MT |
50 | |
51 | handler = logging.StreamHandler() | |
52 | rootlogger.addHandler(handler) | |
53 | ||
54 | setup_logging() | |
55 | ||
56 | class DDNSCore(object): | |
57 | def __init__(self, debug=False): | |
58 | # In debug mode, enable debug logging. | |
59 | if debug: | |
87649e22 MT |
60 | rootlogger = logging.getLogger("ddns") |
61 | rootlogger.setLevel(logging.DEBUG) | |
87649e22 | 62 | |
4bba8ae2 | 63 | logger.debug(_("Debugging mode enabled")) |
f22ab085 MT |
64 | |
65 | # Initialize the settings array. | |
66 | self.settings = {} | |
67 | ||
68 | # Dict with all providers, that are supported. | |
adfe6272 MT |
69 | self.providers = providers.get() |
70 | ||
71 | for handle, provider in sorted(self.providers.items()): | |
72 | logger.debug("Registered new provider: %s (%s)" % (provider.name, provider.handle)) | |
f22ab085 MT |
73 | |
74 | # List of configuration entries. | |
75 | self.entries = [] | |
76 | ||
77 | # Add the system class. | |
78 | self.system = DDNSSystem(self) | |
79 | ||
37e24fbf MT |
80 | # Open the database. |
81 | self.db = database.DDNSDatabase(self, "/var/lib/ddns.db") | |
82 | ||
2dae4713 MT |
83 | def get_provider_names(self): |
84 | """ | |
85 | Returns a list of names of all registered providers. | |
86 | """ | |
87 | return sorted(self.providers.keys()) | |
88 | ||
f22ab085 | 89 | def load_configuration(self, filename): |
332fd7cc MT |
90 | logger.debug(_("Loading configuration file %s") % filename) |
91 | ||
e2f7e200 | 92 | configs = ConfigParser.RawConfigParser() |
f22ab085 MT |
93 | configs.read([filename,]) |
94 | ||
95 | # First apply all global configuration settings. | |
573b679e MT |
96 | try: |
97 | for k, v in configs.items("config"): | |
98 | self.settings[k] = v | |
99 | ||
100 | # Allow missing config section | |
101 | except ConfigParser.NoSectionError: | |
102 | pass | |
f22ab085 MT |
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 | ||
1b8c6925 MT |
135 | def updateone(self, hostname, **kwargs): |
136 | for entry in self.entries: | |
137 | if not entry.hostname == hostname: | |
138 | continue | |
139 | ||
140 | return self._update(entry, **kwargs) | |
141 | ||
142 | raise DDNSHostNotFoundError(hostname) | |
143 | ||
144 | def updateall(self, **kwargs): | |
145 | """ | |
146 | Update all configured entries. | |
147 | """ | |
f22ab085 MT |
148 | # If there are no entries, there is nothing to do. |
149 | if not self.entries: | |
150 | logger.debug(_("Found no entries in the configuration file. Exiting.")) | |
151 | return | |
152 | ||
f22ab085 | 153 | for entry in self.entries: |
1b8c6925 | 154 | self._update(entry, **kwargs) |
f22ab085 | 155 | |
1b8c6925 | 156 | def _update(self, entry, force=False): |
f22ab085 | 157 | try: |
9da3e685 | 158 | entry(force=force) |
f22ab085 | 159 | |
4689f5d3 | 160 | except DDNSError, e: |
f22ab085 MT |
161 | logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % \ |
162 | { "hostname" : entry.hostname, "provider" : entry.name }) | |
4689f5d3 MT |
163 | logger.error(" %s: %s" % (e.__class__.__name__, e.reason)) |
164 | if e.message: | |
165 | logger.error(" %s" % e.message) | |
f22ab085 MT |
166 | |
167 | except Exception, e: | |
168 | logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) throwed an unhandled exception:") % \ | |
708da5a4 | 169 | { "hostname" : entry.hostname, "provider" : entry.name }, exc_info=True) |