annotate kcl/run_cmd.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 # -*- coding: utf-8 -*-
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 """
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 run a command on some things
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
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import subprocess
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import sys
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import time
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 # module globals
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 __all__ = ['print_command', 'call', 'RunCmdParser']
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 string = (str, unicode)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 def print_command(command, **kwargs):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 if not isinstance(command, string):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 command = subprocess.list2cmdline(command)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 print (command)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 def call(command, **kwargs):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 process = subprocess.Popen(command, **kwargs)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 stdout, stderr = process.communicate()
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 if process.returncode:
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 cmdline = subprocess.list2cmdline(command)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 print cmdline
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 raise subprocess.CalledProcessError(process.returncode,
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 cmdline)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 def ssh_call(host, command, ssh='ssh'):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 call([ssh, host, command], stderr=subprocess.PIPE)
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
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 class RunCmdParser(argparse.ArgumentParser):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 """CLI option parser"""
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 def __init__(self, **kwargs):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 kwargs.setdefault('description', __doc__)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 argparse.ArgumentParser.__init__(self, **kwargs)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 self.add_argument('-H', '--host', dest='hosts', nargs='+',
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 help="hosts to run on; or read from stdin if omitted")
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 self.add_argument("command",
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 help="command to run")
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 self.options = None
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 self._hosts = None
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 def parse_args(self, *args, **kw):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 self.validate(options)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 self.options = options
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 return options
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 def validate(self, options):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 """validate options"""
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 def hosts(self):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 if self._hosts is None:
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 assert self.options is not None
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 self._hosts = self.options.hosts or sys.stdin.read().strip().split()
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 return self._hosts
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 def command(self):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 return self.options.command
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 def main(args=sys.argv[1:]):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 """CLI"""
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 # parse command line options
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 parser = RunCmdParser()
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 options = parser.parse_args(args)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 # get command to run
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 command = parser.command()
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 # call the command on all hosts
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 for host in parser.hosts():
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 ssh_call(host, command)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 if __name__ == '__main__':
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 main()
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88