Skip to content

Commit

Permalink
Switched to shflags in create-machines and enabled SSH method (issue #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
peconi committed Jan 21, 2016
1 parent 2d8b8ac commit 2a25e3b
Showing 1 changed file with 76 additions and 77 deletions.
153 changes: 76 additions & 77 deletions src/create-machines.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@
## - 12/15/2015 - added option switches and help - Petar Smilajkov
## ================================================================
## Todo:
## [] Check if long options work
## [] Add other providers
## [] Add other providers (amazon, linode, etc.)
## [] Do some error checking for provided argument values
## [] Remove API keys from this file, move into ENV variables
## [] Create better help printout (default one is kindof janky)
## [] Read API Keys for services from Environment Variables
## [] We can create only 1 docker host when using SSH method
## ================================================================

# TODO source shflags using make expansion
# source shflags
source @BASHLIBS@/shflags

DEFINE_string 'name' 'dh' 'name prefix to be used in the full docker host name' 'n'
DEFINE_string 'provider' 'vb' 'provider identifier on which to spin up docker hosts -> vb = VirtualBox, do = Digital Ocean, aws = Amazon Web Services, ssh = Generic Linux Host (via SSH)' 'p'
DEFINE_string 'ip' '127.0.0.1' 'used only with -p as the IP address of the Linux host' 'i'
DEFINE_string 'user' 'root' 'used only with -p as the SSH user' 'u'
DEFINE_string 'key' '~/.ssh/id_rsa' 'used only with -p as the path to the SSH key to log into the Linux host' 'k'
DEFINE_string 'quantity' '3' 'number of docker hosts to spin up' 'q'
DEFINE_string 'region' 'local' 'region identifier for new docker hosts -> local (for VirtualBox), nyc1..3, ams1..3, sfo1..3, sgp1..3, lon1..3 (DigitalOcean)' 'r'
DEFINE_string 'size' '2gb' 'Size of machine (512mb, 1gb, 2gb, ..., 64gb)' 's'

# parse the command-line
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

# if help - just exit
if [ "${FLAGS_help}" -eq ${FLAGS_TRUE} ]; then exit 1; fi

# set fonts for help highlights
NORM=`tput sgr0`
BOLD=`tput bold`
Expand All @@ -25,40 +42,15 @@ REV=`tput smso`
# API Keys
DO_KEY=""

function show_help {
cat << EOF
Usage: ${BOLD}create-machines${NORM} [-n|--name | -p|--provider | -q|--quantity | -r|--region | -s|--size]
Example: ${REV}create-machines -n prettyName -p do -q 3 -r nyc3 -s 4gb${NORM}
-> Creates a specified -q NUMBER of Docker Hosts @ -p PROVIDER in -r REGION of -s SIZE prefixed with -n prettyName.
-h|--help displays this help
-n|--name NAME name prefix to be used in the full docker host name, default=dh
full name will be namePrefix.provider.region.host#
ex: dh.do.nyc3.0001, or prettyName.aws.us-east-1a.1234
-p|--provider PROVIDER provider identifier on which to spin up docker hosts
vb = VirtualBox (default)
do = Digital Ocean
-q|--quantity NUMBER number of docker hosts to spin up (default: 3)
-r|--region REGION region identifier for new docker hosts
- VirtualBox: local (default)
- DigitalOcean: nyc1..3, ams1..3, sfo1..3, sgp1..3, lon1..3
-s|--size SIZE Size of machine (512mb, 1gb, 2gb (default), 4gb, 8gb, ... 64gb)
EOF
}

function create_machine {
local PROVIDER=$1
local REGION=$2
local SIZE=$3
local FULLNAME=$4
local NODENUMBER=$5
local IP_ADDRESS=$5
local SSH_KEY=$6
local SSH_USER=$7
local NODENUMBER=$8
local PREFIX=""

case ${#NODENUMBER} in
Expand All @@ -82,6 +74,7 @@ function create_machine {

case $PROVIDER in
vb)
echo $PREFIX.$FULLNAME
docker-machine create \
--driver virtualbox \
$PREFIX.$FULLNAME &
Expand All @@ -95,65 +88,71 @@ function create_machine {
$PREFIX.$FULLNAME &
;;
aws)
echo "Amazon Web Services"
echo "Amazon Web Services (TODO)"
;;
ssh)
docker-machine create \
--driver generic \
--generic-ip-address $IP_ADDRESS \
--generic-ssh-user $SSH_USER \
--generic-ssh-key $SSH_KEY \
$PREFIX.$FULLNAME &
;;
*)
echo "ERROR: Unknown Provider: $PROVIDER"
flags_help
exit 1
esac
}

NUMARGS=$#
if [ $NUMARGS -eq 0 ]; then
echo "Spinning up default Docker Cluster: 3 hosts on VirtualBox, 2gb RAM, 001..3.dh-virtualbox-local"
fi

# init vars to default values
NAME="dh"
PROVIDER="vb"
IP_ADDRESS=""
SSH_KEY=""
SSH_USER="root"
QUANTITY=3
REGION="local"
SIZE="2gb"
NAME="dh"

while getopts "n:name:p:provider:q:quantity:r:region:s:size:h help" opt
do

echo "-----$OPTIND----------$opt-----------$OPTARG"

case $opt in
n|name)
NAME="$OPTARG"
;;
p|provider)
PROVIDER="$OPTARG"
;;
q|quantity)
QUANTITY="$OPTARG"
;;
r|region)
echo "Getting Region $opt --> $OPTARG"
REGION="$OPTARG"
;;
s|size)
SIZE="$OPTARG"
;;
h|help)
show_help
exit 1
;;
\?|*)
show_help
exit 1
;;
esac
done
shift "$((OPTIND-1))"
echo ""
echo "FLAGS:"
echo "- Name : ${FLAGS_name}"
echo "- Provider: ${FLAGS_provider}"
echo "- IP : ${FLAGS_ip}"
echo "- SSH KEY : ${FLAGS_key}"
echo "- SSH USER: ${$FLAGS_user}"
echo "- QUANTITY: ${FLAGS_quantity}"
echo "- REGION : ${FLAGS_region}"
echo "- VM SIZE : ${FLAGS_size}"

# update variables with submitted data
if [[ "${FLAGS_name}" ]]; then NAME="${FLAGS_name}"; fi
if [[ "${FLAGS_provider}" ]]; then PROVIDER="${FLAGS_provider}"; fi
if [[ "${FLAGS_ip}" ]]; then IP_ADDRESS="${FLAGS_ip}"; fi
if [[ "${FLAGS_key}" ]]; then SSH_KEY="${FLAGS_key}"; fi
if [[ "${FLAGS_user}" ]]; then SSH_USER="${FLAGS_user}"; fi
if [[ "${FLAGS_quantity}" ]]; then QUANTITY="${FLAGS_quantity}"; fi
if [[ "${FLAGS_region}" ]]; then REGION="${FLAGS_region}"; fi
if [[ "${FLAGS_size}" ]]; then SIZE="${FLAGS_size}"; fi

echo ""
echo "New Vars:"
echo "- Name : $NAME"
echo "- Provider: $PROVIDER"
echo "- IP : $IP_ADDRESS"
echo "- SSH KEY : $SSH_KEY"
echo "- SSH USER: $SSH_USER"
echo "- QUANTITY: $QUANTITY"
echo "- REGION : $REGION"
echo "- VM SIZE : $SIZE"

echo ""
echo "Spinning up Docker Cluster: 0001-$QUANTITY.$NAME.$PROVIDER.$REGION"

FULLNAME="$NAME.$PROVIDER.$REGION"

echo $FULLNAME.$QUANTITY

#create docker host nodes
for ((a=1; a <= QUANTITY ; a++)) do
create_machine $PROVIDER $REGION $SIZE $FULLNAME $a
for ((i=1; i <= QUANTITY ; i++)) do
create_machine $PROVIDER $REGION $SIZE $FULLNAME $IP_ADDRESS $SSH_KEY $SSH_USER $i
done

0 comments on commit 2a25e3b

Please sign in to comment.