Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- set -o errexit
- set -o nounset
- set -o pipefail
- DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
- PROJECT=$(gcloud config list --format 'value(core.project)')
- CLUSTER=$(gcloud config list --format 'value(container.cluster)')
- ZONE=$(gcloud config list --format 'value(compute.zone)')
- GCE_IMAGE=${CLUSTER}-ubuntu-1604
- GCE_IMAGE_FAMILY=ubuntu-1604-lts
- BASE_INSTANCE=${CLUSTER}-foo-base-image
- function get-base-image-name {
- gcloud compute images list --filter ${GCE_IMAGE_FAMILY} | awk '{if(NR>1)print $1}'
- }
- function get-base-image-project {
- gcloud compute images list --filter ${GCE_IMAGE_FAMILY} | awk '{if(NR>1)print $2}'
- }
- function get-foo-image-name {
- gcloud compute images list --filter foo --format json | jq -r ".[0].name"
- }
- create-base-instance() {
- echo "Creating instance ${BASE_INSTANCE}..."
- local base_image=$(get-base-image-name)
- local base_image_project=$(get-base-image-project)
- gcloud compute instances create ${BASE_INSTANCE} \
- --image ${base_image} \
- --image-project ${base_image_project}
- }
- stop-base-instance() {
- echo "Stopping instance ${BASE_INSTANCE}..."
- gcloud compute instances stop ${BASE_INSTANCE} --quiet
- }
- delete-base-instance() {
- echo "Deleting instance ${BASE_INSTANCE}..."
- gcloud compute instances delete ${BASE_INSTANCE} --quiet
- }
- create-base-image() {
- echo "Creating base image ${GCE_IMAGE}..."
- gcloud compute images create ${GCE_IMAGE} \
- --source-disk ${BASE_INSTANCE} \
- --family ${GCE_IMAGE_FAMILY}
- }
- apply-configure-script() {
- echo "Applying configure script to ${BASE_INSTANCE}..."
- gcloud compute scp $DIR/configure-image.sh ${BASE_INSTANCE}: --quiet
- gcloud compute ssh ${BASE_INSTANCE} --command "sudo ./configure-image.sh &> configure-image.log" --quiet
- }
- echo
- echo "========================================"
- echo "create base OS image"
- echo "========================================"
- echo
- foo_image=$(get-foo-image-name)
- if [ "${foo_image}" != "null" ]; then
- echo "Found image ${foo_image} ... skipping"
- echo
- exit 0
- fi
- create-base-instance
- apply-configure-script
- stop-base-instance
- create-base-image
- delete-base-instance
Add Comment
Please, Sign In to add comment