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