System configuration for OpenStack Infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

install_modules.sh 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/bash
  2. # Copyright 2014 OpenStack Foundation.
  3. # Copyright 2014 Hewlett-Packard Development Company, L.P.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. function puppet_version {
  17. # Default to 3 for the cases, like bridge, where there is no puppet
  18. (PATH=/opt/puppetlabs/bin:$PATH puppet --version || echo 3) | cut -d '.' -f 1
  19. }
  20. export PUPPET_VERSION=$(puppet_version)
  21. if [ "$PUPPET_VERSION" == "3" ] ; then
  22. export MODULE_PATH=/etc/puppet/modules
  23. elif [ "$PUPPET_VERSION" == "4" ] || [ "$PUPPET_VERSION" == "5" ]; then
  24. # Using puppetlabs builds for wider compatability across distros
  25. # than system packages provide.
  26. export MODULE_PATH=/etc/puppetlabs/code/modules
  27. else
  28. echo "ERROR: unsupported puppet version $PUPPET_VERSION"
  29. exit 1
  30. fi
  31. SCRIPT_NAME=$(basename $0)
  32. SCRIPT_DIR=$(readlink -f "$(dirname $0)")
  33. JUST_CLONED=0
  34. function remove_module {
  35. local SHORT_MODULE_NAME=$1
  36. if [ -n "$SHORT_MODULE_NAME" ]; then
  37. rm -Rf "$MODULE_PATH/$SHORT_MODULE_NAME"
  38. else
  39. echo "ERROR: remove_module requires a SHORT_MODULE_NAME."
  40. fi
  41. }
  42. function git_clone {
  43. local MOD=$1
  44. local DEST=$2
  45. JUST_CLONED=1
  46. for attempt in $(seq 0 3); do
  47. clone_error=0
  48. git clone $MOD $DEST && break || true
  49. rm -rf $DEST
  50. clone_error=1
  51. done
  52. return $clone_error
  53. }
  54. # Array of modules to be installed key:value is module:version.
  55. declare -A MODULES
  56. # Array of modues to be installed from source and without dependency resolution.
  57. # key:value is source location, revision to checkout
  58. declare -A SOURCE_MODULES
  59. # Array of modues to be installed from source and without dependency resolution from openstack git
  60. # key:value is source location, revision to checkout
  61. declare -A INTEGRATION_MODULES
  62. # load modules.env to populate MODULES[*] and SOURCE_MODULES[*]
  63. # for processing.
  64. MODULE_ENV_FILE=${MODULE_FILE:-modules.env}
  65. MODULE_ENV_PATH=${MODULE_ENV_PATH:-${SCRIPT_DIR}}
  66. if [ -f "${MODULE_ENV_PATH}/${MODULE_ENV_FILE}" ] ; then
  67. . "${MODULE_ENV_PATH}/${MODULE_ENV_FILE}"
  68. fi
  69. if [ -z "${!MODULES[*]}" ] && [ -z "${!SOURCE_MODULES[*]}" ] ; then
  70. echo ""
  71. echo "WARNING: nothing to do, unable to find MODULES or SOURCE_MODULES"
  72. echo " export options, try setting MODULE_ENV_PATH or MODULE_ENV_FILE"
  73. echo " export to the proper location of modules.env file."
  74. echo ""
  75. exit 0
  76. fi
  77. MODULE_LIST=$(find $MODULE_PATH -maxdepth 1 -mindepth 1 -printf '%f\n')
  78. # Install modules from source
  79. for MOD in ${!SOURCE_MODULES[*]} ; do
  80. JUST_CLONED=0
  81. # get the name of the module directory
  82. if [ `echo $MOD | awk -F. '{print $NF}'` = 'git' ]; then
  83. echo "Remote repos of the form repo.git are not supported: ${MOD}"
  84. exit 1
  85. fi
  86. MODULE_NAME=`echo $MOD | awk -F- '{print $NF}'`
  87. # set up git base command to use the correct path
  88. GIT_CMD_BASE="git --git-dir=${MODULE_PATH}/${MODULE_NAME}/.git --work-tree ${MODULE_PATH}/${MODULE_NAME}"
  89. # treat any occurrence of the module as a match
  90. if ! echo $MODULE_LIST | grep "${MODULE_NAME}" >/dev/null 2>&1; then
  91. # clone modules that are not installed
  92. git_clone $MOD "${MODULE_PATH}/${MODULE_NAME}"
  93. else
  94. if [ ! -d ${MODULE_PATH}/${MODULE_NAME}/.git ]; then
  95. echo "Found directory ${MODULE_PATH}/${MODULE_NAME} that is not a git repo, deleting it and reinstalling from source"
  96. remove_module $MODULE_NAME
  97. git_clone $MOD "${MODULE_PATH}/${MODULE_NAME}"
  98. elif [ `${GIT_CMD_BASE} remote show origin | grep 'Fetch URL' | awk -F'URL: ' '{print $2}'` != $MOD ]; then
  99. echo "Found remote in ${MODULE_PATH}/${MODULE_NAME} that does not match desired remote ${MOD}, deleting dir and re-cloning"
  100. remove_module $MODULE_NAME
  101. git_clone $MOD "${MODULE_PATH}/${MODULE_NAME}"
  102. fi
  103. fi
  104. # fetch the latest refs from the repo
  105. if [[ $JUST_CLONED -eq 0 ]] ; then
  106. # If we just cloned the repo, we do not need to remote update
  107. for attempt in $(seq 0 3); do
  108. clone_error=0
  109. $GIT_CMD_BASE remote update && break || true
  110. clone_error=1
  111. done
  112. if [[ $clone_error -ne 0 ]] ; then
  113. exit $clone_error
  114. fi
  115. fi
  116. # make sure the correct revision is installed, I have to use rev-list b/c rev-parse does not work with tags
  117. if [ `${GIT_CMD_BASE} rev-list HEAD --max-count=1` != `${GIT_CMD_BASE} rev-list ${SOURCE_MODULES[$MOD]} --max-count=1` ]; then
  118. # checkout correct revision
  119. $GIT_CMD_BASE checkout ${SOURCE_MODULES[$MOD]}
  120. fi
  121. done