pvl.backup-snapshot: support loading *.conf from --config pvl-backup.d/
authorTero Marttila <terom@paivola.fi>
Thu, 24 May 2012 12:27:16 +0300
changeset 55 95c86df4807a
parent 54 12fbf099be99
child 56 ae4b24ae4f70
pvl.backup-snapshot: support loading *.conf from --config pvl-backup.d/
bin/pvl.backup-snapshot
pvl-backup.conf
pvl-backup.d/local-fs.conf
pvl-backup.d/local-lvm.conf
pvl-backup.d/remote-fs.conf
pvl-backup.d/remote-lvm.conf
rsync-snapshot.conf
--- a/bin/pvl.backup-snapshot	Sun Apr 22 14:08:11 2012 +0300
+++ b/bin/pvl.backup-snapshot	Thu May 24 12:27:16 2012 +0300
@@ -15,7 +15,7 @@
 
 import optparse, ConfigParser
 import os, os.path, stat
-import shutil
+import shutil, glob
 import datetime
 import logging
 
@@ -73,8 +73,8 @@
         help="Don't actually clean anything")
 
     #
-    parser.add_option('-c', '--config',     metavar='FILE',
-        help="Load configuration file")
+    parser.add_option('-c', '--config',     metavar='FILE/DIR', action='append',    # multi
+        help="Load configuration file(s)")
 
     parser.add_option('-r', '--run',        metavar='NAME',
         help="Run given set of targets, per config [run/...]")
@@ -93,7 +93,8 @@
     # defaults
     parser.set_defaults(
         loglevel            = logging.INFO,
-
+        
+        config              = [],
         target_intervals    = [],
     )
     parser.set_defaults(**defaults)
@@ -132,7 +133,7 @@
 
 def parse_config (path, defaults) :
     """
-        Parse given config file
+        Parse given config file, returning updated set of configs based on given defaults.
     """
 
     log.debug("loading config: %s", path)
@@ -701,6 +702,32 @@
 
         yield target
 
+def load_configs (configs, confglob='*.conf') :
+    """
+        Load configuration files from given list of config paths; supports loading a conf.d
+    """
+
+    for path in configs :
+        log.debug("%s", path)
+
+        if os.path.isdir(path) :
+            # glob dir: $path/$glob
+            for globpath in glob.glob(os.path.join(path, confglob)) :
+                if os.path.exists(globpath) :
+                    yield globpath
+                else :
+                    raise Exception("Globbed file does not exist: {0}".format(globpath))
+
+        elif os.path.isfile(path) :
+            # normal file
+            yield path
+
+        elif os.path.exists(path) :
+            raise Exception("Unrecognized config file type: {0}".format(path))
+
+        else :
+            raise Exception("Given config file does not exist: {0}".format(path))
+
 def run (options, run_targets) :
     # default config
     config = dict(
@@ -708,13 +735,14 @@
         intervals       = {},
         targets         = {},
     )
-
-    if options.config :
+    
+    # config?
+    for path in load_configs(options.config) :
         # load
         try :
-            config = parse_config(options.config, config)
+            config = parse_config(path, config)
         except ConfigError as e:
-            log.error("Configuration error: %s: %s", options.config, e)
+            log.error("Configuration error: %s: %s", path, e)
             return 2
 
     # targets to run
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl-backup.conf	Thu May 24 12:27:16 2012 +0300
@@ -0,0 +1,38 @@
+## Options
+[snapshots]
+format      = %Y%m%d-%H%M%S
+
+# rsync options, in invoke.optargs format
+[rsync-options]
+archive             = true
+hard-links          = true
+one-file-system     = true
+numeric-ids         = true
+delete              = true
+
+## Intervals
+[intervals/recent]
+format  = 
+
+[intervals/day]
+format  = %Y-%m-%d
+
+[intervals/week]
+format  = %Y-%W
+
+[intervals/month]
+format  = %Y-%m
+
+[intervals/year]
+format  = %Y
+
+## Targets
+# from pvl-backup.d
+
+## Runs
+[run/twice-daily]
+test        = true
+
+[run/hourly]
+test-lvm    = true
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl-backup.d/local-fs.conf	Thu May 24 12:27:16 2012 +0300
@@ -0,0 +1,14 @@
+# fs
+[targets/test]
+path    = ./test
+source  = /srv/lvm-test/
+
+[targets/test/intervals]
+recent  = 4
+day     = 7
+year    = 
+
+[targets/test/rsync_options]
+exclude-from    = ignore.list
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl-backup.d/local-lvm.conf	Thu May 24 12:27:16 2012 +0300
@@ -0,0 +1,12 @@
+# local lvm
+[targets/test-lvm]
+path    = ./test-lvm
+source  = lvm:asdf/test
+
+[targets/test-lvm/intervals]
+recent  = 4
+
+[targets/test-lvm/lvm-options]
+wait    = 1
+size    = 1G
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl-backup.d/remote-fs.conf	Thu May 24 12:27:16 2012 +0300
@@ -0,0 +1,8 @@
+# remote
+[targets/test2]
+path    = ./test
+source  = localhost:/
+
+[targets/test2/intervals]
+recent  = 4
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl-backup.d/remote-lvm.conf	Thu May 24 12:27:16 2012 +0300
@@ -0,0 +1,9 @@
+# remote lvm
+# [targets/test-lvm2]
+# path  = ./test-lvm
+# source = localhost:lvm:asdf/test
+# 
+#[targets/test-lvm2/intervals]
+#recent  = 4
+
+
--- a/rsync-snapshot.conf	Sun Apr 22 14:08:11 2012 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,79 +0,0 @@
-## Options
-[snapshots]
-format      = %Y%m%d-%H%M%S
-
-# rsync options, in invoke.optargs format
-[rsync-options]
-archive             = true
-hard-links          = true
-one-file-system     = true
-numeric-ids         = true
-delete              = true
-
-## Intervals
-[intervals/recent]
-format  = 
-
-[intervals/day]
-format  = %Y-%m-%d
-
-[intervals/week]
-format  = %Y-%W
-
-[intervals/month]
-format  = %Y-%m
-
-[intervals/year]
-format  = %Y
-
-## Targets
-# fs
-[targets/test]
-path    = ./test
-source  = /srv/lvm-test/
-
-[targets/test/intervals]
-recent  = 4
-day     = 7
-year    = 
-
-[targets/test/rsync_options]
-exclude-from    = ignore.list
-
-# remote
-[targets/test2]
-path    = ./test
-source  = localhost:/
-
-[targets/test2/intervals]
-recent  = 4
-
-# local lvm
-[targets/test-lvm]
-path    = ./test-lvm
-source  = lvm:asdf/test
-
-[targets/test-lvm/intervals]
-recent  = 4
-
-[targets/test-lvm/lvm-options]
-wait    = 1
-size    = 1G
-
-# remote lvm
-# [targets/test-lvm2]
-# path  = ./test-lvm
-# source = localhost:lvm:asdf/test
-# 
-#[targets/test-lvm2/intervals]
-#recent  = 4
-
-
-
-## Runs
-[run/twice-daily]
-test        = true
-
-[run/hourly]
-test-lvm    = true
-