#!/bin/bash
# Share file(s) with all workers in a scale set
if [ $# -lt 2 ]; then
  cat 1>&2 <<EOF
Usage: $0 SSname [-d dir] files...
Where name is the name of a VM scale set.

Distribute files to every machine in the SS named SSname
  if -d, into dir, otherwise home directory
 by doing as it were
  tar -czf - files | ssh machine "[cd $dir &&] tar -xzf -"
EOF
 exit 1
fi
group=cc
name=$1
shift
if [ "$1" = "-d" ]
then
 dodir="cd $2 && "
 shift 2
else
 dodir=""
fi
cmd="${dodir}tar -xzf -"
np=$(az vmss get-instance-view -g $group -n $name | jq '.virtualMachine.statusesSummary | .[0].count')
echo "|$name|$cmd|$@|$np|" 1>&2
az vmss list-instance-connection-info -g $group -n $name | tr -s ',": ' '\t' | \
    tail -n +2 | head -$np |cut -f 3-5 | tee /dev/stderr |\
  while read id ip port
    do tar -czf - "$@" | "$(dirname "$0")"/internal/old_invoke.sh -x "" bash /dev/null "" $port $ip -c \""$cmd"\"
  done

