]>
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")
31 from . import database
32 from . import providers
35 from .system
import DDNSSystem
39 rootlogger
= logging
.getLogger("ddns")
40 rootlogger
.setLevel(logging
.INFO
)
42 # Setup a logger that logs to syslog.
43 handler
= logging
.handlers
.SysLogHandler(address
="/dev/log",
44 facility
=logging
.handlers
.SysLogHandler
.LOG_DAEMON
46 formatter
= logging
.Formatter("ddns[%(process)d]: %(message)s")
47 handler
.setFormatter(formatter
)
48 handler
.setLevel(logging
.INFO
)
49 rootlogger
.addHandler(handler
)
51 handler
= logging
.StreamHandler()
52 rootlogger
.addHandler(handler
)
56 class DDNSCore(object):
57 def __init__(self
, debug
=False):
58 # In debug mode, enable debug logging.
60 rootlogger
= logging
.getLogger("ddns")
61 rootlogger
.setLevel(logging
.DEBUG
)
63 logger
.debug(_("Debugging mode enabled"))
65 # Initialize the settings array.
68 # Dict with all providers, that are supported.
69 self
.providers
= providers
.get()
71 for handle
, provider
in sorted(self
.providers
.items()):
72 logger
.debug("Registered new provider: %s (%s)" % (provider
.name
, provider
.handle
))
74 # List of configuration entries.
77 # Add the system class.
78 self
.system
= DDNSSystem(self
)
81 self
.db
= database
.DDNSDatabase(self
, "/var/lib/ddns.db")
83 def get_provider_names(self
):
85 Returns a list of names of all registered providers.
87 return sorted(self
.providers
.keys())
89 def load_configuration(self
, filename
):
90 logger
.debug(_("Loading configuration file %s") % filename
)
92 configs
= configparser
.RawConfigParser()
93 configs
.read([filename
,])
95 # First apply all global configuration settings.
97 for k
, v
in configs
.items("config"):
100 # Allow missing config section
101 except configparser
.NoSectionError
:
104 for entry
in configs
.sections():
105 # Skip the special config section.
106 if entry
== "config":
110 for k
, v
in configs
.items(entry
):
112 settings
["hostname"] = entry
114 # Get the name of the provider.
115 provider
= settings
.get("provider", None)
117 logger
.warning("Entry '%s' lacks a provider setting. Skipping." % entry
)
120 # Try to find the provider with the wanted name.
122 provider
= self
.providers
[provider
]
124 logger
.warning("Could not find provider '%s' for entry '%s'." % (provider
, entry
))
127 # Check if the provider is actually supported and if there are
128 # some dependencies missing on this system.
129 if not provider
.supported():
130 logger
.warning("Provider '%s' is known, but not supported on this machine" % provider
.name
)
133 # Create an instance of the provider object with settings from the
134 # configuration file.
135 entry
= provider(self
, **settings
)
137 # Add new entry to list (if not already exists).
138 if not entry
in self
.entries
:
139 self
.entries
.append(entry
)
141 def updateone(self
, hostname
, **kwargs
):
142 for entry
in self
.entries
:
143 if not entry
.hostname
== hostname
:
146 return self
._update
(entry
, **kwargs
)
148 raise DDNSHostNotFoundError(hostname
)
150 def updateall(self
, **kwargs
):
152 Update all configured entries.
154 # If there are no entries, there is nothing to do.
156 logger
.debug(_("Found no entries in the configuration file. Exiting."))
159 for entry
in self
.entries
:
160 self
._update
(entry
, **kwargs
)
162 def _update(self
, entry
, force
=False):
166 except DDNSError
as e
:
167 logger
.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) failed:") %
168 {"hostname": entry
.hostname
, "provider": entry
.name
})
169 logger
.error(" %s: %s" % (e
.__class
__.__name
__, e
.reason
))
171 logger
.error(" %s" % e
.message
)
174 logger
.error(_("Dynamic DNS update for %(hostname)s (%(provider)s) threw an unhandled exception:") %
175 {"hostname": entry
.hostname
, "provider": entry
.name
}, exc_info
=True)