#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

bin=`dirname "$0"`
bin=`cd "$bin"; pwd`

# if no args specified, show usage
if [ $# = 0 ]; then
  echo "Usage: hadoop-ec2 COMMAND"
  echo "where COMMAND is one of:"
  echo "  list                                 list all running Hadoop EC2 clusters"
  echo "  launch-cluster <group> <num slaves>  launch a cluster of Hadoop EC2 instances - launch-master then launch-slaves"
  echo "  launch-master  <group>               launch or find a cluster master"
  echo "  launch-slaves  <group> <num slaves>  launch the cluster slaves"
  echo "  terminate-cluster  <group>           terminate all Hadoop EC2 instances"
  echo "  delete-cluster <group>               delete the group information for a terminated cluster"
  echo "  login  <group|instance id>           login to the master node of the Hadoop EC2 cluster"
  echo "  screen <group|instance id>           start or attach 'screen' on the master node of the Hadoop EC2 cluster"
  echo "  proxy  <group|instance id>           start a socks proxy on localhost:6666 (use w/foxyproxy)"
  echo "  push   <group> <file>                scp a file to the master node of the Hadoop EC2 cluster"
  echo "  <shell cmd> <group|instance id>      execute any command remotely on the master"
  echo "  create-image                         create a Hadoop AMI"
  exit 1
fi

# get arguments
COMMAND="$1"
shift

if [ "$COMMAND" = "create-image" ] ; then
  . "$bin"/create-hadoop-image $*
elif [ "$COMMAND" = "launch-cluster" ] ; then
  . "$bin"/launch-hadoop-cluster $*
elif [ "$COMMAND" = "launch-master" ] ; then
  . "$bin"/launch-hadoop-master $*
elif [ "$COMMAND" = "launch-slaves" ] ; then
  . "$bin"/launch-hadoop-slaves $*
elif [ "$COMMAND" = "delete-cluster" ] ; then
  . "$bin"/delete-hadoop-cluster $*
elif [ "$COMMAND" = "terminate-cluster" ] ; then
  . "$bin"/terminate-hadoop-cluster $*
elif [ "$COMMAND" = "list" ] ; then
  . "$bin"/list-hadoop-clusters
else
  . "$bin"/cmd-hadoop-cluster "$COMMAND" $*
fi

