terom@0: #!/usr/bin/python terom@0: terom@8: """ terom@8: SSH authorized_keys command="..." wrapper for rsync. terom@28: terom@28: Testing goes something like: terom@28: sudo sh -c "PYTHONPATH=. rsync -e './scripts/pvlbackup-rsync-wrapper --debug -C --' -ax testing:lvm:asdf:test test/tmp" terom@8: """ terom@8: terom@30: from pvl.backup import __version__ terom@5: from pvl.backup.rsync import RSyncCommandFormatError terom@5: from pvl.backup.invoke import InvokeError terom@5: from pvl.backup import rsync terom@3: terom@5: import optparse terom@28: import shlex terom@5: import os terom@0: import logging terom@0: terom@0: log = logging.getLogger() terom@0: terom@3: # command-line options terom@3: options = None terom@3: terom@3: def parse_options (argv) : terom@3: """ terom@3: Parse command-line arguments. terom@3: """ terom@3: terom@28: # import sys; sys.stderr.write("%s\n" % (argv, )) terom@28: terom@8: parser = optparse.OptionParser( terom@8: prog = argv[0], terom@3: terom@8: # module docstring terom@8: description = __doc__, terom@30: version = __version__, terom@8: ) terom@3: terom@3: # logging terom@8: general = optparse.OptionGroup(parser, "General Options") terom@3: terom@8: general.add_option('-q', '--quiet', dest='loglevel', action='store_const', const=logging.WARNING, help="Less output") terom@8: general.add_option('-v', '--verbose', dest='loglevel', action='store_const', const=logging.INFO, help="More output") terom@8: general.add_option('-D', '--debug', dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output") terom@32: general.add_option('--debug-for', action='append', metavar='MODULE', help="Enable logging for the given logger/module name") terom@8: terom@8: parser.add_option_group(general) terom@8: terom@8: # terom@8: parser.add_option('-c', '--command', metavar='CMD', default=os.environ.get('SSH_ORIGINAL_COMMAND'), terom@3: help="rsync command to execute") terom@3: terom@28: parser.add_option('-C', '--given-command', action='store_true', default=False, terom@28: help="use given command in `rsync -e %prog` format") terom@28: terom@3: parser.add_option('-R', '--readonly', action='store_true', default=False, terom@8: help="restrict to read/source mode") terom@3: terom@8: parser.add_option('-P', '--restrict-path', metavar='PATH', default=False, terom@8: help="restrict to given path prefix") terom@3: terom@3: # defaults terom@3: parser.set_defaults( terom@32: debug_for = [], terom@34: loglevel = logging.INFO, terom@3: ) terom@3: terom@3: # parse terom@3: options, args = parser.parse_args(argv[1:]) terom@3: terom@3: # configure terom@3: logging.basicConfig( terom@11: format = '%(levelname)6s %(name)s : %(funcName)s : %(message)s', terom@3: level = options.loglevel, terom@3: ) terom@3: terom@32: # enable debugging for specific targets terom@32: for target in options.debug_for : terom@32: logging.getLogger(target).setLevel(logging.DEBUG) terom@32: terom@3: return options, args terom@3: terom@3: def rsync_wrapper (command, restrict='lvm:') : terom@3: """ terom@3: Wrap given rsync command. terom@8: terom@8: Parses the command, the source path, and then executes rsync within the source path (which may be a special terom@8: pseudo-path with additional handling). terom@3: """ terom@3: terom@3: try : terom@8: # parse the rsync command sent by the client terom@5: rsync_cmd, rsync_options, source_path, dest_path = rsync.parse_command(command, terom@3: restrict_readonly = options.readonly, terom@3: ) terom@3: terom@3: except RSyncCommandFormatError, e: terom@3: log.error("invalid rsync command: %r: %s", command, e) terom@3: return 2 terom@3: terom@3: # XXX: the real path is always given second.. terom@3: path = dest_path terom@3: terom@3: try : terom@8: # parse the source path as given by the client, may be a real path or pseudo-path terom@5: source = rsync.parse_source(path, terom@3: restrict_path = options.restrict_path, terom@3: ) terom@3: terom@3: except RSyncCommandFormatError, e: terom@3: log.error("invalid rsync source: %r: %s", path, e) terom@3: return 2 terom@3: terom@3: try : terom@8: # run rsync within the source (may perform additional stuff like snapshotting...) terom@3: source.execute(rsync_options) terom@3: terom@3: except InvokeError, e: terom@3: log.error("%s failed: %d", e.cmd, e.exit) terom@3: return e.exit terom@3: terom@3: # ok terom@3: return 0 terom@3: terom@3: def main (argv) : terom@3: global options terom@3: terom@3: # global options + args terom@3: options, args = parse_options(argv) terom@3: terom@28: # command required terom@28: if options.given_command : terom@28: # from args (as given by `rsync -e pvlbackup-rsync-wrapper`) -> 'pvlbackup-rsync-wrapper ( ...)' terom@28: host = args.pop(0) terom@28: command_parts = args terom@28: terom@28: log.debug("using command from args: %r", command_parts) terom@28: terom@3: # args terom@28: elif args : terom@3: log.error("No arguments are handled") terom@3: return 2 terom@3: terom@28: elif options.command: terom@28: # as given terom@28: command_parts = shlex.split(options.command) terom@28: terom@28: else : terom@3: log.error("SSH_ORIGINAL_COMMAND not given") terom@3: return 2 terom@3: terom@28: terom@28: # run terom@3: try : terom@28: return rsync_wrapper(command_parts) terom@3: terom@3: except Exception, e: terom@3: log.error("Internal error:", exc_info=e) terom@3: return 3 terom@3: terom@3: # ok terom@3: return 0 terom@0: terom@0: if __name__ == '__main__' : terom@0: import sys terom@0: terom@0: sys.exit(main(sys.argv)) terom@0: