Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 1 | # functions - Common functions used by DevStack components |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 2 | # |
| 3 | # ENABLED_SERVICES is used by is_service_enabled() |
| 4 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 5 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 6 | # Save trace setting |
| 7 | XTRACE=$(set +o | grep xtrace) |
| 8 | set +o xtrace |
| 9 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 10 | |
| 11 | # apt-get wrapper to set arguments correctly |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 12 | # apt_get operation package [package ...] |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 13 | function apt_get() { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 14 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 15 | local sudo="sudo" |
| 16 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 17 | $sudo DEBIAN_FRONTEND=noninteractive \ |
| 18 | http_proxy=$http_proxy https_proxy=$https_proxy \ |
| 19 | apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" |
| 20 | } |
| 21 | |
| 22 | |
| 23 | # Gracefully cp only if source file/dir exists |
| 24 | # cp_it source destination |
| 25 | function cp_it { |
| 26 | if [ -e $1 ] || [ -d $1 ]; then |
| 27 | cp -pRL $1 $2 |
| 28 | fi |
| 29 | } |
| 30 | |
| 31 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 32 | # Prints "message" and exits |
| 33 | # die "message" |
| 34 | function die() { |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 35 | local exitcode=$? |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 36 | set +o xtrace |
| 37 | echo $@ |
| 38 | exit $exitcode |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | |
| 42 | # Checks an environment variable is not set or has length 0 OR if the |
| 43 | # exit code is non-zero and prints "message" and exits |
| 44 | # NOTE: env-var is the variable name without a '$' |
| 45 | # die_if_not_set env-var "message" |
| 46 | function die_if_not_set() { |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 47 | ( |
| 48 | local exitcode=$? |
| 49 | set +o xtrace |
| 50 | local evar=$1; shift |
| 51 | if ! is_set $evar || [ $exitcode != 0 ]; then |
| 52 | set +o xtrace |
| 53 | echo $@ |
| 54 | exit -1 |
| 55 | fi |
| 56 | ) |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | # Grab a numbered field from python prettytable output |
| 61 | # Fields are numbered starting with 1 |
| 62 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. |
| 63 | # get_field field-number |
| 64 | function get_field() { |
| 65 | while read data; do |
| 66 | if [ "$1" -lt 0 ]; then |
| 67 | field="(\$(NF$1))" |
| 68 | else |
| 69 | field="\$$(($1 + 1))" |
| 70 | fi |
| 71 | echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" |
| 72 | done |
| 73 | } |
| 74 | |
| 75 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 76 | # Determine OS Vendor, Release and Update |
| 77 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora |
| 78 | # Returns results in global variables: |
| 79 | # os_VENDOR - vendor name |
| 80 | # os_RELEASE - release |
| 81 | # os_UPDATE - update |
| 82 | # os_PACKAGE - package type |
| 83 | # os_CODENAME - vendor's codename for release |
| 84 | # GetOSVersion |
| 85 | GetOSVersion() { |
| 86 | # Figure out which vendor we are |
| 87 | if [[ -n "`which sw_vers 2>/dev/null`" ]]; then |
| 88 | # OS/X |
| 89 | os_VENDOR=`sw_vers -productName` |
| 90 | os_RELEASE=`sw_vers -productVersion` |
| 91 | os_UPDATE=${os_RELEASE##*.} |
| 92 | os_RELEASE=${os_RELEASE%.*} |
| 93 | os_PACKAGE="" |
| 94 | if [[ "$os_RELEASE" =~ "10.7" ]]; then |
| 95 | os_CODENAME="lion" |
| 96 | elif [[ "$os_RELEASE" =~ "10.6" ]]; then |
| 97 | os_CODENAME="snow leopard" |
| 98 | elif [[ "$os_RELEASE" =~ "10.5" ]]; then |
| 99 | os_CODENAME="leopard" |
| 100 | elif [[ "$os_RELEASE" =~ "10.4" ]]; then |
| 101 | os_CODENAME="tiger" |
| 102 | elif [[ "$os_RELEASE" =~ "10.3" ]]; then |
| 103 | os_CODENAME="panther" |
| 104 | else |
| 105 | os_CODENAME="" |
| 106 | fi |
| 107 | elif [[ -x $(which lsb_release 2>/dev/null) ]]; then |
| 108 | os_VENDOR=$(lsb_release -i -s) |
| 109 | os_RELEASE=$(lsb_release -r -s) |
| 110 | os_UPDATE="" |
| 111 | if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then |
| 112 | os_PACKAGE="deb" |
| 113 | else |
| 114 | os_PACKAGE="rpm" |
| 115 | fi |
| 116 | os_CODENAME=$(lsb_release -c -s) |
| 117 | elif [[ -r /etc/redhat-release ]]; then |
| 118 | # Red Hat Enterprise Linux Server release 5.5 (Tikanga) |
| 119 | # CentOS release 5.5 (Final) |
| 120 | # CentOS Linux release 6.0 (Final) |
| 121 | # Fedora release 16 (Verne) |
| 122 | os_CODENAME="" |
| 123 | for r in "Red Hat" CentOS Fedora; do |
| 124 | os_VENDOR=$r |
| 125 | if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then |
| 126 | ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` |
| 127 | os_CODENAME=${ver#*|} |
| 128 | os_RELEASE=${ver%|*} |
| 129 | os_UPDATE=${os_RELEASE##*.} |
| 130 | os_RELEASE=${os_RELEASE%.*} |
| 131 | break |
| 132 | fi |
| 133 | os_VENDOR="" |
| 134 | done |
| 135 | os_PACKAGE="rpm" |
| 136 | fi |
| 137 | export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME |
| 138 | } |
| 139 | |
| 140 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 141 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 142 | # be owned by the installation user, we create the directory and change the |
| 143 | # ownership to the proper user. |
| 144 | # Set global RECLONE=yes to simulate a clone when dest-dir exists |
| 145 | # git_clone remote dest-dir branch |
| 146 | function git_clone { |
| 147 | [[ "$OFFLINE" = "True" ]] && return |
| 148 | |
| 149 | GIT_REMOTE=$1 |
| 150 | GIT_DEST=$2 |
| 151 | GIT_BRANCH=$3 |
| 152 | |
| 153 | if echo $GIT_BRANCH | egrep -q "^refs"; then |
| 154 | # If our branch name is a gerrit style refs/changes/... |
| 155 | if [[ ! -d $GIT_DEST ]]; then |
| 156 | git clone $GIT_REMOTE $GIT_DEST |
| 157 | fi |
| 158 | cd $GIT_DEST |
| 159 | git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD |
| 160 | else |
| 161 | # do a full clone only if the directory doesn't exist |
| 162 | if [[ ! -d $GIT_DEST ]]; then |
| 163 | git clone $GIT_REMOTE $GIT_DEST |
| 164 | cd $GIT_DEST |
| 165 | # This checkout syntax works for both branches and tags |
| 166 | git checkout $GIT_BRANCH |
| 167 | elif [[ "$RECLONE" == "yes" ]]; then |
| 168 | # if it does exist then simulate what clone does if asked to RECLONE |
| 169 | cd $GIT_DEST |
| 170 | # set the url to pull from and fetch |
| 171 | git remote set-url origin $GIT_REMOTE |
| 172 | git fetch origin |
| 173 | # remove the existing ignored files (like pyc) as they cause breakage |
| 174 | # (due to the py files having older timestamps than our pyc, so python |
| 175 | # thinks the pyc files are correct using them) |
| 176 | find $GIT_DEST -name '*.pyc' -delete |
| 177 | git checkout -f origin/$GIT_BRANCH |
| 178 | # a local branch might not exist |
| 179 | git branch -D $GIT_BRANCH || true |
| 180 | git checkout -b $GIT_BRANCH |
| 181 | fi |
| 182 | fi |
| 183 | } |
| 184 | |
| 185 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 186 | # Comment an option in an INI file |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 187 | # inicomment config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 188 | function inicomment() { |
| 189 | local file=$1 |
| 190 | local section=$2 |
| 191 | local option=$3 |
| 192 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file |
| 193 | } |
| 194 | |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 195 | # Uncomment an option in an INI file |
| 196 | # iniuncomment config-file section option |
| 197 | function iniuncomment() { |
| 198 | local file=$1 |
| 199 | local section=$2 |
| 200 | local option=$3 |
| 201 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file |
| 202 | } |
| 203 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 204 | |
| 205 | # Get an option from an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 206 | # iniget config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 207 | function iniget() { |
| 208 | local file=$1 |
| 209 | local section=$2 |
| 210 | local option=$3 |
| 211 | local line |
| 212 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file) |
| 213 | echo ${line#*=} |
| 214 | } |
| 215 | |
| 216 | |
| 217 | # Set an option in an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 218 | # iniset config-file section option value |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 219 | function iniset() { |
| 220 | local file=$1 |
| 221 | local section=$2 |
| 222 | local option=$3 |
| 223 | local value=$4 |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 224 | if ! grep -q "^\[$section\]" $file; then |
| 225 | # Add section at the end |
| 226 | echo -e "\n[$section]" >>$file |
| 227 | fi |
| 228 | if [[ -z "$(iniget $file $section $option)" ]]; then |
| 229 | # Add it |
| 230 | sed -i -e "/^\[$section\]/ a\\ |
| 231 | $option = $value |
| 232 | " $file |
| 233 | else |
| 234 | # Replace it |
| 235 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file |
| 236 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 240 | # is_service_enabled() checks if the service(s) specified as arguments are |
| 241 | # enabled by the user in **ENABLED_SERVICES**. |
| 242 | # |
| 243 | # If there are multiple services specified as arguments the test performs a |
| 244 | # boolean OR or if any of the services specified on the command line |
| 245 | # return true. |
| 246 | # |
| 247 | # There is a special cases for some 'catch-all' services:: |
| 248 | # **nova** returns true if any service enabled start with **n-** |
| 249 | # **glance** returns true if any service enabled start with **g-** |
| 250 | # **quantum** returns true if any service enabled start with **q-** |
| 251 | function is_service_enabled() { |
| 252 | services=$@ |
| 253 | for service in ${services}; do |
| 254 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 255 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 |
Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame^] | 256 | [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0 |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 257 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 |
| 258 | [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 |
| 259 | done |
| 260 | return 1 |
| 261 | } |
| 262 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 263 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 264 | # Distro-agnostic package installer |
| 265 | # install_package package [package ...] |
| 266 | function install_package() { |
| 267 | if [[ -z "$os_PACKAGE" ]]; then |
| 268 | GetOSVersion |
| 269 | fi |
| 270 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 271 | apt_get install "$@" |
| 272 | else |
| 273 | yum_install "$@" |
| 274 | fi |
| 275 | } |
| 276 | |
| 277 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 278 | # Test if the named environment variable is set and not zero length |
| 279 | # is_set env-var |
| 280 | function is_set() { |
| 281 | local var=\$"$1" |
| 282 | if eval "[ -z $var ]"; then |
| 283 | return 1 |
| 284 | fi |
| 285 | return 0 |
| 286 | } |
| 287 | |
| 288 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 289 | # pip install wrapper to set cache and proxy environment variables |
| 290 | # pip_install package [package ...] |
| 291 | function pip_install { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 292 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 293 | if [[ -z "$os_PACKAGE" ]]; then |
| 294 | GetOSVersion |
| 295 | fi |
| 296 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 297 | CMD_PIP=/usr/bin/pip |
| 298 | else |
| 299 | CMD_PIP=/usr/bin/pip-python |
| 300 | fi |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 301 | sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \ |
| 302 | HTTP_PROXY=$http_proxy \ |
| 303 | HTTPS_PROXY=$https_proxy \ |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 304 | $CMD_PIP install --use-mirrors $@ |
| 305 | } |
| 306 | |
| 307 | |
| 308 | # Service wrapper to restart services |
| 309 | # restart_service service-name |
| 310 | function restart_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 311 | if [[ -z "$os_PACKAGE" ]]; then |
| 312 | GetOSVersion |
| 313 | fi |
| 314 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 315 | sudo /usr/sbin/service $1 restart |
| 316 | else |
| 317 | sudo /sbin/service $1 restart |
| 318 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 322 | # pip install the dependencies of the package before we do the setup.py |
| 323 | # develop, so that pip and not distutils process the dependency chain |
| 324 | # setup_develop directory |
| 325 | function setup_develop() { |
| 326 | (cd $1; \ |
| 327 | python setup.py egg_info; \ |
| 328 | raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \ |
| 329 | depend_links=$(echo $raw_links | xargs); \ |
| 330 | pip_install -r *-info/requires.txt $depend_links; \ |
| 331 | sudo \ |
| 332 | HTTP_PROXY=$http_proxy \ |
| 333 | HTTPS_PROXY=$https_proxy \ |
| 334 | python setup.py develop \ |
| 335 | ) |
| 336 | } |
| 337 | |
| 338 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 339 | # Service wrapper to start services |
| 340 | # start_service service-name |
| 341 | function start_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 342 | if [[ -z "$os_PACKAGE" ]]; then |
| 343 | GetOSVersion |
| 344 | fi |
| 345 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 346 | sudo /usr/sbin/service $1 start |
| 347 | else |
| 348 | sudo /sbin/service $1 start |
| 349 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | # Service wrapper to stop services |
| 354 | # stop_service service-name |
| 355 | function stop_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 356 | if [[ -z "$os_PACKAGE" ]]; then |
| 357 | GetOSVersion |
| 358 | fi |
| 359 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 360 | sudo /usr/sbin/service $1 stop |
| 361 | else |
| 362 | sudo /sbin/service $1 stop |
| 363 | fi |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
| 367 | # Normalize config values to True or False |
| 368 | # VAR=`trueorfalse default-value test-value` |
| 369 | function trueorfalse() { |
| 370 | local default=$1 |
| 371 | local testval=$2 |
| 372 | |
| 373 | [[ -z "$testval" ]] && { echo "$default"; return; } |
| 374 | [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } |
| 375 | [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } |
| 376 | echo "$default" |
| 377 | } |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 378 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 379 | |
| 380 | # yum wrapper to set arguments correctly |
| 381 | # yum_install package [package ...] |
| 382 | function yum_install() { |
| 383 | [[ "$OFFLINE" = "True" ]] && return |
| 384 | local sudo="sudo" |
| 385 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 386 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ |
| 387 | yum install -y "$@" |
| 388 | } |
| 389 | |
| 390 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 391 | # Restore xtrace |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 392 | $XTRACE |