comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:0f44ee073173
1 #!/usr/bin/env python
2
3 """
4 scp with sudo and magic
5 """
6
7 # imports
8 import argparse
9 import os
10 import subprocess
11 import sys
12
13 from run_cmd import call
14 from run_cmd import print_command
15
16
17 def main(args=sys.argv[1:]):
18 """CLI"""
19
20 # parse command line
21 parser = argparse.ArgumentParser(description=__doc__)
22 parser.add_argument('src')
23 parser.add_argument('dest')
24 parser.add_argument('host', nargs='*',
25 help="hosts to copy to, or stdin if omitted")
26 parser.add_argument('-u', '--user', dest='user',
27 help="what user should own this file?")
28 parser.add_argument('--dry-run', dest='dry_run',
29 action='store_true', default=False,
30 help="don't actually do anything; just print what would be done")
31 options = parser.parse_args(args)
32
33 # sanity
34 if not os.path.exists(options.src):
35 parser.error("'{}' does not exist".format(options.src))
36
37 if options.dry_run:
38 call = print_command
39 else:
40 call = globals()['call']
41
42 basename = os.path.basename(options.src)
43
44 TMPDIR = '/tmp'
45
46 tmpdest = os.path.join(TMPDIR, basename)
47
48 hosts = options.host or sys.stdin.read().strip().split()
49
50
51 # copy to all hosts
52 for host in hosts:
53 call(['scp', options.src, '{host}:{dest}'.format(host=host, dest=tmpdest)],
54 stderr=subprocess.PIPE)
55 call(['ssh', host, "sudo mv {tmpdest} {dest}".format(tmpdest=tmpdest, dest=options.dest)],
56 stderr=subprocess.PIPE)
57 if options.user:
58 call(['ssh', host, "sudo chown {user} {dest}".format(user=options.user, dest=options.dest)],
59 stderr=subprocess.PIPE)
60 call(['ssh', host, "sudo chmod a+x {dest}".format(dest=options.dest)],
61 stderr=subprocess.PIPE)
62 call(['ssh', host, "sudo chmod a+r {dest}".format(dest=options.dest)],
63 stderr=subprocess.PIPE)
64
65
66 if __name__ == '__main__':
67 main()