From 9d41ce2ef5a7294510609addde4e66d08e377eca Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Mon, 14 May 2018 09:55:29 -0400 Subject: [PATCH] Merge pull request #17384 from jeffmcutter/miqssh_fixes Various fixes to miqssh. (cherry picked from commit 7c62f901280e2094495428f3ac4f94a20c104319) Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1640258 --- tools/miqssh/README.md | 2 +- tools/miqssh/miqhosts.rb | 5 +++- tools/miqssh/miqssh | 64 ++++++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/tools/miqssh/README.md b/tools/miqssh/README.md index 1785b3fe1f8..5b71c3db433 100644 --- a/tools/miqssh/README.md +++ b/tools/miqssh/README.md @@ -4,7 +4,7 @@ These tools allow for running commands and copying files and searching log files Also, even if you do have pssh or other tools for running commands on multiple systems, the new miqgrep, miqgrep -r, miqtail, and miqtail -r are worth a look. -Different transport mechanisms are now supported and can be selected by updating the .config file. By default, Ansible is used since it can work in parallel and is included by default with ManageIQ. Alternatively, if installed and selected, Parallel SSH can be used and is faster opening connections than Ansible. +Different transport mechanisms are now supported and can be selected by updating the .config file. By default, Ansible is used since it can work in parallel and is included by default with ManageIQ. Ansible is now the recommended transport for best functionality. **Note that this program can enable you to do things faster, including mistakes, use at your own risk.** diff --git a/tools/miqssh/miqhosts.rb b/tools/miqssh/miqhosts.rb index 81c453d21a3..83f4dd13c3f 100755 --- a/tools/miqssh/miqhosts.rb +++ b/tools/miqssh/miqhosts.rb @@ -43,7 +43,10 @@ def list_groups(data) # Method to list the servers belonging to the specified group. def list_servers(data, group) - puts data[group.to_sym].join(" ") + groups = group.split(',') + servers = [] + groups.each { |g| servers.push(data[g.to_sym]) } + puts servers.uniq.sort.join(" ") end file = ARGV[0] diff --git a/tools/miqssh/miqssh b/tools/miqssh/miqssh index 129dc976f45..8f3a0bdc747 100755 --- a/tools/miqssh/miqssh +++ b/tools/miqssh/miqssh @@ -44,7 +44,7 @@ groups=$($miqhosts_cmd $miqhosts_file list_groups) function usage_common { echo "DESCRIPTION: $2" echo - echo "USAGE: $NAME [-g group] [-s] $1" + echo "USAGE: $NAME [-g group1[,groupN]] [-s] $1" echo shift 2 echo "DETAILS:" @@ -159,9 +159,9 @@ function clean_pssh_out { if [ $NAME == 'miqtail' ] then - GREP='grep -e' + GREP='grep' else - GREP='zgrep -e' + GREP='zgrep' fi L='L' @@ -191,6 +191,21 @@ done shift $((OPTIND-1)) +GREP="$GREP -e" + +if [[ "$TRANSPORT" == 'ansible' && $NAME == 'miqcollect' ]] +then + wildcards=( '*' '.' '?' '|' ']' '[' ) + for wildcard in "${wildcards[@]}" + do + if [[ $1 == *"${wildcard}"* ]] + then + echo "*** Switching TRANSPORT to ssh due to wild card in source file name ***" + TRANSPORT=ssh + fi + done +fi + if [[ -z "$TRANSPORT" || -z "$PARALLELISM" ]] then echo "TRANSPORT and/or PARALLELISM not specified, see $DIR/.config." @@ -274,11 +289,14 @@ then echo "Not enough arguments!" usage fi -elif [[ "$2" == "list" && $# -lt 2 ]] +elif [ "$1" == "list" ] then - echo - echo "Not enough arguments!" - usage + if [ $# -ne 1 ] + then + echo + echo "Wrong number of arguments!" + usage + fi elif [[ $NAME == 'miqgrep' || $NAME == "miqtail" ]] then if [[ $# -ne 1 && $# -ne 2 ]] @@ -357,30 +375,20 @@ then exit 1 fi -group_good=false - -for i in $groups -do - if [ "$i" == "$group" ] - then - group_good=true - fi -done - -if [ "$group_good" != "true" ] +if [[ "$QUIET" != 'true' && "$TRANSPORT" != 'pssh' && "$TRANSPORT" != 'ansible' || ( $NAME == 'miqgrep' && $REQUEST == 'true' ) ]] then echo - echo "Group $group not found in $miqhosts_file" - usage fi -if [[ "$QUIET" != 'true' && "$TRANSPORT" != 'pssh' && "$TRANSPORT" != 'ansible' || ( $NAME == 'miqgrep' && $REQUEST == 'true' ) ]] +SERVERS=$($miqhosts_cmd $miqhosts_file list_servers $group) + +if [ -z "$SERVERS" ] then echo + echo "No servers found matching group(s) $group in $miqhosts_file" + usage fi -SERVERS=$($miqhosts_cmd $miqhosts_file list_servers $group) - if [ "$1" == "list" ] then if [ "$QUIET" != 'true' ] @@ -390,7 +398,7 @@ then for i in $SERVERS do echo $i - done + done | sort exit fi @@ -450,7 +458,7 @@ then fi ARGS=$* -ARGS=$(echo $ARGS | sed 's/*/\\*/g') +#ARGS=$(echo $ARGS | sed 's/*/\\*/g') if [ $NAME != 'miqtail' ] then @@ -482,7 +490,7 @@ then case $NAME in "miqssh") pssh -t 0 -i $HOSTS $ARGS ;; "miqscp") pscp.pssh -t 0 $HOSTS $1 $2 ;; - "miqcollect") pslurp -t 0 $HOSTS -L $2 $1 . ;; + "miqcollect") pslurp -t 0 $HOSTS -L $2 "$1" . ;; "miqgrep") pssh -t 0 $HOSTS -o $PSSH_OUTDIR "$GREP \"$PATTERN\" $LOG | perl -pe 's/^\/.*?://' | sed -e \"s/^/[\$(hostname -s)] /\"" ;; esac @@ -498,9 +506,9 @@ then "miqgrep") if [ -n "$PATTERN" ] then - ansible all -i $HOSTS -f $PARALLELISM -m shell -a "$GREP \"$PATTERN\" $LOG | perl -pe 's/^\/.*?://' | sed -e \"s/^/[\$(hostname -s)] /\"" | tee $TMPFILE + ansible all -i $HOSTS -f $PARALLELISM -m shell -a "$GREP \"$PATTERN\" $LOG | perl -pe 's/^\/.*?://' | sed -e \"s/^/[\$(hostname -s)] /\"" | tee $TMPFILE | perl -ne "print if /^\S+\s\|\s\S+\s\|\src=\d+\s>>\s*$|^\S+\s\|\s\S+\s=>\s\{\s*$/" else - ansible all -i $HOSTS -f $PARALLELISM -m shell -a "$GREP $LOG | perl -pe 's/^\/.*?://' | sed -e \"s/^/[\$(hostname -s)] /\"" | tee $TMPFILE + ansible all -i $HOSTS -f $PARALLELISM -m shell -a "$GREP $LOG | perl -pe 's/^\/.*?://' | sed -e \"s/^/[\$(hostname -s)] /\"" | tee $TMPFILE | perl -ne "print if /\|\src=\d+\s>>/" fi ;; esac