summaryrefslogtreecommitdiffstats
path: root/bin/opssh
blob: 8ac52604920bb22b7ff6ea3c91320b96dc2d587c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
# vim: expandtab:tabstop=4:shiftwidth=4


function usage() {
    cat << EOF
Usage: opssh [OPTIONS] command [...]

Options:
  --version             show program's version number and exit
  --help                show this help message and exit
  -l USER, --user=USER  username (OPTIONAL)
  -p PAR, --par=PAR     max number of parallel threads (OPTIONAL)
  --outdir=OUTDIR       output directory for stdout files (OPTIONAL)
  --errdir=ERRDIR       output directory for stderr files (OPTIONAL)
  -e ENV, --env ENV     which environment to use
  -t HOST_TYPE, --host-type HOST_TYPE
                        which host type to use
  --list-host-types     list all of the host types
  --timeout=TIMEOUT     timeout (secs) (0 = no timeout) per host (OPTIONAL)
  -O OPTION, --option=OPTION
                        SSH option (OPTIONAL)
  -v, --verbose         turn on warning and diagnostic messages (OPTIONAL)
  -A, --askpass         Ask for a password (OPTIONAL)
  -x ARGS, --extra-args=ARGS
                        Extra command-line arguments, with processing for
                        spaces, quotes, and backslashes
  -X ARG, --extra-arg=ARG
                        Extra command-line argument
  -i, --inline          inline aggregated output and error for each server
  --inline-stdout       inline standard output for each server
  -I, --send-input      read from standard input and send as input to ssh
  -P, --print           print output as we get it

Example: opssh -t ex-srv -e stg -l irb2 --outdir /tmp/foo uptime

EOF
}

if [ $# -eq 0 ] || [ "$1" == "--help" ]
then
    usage
    exit 1
fi

# See if ohi is installed
if ! which ohi &>/dev/null ; then
    echo "ERROR: can't find ohi (OpenShift Host Inventory) on your system, please either install the openshift-ansible-bin package, or add openshift-ansible/bin to your path."

    exit 10
fi

PAR=200
USER=root
TIMEOUT=0
ARGS=()
ENV=""
HOST_TYPE=""
while [ $# -gt 0 ] ; do
    case $1 in
        -t|--host-type)
            shift # get past the option
            HOST_TYPE=$1
            shift # get past the value of the option
            ;;

        -e)
            shift # get past the option
            ENV=$1
            shift # get past the value of the option
            ;;

        --timeout)
            shift # get past the option
            TIMEOUT=$1
            shift # get past the value of the option
            ;;

        -p|--par)
            shift # get past the option
            PAR=$1
            shift # get past the value of the option
            ;;

        -l|--user)
            shift # get past the option
            USER=$1
            shift # get past the value of the option
            ;;

        --list-host-types)
            ohi --list-host-types
            exit 0
            ;;

        -h|--hosts|-H|--host|-o)
            echo "ERROR: unknown option $1"
            exit 20
            ;;

        *)
            args+=("$1")
            shift
            ;;
    esac
done

# Get host list from ohi
if [ -n "$ENV" -a -n "$HOST_TYPE" ] ; then
    HOSTS="$(ohi -t "$HOST_TYPE" -e "$ENV" 2>/dev/null)"
    OHI_ECODE=$?
elif [ -n "$ENV" ] ; then
    HOSTS="$(ohi -e "$ENV" 2>/dev/null)"
    OHI_ECODE=$?
elif [ -n "$HOST_TYPE" ] ; then
    HOSTS="$(ohi -t "$HOST_TYPE" 2>/dev/null)"
    OHI_ECODE=$?
else
    echo
    echo "Error: either -e or -t must be specified"
    echo
    exit 10
fi

if [ $OHI_ECODE -ne 0 ] ; then
    echo
    echo "ERROR: ohi failed with exit code $OHI_ECODE"
    echo
    echo "This is usually caused by a bad value passed for host-type or environment."
    echo
    exit 25
fi

exec pssh -t $TIMEOUT -p $PAR -l $USER -h <(echo "$HOSTS") "${args[@]}"