Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -o errexit
  4. set -o nounset
  5. set -o pipefail
  6.  
  7. DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  8.  
  9. PROJECT=$(gcloud config list --format 'value(core.project)')
  10. CLUSTER=$(gcloud config list --format 'value(container.cluster)')
  11. ZONE=$(gcloud config list --format 'value(compute.zone)')
  12.  
  13. GCE_IMAGE=${CLUSTER}-ubuntu-1604
  14. GCE_IMAGE_FAMILY=ubuntu-1604-lts
  15.  
  16. BASE_INSTANCE=${CLUSTER}-foo-base-image
  17.  
  18. function get-base-image-name {
  19. gcloud compute images list --filter ${GCE_IMAGE_FAMILY} | awk '{if(NR>1)print $1}'
  20. }
  21.  
  22. function get-base-image-project {
  23. gcloud compute images list --filter ${GCE_IMAGE_FAMILY} | awk '{if(NR>1)print $2}'
  24. }
  25.  
  26. function get-foo-image-name {
  27. gcloud compute images list --filter foo --format json | jq -r ".[0].name"
  28. }
  29.  
  30. create-base-instance() {
  31. echo "Creating instance ${BASE_INSTANCE}..."
  32. local base_image=$(get-base-image-name)
  33. local base_image_project=$(get-base-image-project)
  34. gcloud compute instances create ${BASE_INSTANCE} \
  35. --image ${base_image} \
  36. --image-project ${base_image_project}
  37. }
  38.  
  39. stop-base-instance() {
  40. echo "Stopping instance ${BASE_INSTANCE}..."
  41. gcloud compute instances stop ${BASE_INSTANCE} --quiet
  42. }
  43.  
  44. delete-base-instance() {
  45. echo "Deleting instance ${BASE_INSTANCE}..."
  46. gcloud compute instances delete ${BASE_INSTANCE} --quiet
  47. }
  48.  
  49. create-base-image() {
  50. echo "Creating base image ${GCE_IMAGE}..."
  51. gcloud compute images create ${GCE_IMAGE} \
  52. --source-disk ${BASE_INSTANCE} \
  53. --family ${GCE_IMAGE_FAMILY}
  54. }
  55.  
  56. apply-configure-script() {
  57. echo "Applying configure script to ${BASE_INSTANCE}..."
  58. gcloud compute scp $DIR/configure-image.sh ${BASE_INSTANCE}: --quiet
  59. gcloud compute ssh ${BASE_INSTANCE} --command "sudo ./configure-image.sh &> configure-image.log" --quiet
  60. }
  61.  
  62. echo
  63. echo "========================================"
  64. echo "create base OS image"
  65. echo "========================================"
  66. echo
  67.  
  68. foo_image=$(get-foo-image-name)
  69. if [ "${foo_image}" != "null" ]; then
  70. echo "Found image ${foo_image} ... skipping"
  71. echo
  72. exit 0
  73. fi
  74.  
  75. create-base-instance
  76. apply-configure-script
  77. stop-base-instance
  78. create-base-image
  79. delete-base-instance
Add Comment
Please, Sign In to add comment