#!/bin/bash
# Create an HDInsight cluster, and initialise ssh access to the head
u="Usage: hd_create.sh [-c filename] HDname\nWhere name will be prefixed with 'cc' to form\nthe name of an HDInsight cluster to create, and with 'CommonCrawl_' to give its password\nSaves config in named file if -c is present and file is writeable\n"
set -e -o pipefail
if [ $# -eq 1 ]
then
 :
elif [ \( $# -eq 3 \) -a \( "$1" = "-c" \) -a \( ! -d "$2" \) ]
then
 if >> "$2" 2>/dev/null
 then
  save_conf=" | tee \"$2\""
  shift 2
 else  
  printf "$u" 1>&2
  exit 1
 fi
else
 printf "$u" 1>&2
 exit 2
fi
name=cc"$1"
pwd=CommonCrawl_"$1"
echo "Starting creation of $name, will take 15 minutes or so..."
cmd="az hdinsight create -g cc -n $name -l southcentralus -t hadoop -p $pwd -U cc --storage-account ccscsg.blob.core.windows.net --workernode-size Standard_D13_V2 --headnode-size Standard_D12_V2"
bash -c "$cmd$save_conf"
# remove old host key, if any
ssh-keygen -R $name-ssh.azurehdinsight.net
# let us in w/o password after this
ssh $name-ssh.azurehdinsight.net "cat >> .ssh/authorized_keys" < /home/cc/.ssh/id_rsa.pub
