Skip to content

Commit

Permalink
Script to create/delete multiple GCE instances
Browse files Browse the repository at this point in the history
- This script outputs the IP address array that can be used
  with start_nodes script to launch multinode demo
  • Loading branch information
pgarg66 committed Jul 3, 2018
1 parent 0672794 commit 98b9f11
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions multinode-demo/gce_multinode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash

command=$1
prefix=
num_nodes=
out_file=
image_name="ubuntu-16-04-cuda-9-2-new"

OPTIND=$OPTIND+1

usage() {
exitcode=0
if [[ -n "$1" ]]; then
exitcode=1
echo "Error: $*"
fi
cat <<EOF
usage: $0 <create/delete> <-p prefix> <-n num_nodes> <-o file> [-i image-name]
Creates a GCE multinode network
create/delete - Create or delete the network
-p prefix - A common prefix for node names, to avoid collision
-n num_nodes - Number of nodes
-o out_file - Used for create option. Outputs an array of IP addresses
of new nodes to the file
-i image_name - Existing image on GCE (default $image_name)
EOF
exit $exitcode
}

while getopts "h?p:i:n:o:" opt; do
case $opt in
h | \?)
usage
exit 0
;;
p)
prefix="$OPTARG"
;;
i)
image_name="$OPTARG"
;;
o)
out_file="$OPTARG"
;;
n)
num_nodes="$OPTARG"
;;
*)
usage "Error: unhandled option: $opt"
;;
esac
done

set -e

if [[ -z "$command" ]]; then
usage
fi

if [[ -z "$prefix" ]]; then
usage
fi

if [[ -z "$num_nodes" ]]; then
usage
fi

nodes=()
for i in $(seq 1 "$num_nodes"); do
nodes+=("$prefix$i")
done

if [ "$command" = "create" ]; then
if [[ -z "$out_file" ]]; then
usage
fi

ip_addr_list=$(gcloud beta compute instances create "${nodes[@]}" --zone=us-west1-b --tags=testnet \
--image="$image_name" | grep "RUNNING" | tr -s ' ' | cut -f 5 -d ' ')

echo "ip_addr_array=($ip_addr_list)" >"$out_file"
elif [ "$command" = "delete" ]; then
gcloud beta compute instances delete "${nodes[@]}"
else
usage
fi

0 comments on commit 98b9f11

Please sign in to comment.