]>
git.ipfire.org Git - 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 formatter
= logging
.Formatter("ddns[%(process)d]: %(message)s")
46 handler
.setFormatter(formatter
)
47 handler
.setLevel(logging
.INFO
)
48 rootlogger
.addHandler(handler
)
50 handler
= logging
.StreamHandler()
51 rootlogger
.addHandler(handler
)
55 class DDNSCore(object):
56 def __init__(self
, debug
=False):
57 # In debug mode, enable debug logging.
59 rootlogger
= logging
.getLogger("ddns")
60 rootlogger
.setLevel(logging
.DEBUG
)
62 logger
.debug(_("Debugging mode enabled"))
64 # Initialize the settings array.
67 # Dict with all providers, that are supported.
68 self
.providers
= providers
.get()
70 for handle
, provider
in sorted(self
.providers
.items()):
71 logger
.debug("Registered new provider: %s (%s)" % (provider
.name
, provider
.handle
))
73 # List of configuration entries.
76 # Add the system class.
77 self
.system
= DDNSSystem(self
)
79 def get_provider_names(self
):
81 Returns a list of names of all registered providers.
83 return sorted(self
.providers
.keys())
85 def load_configuration(self
, filename
):
86 logger
.debug(_("Loading configuration file %s") % filename
)
88 configs
= ConfigParser
.RawConfigParser()
89 configs
.read([filename
,])
91 # First apply all global configuration settings.
93 for k
, v
in configs
.items("config"):
96 # Allow missing config section
97 except ConfigParser
.NoSectionError
:
100 for entry
in configs
.sections():
101 # Skip the special config section.
102 if entry
== "config":
106 for k
, v
in configs
.items(entry
):
108 settings
["hostname"] = entry
110 # Get the name of the provider.
111 provider
= settings
.get("provider", None)
113 logger
.warning("Entry '%s' lacks a provider setting. Skipping." % entry
)
116 # Try to find the provider with the wanted name.
118 provider
= self
.providers
[provider
]
120 logger
.warning("Could not find provider '%s' for entry '%s'." % (provider
, entry
))
123 # Create an instance of the provider object with settings from the
124 # configuration file.
125 entry
= provider(self
, **settings
)
127 # Add new entry to list (if not already exists).
128 if not entry
in self
.entries
:
129 self
.entries
.append(entry
)
131 def updateone(self
, hostname
, **kwargs
):
132 for entry
in self
.entries
:
133 if not entry
.hostname
== hostname
:
136 return self
._update
(entry
, **kwargs
)
138 raise DDNSHostNotFoundError(hostname
)
140 def updateall(self
, **kwargs
):
142 Update all configured entries.
144 # If there are no entries, there is nothing to do.
146 logger
.debug(_("Found no entries in the configuration file. Exiting."))
149 for entry
in self
.entries
:
150 self
._update
(entry
, **kwargs
)
152 def _update(self
, entry
, force
=False):
157 logger
.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % \
158 { "hostname" : entry
.hostname
, "provider" : entry
.name
})
159 logger
.error(" %s: %s" % (e
.__class
__.__name
__, e
.reason
))
161 logger
.error(" %s" % e
.message
)
164 logger
.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) throwed an unhandled exception:") % \
165 { "hostname" : entry
.hostname
, "provider" : entry
.name
}, exc_info
=True)