#!/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 push local files or directories to all 
# machines in a cluster.
#
# usage: See sw-pull -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|--pullfile)
      PULLFILE=$2
      shift 2
    ;;
    
    -t|--target)
      TARGET=$2
      shift 2
    ;;

    -v)
      VERBOSE=1
      shift 1
    ;;

    -h|*)
      echo "Pulls files or directories from a cluster and stores them locally."
      echo "usage: sw-pull -p source -t target [-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|--pullfile: the file to pull from the cluster"
      echo "-t|--target: the local target path (new per-worker directories will be created here)"
      echo "-r|--swroot: the root directory of the remote Skywriting installation"
      echo "             (defaults to '/opt/skywriting')"
      echo "--recursive: pull recursively"
      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 == "" || $PULLFILE == "" ]]; then
   echo "source (-p) and target (-t) must be specified!"
   exit 2
fi

I=0
cat $SOURCE | while myLine=`line`
do
    echo -n "Pulling from to instance $I: "
    mkdir -p $TARGET/$myLine/
    scp $RECURSIVE -o StrictHostKeyChecking=no -i $KEY $SWUSER@$myLine:$PULLFILE $TARGET/$myLine/
    echo $myLine
    I=`expr $I + 1`
done

exit 0
