summaryrefslogtreecommitdiffstats
path: root/bin/opssh
blob: 0836f03542d71574acbb6155b3312a49d8569a67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# vim: expandtab:tabstop=4:shiftwidth=4

import argparse
import ansibleutil
import traceback
import sys
import os
import re
import tempfile
import time
import subprocess

DEFAULT_PSSH_PAR=200
PSSH = '/usr/bin/pssh'

class Opssh(object):
    def __init__(self):
        self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
        self.ansible = ansibleutil.AnsibleUtil()

        self.parse_cli_args()

        if self.args.list_host_types:
            self.ansible.print_host_types()
            return

        if self.args.env and \
           self.args.host_type and \
           self.args.command:
            retval = self.run_pssh()
            if retval != 0:
                raise ValueError("ansible run failed")

            return

        # If it makes it here, we weren't able to determine what they wanted to do
        raise ValueError("Invalid combination of arguments")

    def run_pssh(self):
        """Actually run the pssh command based off of the supplied options
        """

        # Default set of options
        pssh_args = [PSSH, '-i', '-t', '0', '-p', str(self.args.par), '--user', self.args.user]

        if self.args.outdir:
            pssh_args.append("--outdir='%s'" % self.args.outdir)

        if self.args.errdir:
            pssh_args.append("--errdir='%s'" % self.args.errdir)

        hosts = self.ansible.get_host_list(self.args.host_type, self.args.env)
        with tempfile.NamedTemporaryFile(prefix='opssh-', delete=True) as f:
            for h in hosts:
                f.write(h + os.linesep)
            f.flush()

            pssh_args.extend(["-h", "%s" % f.name])
            pssh_args.append("%s" % self.args.command)

            print
            print "Running: %s" % ' '.join(pssh_args)
            print
            return subprocess.call(pssh_args)

        return None


    def parse_cli_args(self):
        """Setup the command line parser with the options we want
        """

        parser = argparse.ArgumentParser(description='Openshift Online PSSH Tool.')

        parser.add_argument('--list-host-types', default=False, action='store_true',
                       help='List all of the host types')

        parser.add_argument('-e', '--env', action="store",
                       help="Which environment to use")

        parser.add_argument('-t', '--host-type', action="store",
                       help="Which host type to use")

        parser.add_argument('-c', '--command', action='store',
                       help='Command to run on remote host(s)')

        parser.add_argument('--user', action='store', default='root',
                       help='username')

        parser.add_argument('-p', '--par', action='store', default=DEFAULT_PSSH_PAR,
                       help=('max number of parallel threads (default %s)' % DEFAULT_PSSH_PAR))

        parser.add_argument('--outdir', action='store',
                       help='output directory for stdout files')

        parser.add_argument('--errdir', action='store',
                       help='output directory for stderr files')

        self.args = parser.parse_args()


if __name__ == '__main__':
    if len(sys.argv) == 1:
        print "\nError: No options given. Use --help to see the available options\n"
        sys.exit(0)

    try:
        opssh = Opssh()
    except ValueError as e:
        print "\nError: %s\n" % e.message