73 help="Don't actually clean anything") |
73 help="Don't actually clean anything") |
74 |
74 |
75 # |
75 # |
76 parser.add_option('-c', '--config', metavar='FILE', |
76 parser.add_option('-c', '--config', metavar='FILE', |
77 help="Load configuration file") |
77 help="Load configuration file") |
|
78 |
|
79 parser.add_option('-r', '--run', metavar='NAME', |
|
80 help="Run given set of targets, per config [run/...]") |
78 |
81 |
79 # |
82 # |
80 parser.add_option('-T', '--target', metavar='PATH', |
83 parser.add_option('-T', '--target', metavar='PATH', |
81 help="Target path") |
84 help="Target path") |
82 |
85 |
302 @classmethod |
305 @classmethod |
303 def config_intervals (cls, name, intervals) : |
306 def config_intervals (cls, name, intervals) : |
304 for interval, arg in intervals.iteritems() : |
307 for interval, arg in intervals.iteritems() : |
305 # lookup base from options.intervals |
308 # lookup base from options.intervals |
306 try : |
309 try : |
307 base = options.intervals[interval] |
310 base = options.intervals[process_config_name(interval)] |
308 except KeyError: |
311 except KeyError: |
309 raise ConfigError("Unknown interval for [target/{target}]: {interval}".format(target=name, interval=interval)) |
312 raise ConfigError("Unknown interval for [target/{target}]: {interval}".format(target=name, interval=interval)) |
310 |
313 |
311 # parse |
314 # parse |
312 yield Interval.from_target_config(interval, base, arg) |
315 yield Interval.from_target_config(interval, base, arg) |
661 return 1 |
664 return 1 |
662 |
665 |
663 def __str__ (self) : |
666 def __str__ (self) : |
664 return self.name |
667 return self.name |
665 |
668 |
666 def run (options, targets) : |
669 def _parse_run_targets (options, config, run) : |
|
670 """ |
|
671 Parse given run section from config into a series of target names to run. |
|
672 """ |
|
673 |
|
674 for target, enable in config['run'][process_config_name(options.run)].iteritems() : |
|
675 # enabled? |
|
676 enable = config_bool('enable', enable) |
|
677 |
|
678 if not enable : |
|
679 continue |
|
680 |
|
681 # check |
|
682 if target not in options.targets : |
|
683 raise ConfigError("Unknown [target/{target}] in [run/{run}]".format(target=target, run=run)) |
|
684 |
|
685 yield target |
|
686 |
|
687 def run (options, run_targets) : |
667 # default config |
688 # default config |
668 config = dict( |
689 config = dict( |
669 rsync_options = {}, |
690 rsync_options = {}, |
670 intervals = {}, |
691 intervals = {}, |
671 targets = {}, |
692 targets = {}, |
676 try : |
697 try : |
677 config = parse_config(options.config, config) |
698 config = parse_config(options.config, config) |
678 except ConfigError as e: |
699 except ConfigError as e: |
679 log.error("Configuration error: %s: %s", options.config, e) |
700 log.error("Configuration error: %s: %s", options.config, e) |
680 return 2 |
701 return 2 |
|
702 |
|
703 # targets to run |
|
704 options.targets = {} |
681 |
705 |
682 # manual? |
706 # manual? |
683 if options.target : |
707 if options.target : |
684 config['targets'][options.target] = dict( |
708 options.targets['console'] = Target.from_config( |
685 path = options.target, |
709 path = options.target, |
686 source = options.target_source, |
710 source = options.target_source, |
687 intervals = dict((name, None) for name in options.target_intervals), |
711 intervals = dict((name, None) for name in options.target_intervals), |
688 ) |
712 ) |
689 |
713 |
697 log.debug("config interval: %s", name) |
721 log.debug("config interval: %s", name) |
698 |
722 |
699 # store |
723 # store |
700 options.intervals[name] = interval |
724 options.intervals[name] = interval |
701 |
725 |
|
726 # rsync options |
702 for option in config['rsync_options'] : |
727 for option in config['rsync_options'] : |
703 value = config['rsync_options'][option] |
728 value = config['rsync_options'][option] |
704 |
729 |
705 # parse, allowing non-boolean values as well... |
730 # parse, allowing non-boolean values as well... |
706 value = config_bool(option, value, strict=False) |
731 value = config_bool(option, value, strict=False) |
708 log.debug("rsync option: %s=%s", option, value) |
733 log.debug("rsync option: %s=%s", option, value) |
709 |
734 |
710 # store |
735 # store |
711 options.rsync_options[option] = value |
736 options.rsync_options[option] = value |
712 |
737 |
713 # what targets? |
738 # target definitions |
714 if not targets : |
739 for name in config['targets'] : |
715 # default to all defined targets |
|
716 targets = list(config['targets']) |
|
717 |
|
718 else : |
|
719 # given ones, but verify they exist |
|
720 for target in targets : |
|
721 if target not in config['targets'] : |
|
722 log.error("Unknown target given: %s", target) |
|
723 log.info("Defined targets: %s", ' '.join(config['targets'])) |
|
724 |
|
725 return 2 |
|
726 |
|
727 # targets |
|
728 for name in targets : |
|
729 target_config = config['targets'][name] |
740 target_config = config['targets'][name] |
730 |
741 |
731 # parse |
742 # parse |
732 target = Target.from_config(options, name, **target_config) |
743 target = Target.from_config(options, name, **target_config) |
733 |
744 |
734 log.info("Config target: %s", name) |
745 log.debug("config target: %s", name) |
735 |
746 |
736 # run |
747 options.targets[name] = target |
737 target.run(options) |
748 |
|
749 # what targets? |
|
750 if run_targets : |
|
751 # keep as-is |
|
752 log.debug("Running given targets: %s", run_targets) |
|
753 |
|
754 if options.run : |
|
755 |
|
756 # given [run/...] definition.. |
|
757 run_targets = list(_parse_run_targets(options, config, options.run)) |
|
758 |
|
759 log.info("Running %d given [run/%s] targets", len(run_targets), options.run) |
|
760 log.debug("[run/%s]: %s", options.run, run_targets) |
|
761 |
|
762 # run |
|
763 if run_targets : |
|
764 log.info("Running %d given targets...", len(run_targets)) |
|
765 |
|
766 # run given ones |
|
767 for name in run_targets : |
|
768 try : |
|
769 # get |
|
770 target = options.targets[name] |
|
771 |
|
772 except KeyError: |
|
773 log.error("Unknown target given: %s", name) |
|
774 log.info("Defined targets: %s", ' '.join(options.targets)) |
|
775 return 2 |
|
776 |
|
777 |
|
778 # run |
|
779 log.info("Target: %s", name) |
|
780 |
|
781 target.run(options) |
|
782 |
|
783 else : |
|
784 # all targets |
|
785 log.info("Running all %d targets...", len(options.targets)) |
|
786 |
|
787 # targets |
|
788 for name, target in options.targets.iteritems() : |
|
789 log.info("Target: %s", name) |
|
790 |
|
791 # run |
|
792 target.run(options) |
738 |
793 |
739 # ok |
794 # ok |
740 return 0 |
795 return 0 |
741 |
796 |
742 def config_defaults () : |
797 def config_defaults () : |