setup-prereq.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/bin/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. # This sets up a Photon Controller with the tenant, project, flavors
  16. # and image that are needed to deploy Kubernetes with kube-up.
  17. #
  18. # This is not meant to be used in production: it creates resource tickets
  19. # (quotas) that are arbitrary and not likely to work in your environment.
  20. # However, it may be a quick way to get your environment set up to try out
  21. # a Kubernetes installation.
  22. #
  23. # It uses the names for the tenant, project, and flavors as specified in the
  24. # config-common.sh file
  25. #
  26. # If you want to do this by hand, this script is equivalent to the following
  27. # Photon Controller commands (assuming you haven't edited config-common.sh
  28. # to change the names)
  29. #
  30. # photon target set https://192.0.2.2
  31. # photon tenant create kube-tenant
  32. # photon tenant set kube-tenant
  33. # photon resource-ticket create --tenant kube-tenant --name kube-resources --limits "vm.memory 1000 GB, vm 1000 COUNT"
  34. # photon project create --tenant kube-tenant --resource-ticket kube-resources --name kube-project --limits "vm.memory 1000 GB, vm 1000 COUNT"
  35. # photon project set kube-project
  36. # photon -n flavor create --name "kube-vm" --kind "vm" --cost "vm 1 COUNT, vm.cpu 1 COUNT, vm.memory 2 GB"
  37. # photon -n flavor create --name "kube-disk" --kind "ephemeral-disk" --cost "ephemeral-disk 1 COUNT"
  38. # photon image create kube.vmdk -n kube-image -i EAGER
  39. #
  40. # Note that the kube.vmdk can be downloaded as specified in the documentation.
  41. set -o errexit
  42. set -o nounset
  43. set -o pipefail
  44. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
  45. # shellcheck source=./util.sh
  46. source "${KUBE_ROOT}/cluster/photon-controller/util.sh"
  47. function main {
  48. verify-cmd-in-path photon
  49. set-target
  50. create-tenant
  51. create-project
  52. create-vm-flavor "${PHOTON_MASTER_FLAVOR}" "${SETUP_MASTER_FLAVOR_SPEC}"
  53. if [ "${PHOTON_MASTER_FLAVOR}" != "${PHOTON_NODE_FLAVOR}" ]; then
  54. create-vm-flavor "${PHOTON_NODE_FLAVOR}" "${SETUP_NODE_FLAVOR_SPEC}"
  55. fi
  56. create-disk-flavor
  57. create-image
  58. }
  59. function parse-cmd-line {
  60. PHOTON_TARGET=${1:-""}
  61. PHOTON_VMDK=${2:-""}
  62. if [[ "${PHOTON_TARGET}" = "" || "${PHOTON_VMDK}" = "" ]]; then
  63. echo "Usage: setup-prereq <photon target> <path-to-kube-vmdk>"
  64. echo "Target should be a URL like https://192.0.2.1"
  65. echo ""
  66. echo "This will create the following, based on the configuration in config-common.sh"
  67. echo " * A tenant named ${PHOTON_TENANT}"
  68. echo " * A project named ${PHOTON_PROJECT}"
  69. echo " * A VM flavor named ${PHOTON_MASTER_FLAVOR}"
  70. echo " * A disk flavor named ${PHOTON_DISK_FLAVOR}"
  71. echo "It will also upload the Kube VMDK"
  72. echo ""
  73. echo "It creates the tenant with a resource ticket (quota) that may"
  74. echo "be inappropriate for your environment. For a production"
  75. echo "environment, you should configure these to match your"
  76. echo "environment."
  77. exit 1
  78. fi
  79. echo "Photon Target: ${PHOTON_TARGET}"
  80. echo "Photon VMDK: ${PHOTON_VMDK}"
  81. }
  82. function set-target {
  83. ${PHOTON} target set "${PHOTON_TARGET}" > /dev/null 2>&1
  84. }
  85. function create-tenant {
  86. local rc=0
  87. local output
  88. ${PHOTON} tenant list | grep -q "\t${PHOTON_TENANT}$" > /dev/null 2>&1 || rc=$?
  89. if [[ ${rc} -eq 0 ]]; then
  90. echo "Tenant ${PHOTON_TENANT} already made, skipping"
  91. else
  92. echo "Making tenant ${PHOTON_TENANT}"
  93. rc=0
  94. output=$(${PHOTON} tenant create "${PHOTON_TENANT}" 2>&1) || {
  95. echo "ERROR: Could not create tenant \"${PHOTON_TENANT}\", exiting"
  96. echo "Output from tenant creation:"
  97. echo "${output}"
  98. exit 1
  99. }
  100. fi
  101. ${PHOTON} tenant set "${PHOTON_TENANT}" > /dev/null 2>&1
  102. }
  103. function create-project {
  104. local rc=0
  105. local output
  106. ${PHOTON} project list | grep -q "\t${PHOTON_PROJECT}\t" > /dev/null 2>&1 || rc=$?
  107. if [[ ${rc} -eq 0 ]]; then
  108. echo "Project ${PHOTON_PROJECT} already made, skipping"
  109. else
  110. echo "Making project ${PHOTON_PROJECT}"
  111. rc=0
  112. output=$(${PHOTON} resource-ticket create --tenant "${PHOTON_TENANT}" --name "${PHOTON_TENANT}-resources" --limits "${SETUP_TICKET_SPEC}" 2>&1) || {
  113. echo "ERROR: Could not create resource ticket, exiting"
  114. echo "Output from resource ticket creation:"
  115. echo "${output}"
  116. exit 1
  117. }
  118. rc=0
  119. output=$(${PHOTON} project create --tenant "${PHOTON_TENANT}" --resource-ticket "${PHOTON_TENANT}-resources" --name "${PHOTON_PROJECT}" --limits "${SETUP_PROJECT_SPEC}" 2>&1) || {
  120. echo "ERROR: Could not create project \"${PHOTON_PROJECT}\", exiting"
  121. echo "Output from project creation:"
  122. echo "${output}"
  123. exit 1
  124. }
  125. fi
  126. ${PHOTON} project set "${PHOTON_PROJECT}"
  127. }
  128. function create-vm-flavor {
  129. local flavor_name=${1}
  130. local flavor_spec=${2}
  131. local rc=0
  132. local output
  133. ${PHOTON} flavor list | grep -q "\t${flavor_name}\t" > /dev/null 2>&1 || rc=$?
  134. if [[ ${rc} -eq 0 ]]; then
  135. check-flavor-ready "${flavor_name}"
  136. echo "Flavor ${flavor_name} already made, skipping"
  137. else
  138. echo "Making VM flavor ${flavor_name}"
  139. rc=0
  140. output=$(${PHOTON} -n flavor create --name "${flavor_name}" --kind "vm" --cost "${flavor_spec}" 2>&1) || {
  141. echo "ERROR: Could not create vm flavor \"${flavor_name}\", exiting"
  142. echo "Output from flavor creation:"
  143. echo "${output}"
  144. exit 1
  145. }
  146. fi
  147. }
  148. function create-disk-flavor {
  149. local rc=0
  150. local output
  151. ${PHOTON} flavor list | grep -q "\t${PHOTON_DISK_FLAVOR}\t" > /dev/null 2>&1 || rc=$?
  152. if [[ ${rc} -eq 0 ]]; then
  153. check-flavor-ready "${PHOTON_DISK_FLAVOR}"
  154. echo "Flavor ${PHOTON_DISK_FLAVOR} already made, skipping"
  155. else
  156. echo "Making disk flavor ${PHOTON_DISK_FLAVOR}"
  157. rc=0
  158. output=$(${PHOTON} -n flavor create --name "${PHOTON_DISK_FLAVOR}" --kind "ephemeral-disk" --cost "${SETUP_DISK_FLAVOR_SPEC}" 2>&1) || {
  159. echo "ERROR: Could not create disk flavor \"${PHOTON_DISK_FLAVOR}\", exiting"
  160. echo "Output from flavor creation:"
  161. echo "${output}"
  162. exit 1
  163. }
  164. fi
  165. }
  166. function check-flavor-ready {
  167. local flavor_name=${1}
  168. local rc=0
  169. local flavor_id
  170. flavor_id=$(${PHOTON} flavor list | grep "\t${flavor_name}\t" | awk '{print $1}') || {
  171. echo "ERROR: Found ${flavor_name} but cannot find it's id"
  172. exit 1
  173. }
  174. ${PHOTON} flavor show "${flavor_id}" | grep "\tREADY\$" > /dev/null 2>&1 || {
  175. echo "ERROR: Flavor \"${flavor_name}\" already exists but is not READY. Please delete or fix it."
  176. exit 1
  177. }
  178. }
  179. function create-image {
  180. local rc=0
  181. local num_images
  182. local output
  183. ${PHOTON} image list | grep "\t${PHOTON_IMAGE}\t" | grep -q ERROR > /dev/null 2>&1 || rc=$?
  184. if [[ ${rc} -eq 0 ]]; then
  185. echo "Warning: You have at least one ${PHOTON_IMAGE} image in the ERROR state. You may want to investigate."
  186. echo "Images in the ERROR state will be ignored."
  187. fi
  188. rc=0
  189. # We don't use grep -c because it exists non-zero when there are no matches, tell shellcheck
  190. # shellcheck disable=SC2126
  191. num_images=$(${PHOTON} image list | grep "\t${PHOTON_IMAGE}\t" | grep READY | wc -l)
  192. if [[ "${num_images}" -gt 1 ]]; then
  193. echo "Warning: You have more than one good ${PHOTON_IMAGE} image. You may want to remove duplicates."
  194. fi
  195. ${PHOTON} image list | grep "\t${PHOTON_IMAGE}\t" | grep -q READY > /dev/null 2>&1 || rc=$?
  196. if [[ ${rc} -eq 0 ]]; then
  197. echo "Image ${PHOTON_VMDK} already uploaded, skipping"
  198. else
  199. echo "Uploading image ${PHOTON_VMDK}"
  200. rc=0
  201. output=$(${PHOTON} image create "${PHOTON_VMDK}" -n "${PHOTON_IMAGE}" -i EAGER 2>&1) || {
  202. echo "ERROR: Could not upload image, exiting"
  203. echo "Output from image create:"
  204. echo "${output}"
  205. exit 1
  206. }
  207. fi
  208. }
  209. # We don't want silent pipeline failure: we check for failure
  210. set +o pipefail
  211. parse-cmd-line "$@"
  212. main