Mercurial > hg > KCl
diff kcl/sudo_scp.py @ 0:0f44ee073173 default tip
fake salt, initial commit
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 06 Feb 2017 01:10:22 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kcl/sudo_scp.py Mon Feb 06 01:10:22 2017 +0000 @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +""" +scp with sudo and magic +""" + +# imports +import argparse +import os +import subprocess +import sys + +from run_cmd import call +from run_cmd import print_command + + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('src') + parser.add_argument('dest') + parser.add_argument('host', nargs='*', + help="hosts to copy to, or stdin if omitted") + parser.add_argument('-u', '--user', dest='user', + help="what user should own this file?") + parser.add_argument('--dry-run', dest='dry_run', + action='store_true', default=False, + help="don't actually do anything; just print what would be done") + options = parser.parse_args(args) + + # sanity + if not os.path.exists(options.src): + parser.error("'{}' does not exist".format(options.src)) + + if options.dry_run: + call = print_command + else: + call = globals()['call'] + + basename = os.path.basename(options.src) + + TMPDIR = '/tmp' + + tmpdest = os.path.join(TMPDIR, basename) + + hosts = options.host or sys.stdin.read().strip().split() + + + # copy to all hosts + for host in hosts: + call(['scp', options.src, '{host}:{dest}'.format(host=host, dest=tmpdest)], + stderr=subprocess.PIPE) + call(['ssh', host, "sudo mv {tmpdest} {dest}".format(tmpdest=tmpdest, dest=options.dest)], + stderr=subprocess.PIPE) + if options.user: + call(['ssh', host, "sudo chown {user} {dest}".format(user=options.user, dest=options.dest)], + stderr=subprocess.PIPE) + call(['ssh', host, "sudo chmod a+x {dest}".format(dest=options.dest)], + stderr=subprocess.PIPE) + call(['ssh', host, "sudo chmod a+r {dest}".format(dest=options.dest)], + stderr=subprocess.PIPE) + + +if __name__ == '__main__': + main()