#!/bin/bash
# Copyright (c) 2010 Malte Schwarzkopf <malte.schwarzkopf@cl.cam.ac.uk>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# ----
#
# Skywriting/Ciel helper script to pull files or directories off all machines
# in a cluster to a local target.
#
# usage: See sw-push -h

# defaults
KEY="sw-masterkey"
SWUSER="root"
SWROOT="/opt/skywriting"
VERBOSE=0
RECURSIVE=""

# ---------------------------------------------
# option processing

while [ $# -gt 0 ]
do
  case $1
  in
    -i|-k)
      KEY=$2
      shift 2
    ;;

    -f)
      if [[ $2 = '--' ]]; then
         # STDIN input
         SOURCE=''
      else
         # Cluster definition file
         SOURCE=$2
      fi
      shift 2
    ;;
    
    -r|--swroot)
      SWROOT=$2
      shift 2
    ;;
    
    --recursive)
      RECURSIVE="-r"
      shift 1
    ;;
    
    -u|--swuser)
      SWUSER=$2
      shift 2
    ;;
    
    -p|--pushfile)
      PUSHFILE=$2
      shift 2
    ;;
    
    -t|--target)
      TARGET=$2
      shift 2
    ;;

    -v)
      VERBOSE=1
      shift 1
    ;;

    -h|*)
      echo "Pushes a local file or directory to all machines in a cluster."
      echo "usage: sw-push [-f cluster-file|(-i|-k) key|-r swroot|-u swuser|-v]"
      echo ""
      echo "-f: the file listing the machines in the cluster, one per line."
      echo "    If '--' is passed, STDIN is assumed."
      echo "-i|-k: the private key to use for authentication to cluster machines"
      echo "       (defaults to 'sw-masterkey')"
      echo "-p|--pushfile: the file to push to the cluster"
      echo "-t|--target: the target file name/path on the cluster"
      echo "-r|--swroot: the root directory of the remote Skywriting installation"
      echo "             (defaults to '/opt/skywriting')"
      echo "--recursive: recursively push data"
      echo "-u|--swuser: the user name of the Skywriting user on the cluster"
      echo "             (defaults to 'root')"
      echo "-v: verbose mode (don't surpress output from remote machines)"
      shift 1
      exit 0
    ;;
  esac
done

# ---------------------------------------------
# main script

if [[ $TARGET == "" || $PUSHFILE == "" ]]; then
   echo "source (-p) and target (-t) must be specified!"
   exit 2
fi

exec 3<$SOURCE
I=0
while read -u 3 myLine
do
    echo -n "Pushing to instance $I: "
    if [[ $VERBOSE -eq 1 ]]; then
	scp $RECURSIVE -o StrictHostKeyChecking=no -i $KEY $PUSHFILE $SWUSER@$myLine:$TARGET
    else
	scp -q $RECURSIVE -o StrictHostKeyChecking=no -i $KEY $PUSHFILE $SWUSER@$myLine:$TARGET
    fi
    echo $myLine
    I=`expr $I + 1`
done

exit 0
