]>
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")
33 from .system
import DDNSSystem
37 rootlogger
= logging
.getLogger("ddns")
38 rootlogger
.setLevel(logging
.INFO
)
40 # Setup a logger that logs to syslog.
41 handler
= logging
.handlers
.SysLogHandler(address
="/dev/log",
42 facility
=logging
.handlers
.SysLogHandler
.LOG_DAEMON
44 handler
.setLevel(logging
.INFO
)
45 rootlogger
.addHandler(handler
)
47 handler
= logging
.StreamHandler()
48 rootlogger
.addHandler(handler
)
52 class DDNSCore(object):
53 def __init__(self
, debug
=False):
54 # In debug mode, enable debug logging.
56 rootlogger
= logging
.getLogger("ddns")
57 rootlogger
.setLevel(logging
.DEBUG
)
59 logger
.debug(_("Debugging mode enabled"))
61 # Initialize the settings array.
64 # Dict with all providers, that are supported.
65 self
.providers
= providers
.get()
67 for handle
, provider
in sorted(self
.providers
.items()):
68 logger
.debug("Registered new provider: %s (%s)" % (provider
.name
, provider
.handle
))
70 # List of configuration entries.
73 # Add the system class.
74 self
.system
= DDNSSystem(self
)
76 def get_provider_names(self
):
78 Returns a list of names of all registered providers.
80 return sorted(self
.providers
.keys())
82 def load_configuration(self
, filename
):
83 logger
.debug(_("Loading configuration file %s") % filename
)
85 configs
= ConfigParser
.SafeConfigParser()
86 configs
.read([filename
,])
88 # First apply all global configuration settings.
90 for k
, v
in configs
.items("config"):
93 # Allow missing config section
94 except ConfigParser
.NoSectionError
:
97 for entry
in configs
.sections():
98 # Skip the special config section.
103 for k
, v
in configs
.items(entry
):
105 settings
["hostname"] = entry
107 # Get the name of the provider.
108 provider
= settings
.get("provider", None)
110 logger
.warning("Entry '%s' lacks a provider setting. Skipping." % entry
)
113 # Try to find the provider with the wanted name.
115 provider
= self
.providers
[provider
]
117 logger
.warning("Could not find provider '%s' for entry '%s'." % (provider
, entry
))
120 # Create an instance of the provider object with settings from the
121 # configuration file.
122 entry
= provider(self
, **settings
)
124 # Add new entry to list (if not already exists).
125 if not entry
in self
.entries
:
126 self
.entries
.append(entry
)
128 def updateone(self
, hostname
, **kwargs
):
129 for entry
in self
.entries
:
130 if not entry
.hostname
== hostname
:
133 return self
._update
(entry
, **kwargs
)
135 raise DDNSHostNotFoundError(hostname
)
137 def updateall(self
, **kwargs
):
139 Update all configured entries.
141 # If there are no entries, there is nothing to do.
143 logger
.debug(_("Found no entries in the configuration file. Exiting."))
146 for entry
in self
.entries
:
147 self
._update
(entry
, **kwargs
)
149 def _update(self
, entry
, force
=False):
154 logger
.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") % \
155 { "hostname" : entry
.hostname
, "provider" : entry
.name
})
156 logger
.error(" %s: %s" % (e
.__class
__.__name
__, e
.reason
))
158 logger
.error(" %s" % e
.message
)
161 logger
.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) throwed an unhandled exception:") % \
162 { "hostname" : entry
.hostname
, "provider" : entry
.name
}, exc_info
=True)
165 logger
.info(_("Dynamic DNS update for %(hostname)s (%(provider)s) successful") % \
166 { "hostname" : entry
.hostname
, "provider" : entry
.name
})