Add initial puppet and hiera files
[releng.git] / prototypes / puppet-infracloud / install_modules.sh
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
17 MODULE_PATH=`puppet config print modulepath | cut -d ':' -f 1`
18 SCRIPT_NAME=$(basename $0)
19 SCRIPT_DIR=$(readlink -f "$(dirname $0)")
20 JUST_CLONED=0
21
22 function remove_module {
23     local SHORT_MODULE_NAME=$1
24     if [ -n "$SHORT_MODULE_NAME" ]; then
25         rm -Rf "$MODULE_PATH/$SHORT_MODULE_NAME"
26     else
27         echo "ERROR: remove_module requires a SHORT_MODULE_NAME."
28     fi
29 }
30
31 function git_clone {
32     local MOD=$1
33     local DEST=$2
34
35     JUST_CLONED=1
36     for attempt in $(seq 0 3); do
37         clone_error=0
38         git clone $MOD $DEST && break || true
39         rm -rf $DEST
40         clone_error=1
41     done
42     return $clone_error
43 }
44
45 # Array of modules to be installed key:value is module:version.
46 declare -A MODULES
47
48 # Array of modues to be installed from source and without dependency resolution.
49 # key:value is source location, revision to checkout
50 declare -A SOURCE_MODULES
51
52 # Array of modues to be installed from source and without dependency resolution from openstack git
53 # key:value is source location, revision to checkout
54 declare -A INTEGRATION_MODULES
55
56 # load modules.env to populate MODULES[*] and SOURCE_MODULES[*]
57 # for processing.
58 MODULE_ENV_FILE=${MODULE_FILE:-modules.env}
59 MODULE_ENV_PATH=${MODULE_ENV_PATH:-${SCRIPT_DIR}}
60 if [ -f "${MODULE_ENV_PATH}/${MODULE_ENV_FILE}" ] ; then
61     . "${MODULE_ENV_PATH}/${MODULE_ENV_FILE}"
62 fi
63
64 if [ -z "${!MODULES[*]}" ] && [ -z "${!SOURCE_MODULES[*]}" ] ; then
65     echo ""
66     echo "WARNING: nothing to do, unable to find MODULES or SOURCE_MODULES"
67     echo "  export options, try setting MODULE_ENV_PATH or MODULE_ENV_FILE"
68     echo "  export to the proper location of modules.env file."
69     echo ""
70     exit 0
71 fi
72
73 MODULE_LIST=`puppet module list --color=false`
74
75 # Install modules from source
76 for MOD in ${!SOURCE_MODULES[*]} ; do
77     JUST_CLONED=0
78     # get the name of the module directory
79     if [ `echo $MOD | awk -F. '{print $NF}'` = 'git' ]; then
80         echo "Remote repos of the form repo.git are not supported: ${MOD}"
81         exit 1
82     fi
83
84     MODULE_NAME=`echo $MOD | awk -F- '{print $NF}'`
85
86     # set up git base command to use the correct path
87     GIT_CMD_BASE="git --git-dir=${MODULE_PATH}/${MODULE_NAME}/.git --work-tree ${MODULE_PATH}/${MODULE_NAME}"
88     # treat any occurrence of the module as a match
89     if ! echo $MODULE_LIST | grep "${MODULE_NAME}" >/dev/null 2>&1; then
90         # clone modules that are not installed
91         git_clone $MOD "${MODULE_PATH}/${MODULE_NAME}"
92     else
93         if [ ! -d ${MODULE_PATH}/${MODULE_NAME}/.git ]; then
94             echo "Found directory ${MODULE_PATH}/${MODULE_NAME} that is not a git repo, deleting it and reinstalling from source"
95             remove_module $MODULE_NAME
96             git_clone $MOD "${MODULE_PATH}/${MODULE_NAME}"
97         elif [ `${GIT_CMD_BASE} remote show origin | grep 'Fetch URL' | awk -F'URL: ' '{print $2}'` != $MOD ]; then
98             echo "Found remote in ${MODULE_PATH}/${MODULE_NAME} that does not match desired remote ${MOD}, deleting dir and re-cloning"
99             remove_module $MODULE_NAME
100             git_clone $MOD "${MODULE_PATH}/${MODULE_NAME}"
101         fi
102     fi
103
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