Skip to content

Instantly share code, notes, and snippets.

@hakobera
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakobera/b61423854bc83fb9f3bc to your computer and use it in GitHub Desktop.
Save hakobera/b61423854bc83fb9f3bc to your computer and use it in GitHub Desktop.
Shell function for Google Container Engine
#######################################################
# Google Container Engine helper functions
#
# Prerequisities
# - gcloud
# - jq
#######################################################
function create_network() {
network=$1
echo "Create network: $network"
gcloud compute networks create $network --range 10.241.0.0/16
add_firewall_rule $network ssh 22 0.0.0.0/0
add_firewall_rule $network http 80 0.0.0.0/0
}
function delete_network() {
network=$1
echo "Delete network: $network"
gcloud compute networks describe $network
if [ "$?" != "0" ]; then
return 1
fi
echo ""
echo "Delete following firewall rules used by $network"
gcloud compute firewall-rules list --regexp "^$network-.*$"
echo ""
for rule in $(gcloud compute firewall-rules list --regexp "^$network-.*$" --format json | jq -r ".[].name" | tr '\n' ' ')
do
gcloud compute firewall-rules delete $rule
echo ""
done
gcloud compute networks delete $network
}
function add_firewall_rule() {
network=$1
name=$2
port=$3
range=$4
echo "Add firewall rule: $1-allow-$name"
gcloud compute firewall-rules create $1-allow-$name \
--allow tcp:$3 \
--network $1 \
--source-ranges $4
echo ""
}
function create_cluster() {
cluster=$1
zone=$2
network=$3
num_nodes=${4:-3}
machine_type=${5:-n1-standard-1}
echo "Create cluster: $cluster"
gcloud preview container clusters create $cluster \
--zone $zone \
--network $network \
--num-nodes $num_nodes \
--machine-type $machine_type \
--no-set-default
}
function delete_cluster() {
cluster=$1
zone=$2
echo "Delete cluster: $cluster"
gcloud preview container clusters describe $cluster --zone $zone
if [ "$?" != "0" ]; then
return 1
fi
gcloud preview container clusters delete $cluster --zone $zone
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment