]> git.ipfire.org Git - ddns.git/commitdiff
Add a basic command line interface.
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 10 Jun 2014 23:16:24 +0000 (01:16 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 10 Jun 2014 23:16:24 +0000 (01:16 +0200)
ddns.in
src/ddns/__init__.py
src/ddns/errors.py

diff --git a/ddns.in b/ddns.in
index 8e96858505a951b8e4e0665ec8cd32e7ed0156c4..455b0ad02169797530f969496a92986aae9897d2 100644 (file)
--- a/ddns.in
+++ b/ddns.in
 #                                                                             #
 ###############################################################################
 
+import argparse
+
 import ddns
 
-d = ddns.DDNSCore(debug=1)
-d.load_configuration("@configsdir@/ddns.conf")
+from ddns.i18n import _
+
+CONFIGURATION_FILE = "@configsdir@/ddns.conf"
+
+def main():
+       # Parse command line
+       p = argparse.ArgumentParser(description=_("Dynamic DNS Updater"))
+
+       p.add_argument("-d", "--debug", action="store_true",
+               help=_("Enable debugging output"))
+
+       p.add_argument("-c", "--config", default=CONFIGURATION_FILE,
+               help=_("Load configuration file (Default: %s)") % CONFIGURATION_FILE)
+
+       # Create subparsers for commands.
+       subparsers = p.add_subparsers(help=_("Sub-command help"),
+               dest="subparsers_name")
+
+       # update
+       p_update = subparsers.add_parser("update", help=_("Update DNS record"))
+       p_update.add_argument("hostname")
+       p_update.add_argument("--force", action="store_true",
+               help=_("Execute update even if the record is already up to date"))
+
+       # update-all
+       p_update_all = subparsers.add_parser("update-all", help=_("Update all DNS records"))
+       p_update_all.add_argument("--force", action="store_true",
+               help=_("Execute update even if the record is already up to date"))
+
+       args = p.parse_args()
+
+       # Initialise the DDNSCore module.
+       d = ddns.DDNSCore(debug=args.debug)
+
+       # Load configuration.
+       if args.config:
+               d.load_configuration(args.config)
+
+       # Handle commands...
+       if args.subparsers_name == "update":
+               d.updateone(hostname=args.hostname, force=args.force)
+
+       elif args.subparsers_name == "update-all":
+               d.updateall(force=args.force)
+
+       else:
+               raise RuntimeError("Unhandled command: %s" % args.subparsers_name)
 
-d.updateall()
+main()
index 2fb5e021e969f56ed4570b4ca4fc78ad6a90ebda..74be346ed7628ef77d042cc3aa27e471446667c7 100644 (file)
@@ -133,17 +133,28 @@ class DDNSCore(object):
                        if not entry in self.entries:
                                self.entries.append(entry)
 
-       def updateall(self, force=False):
+       def updateone(self, hostname, **kwargs):
+               for entry in self.entries:
+                       if not entry.hostname == hostname:
+                               continue
+
+                       return self._update(entry, **kwargs)
+
+               raise DDNSHostNotFoundError(hostname)
+
+       def updateall(self, **kwargs):
+               """
+                       Update all configured entries.
+               """
                # If there are no entries, there is nothing to do.
                if not self.entries:
                        logger.debug(_("Found no entries in the configuration file. Exiting."))
                        return
 
-               # Update them all.
                for entry in self.entries:
-                       self.update(entry, force=force)
+                       self._update(entry, **kwargs)
 
-       def update(self, entry, force=False):
+       def _update(self, entry, force=False):
                try:
                        entry(force=force)
 
index 2a6f4a989907d9c75927620cc9fbf38e9f219df5..cd935d032001e65dfa25d08c82d2738f25fc9e59 100644 (file)
@@ -56,6 +56,14 @@ class DDNSConfigurationError(DDNSError):
        pass
 
 
+class DDNSHostNotFoundError(DDNSError):
+       """
+               Thrown when a configuration entry could
+               not be found.
+       """
+       pass
+
+
 class DDNSInternalServerError(DDNSError):
        """
                Thrown when the remote server reported