view master/bin/wrun.sh @ 9:55e953e5c66f

wrun.sh, invoke.sh: add control of use of nohup, with -i(mmediately) and -w(ait) respectively wrun.sh: add support for fixed file input via -ff, handle -ff - in obvious way wbash.sh: use -ff - and -i to execute shell commands on all workers
author Henry S. Thompson <ht@markup.co.uk>
date Tue, 02 Oct 2018 10:52:45 +0000
parents a7637c994964
children 2fbefb8d1a9e
line wrap: on
line source

#!/bin/bash
set -e -o pipefail
if [ $# -lt 2 ]; then
  cat 1>&2 <<EOF
Usage: $0 SSname cmd [-x] [-np n] [-f[f] file] [-i] [...args]
Where name is the name of a VM scale set.

Runs cmd on every machine in a scale set,
 passing args and, 
  if -f, lines from file split per worker
  if -ff, complete file sent to all workers
  unless -x, worker id
 by doing as it were
  [ echo line(s)-from-file |] ssh machine "$cmd [id] "$args"" 
  if -i, don\'t use nohup on the workers so returns immediately
   [default is to use nohup unless neither -f or -ff]
EOF
  exit 1
fi
group=cc
name=$1
cmd="$2"
shift 2
if [ "$1" = "-x" ]
then
 id=-x
 shift
else
 id=
fi
if [ "$1" = "-np" ]
then
 np=$2
 shift
 shift
else
 np=$(az vmss get-instance-view -g $group -n $name | jq '.virtualMachine.statusesSummary | .[0].count')
fi
echo "|$name|$cmd|$@|$np|" 1>&2
me=$(az vm list-ip-addresses -g cc -n Deb1 | jq -r '.[0].virtualMachine.network.publicIpAddresses|.[0].ipAddress')
echo me=$me 1>&2
wait='-w'
if [ "$1" = "-f" ]
then
 file=$2
 shift 2
 tfile=$(mktemp)
 split -n r/$np -u --filter="cat > $tfile\$\$" $file &
 # Wait for files to be built
 while [ $np -gt $(ls ${tfile}* | wc -l) ]; do echo -n . 1>&2; sleep 1; done
 echo 1>&2
 wc -l ${tfile}* 1>&2
elif [ "$1" = "-ff" ]
then
 file=$2
 shift 2
 if [ "$file" = "-" ]
 then
  file=$(mktemp)
  cat > $file
 fi
else
 file=/dev/null
 wait=''
fi
if [ "$1" = "-i" ]
then 
 shift
 wait=''
fi
echo "|$np|$file|$tfile|$wait|" 1>&2
paste -d ' ' \
 <(if [ -z "$tfile" ]
   then yes $file|head -$np
   else
     echo ${tfile}?* | tr ' ' '\012' # the name of the file of
                                    # input lines for each worker
   fi
  ) \
 <(az vmss list-instance-connection-info -g $group -n $name | tr -s ',": ' '\t' | \
    tail -n +2 | head -$np |cut -f 3-5 | \
  while read i ip port
   do
    echo $i $port $ip
   done) | \
 parallel --gnu --colsep ' ' -j $np "$(dirname "$0")"/internal/invoke.sh $wait $id $me "$cmd" {} "$@" || { r=$? ; echo parallel exited with status=$r 1>&2;}
if [ "${tfile}" ]; then rm ${tfile}*; fi