aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-06-07 20:59:48 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2020-06-07 20:59:48 +0200
commit9e7ca7538e2f96fda9cbdc126887afc8d7d15b11 (patch)
tree1c80ab7de0ef0ef9fe62f429f246484b3a35da2b
parenta1407e29cf0b47d5db157ebedcec8139f82cfc6c (diff)
downloadltask-9e7ca7538e2f96fda9cbdc126887afc8d7d15b11.tar.gz
Added parallel task execution
Parallel task execution + hostname and stderr prefix to output
-rwxr-xr-xltask42
-rw-r--r--tasks/example5
-rwxr-xr-xtools/rexec.sh2
3 files changed, 36 insertions, 13 deletions
diff --git a/ltask b/ltask
index 306e972..ee4829b 100755
--- a/ltask
+++ b/ltask
@@ -36,6 +36,8 @@ export readonly INCLUDE_DIR=$BASEDIR/libs
TASK=
TARGET=
+PARALLEL="false"
+
HOSTS=()
FILES=()
INCLUDES=()
@@ -61,6 +63,8 @@ function parse_args() {
-H)
i=$((i+1))
HOSTS+=(${ARGV[$i]});;
+ -p)
+ PARALLEL="true";;
-h)
print_help 0;;
*)
@@ -79,6 +83,7 @@ Options:
-H HOST Add HOST to target hosts. can be used
to target single hosts without defining
a custom target
+ -p Force parallel execution
-h print this help text
EOF
exit $1
@@ -97,8 +102,8 @@ fi
unset TASK_ISSET
-[ -f $TASK_DIR/$TASK ] && source $TASK_DIR/$TASK
[ -f $TARGET_DIR/$TARGET ] && source $TARGET_DIR/$TARGET
+[ -f $TASK_DIR/$TASK ] && source $TASK_DIR/$TASK
if [ -z $TASK_ISSET ]; then
perror Task did not load correctly
@@ -110,6 +115,9 @@ HOSTS_UNIQUE=($(tr ' ' '\n' <<< "${HOSTS[@]}" | sort -u))
COUNTER=0
HOSTCOUNT=${#HOSTS_UNIQUE[@]}
+[ $PARALLEL = "true" ] && AND="&"
+declare -A HOST_PID
+
for hoststring in "${HOSTS_UNIQUE[@]}"; do
rexecargs=""
IFS=$':' read hostname port <<< "$hoststring"
@@ -121,20 +129,32 @@ for hoststring in "${HOSTS_UNIQUE[@]}"; do
rexecargs+="$hostname ${INCLUDES[@]} $TASK_DIR/$TASK"
- echo
- echo "<=== $hostname ===>"
- echo
- $TOOL_DIR/rexec.sh $rexecargs
- [ $? -ne 0 ] && FAILED_HOSTS+=($hostname)
-
- COUNTER=$(($COUNTER + 1))
- PERCENT=$(($COUNTER * 100 / $HOSTCOUNT))
- perror -n "\r[$COUNTER: $PERCENT%] "
+ set -o pipefail
+ eval "$TOOL_DIR/rexec.sh $rexecargs | (while read l; do echo [$hostname] \$l; done) $AND"
+ RET=$?
+
+ if [ $PARALLEL = "true" ]; then
+ HOST_PID[$hostname]=$!
+ else
+ [ $RET -ne 0 ] && FAILED_HOSTS+=($hostname)
+ COUNTER=$(($COUNTER + 1))
+ PERCENT=$(($COUNTER * 100 / $HOSTCOUNT))
+ perror -n "\r[$COUNTER: $PERCENT%] "
+ fi
done
+if [ $PARALLEL = "true" ]; then
+ perror Waiting for remote tasks to finish...
+ for hostname in ${!HOST_PID[@]}; do
+ wait ${HOST_PID[$hostname]}
+ [ $? -ne 0 ] && FAILED_HOSTS+=($hostname)
+ done
+ perror Done!
+fi
+
if [ ${#FAILED_HOSTS[@]} -gt 0 ]; then
perror "\n"
- perror ${#FAILED_HOSTS[@]} hosts failed the task:
+ perror ${#FAILED_HOSTS[@]} targets failed:
perror ${FAILED_HOSTS[@]}
exit 1
fi
diff --git a/tasks/example b/tasks/example
index bc78361..deb3c94 100644
--- a/tasks/example
+++ b/tasks/example
@@ -3,7 +3,7 @@
function task_exec() {
whoami
hostname
- uptime
+ uptime 1>&2
}
function task_setup() {
@@ -12,6 +12,9 @@ function task_setup() {
# $TOOL_DIR
# $INCLUDE_DIR
+ # Available Configuration
+ # $PARALLEL "true"/"false"
+
INCLUDES+=()
SSH_USER=server
diff --git a/tools/rexec.sh b/tools/rexec.sh
index 388cbc3..d60c021 100755
--- a/tools/rexec.sh
+++ b/tools/rexec.sh
@@ -86,5 +86,5 @@ parse_args
[ ${#FILES[@]} -gt 0 ] && scp $SSH_OPTIONS -i $SSH_IDENTITY -P $SSH_PORT ${FILES[@]} $SSH_HOST:
-cat ${SCRIPT_FILES[@]} | $SSH -p $SSH_PORT -i $SSH_IDENTITY $SSH_HOST "/bin/bash 2>&1" 2>/dev/null
+cat ${SCRIPT_FILES[@]} | $SSH -p $SSH_PORT -i $SSH_IDENTITY $SSH_HOST '/bin/bash 2> >(while read line; do echo [ERR] $line; done)' 2>/dev/null
exit $?