opssh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/bash
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. function usage() {
  4. cat << EOF
  5. Usage: opssh [OPTIONS] command [...]
  6. Options:
  7. --version show program's version number and exit
  8. --help show this help message and exit
  9. -l USER, --user=USER username (OPTIONAL)
  10. -p PAR, --par=PAR max number of parallel threads (OPTIONAL)
  11. --outdir=OUTDIR output directory for stdout files (OPTIONAL)
  12. --errdir=ERRDIR output directory for stderr files (OPTIONAL)
  13. -c CLUSTER, --cluster CLUSTER
  14. which cluster to use
  15. -e ENV, --env ENV which environment to use
  16. --v3 When working with v3 environments. v2 by default
  17. -t HOST_TYPE, --host-type HOST_TYPE
  18. which host type to use
  19. --list-host-types list all of the host types
  20. --timeout=TIMEOUT timeout (secs) (0 = no timeout) per host (OPTIONAL)
  21. -O OPTION, --option=OPTION
  22. SSH option (OPTIONAL)
  23. -v, --verbose turn on warning and diagnostic messages (OPTIONAL)
  24. -A, --askpass Ask for a password (OPTIONAL)
  25. -x ARGS, --extra-args=ARGS
  26. Extra command-line arguments, with processing for
  27. spaces, quotes, and backslashes
  28. -X ARG, --extra-arg=ARG
  29. Extra command-line argument
  30. -i, --inline inline aggregated output and error for each server
  31. --inline-stdout inline standard output for each server
  32. -I, --send-input read from standard input and send as input to ssh
  33. -P, --print print output as we get it
  34. Example: opssh -t ex-srv -e stg -l irb2 --outdir /tmp/foo uptime
  35. EOF
  36. }
  37. if [ $# -eq 0 ] || [ "$1" == "--help" ]
  38. then
  39. usage
  40. exit 1
  41. fi
  42. # See if ohi is installed
  43. if ! which ohi &>/dev/null ; then
  44. 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."
  45. exit 10
  46. fi
  47. PAR=200
  48. USER=root
  49. TIMEOUT=0
  50. ARGS=()
  51. ENV=""
  52. HOST_TYPE=""
  53. while [ $# -gt 0 ] ; do
  54. case $1 in
  55. -t|--host-type)
  56. shift # get past the option
  57. HOST_TYPE=$1
  58. shift # get past the value of the option
  59. ;;
  60. -c)
  61. shift # get past the option
  62. CLUSTER=$1
  63. shift # get past the value of the option
  64. ;;
  65. -e)
  66. shift # get past the option
  67. ENV=$1
  68. shift # get past the value of the option
  69. ;;
  70. --v3)
  71. OPENSHIFT_VERSION="--v3"
  72. shift # get past the value of the option
  73. ;;
  74. --timeout)
  75. shift # get past the option
  76. TIMEOUT=$1
  77. shift # get past the value of the option
  78. ;;
  79. -p|--par)
  80. shift # get past the option
  81. PAR=$1
  82. shift # get past the value of the option
  83. ;;
  84. -l|--user)
  85. shift # get past the option
  86. USER=$1
  87. shift # get past the value of the option
  88. ;;
  89. --list-host-types)
  90. ohi --list-host-types
  91. exit 0
  92. ;;
  93. -h|--hosts|-H|--host|-o)
  94. echo "ERROR: unknown option $1"
  95. exit 20
  96. ;;
  97. *)
  98. args+=("$1")
  99. shift
  100. ;;
  101. esac
  102. done
  103. # Get host list from ohi
  104. CMD=""
  105. if [ -n "$CLUSTER" ] ; then
  106. CMD="$CMD -c $CLUSTER"
  107. fi
  108. if [ -n "$ENV" ] ; then
  109. CMD="$CMD -e $ENV"
  110. fi
  111. if [ -n "$HOST_TYPE" ] ; then
  112. CMD="$CMD -t $HOST_TYPE"
  113. fi
  114. if [ -n "$OPENSHIFT_VERSION" ] ; then
  115. CMD="$CMD $OPENSHIFT_VERSION"
  116. fi
  117. if [ -n "$CMD" ] ; then
  118. HOSTS="$(ohi $CMD 2>/dev/null)"
  119. OHI_ECODE=$?
  120. fi
  121. if [ $OHI_ECODE -ne 0 ] ; then
  122. echo
  123. echo "ERROR: ohi failed with exit code $OHI_ECODE"
  124. echo
  125. echo "This is usually caused by a bad value passed for host-type or environment."
  126. echo
  127. exit 25
  128. fi
  129. exec pssh -t $TIMEOUT -p $PAR -l $USER -h <(echo "$HOSTS") "${args[@]}"