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