#!/bin/bash

function usage () {
  echo "Usage: weihu cmd COMMAND, 在集群中所有的机器上执行对应COMMAND命令"
  echo "Usage: weihu copy source target，将本地source文件，推送到集群中所有的机器上"
  exit
}

action=$1

HOSTLIST='ha2 proxy1 proxy2 network1 network2 ha1'

(( $#<=1 )) && usage

case "$action" in
  "cmd")
    # 删除参数1
    shift
    COMMAND="$*"
    for host in $HOSTLIST
    do 
      ssh root@$host "$COMMAND"
    done
    ;;
  "copy")
    # 删除参数1
    shift
    for host in $HOSTLIST
    do 
      num=$#
      case $num in
        2)
          scp -r $1 root@$host:$2
          ;;
        #[3-9]|[1-9][0-9])
        [3-9])
	  last=$(echo $* | awk '{print $NF}')
          args_exclude_last=$(echo $* | sed "s#$last##")
          scp -r ${args_exclude_last} root@$host:${last}
          ;;
        *)
          usage
      esac
    done
    ;;
  *)
    usage
    ;;
esac
