Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 1 | # functions - Common functions used by DevStack components |
| 2 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 3 | # Save trace setting |
| 4 | XTRACE=$(set +o | grep xtrace) |
| 5 | set +o xtrace |
| 6 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 7 | |
| 8 | # apt-get wrapper to set arguments correctly |
| 9 | # apt_get package [package ...] |
| 10 | function apt_get() { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 11 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 12 | local sudo="sudo" |
| 13 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 14 | $sudo DEBIAN_FRONTEND=noninteractive \ |
| 15 | http_proxy=$http_proxy https_proxy=$https_proxy \ |
| 16 | apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" |
| 17 | } |
| 18 | |
| 19 | |
| 20 | # Gracefully cp only if source file/dir exists |
| 21 | # cp_it source destination |
| 22 | function cp_it { |
| 23 | if [ -e $1 ] || [ -d $1 ]; then |
| 24 | cp -pRL $1 $2 |
| 25 | fi |
| 26 | } |
| 27 | |
| 28 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 29 | # Prints "message" and exits |
| 30 | # die "message" |
| 31 | function die() { |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 32 | local exitcode=$? |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 33 | set +o xtrace |
| 34 | echo $@ |
| 35 | exit $exitcode |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
| 39 | # Checks an environment variable is not set or has length 0 OR if the |
| 40 | # exit code is non-zero and prints "message" and exits |
| 41 | # NOTE: env-var is the variable name without a '$' |
| 42 | # die_if_not_set env-var "message" |
| 43 | function die_if_not_set() { |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 44 | ( |
| 45 | local exitcode=$? |
| 46 | set +o xtrace |
| 47 | local evar=$1; shift |
| 48 | if ! is_set $evar || [ $exitcode != 0 ]; then |
| 49 | set +o xtrace |
| 50 | echo $@ |
| 51 | exit -1 |
| 52 | fi |
| 53 | ) |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | # Grab a numbered field from python prettytable output |
| 58 | # Fields are numbered starting with 1 |
| 59 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. |
| 60 | # get_field field-number |
| 61 | function get_field() { |
| 62 | while read data; do |
| 63 | if [ "$1" -lt 0 ]; then |
| 64 | field="(\$(NF$1))" |
| 65 | else |
| 66 | field="\$$(($1 + 1))" |
| 67 | fi |
| 68 | echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" |
| 69 | done |
| 70 | } |
| 71 | |
| 72 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 73 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 74 | # be owned by the installation user, we create the directory and change the |
| 75 | # ownership to the proper user. |
| 76 | # Set global RECLONE=yes to simulate a clone when dest-dir exists |
| 77 | # git_clone remote dest-dir branch |
| 78 | function git_clone { |
| 79 | [[ "$OFFLINE" = "True" ]] && return |
| 80 | |
| 81 | GIT_REMOTE=$1 |
| 82 | GIT_DEST=$2 |
| 83 | GIT_BRANCH=$3 |
| 84 | |
| 85 | if echo $GIT_BRANCH | egrep -q "^refs"; then |
| 86 | # If our branch name is a gerrit style refs/changes/... |
| 87 | if [[ ! -d $GIT_DEST ]]; then |
| 88 | git clone $GIT_REMOTE $GIT_DEST |
| 89 | fi |
| 90 | cd $GIT_DEST |
| 91 | git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD |
| 92 | else |
| 93 | # do a full clone only if the directory doesn't exist |
| 94 | if [[ ! -d $GIT_DEST ]]; then |
| 95 | git clone $GIT_REMOTE $GIT_DEST |
| 96 | cd $GIT_DEST |
| 97 | # This checkout syntax works for both branches and tags |
| 98 | git checkout $GIT_BRANCH |
| 99 | elif [[ "$RECLONE" == "yes" ]]; then |
| 100 | # if it does exist then simulate what clone does if asked to RECLONE |
| 101 | cd $GIT_DEST |
| 102 | # set the url to pull from and fetch |
| 103 | git remote set-url origin $GIT_REMOTE |
| 104 | git fetch origin |
| 105 | # remove the existing ignored files (like pyc) as they cause breakage |
| 106 | # (due to the py files having older timestamps than our pyc, so python |
| 107 | # thinks the pyc files are correct using them) |
| 108 | find $GIT_DEST -name '*.pyc' -delete |
| 109 | git checkout -f origin/$GIT_BRANCH |
| 110 | # a local branch might not exist |
| 111 | git branch -D $GIT_BRANCH || true |
| 112 | git checkout -b $GIT_BRANCH |
| 113 | fi |
| 114 | fi |
| 115 | } |
| 116 | |
| 117 | |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame^] | 118 | # is_service_enabled() checks if the service(s) specified as arguments are |
| 119 | # enabled by the user in **ENABLED_SERVICES**. |
| 120 | # |
| 121 | # If there are multiple services specified as arguments the test performs a |
| 122 | # boolean OR or if any of the services specified on the command line |
| 123 | # return true. |
| 124 | # |
| 125 | # There is a special cases for some 'catch-all' services:: |
| 126 | # **nova** returns true if any service enabled start with **n-** |
| 127 | # **glance** returns true if any service enabled start with **g-** |
| 128 | # **quantum** returns true if any service enabled start with **q-** |
| 129 | function is_service_enabled() { |
| 130 | services=$@ |
| 131 | for service in ${services}; do |
| 132 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 133 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 |
| 134 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 |
| 135 | [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 |
| 136 | done |
| 137 | return 1 |
| 138 | } |
| 139 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 140 | |
| 141 | # Test if the named environment variable is set and not zero length |
| 142 | # is_set env-var |
| 143 | function is_set() { |
| 144 | local var=\$"$1" |
| 145 | if eval "[ -z $var ]"; then |
| 146 | return 1 |
| 147 | fi |
| 148 | return 0 |
| 149 | } |
| 150 | |
| 151 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 152 | # pip install wrapper to set cache and proxy environment variables |
| 153 | # pip_install package [package ...] |
| 154 | function pip_install { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 155 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 156 | sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \ |
| 157 | HTTP_PROXY=$http_proxy \ |
| 158 | HTTPS_PROXY=$https_proxy \ |
| 159 | pip install --use-mirrors $@ |
| 160 | } |
| 161 | |
| 162 | |
| 163 | # Normalize config values to True or False |
| 164 | # VAR=`trueorfalse default-value test-value` |
| 165 | function trueorfalse() { |
| 166 | local default=$1 |
| 167 | local testval=$2 |
| 168 | |
| 169 | [[ -z "$testval" ]] && { echo "$default"; return; } |
| 170 | [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } |
| 171 | [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } |
| 172 | echo "$default" |
| 173 | } |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 174 | |
| 175 | # Restore xtrace |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame^] | 176 | $XTRACE |