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