util.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env bash
  2. # Copyright 2017 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../../..
  16. source "${KUBE_ROOT}/test/kubemark/common/util.sh"
  17. # Wrapper for gcloud compute, running it $RETRIES times in case of failures.
  18. # Args:
  19. # $@: all stuff that goes after 'gcloud compute'
  20. function run-gcloud-compute-with-retries {
  21. run-cmd-with-retries gcloud compute "$@"
  22. }
  23. function authenticate-docker {
  24. echo "Configuring registry authentication"
  25. mkdir -p "${HOME}/.docker"
  26. gcloud beta auth configure-docker -q
  27. }
  28. # This function isn't too robust to race, but that should be ok given its one-off usage during setup.
  29. function get-or-create-master-ip {
  30. MASTER_IP=$(gcloud compute addresses describe "${MASTER_NAME}-ip" \
  31. --project "${PROJECT}" --region "${REGION}" -q --format='value(address)') 2>/dev/null || true
  32. if [[ -z "${MASTER_IP:-}" ]]; then
  33. run-gcloud-compute-with-retries addresses create "${MASTER_NAME}-ip" \
  34. --project "${PROJECT}" \
  35. --region "${REGION}" -q
  36. MASTER_IP=$(gcloud compute addresses describe "${MASTER_NAME}-ip" \
  37. --project "${PROJECT}" --region "${REGION}" -q --format='value(address)')
  38. fi
  39. }
  40. function create-master-instance-with-resources {
  41. GCLOUD_COMMON_ARGS=(--project "${PROJECT}" --zone "${ZONE}")
  42. # Override the master image project to cos-cloud for COS images staring with `cos` string prefix.
  43. DEFAULT_GCI_PROJECT=google-containers
  44. if [[ "${GCI_VERSION}" == "cos"* ]]; then
  45. DEFAULT_GCI_PROJECT=cos-cloud
  46. fi
  47. MASTER_IMAGE_PROJECT=${KUBE_GCE_MASTER_PROJECT:-${DEFAULT_GCI_PROJECT}}
  48. run-gcloud-compute-with-retries disks create "${MASTER_NAME}-pd" \
  49. "${GCLOUD_COMMON_ARGS[@]}" \
  50. --type "${MASTER_DISK_TYPE}" \
  51. --size "${MASTER_DISK_SIZE}" &
  52. if [ "${EVENT_PD:-}" == "true" ]; then
  53. run-gcloud-compute-with-retries disks create "${MASTER_NAME}-event-pd" \
  54. "${GCLOUD_COMMON_ARGS[@]}" \
  55. --type "${MASTER_DISK_TYPE}" \
  56. --size "${MASTER_DISK_SIZE}" &
  57. fi
  58. get-or-create-master-ip &
  59. wait
  60. run-gcloud-compute-with-retries instances create "${MASTER_NAME}" \
  61. "${GCLOUD_COMMON_ARGS[@]}" \
  62. --address "${MASTER_IP}" \
  63. --machine-type "${MASTER_SIZE}" \
  64. --image-project="${MASTER_IMAGE_PROJECT}" \
  65. --image "${MASTER_IMAGE}" \
  66. --tags "${MASTER_TAG}" \
  67. --subnet "${SUBNETWORK:-${NETWORK}}" \
  68. --scopes "storage-ro,logging-write" \
  69. --boot-disk-size "${MASTER_ROOT_DISK_SIZE}" \
  70. --disk "name=${MASTER_NAME}-pd,device-name=master-pd,mode=rw,boot=no,auto-delete=no"
  71. run-gcloud-compute-with-retries instances add-metadata "${MASTER_NAME}" \
  72. "${GCLOUD_COMMON_ARGS[@]}" \
  73. --metadata-from-file startup-script="${KUBE_ROOT}/test/kubemark/resources/start-kubemark-master.sh" &
  74. if [ "${EVENT_PD:-}" == "true" ]; then
  75. echo "Attaching ${MASTER_NAME}-event-pd to ${MASTER_NAME}"
  76. run-gcloud-compute-with-retries instances attach-disk "${MASTER_NAME}" \
  77. "${GCLOUD_COMMON_ARGS[@]}" \
  78. --disk "${MASTER_NAME}-event-pd" \
  79. --device-name="master-event-pd" &
  80. fi
  81. run-gcloud-compute-with-retries firewall-rules create "${MASTER_NAME}-https" \
  82. --project "${PROJECT}" \
  83. --network "${NETWORK}" \
  84. --source-ranges "0.0.0.0/0" \
  85. --target-tags "${MASTER_TAG}" \
  86. --allow "tcp:443" &
  87. run-gcloud-compute-with-retries firewall-rules create "${MASTER_NAME}-internal" \
  88. --project "${PROJECT}" \
  89. --network "${NETWORK}" \
  90. --source-ranges "10.0.0.0/8" \
  91. --target-tags "${MASTER_TAG}" \
  92. --allow "tcp:1-2379,tcp:2382-65535,udp:1-65535,icmp" &
  93. wait
  94. }
  95. # Command to be executed is '$1'.
  96. # No. of retries is '$2' (if provided) or 1 (default).
  97. function execute-cmd-on-master-with-retries() {
  98. RETRIES="${2:-1}" run-gcloud-compute-with-retries ssh "${MASTER_NAME}" --zone="${ZONE}" --project="${PROJECT}" --command="$1"
  99. }
  100. function copy-files() {
  101. run-gcloud-compute-with-retries scp --recurse --zone="${ZONE}" --project="${PROJECT}" "$@"
  102. }
  103. function delete-master-instance-and-resources {
  104. GCLOUD_COMMON_ARGS=(--project "${PROJECT}" --zone "${ZONE}" --quiet)
  105. gcloud compute instances delete "${MASTER_NAME}" \
  106. "${GCLOUD_COMMON_ARGS[@]}" || true
  107. gcloud compute disks delete "${MASTER_NAME}-pd" \
  108. "${GCLOUD_COMMON_ARGS[@]}" || true
  109. gcloud compute disks delete "${MASTER_NAME}-event-pd" \
  110. "${GCLOUD_COMMON_ARGS[@]}" &> /dev/null || true
  111. gcloud compute addresses delete "${MASTER_NAME}-ip" \
  112. --project "${PROJECT}" \
  113. --region "${REGION}" \
  114. --quiet || true
  115. gcloud compute firewall-rules delete "${MASTER_NAME}-https" \
  116. --project "${PROJECT}" \
  117. --quiet || true
  118. gcloud compute firewall-rules delete "${MASTER_NAME}-internal" \
  119. --project "${PROJECT}" \
  120. --quiet || true
  121. if [ "${SEPARATE_EVENT_MACHINE:-false}" == "true" ]; then
  122. gcloud compute instances delete "${EVENT_STORE_NAME}" \
  123. "${GCLOUD_COMMON_ARGS[@]}" || true
  124. gcloud compute disks delete "${EVENT_STORE_NAME}-pd" \
  125. "${GCLOUD_COMMON_ARGS[@]}" || true
  126. fi
  127. }