Skip to content

Instantly share code, notes, and snippets.

@nkashy1 nkashy1/create.sh Secret
Created Dec 21, 2018

Embed
What would you like to do?
Experiment
#!/bin/bash
# Creates a VM instance for model training
#
# Requires:
# + gcloud command line tool -- https://cloud.google.com/sdk/
# + gcloud should be authenticated with credentials which also provide access to pulling VM images
# from the `neural-noise` project
#
# Configuration from prompt
# -------------------------
# Requires:
# 1. git to be installed in the environment in which the script is running
# 2. script is run from the phenomenal-face git repo
# 3. git is currently on the reference that you wish to deploy from
# 4. python command to point to a python binary capable of running create_metadata.py
#
# When run TRAINER_ARGS_PATH=PROMPT, the script
# 1. Pushes the current git ref to the ref of name $INSTANCE_NAME on the appropriate remote
# 2. Creates a metadata file named after $INSTANCE_NAME (uses the $INSTANCE_NAME ref as the
# GIT_TARGET in the generated metadata)
# 3. Creates the instance to run the experiment specified by the user at metadata file creation
# time
#
# NOTE: When the script has finished running in prompt mode, your current git branch will be the new
# branch that was created for the deployment of the given experiment.
set -e -o pipefail
USAGE="Usage: $0 INSTANCE_NAME ZONE MACHINE_TYPE DISK_SIZE ACCELERATOR_TYPE NUM_ACCELERATORS [SERVICE_ACCOUNT IMAGE TRAINER_ARGS_PATH INSTANCE_TEMPLATE PREEMPTIBLE]"
if [ $1 = "-h" ] || [ $1 = "--help" ] ; then
echo -e "$USAGE\n"
echo -e "\tINSTANCE_NAME"
echo -e "\t\tName of compute engine instance to create."
echo ""
echo -e "\tZONE"
echo -e "\t\tGCE zone in which to create instance. For a full list, run:"
echo -e "\t\t\t$ gcloud compute zones list"
echo ""
echo -e "\tMACHINE_TYPE"
echo -e "\t\tGCE machine type to assign to instance. For a full list, run:"
echo -e "\t\t\t$ gcloud compute machine-types list"
echo -e "\t\t For a list of machine types available in a given \$ZONE, run:"
echo -e "\t\t\t$ gcloud compute machine-types list | grep \$ZONE"
echo ""
echo -e "\tDISK_SIZE"
echo -e "\t\tDisk size in gigabytes that you would like to provision onto the VM."
echo ""
echo -e "\tACCELERATOR_TYPE"
echo -e "\t\tType of GPU you would like to attach to the VM. For a full list, run:"
echo -e "\t\t\t$ gcloud compute accelerator-types list"
echo -e "\t\tFor a list of accelerator types available in a given \$ZONE, run:"
echo -e "\t\t\t$ gcloud compute accelerator-types list | grep \$ZONE"
echo ""
echo -e "\tNUM_ACCELERATORS"
echo -e "\t\tNumber of GPUs of the given ACCELERATOR_TYPE you would like to attach to the VM.\n"
echo ""
echo -e "\tSERVICE_ACCOUNT"
echo -e "\t\tOptional argument specifying which service account should be used on the VM"
echo -e "\t\tinstance being provisioned. Should be provided in the form of the service"
echo -e "\t\taccount e-mail address."
echo ""
echo -e "\tIMAGE"
echo -e "\t\tOptional argument specifying which image from the trainer image family in the"
echo -e "\t\tneural-noise project you would like to use with the VM."
echo ""
echo -e "\tTRAINER_ARGS_PATH"
echo -e "\t\tPath to a file defining trainer arguments"
echo ""
echo -e "\tINSTANCE_TEMPLATE"
echo -e "\t\tSet to true if, instead of creating an instance, you would like to create an"
echo -e "\t\tinstance template."
echo ""
echo -e "\tPREEMPTIBLE"
echo -e "\t\tSet to true if, you would like to use preemptible instances. For more information:"
echo -e "\t\t\thttps://cloud.google.com/compute/docs/instances/preemptible"
exit 1
fi
# Optional argument specifying which VM image to use
SERVICE_ACCOUNT=$7
IMAGE=$8
TRAINER_ARGS_PATH=$9
INSTANCE_TEMPLATE=${10}
PREEMPTIBLE=${11}
set -u
INSTANCE_NAME=$1
ZONE=$2
MACHINE_TYPE=$3
DISK_SIZE=$4
ACCELERATOR_TYPE=$5
NUM_ACCELERATORS=$6
# Path to current script (used to load metadata onto instance from files in this directory)
SCRIPT_PATH=$(dirname $0)
echo "Creating instance $INSTANCE_NAME:"
echo -e "\tMachine type: $MACHINE_TYPE"
echo -e "\tAccelerators: $ACCELERATOR_TYPE"
ZONE_STRING="--zone=$ZONE"
INSTANCE_STRING="instances"
if [ ! -z $INSTANCE_TEMPLATE ] && [ $INSTANCE_TEMPLATE = true ]; then
INSTANCE_STRING="instance-templates"
# Instance templates do not take zones
ZONE_STRING=""
fi
IMAGE_STRING=""
if [ ! -z $IMAGE ] ; then
echo -e "\tUsing image: $IMAGE"
IMAGE_STRING="--image=$IMAGE"
else
echo -e "\tNo image specified, using latest image in the trainer image family"
IMAGE_STRING="--image-family=trainer"
fi
SERVICE_ACCOUNT_STRING="--service-account=default"
if [ ! -z $SERVICE_ACCOUNT ] ; then
echo -e "\tUsing service account: $SERVICE_ACCOUNT"
SERVICE_ACCOUNT_STRING="--service-account=$SERVICE_ACCOUNT"
fi
if [ $TRAINER_ARGS_PATH = "PROMPT" ] ; then
if [ ! -z $(git branch | sed 's/^*//' | grep $INSTANCE_NAME) ] ; then
echo "Error: Branch already exists with $INSTANCE_NAME as prefix"
exit 1
fi
GCP_REPO_URL="https://source.developers.google.com/p/neural-noise/r/phenomenal-face"
GCP_REPO=$(git remote -v | grep "$GCP_REPO_URL" | head -n 1 | cut -f1)
if [ -z $GCP_REPO ] ; then
echo "Error: No remote found referring to $GCP_REPO_URL"
exit 1
fi
echo -e "\tCreating branch $INSTANCE_NAME from $(git log | head -n 1 | cut -d' ' -f2)"
git checkout -b $INSTANCE_NAME
echo -e "\tPushing clean $INSTANCE_NAME to $GCP_REPO"
git push $GCP_REPO $INSTANCE_NAME
TRAINER_ARGS_PATH="${INSTANCE_NAME}.metadata"
if [ -f $TRAINER_ARGS_PATH ] ; then
echo "Error: File already exists - $TRAINER_ARGS_PATH"
exit 1
fi
echo -e "\tCreating metadata file: $TRAINER_ARGS_PATH"
python create_metadata.py -n $INSTANCE_NAME GIT_TARGET=$INSTANCE_NAME
echo -e "\tCommiting $TRAINER_ARGS_PATH and pushing to $GCP_REPO"
git add $TRAINER_ARGS_PATH
git commit -m "AUTO-GENERATED: Added metadata for $INSTANCE_NAME"
git push $GCP_REPO $INSTANCE_NAME:$INSTANCE_NAME
fi
METADATA_STRING=""
if [ ! -z $TRAINER_ARGS_PATH ] ; then
echo -e "\tLoading metadata from: ${TRAINER_ARGS_PATH}"
METADATA_STRING="--metadata-from-file startup-script=${SCRIPT_PATH}/startup.sh,trainer-args=${TRAINER_ARGS_PATH}"
fi
PREEMPTIBILITY_STRING=""
if [ ! -z $PREEMPTIBLE ] && [ $PREEMPTIBLE = true ] ; then
PREEMPTIBILITY_STRING="--preemptible"
fi
gcloud compute $INSTANCE_STRING create $INSTANCE_NAME \
$ZONE_STRING \
--machine-type=$MACHINE_TYPE \
--boot-disk-size=$DISK_SIZE \
--accelerator "type=$ACCELERATOR_TYPE,count=$NUM_ACCELERATORS" \
$IMAGE_STRING \
--image-project=neural-noise \
$METADATA_STRING \
--maintenance-policy TERMINATE \
$SERVICE_ACCOUNT_STRING \
--scopes cloud-platform \
$PREEMPTIBILITY_STRING
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.