Initial commit.
[ddns.git] / ddns / __init__.py
1 #!/usr/bin/python
2
3 import logging
4 import logging.handlers
5 import ConfigParser
6
7 from i18n import _
8
9 logger = logging.getLogger("ddns.core")
10 logger.propagate = 1
11
12 from .providers import *
13 from .system import DDNSSystem
14
15 # Setup the logger.
16 def setup_logging():
17 rootlogger = logging.getLogger("ddns")
18 rootlogger.setLevel(logging.DEBUG)
19
20 # Setup a logger that logs to syslog.
21 #handler = logging.handlers.SysLogHandler(address="/dev/log")
22
23 handler = logging.StreamHandler()
24 rootlogger.addHandler(handler)
25
26 setup_logging()
27
28 class DDNSCore(object):
29 def __init__(self, debug=False):
30 # In debug mode, enable debug logging.
31 if debug:
32 logger.setLevel(logging.DEBUG)
33
34 # Initialize the settings array.
35 self.settings = {}
36
37 # Dict with all providers, that are supported.
38 self.providers = {}
39 self.register_all_providers()
40
41 # List of configuration entries.
42 self.entries = []
43
44 # Add the system class.
45 self.system = DDNSSystem(self)
46
47 def register_provider(self, provider):
48 """
49 Registers a new provider.
50 """
51 assert issubclass(provider, DDNSProvider)
52
53 provider_handle = provider.INFO.get("handle")
54 assert provider_handle
55
56 assert not self.providers.has_key(provider_handle), \
57 "Provider '%s' has already been registered" % provider_handle
58
59 provider_name = provider.INFO.get("name")
60 assert provider_name
61
62 logger.debug("Registered new provider: %s (%s)" % (provider_name, provider_handle))
63 self.providers[provider_handle] = provider
64
65 def register_all_providers(self):
66 """
67 Simply registers all providers.
68 """
69 for provider in (
70 DDNSProviderNOIP,
71 DDNSProviderSelfhost,
72 ):
73 self.register_provider(provider)
74
75 def load_configuration(self, filename):
76 configs = ConfigParser.SafeConfigParser()
77 configs.read([filename,])
78
79 # First apply all global configuration settings.
80 for k, v in configs.items("config"):
81 self.settings[k] = v
82
83 for entry in configs.sections():
84 # Skip the special config section.
85 if entry == "config":
86 continue
87
88 settings = {}
89 for k, v in configs.items(entry):
90 settings[k] = v
91 settings["hostname"] = entry
92
93 # Get the name of the provider.
94 provider = settings.get("provider", None)
95 if not provider:
96 logger.warning("Entry '%s' lacks a provider setting. Skipping." % entry)
97 continue
98
99 # Try to find the provider with the wanted name.
100 try:
101 provider = self.providers[provider]
102 except KeyError:
103 logger.warning("Could not find provider '%s' for entry '%s'." % (provider, entry))
104 continue
105
106 # Create an instance of the provider object with settings from the
107 # configuration file.
108 entry = provider(self, **settings)
109
110 # Add new entry to list (if not already exists).
111 if not entry in self.entries:
112 self.entries.append(entry)
113
114 def updateall(self):
115 # If there are no entries, there is nothing to do.
116 if not self.entries:
117 logger.debug(_("Found no entries in the configuration file. Exiting."))
118 return
119
120 # Update them all.
121 for entry in self.entries:
122 self.update(entry)
123
124 def update(self, entry):
125 try:
126 entry()
127
128 except DDNSUpdateError, e:
129 logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % \
130 { "hostname" : entry.hostname, "provider" : entry.name })
131 logger.error(" %s" % e)
132
133 except Exception, e:
134 logger.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) throwed an unhandled exception:") % \
135 { "hostname" : entry.hostname, "provider" : entry.name })
136 logger.error(" %s" % e)
137
138 logger.info(_("Dynamic DNS update for %(hostname)s (%(provider)s) successful") % \
139 { "hostname" : entry.hostname, "provider" : entry.name })