]>
git.ipfire.org Git - oddments/ddns.git/blob - src/ddns/__init__.py
2 ###############################################################################
4 # ddns - A dynamic DNS client for IPFire #
5 # Copyright (C) 2012 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
23 import logging
.handlers
28 logger
= logging
.getLogger("ddns.core")
34 from .system
import DDNSSystem
38 rootlogger
= logging
.getLogger("ddns")
39 rootlogger
.setLevel(logging
.INFO
)
41 # Setup a logger that logs to syslog.
42 handler
= logging
.handlers
.SysLogHandler(address
="/dev/log",
43 facility
=logging
.handlers
.SysLogHandler
.LOG_DAEMON
45 handler
.setLevel(logging
.INFO
)
46 rootlogger
.addHandler(handler
)
48 handler
= logging
.StreamHandler()
49 rootlogger
.addHandler(handler
)
53 class DDNSCore(object):
54 def __init__(self
, debug
=False):
55 # In debug mode, enable debug logging.
57 rootlogger
= logging
.getLogger("ddns")
58 rootlogger
.setLevel(logging
.DEBUG
)
60 logger
.debug(_("Debugging mode enabled"))
62 # Initialize the settings array.
65 # Dict with all providers, that are supported.
66 self
.providers
= providers
.get()
68 for handle
, provider
in sorted(self
.providers
.items()):
69 logger
.debug("Registered new provider: %s (%s)" % (provider
.name
, provider
.handle
))
71 # List of configuration entries.
74 # Add the system class.
75 self
.system
= DDNSSystem(self
)
77 def get_provider_names(self
):
79 Returns a list of names of all registered providers.
81 return sorted(self
.providers
.keys())
83 def load_configuration(self
, filename
):
84 logger
.debug(_("Loading configuration file %s") % filename
)
86 configs
= ConfigParser
.SafeConfigParser()
87 configs
.read([filename
,])
89 # First apply all global configuration settings.
91 for k
, v
in configs
.items("config"):
94 # Allow missing config section
95 except ConfigParser
.NoSectionError
:
98 for entry
in configs
.sections():
99 # Skip the special config section.
100 if entry
== "config":
104 for k
, v
in configs
.items(entry
):
106 settings
["hostname"] = entry
108 # Get the name of the provider.
109 provider
= settings
.get("provider", None)
111 logger
.warning("Entry '%s' lacks a provider setting. Skipping." % entry
)
114 # Try to find the provider with the wanted name.
116 provider
= self
.providers
[provider
]
118 logger
.warning("Could not find provider '%s' for entry '%s'." % (provider
, entry
))
121 # Create an instance of the provider object with settings from the
122 # configuration file.
123 entry
= provider(self
, **settings
)
125 # Add new entry to list (if not already exists).
126 if not entry
in self
.entries
:
127 self
.entries
.append(entry
)
129 def updateone(self
, hostname
, **kwargs
):
130 for entry
in self
.entries
:
131 if not entry
.hostname
== hostname
:
134 return self
._update
(entry
, **kwargs
)
136 raise DDNSHostNotFoundError(hostname
)
138 def updateall(self
, **kwargs
):
140 Update all configured entries.
142 # If there are no entries, there is nothing to do.
144 logger
.debug(_("Found no entries in the configuration file. Exiting."))
147 for entry
in self
.entries
:
148 self
._update
(entry
, **kwargs
)
150 def _update(self
, entry
, force
=False):
155 logger
.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % \
156 { "hostname" : entry
.hostname
, "provider" : entry
.name
})
157 logger
.error(" %s: %s" % (e
.__class
__.__name
__, e
.reason
))
159 logger
.error(" %s" % e
.message
)
162 logger
.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) throwed an unhandled exception:") % \
163 { "hostname" : entry
.hostname
, "provider" : entry
.name
}, exc_info
=True)