| 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 |  | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 118 |  | 
 | 119 | # Test if the named environment variable is set and not zero length | 
 | 120 | # is_set env-var | 
 | 121 | function is_set() { | 
 | 122 |     local var=\$"$1" | 
 | 123 |     if eval "[ -z $var ]"; then | 
 | 124 |         return 1 | 
 | 125 |     fi | 
 | 126 |     return 0 | 
 | 127 | } | 
 | 128 |  | 
 | 129 |  | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 130 | # pip install wrapper to set cache and proxy environment variables | 
 | 131 | # pip_install package [package ...] | 
 | 132 | function pip_install { | 
| Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 133 |     [[ "$OFFLINE" = "True" || -z "$@" ]] && return | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 134 |     sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \ | 
 | 135 |         HTTP_PROXY=$http_proxy \ | 
 | 136 |         HTTPS_PROXY=$https_proxy \ | 
 | 137 |         pip install --use-mirrors $@ | 
 | 138 | } | 
 | 139 |  | 
 | 140 |  | 
 | 141 | # Normalize config values to True or False | 
 | 142 | # VAR=`trueorfalse default-value test-value` | 
 | 143 | function trueorfalse() { | 
 | 144 |     local default=$1 | 
 | 145 |     local testval=$2 | 
 | 146 |  | 
 | 147 |     [[ -z "$testval" ]] && { echo "$default"; return; } | 
 | 148 |     [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } | 
 | 149 |     [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } | 
 | 150 |     echo "$default" | 
 | 151 | } | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame^] | 152 |  | 
 | 153 | # Restore xtrace | 
 | 154 | $XTRACE |