Skip to content

Instantly share code, notes, and snippets.

@edinella
Created September 29, 2017 18:48
Show Gist options
  • Save edinella/6c1434055bd2a88652938d3b287a8ecc to your computer and use it in GitHub Desktop.
Save edinella/6c1434055bd2a88652938d3b287a8ecc to your computer and use it in GitHub Desktop.
#! /bin/bash
if [ -z "$1" ] || [ -z "$2" ]; then
printf "Use BRANCH and ALIAS as arguments. Ex:\n"
printf "./deploy.sh develop tangerine\n"
exit 1
fi
GITBRANCH=$1
ALIAS=$2
REGION=us-east1
ZONE=us-east1-c
MIN_INSTANCES=2
MAX_INSTANCES=10
TARGET_UTILIZATION=0.65
COOL_DOWN_PERIOD=300
printf "Going to deploy branch $1 with alias $2\n"
read -p "Are you sure? " -n 1 -r
echo # move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
set -ex
# INSTANCE TEMPLATE
gcloud compute instance-templates create $ALIAS-tmpl \
--tags $ALIAS-http-server,$ALIAS-https-server,$ALIAS-websocket-server \
--metadata-from-file startup-script=gce/startup-script.sh \
--scopes userinfo-email,cloud-platform \
--metadata GITBRANCH=$GITBRANCH \
--image-project=debian-cloud \
--machine-type=g1-small \
--image-family=debian-8
# FIREWALL RULES
gcloud compute firewall-rules create $ALIAS-allow-http \
--description "Allow port 80 access to http-server" \
--target-tags $ALIAS-http-server \
--source-ranges 0.0.0.0/0 \
--allow tcp:80
gcloud compute firewall-rules create $ALIAS-allow-https \
--description "Allow port 443 access to https-server" \
--target-tags $ALIAS-https-server \
--source-ranges 0.0.0.0/0 \
--allow tcp:443
gcloud compute firewall-rules create $ALIAS-allow-websockets \
--description "Allow port 65080 access to websocket-server" \
--target-tags $ALIAS-websocket-server \
--source-ranges 0.0.0.0/0 \
--allow tcp:65080
# PERFORMANCE CHECK
gcloud compute http-health-checks create $ALIAS-performance-check \
--request-path "/health" \
--unhealthy-threshold 1 \
--healthy-threshold 2 \
--check-interval 10s \
--timeout 10s \
--port 80
# TARGET POOL
gcloud compute target-pools create $ALIAS-pool \
--http-health-check $ALIAS-performance-check \
--session-affinity CLIENT_IP \
--region $REGION
# INSTANCE GROUP
gcloud compute instance-groups managed create $ALIAS-group \
--base-instance-name $ALIAS \
--target-pool $ALIAS-pool \
--template $ALIAS-tmpl \
--size $MIN_INSTANCES \
--zone $ZONE
# AUTO SCALING
gcloud compute instance-groups managed set-autoscaling $ALIAS-group \
--target-cpu-utilization $TARGET_UTILIZATION \
--cool-down-period $COOL_DOWN_PERIOD \
--max-num-replicas $MAX_INSTANCES
# FORWARDING RULE
gcloud compute forwarding-rules create $ALIAS-forwarding-rule \
--target-pool $ALIAS-pool \
--region $REGION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment