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 | |
| 25 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 26 | # be owned by the installation user, we create the directory and change the |
| 27 | # ownership to the proper user. |
| 28 | # Set global RECLONE=yes to simulate a clone when dest-dir exists |
| 29 | # git_clone remote dest-dir branch |
| 30 | function git_clone { |
| 31 | [[ "$OFFLINE" = "True" ]] && return |
| 32 | |
| 33 | GIT_REMOTE=$1 |
| 34 | GIT_DEST=$2 |
| 35 | GIT_BRANCH=$3 |
| 36 | |
| 37 | if echo $GIT_BRANCH | egrep -q "^refs"; then |
| 38 | # If our branch name is a gerrit style refs/changes/... |
| 39 | if [[ ! -d $GIT_DEST ]]; then |
| 40 | git clone $GIT_REMOTE $GIT_DEST |
| 41 | fi |
| 42 | cd $GIT_DEST |
| 43 | git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD |
| 44 | else |
| 45 | # do a full clone only if the directory doesn't exist |
| 46 | if [[ ! -d $GIT_DEST ]]; then |
| 47 | git clone $GIT_REMOTE $GIT_DEST |
| 48 | cd $GIT_DEST |
| 49 | # This checkout syntax works for both branches and tags |
| 50 | git checkout $GIT_BRANCH |
| 51 | elif [[ "$RECLONE" == "yes" ]]; then |
| 52 | # if it does exist then simulate what clone does if asked to RECLONE |
| 53 | cd $GIT_DEST |
| 54 | # set the url to pull from and fetch |
| 55 | git remote set-url origin $GIT_REMOTE |
| 56 | git fetch origin |
| 57 | # remove the existing ignored files (like pyc) as they cause breakage |
| 58 | # (due to the py files having older timestamps than our pyc, so python |
| 59 | # thinks the pyc files are correct using them) |
| 60 | find $GIT_DEST -name '*.pyc' -delete |
| 61 | git checkout -f origin/$GIT_BRANCH |
| 62 | # a local branch might not exist |
| 63 | git branch -D $GIT_BRANCH || true |
| 64 | git checkout -b $GIT_BRANCH |
| 65 | fi |
| 66 | fi |
| 67 | } |
| 68 | |
| 69 | |
| 70 | # pip install wrapper to set cache and proxy environment variables |
| 71 | # pip_install package [package ...] |
| 72 | function pip_install { |
| 73 | [[ "$OFFLINE" = "True" ]] && return |
| 74 | sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \ |
| 75 | HTTP_PROXY=$http_proxy \ |
| 76 | HTTPS_PROXY=$https_proxy \ |
| 77 | pip install --use-mirrors $@ |
| 78 | } |
| 79 | |
| 80 | |
| 81 | # Normalize config values to True or False |
| 82 | # VAR=`trueorfalse default-value test-value` |
| 83 | function trueorfalse() { |
| 84 | local default=$1 |
| 85 | local testval=$2 |
| 86 | |
| 87 | [[ -z "$testval" ]] && { echo "$default"; return; } |
| 88 | [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } |
| 89 | [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } |
| 90 | echo "$default" |
| 91 | } |