gubernator.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/env bash
  2. # Copyright 2016 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. # Make bucket and a folder for e2e-node test logs.
  16. # Populate the folder from the logs stored in /tmp/_artifacts/ in the same way as a
  17. # jenkins build would, and then print the URL to view the test results on Gubernator
  18. set -o errexit
  19. set -o nounset
  20. set -o pipefail
  21. source hack/lib/logging.sh
  22. if [[ $# -eq 0 || ! $1 =~ ^[Yy]$ ]]; then
  23. read -p "Do you want to run gubernator.sh and upload logs publicly to GCS? [y/n]" yn
  24. echo
  25. if [[ ! $yn =~ ^[Yy]$ ]]; then
  26. exit 1
  27. fi
  28. fi
  29. # Check that user has gsutil
  30. if [[ $(which gsutil) == "" ]]; then
  31. echo "Could not find gsutil when running \`which gsutil\`"
  32. exit 1
  33. fi
  34. # Check that user has gcloud
  35. if [[ $(which gcloud) == "" ]]; then
  36. echo "Could not find gcloud when running: \`which gcloud\`"
  37. exit 1
  38. fi
  39. # Check that user has Credentialed Active account
  40. if ! gcloud auth list | grep -q "ACTIVE"; then
  41. echo "Could not find active account when running: \`gcloud auth list\`"
  42. exit 1
  43. fi
  44. readonly gcs_acl="public-read"
  45. bucket_name="${USER}-g8r-logs"
  46. echo ""
  47. V=2 kube::log::status "Using bucket ${bucket_name}"
  48. # Check if the bucket exists
  49. if ! gsutil ls gs:// | grep -q "gs://${bucket_name}/"; then
  50. V=2 kube::log::status "Creating public bucket ${bucket_name}"
  51. gsutil mb gs://${bucket_name}/
  52. # Make all files in the bucket publicly readable
  53. gsutil acl ch -u AllUsers:R gs://${bucket_name}
  54. else
  55. V=2 kube::log::status "Bucket already exists"
  56. fi
  57. # Path for e2e-node test results
  58. GCS_JOBS_PATH="gs://${bucket_name}/logs/e2e-node"
  59. ARTIFACTS=${ARTIFACTS:-"/tmp/_artifacts"}
  60. BUILD_LOG_PATH="${ARTIFACTS}/build-log.txt"
  61. if [[ ! -e $BUILD_LOG_PATH ]]; then
  62. echo "Could not find build-log.txt at ${BUILD_LOG_PATH}"
  63. exit 1
  64. fi
  65. # Get start and end timestamps based on build-log.txt file contents
  66. # Line where the actual tests start
  67. start_line=$(grep -n -m 1 "^=" ${BUILD_LOG_PATH} | sed 's/\([0-9]*\).*/\1/')
  68. # Create text file starting where the tests start
  69. after_start=$(tail -n +${start_line} ${BUILD_LOG_PATH})
  70. echo "${after_start}" >> build-log-cut.txt
  71. # Match the first timestamp
  72. start_time_raw=$(grep -m 1 -o '[0-9][0-9][0-9][0-9][[:blank:]][0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]*' build-log-cut.txt)
  73. rm build-log-cut.txt
  74. # Make the date readable by date command (ex: 0101 00:00:00.000 -> 01/01 00:00:00.000)
  75. start_time=$(echo ${start_time_raw} | sed 's/^.\{2\}/&\//')
  76. V=2 kube::log::status "Started at ${start_time}"
  77. # Match the last timestamp in the build-log file
  78. end_time=$(grep -o '[0-9][0-9][0-9][0-9][[:blank:]][0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]*' ${BUILD_LOG_PATH} | tail -1 | sed 's/^.\{2\}/&\//')
  79. # Convert to epoch time for Gubernator
  80. start_time_epoch=$(date -d "${start_time}" +%s)
  81. end_time_epoch=$(date -d "${end_time}" +%s)
  82. # Make folder name for build from timestamp
  83. BUILD_STAMP=$(echo $start_time | sed 's/\///' | sed 's/ /_/')
  84. GCS_LOGS_PATH="${GCS_JOBS_PATH}/${BUILD_STAMP}"
  85. # Check if folder for same logs already exists
  86. if gsutil ls "${GCS_JOBS_PATH}" | grep -q "${BUILD_STAMP}"; then
  87. V=2 kube::log::status "Log files already uploaded"
  88. echo "Gubernator linked below:"
  89. echo "k8s-gubernator.appspot.com/build/${GCS_LOGS_PATH}?local=on"
  90. exit
  91. fi
  92. for result in $(find ${ARTIFACTS} -type d -name "results"); do
  93. if [[ $result != "" && $result != "${ARTIFACTS}/results" && $result != $ARTIFACTS ]]; then
  94. mv $result/* $ARTIFACTS
  95. fi
  96. done
  97. # Upload log files
  98. for upload_attempt in $(seq 3); do
  99. if [[ -d "${ARTIFACTS}" && -n $(ls -A "${ARTIFACTS}") ]]; then
  100. V=2 kube::log::status "Uploading artifacts"
  101. gsutil -m -q -o "GSUtil:use_magicfile=True" cp -a "${gcs_acl}" -r -c \
  102. -z log,xml,json "${ARTIFACTS}" "${GCS_LOGS_PATH}/artifacts" || continue
  103. fi
  104. break
  105. done
  106. for upload_attempt in $(seq 3); do
  107. if [[ -e "${BUILD_LOG_PATH}" ]]; then
  108. V=2 kube::log::status "Uploading build log"
  109. gsutil -q cp -Z -a "${gcs_acl}" "${BUILD_LOG_PATH}" "${GCS_LOGS_PATH}" || continue
  110. fi
  111. break
  112. done
  113. # Find the k8s version for started.json
  114. version=""
  115. if [[ -e "version" ]]; then
  116. version=$(cat "version")
  117. elif [[ -e "hack/lib/version.sh" ]]; then
  118. export KUBE_ROOT="."
  119. source "hack/lib/version.sh"
  120. kube::version::get_version_vars
  121. version="${KUBE_GIT_VERSION-}"
  122. fi
  123. if [[ -n "${version}" ]]; then
  124. V=2 kube::log::status "Found Kubernetes version: ${version}"
  125. else
  126. V=2 kube::log::status "Could not find Kubernetes version"
  127. fi
  128. #Find build result from build-log.txt
  129. if grep -Fxq "Test Suite Passed" "${BUILD_LOG_PATH}"
  130. then
  131. build_result="SUCCESS"
  132. else
  133. build_result="FAILURE"
  134. fi
  135. V=4 kube::log::status "Build result is ${build_result}"
  136. if [[ -e "${ARTIFACTS}/started.json" ]]; then
  137. rm "${ARTIFACTS}/started.json"
  138. fi
  139. if [[ -e "${ARTIFACTS}/finished.json" ]]; then
  140. rm "${ARTIFACTS}/finished.json"
  141. fi
  142. V=2 kube::log::status "Constructing started.json and finished.json files"
  143. echo "{" >> "${ARTIFACTS}/started.json"
  144. echo " \"version\": \"${version}\"," >> "${ARTIFACTS}/started.json"
  145. echo " \"timestamp\": ${start_time_epoch}," >> "${ARTIFACTS}/started.json"
  146. echo " \"jenkins-node\": \"${NODE_NAME:-}\"" >> "${ARTIFACTS}/started.json"
  147. echo "}" >> "${ARTIFACTS}/started.json"
  148. echo "{" >> "${ARTIFACTS}/finished.json"
  149. echo " \"result\": \"${build_result}\"," >> "${ARTIFACTS}/finished.json"
  150. echo " \"timestamp\": ${end_time_epoch}" >> "${ARTIFACTS}/finished.json"
  151. echo "}" >> "${ARTIFACTS}/finished.json"
  152. # Upload started.json
  153. V=2 kube::log::status "Uploading started.json and finished.json"
  154. V=2 kube::log::status "Run started at ${start_time}"
  155. json_file="${GCS_LOGS_PATH}/started.json"
  156. for upload_attempt in $(seq 3); do
  157. V=2 kube::log::status "Uploading started.json to ${json_file} (attempt ${upload_attempt})"
  158. gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" "${ARTIFACTS}/started.json" \
  159. "${json_file}" || continue
  160. break
  161. done
  162. # Upload finished.json
  163. for upload_attempt in $(seq 3); do
  164. V=2 kube::log::status "Uploading finished.json to ${GCS_LOGS_PATH} (attempt ${upload_attempt})"
  165. gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" "${ARTIFACTS}/finished.json" \
  166. "${GCS_LOGS_PATH}/finished.json" || continue
  167. break
  168. done
  169. echo "Gubernator linked below:"
  170. echo "k8s-gubernator.appspot.com/build/${bucket_name}/logs/e2e-node/${BUILD_STAMP}"