Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1 | # functions-common - Common functions used by DevStack components |
| 2 | # |
| 3 | # The canonical copy of this file is maintained in the DevStack repo. |
| 4 | # All modifications should be made there and then sync'ed to other repos |
| 5 | # as required. |
| 6 | # |
| 7 | # This file is sorted alphabetically within the function groups. |
| 8 | # |
| 9 | # - Config Functions |
| 10 | # - Control Functions |
| 11 | # - Distro Functions |
| 12 | # - Git Functions |
| 13 | # - OpenStack Functions |
| 14 | # - Package Functions |
| 15 | # - Process Functions |
| 16 | # - Python Functions |
| 17 | # - Service Functions |
Masayuki Igawa | f6368d3 | 2014-02-20 13:31:26 +0900 | [diff] [blame^] | 18 | # - System Functions |
Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 19 | # |
| 20 | # The following variables are assumed to be defined by certain functions: |
| 21 | # |
| 22 | # - ``ENABLED_SERVICES`` |
| 23 | # - ``ERROR_ON_CLONE`` |
| 24 | # - ``FILES`` |
| 25 | # - ``OFFLINE`` |
| 26 | # - ``PIP_DOWNLOAD_CACHE`` |
| 27 | # - ``PIP_USE_MIRRORS`` |
| 28 | # - ``RECLONE`` |
| 29 | # - ``TRACK_DEPENDS`` |
| 30 | # - ``http_proxy``, ``https_proxy``, ``no_proxy`` |
| 31 | |
| 32 | # Save trace setting |
| 33 | XTRACE=$(set +o | grep xtrace) |
| 34 | set +o xtrace |
| 35 | |
| 36 | |
| 37 | # Config Functions |
| 38 | # ================ |
| 39 | |
| 40 | # Append a new option in an ini file without replacing the old value |
| 41 | # iniadd config-file section option value1 value2 value3 ... |
| 42 | function iniadd() { |
| 43 | local file=$1 |
| 44 | local section=$2 |
| 45 | local option=$3 |
| 46 | shift 3 |
| 47 | local values="$(iniget_multiline $file $section $option) $@" |
| 48 | iniset_multiline $file $section $option $values |
| 49 | } |
| 50 | |
| 51 | # Comment an option in an INI file |
| 52 | # inicomment config-file section option |
| 53 | function inicomment() { |
| 54 | local file=$1 |
| 55 | local section=$2 |
| 56 | local option=$3 |
| 57 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file" |
| 58 | } |
| 59 | |
| 60 | # Get an option from an INI file |
| 61 | # iniget config-file section option |
| 62 | function iniget() { |
| 63 | local file=$1 |
| 64 | local section=$2 |
| 65 | local option=$3 |
| 66 | local line |
| 67 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") |
| 68 | echo ${line#*=} |
| 69 | } |
| 70 | |
| 71 | # Get a multiple line option from an INI file |
| 72 | # iniget_multiline config-file section option |
| 73 | function iniget_multiline() { |
| 74 | local file=$1 |
| 75 | local section=$2 |
| 76 | local option=$3 |
| 77 | local values |
| 78 | values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file") |
| 79 | echo ${values} |
| 80 | } |
| 81 | |
| 82 | # Determinate is the given option present in the INI file |
| 83 | # ini_has_option config-file section option |
| 84 | function ini_has_option() { |
| 85 | local file=$1 |
| 86 | local section=$2 |
| 87 | local option=$3 |
| 88 | local line |
| 89 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") |
| 90 | [ -n "$line" ] |
| 91 | } |
| 92 | |
| 93 | # Set an option in an INI file |
| 94 | # iniset config-file section option value |
| 95 | function iniset() { |
| 96 | local file=$1 |
| 97 | local section=$2 |
| 98 | local option=$3 |
| 99 | local value=$4 |
| 100 | |
| 101 | [[ -z $section || -z $option ]] && return |
| 102 | |
| 103 | if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then |
| 104 | # Add section at the end |
| 105 | echo -e "\n[$section]" >>"$file" |
| 106 | fi |
| 107 | if ! ini_has_option "$file" "$section" "$option"; then |
| 108 | # Add it |
| 109 | sed -i -e "/^\[$section\]/ a\\ |
| 110 | $option = $value |
| 111 | " "$file" |
| 112 | else |
| 113 | local sep=$(echo -ne "\x01") |
| 114 | # Replace it |
| 115 | sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file" |
| 116 | fi |
| 117 | } |
| 118 | |
| 119 | # Set a multiple line option in an INI file |
| 120 | # iniset_multiline config-file section option value1 value2 valu3 ... |
| 121 | function iniset_multiline() { |
| 122 | local file=$1 |
| 123 | local section=$2 |
| 124 | local option=$3 |
| 125 | shift 3 |
| 126 | local values |
| 127 | for v in $@; do |
| 128 | # The later sed command inserts each new value in the line next to |
| 129 | # the section identifier, which causes the values to be inserted in |
| 130 | # the reverse order. Do a reverse here to keep the original order. |
| 131 | values="$v ${values}" |
| 132 | done |
| 133 | if ! grep -q "^\[$section\]" "$file"; then |
| 134 | # Add section at the end |
| 135 | echo -e "\n[$section]" >>"$file" |
| 136 | else |
| 137 | # Remove old values |
| 138 | sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" |
| 139 | fi |
| 140 | # Add new ones |
| 141 | for v in $values; do |
| 142 | sed -i -e "/^\[$section\]/ a\\ |
| 143 | $option = $v |
| 144 | " "$file" |
| 145 | done |
| 146 | } |
| 147 | |
| 148 | # Uncomment an option in an INI file |
| 149 | # iniuncomment config-file section option |
| 150 | function iniuncomment() { |
| 151 | local file=$1 |
| 152 | local section=$2 |
| 153 | local option=$3 |
| 154 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file" |
| 155 | } |
| 156 | |
| 157 | # Normalize config values to True or False |
| 158 | # Accepts as False: 0 no No NO false False FALSE |
| 159 | # Accepts as True: 1 yes Yes YES true True TRUE |
| 160 | # VAR=$(trueorfalse default-value test-value) |
| 161 | function trueorfalse() { |
| 162 | local default=$1 |
| 163 | local testval=$2 |
| 164 | |
| 165 | [[ -z "$testval" ]] && { echo "$default"; return; } |
| 166 | [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; } |
| 167 | [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; } |
| 168 | echo "$default" |
| 169 | } |
| 170 | |
| 171 | |
| 172 | # Control Functions |
| 173 | # ================= |
| 174 | |
| 175 | # Prints backtrace info |
| 176 | # filename:lineno:function |
| 177 | # backtrace level |
| 178 | function backtrace { |
| 179 | local level=$1 |
| 180 | local deep=$((${#BASH_SOURCE[@]} - 1)) |
| 181 | echo "[Call Trace]" |
| 182 | while [ $level -le $deep ]; do |
| 183 | echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}" |
| 184 | deep=$((deep - 1)) |
| 185 | done |
| 186 | } |
| 187 | |
| 188 | # Prints line number and "message" then exits |
| 189 | # die $LINENO "message" |
| 190 | function die() { |
| 191 | local exitcode=$? |
| 192 | set +o xtrace |
| 193 | local line=$1; shift |
| 194 | if [ $exitcode == 0 ]; then |
| 195 | exitcode=1 |
| 196 | fi |
| 197 | backtrace 2 |
| 198 | err $line "$*" |
| 199 | exit $exitcode |
| 200 | } |
| 201 | |
| 202 | # Checks an environment variable is not set or has length 0 OR if the |
| 203 | # exit code is non-zero and prints "message" and exits |
| 204 | # NOTE: env-var is the variable name without a '$' |
| 205 | # die_if_not_set $LINENO env-var "message" |
| 206 | function die_if_not_set() { |
| 207 | local exitcode=$? |
| 208 | FXTRACE=$(set +o | grep xtrace) |
| 209 | set +o xtrace |
| 210 | local line=$1; shift |
| 211 | local evar=$1; shift |
| 212 | if ! is_set $evar || [ $exitcode != 0 ]; then |
| 213 | die $line "$*" |
| 214 | fi |
| 215 | $FXTRACE |
| 216 | } |
| 217 | |
| 218 | # Prints line number and "message" in error format |
| 219 | # err $LINENO "message" |
| 220 | function err() { |
| 221 | local exitcode=$? |
| 222 | errXTRACE=$(set +o | grep xtrace) |
| 223 | set +o xtrace |
| 224 | local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2" |
| 225 | echo $msg 1>&2; |
| 226 | if [[ -n ${SCREEN_LOGDIR} ]]; then |
| 227 | echo $msg >> "${SCREEN_LOGDIR}/error.log" |
| 228 | fi |
| 229 | $errXTRACE |
| 230 | return $exitcode |
| 231 | } |
| 232 | |
| 233 | # Checks an environment variable is not set or has length 0 OR if the |
| 234 | # exit code is non-zero and prints "message" |
| 235 | # NOTE: env-var is the variable name without a '$' |
| 236 | # err_if_not_set $LINENO env-var "message" |
| 237 | function err_if_not_set() { |
| 238 | local exitcode=$? |
| 239 | errinsXTRACE=$(set +o | grep xtrace) |
| 240 | set +o xtrace |
| 241 | local line=$1; shift |
| 242 | local evar=$1; shift |
| 243 | if ! is_set $evar || [ $exitcode != 0 ]; then |
| 244 | err $line "$*" |
| 245 | fi |
| 246 | $errinsXTRACE |
| 247 | return $exitcode |
| 248 | } |
| 249 | |
| 250 | # Exit after outputting a message about the distribution not being supported. |
| 251 | # exit_distro_not_supported [optional-string-telling-what-is-missing] |
| 252 | function exit_distro_not_supported { |
| 253 | if [[ -z "$DISTRO" ]]; then |
| 254 | GetDistro |
| 255 | fi |
| 256 | |
| 257 | if [ $# -gt 0 ]; then |
| 258 | die $LINENO "Support for $DISTRO is incomplete: no support for $@" |
| 259 | else |
| 260 | die $LINENO "Support for $DISTRO is incomplete." |
| 261 | fi |
| 262 | } |
| 263 | |
| 264 | # Test if the named environment variable is set and not zero length |
| 265 | # is_set env-var |
| 266 | function is_set() { |
| 267 | local var=\$"$1" |
| 268 | eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this |
| 269 | } |
| 270 | |
| 271 | # Prints line number and "message" in warning format |
| 272 | # warn $LINENO "message" |
| 273 | function warn() { |
| 274 | local exitcode=$? |
| 275 | errXTRACE=$(set +o | grep xtrace) |
| 276 | set +o xtrace |
| 277 | local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2" |
| 278 | echo $msg 1>&2; |
| 279 | if [[ -n ${SCREEN_LOGDIR} ]]; then |
| 280 | echo $msg >> "${SCREEN_LOGDIR}/error.log" |
| 281 | fi |
| 282 | $errXTRACE |
| 283 | return $exitcode |
| 284 | } |
| 285 | |
| 286 | |
| 287 | # Distro Functions |
| 288 | # ================ |
| 289 | |
| 290 | # Determine OS Vendor, Release and Update |
| 291 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora |
| 292 | # Returns results in global variables: |
| 293 | # os_VENDOR - vendor name |
| 294 | # os_RELEASE - release |
| 295 | # os_UPDATE - update |
| 296 | # os_PACKAGE - package type |
| 297 | # os_CODENAME - vendor's codename for release |
| 298 | # GetOSVersion |
| 299 | GetOSVersion() { |
| 300 | # Figure out which vendor we are |
| 301 | if [[ -x "`which sw_vers 2>/dev/null`" ]]; then |
| 302 | # OS/X |
| 303 | os_VENDOR=`sw_vers -productName` |
| 304 | os_RELEASE=`sw_vers -productVersion` |
| 305 | os_UPDATE=${os_RELEASE##*.} |
| 306 | os_RELEASE=${os_RELEASE%.*} |
| 307 | os_PACKAGE="" |
| 308 | if [[ "$os_RELEASE" =~ "10.7" ]]; then |
| 309 | os_CODENAME="lion" |
| 310 | elif [[ "$os_RELEASE" =~ "10.6" ]]; then |
| 311 | os_CODENAME="snow leopard" |
| 312 | elif [[ "$os_RELEASE" =~ "10.5" ]]; then |
| 313 | os_CODENAME="leopard" |
| 314 | elif [[ "$os_RELEASE" =~ "10.4" ]]; then |
| 315 | os_CODENAME="tiger" |
| 316 | elif [[ "$os_RELEASE" =~ "10.3" ]]; then |
| 317 | os_CODENAME="panther" |
| 318 | else |
| 319 | os_CODENAME="" |
| 320 | fi |
| 321 | elif [[ -x $(which lsb_release 2>/dev/null) ]]; then |
| 322 | os_VENDOR=$(lsb_release -i -s) |
| 323 | os_RELEASE=$(lsb_release -r -s) |
| 324 | os_UPDATE="" |
| 325 | os_PACKAGE="rpm" |
| 326 | if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then |
| 327 | os_PACKAGE="deb" |
| 328 | elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then |
| 329 | lsb_release -d -s | grep -q openSUSE |
| 330 | if [[ $? -eq 0 ]]; then |
| 331 | os_VENDOR="openSUSE" |
| 332 | fi |
| 333 | elif [[ $os_VENDOR == "openSUSE project" ]]; then |
| 334 | os_VENDOR="openSUSE" |
| 335 | elif [[ $os_VENDOR =~ Red.*Hat ]]; then |
| 336 | os_VENDOR="Red Hat" |
| 337 | fi |
| 338 | os_CODENAME=$(lsb_release -c -s) |
| 339 | elif [[ -r /etc/redhat-release ]]; then |
| 340 | # Red Hat Enterprise Linux Server release 5.5 (Tikanga) |
| 341 | # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo) |
| 342 | # CentOS release 5.5 (Final) |
| 343 | # CentOS Linux release 6.0 (Final) |
| 344 | # Fedora release 16 (Verne) |
| 345 | # XenServer release 6.2.0-70446c (xenenterprise) |
| 346 | os_CODENAME="" |
| 347 | for r in "Red Hat" CentOS Fedora XenServer; do |
| 348 | os_VENDOR=$r |
| 349 | if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then |
| 350 | ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` |
| 351 | os_CODENAME=${ver#*|} |
| 352 | os_RELEASE=${ver%|*} |
| 353 | os_UPDATE=${os_RELEASE##*.} |
| 354 | os_RELEASE=${os_RELEASE%.*} |
| 355 | break |
| 356 | fi |
| 357 | os_VENDOR="" |
| 358 | done |
| 359 | os_PACKAGE="rpm" |
| 360 | elif [[ -r /etc/SuSE-release ]]; then |
| 361 | for r in openSUSE "SUSE Linux"; do |
| 362 | if [[ "$r" = "SUSE Linux" ]]; then |
| 363 | os_VENDOR="SUSE LINUX" |
| 364 | else |
| 365 | os_VENDOR=$r |
| 366 | fi |
| 367 | |
| 368 | if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then |
| 369 | os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'` |
| 370 | os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'` |
| 371 | os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'` |
| 372 | break |
| 373 | fi |
| 374 | os_VENDOR="" |
| 375 | done |
| 376 | os_PACKAGE="rpm" |
| 377 | # If lsb_release is not installed, we should be able to detect Debian OS |
| 378 | elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then |
| 379 | os_VENDOR="Debian" |
| 380 | os_PACKAGE="deb" |
| 381 | os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}') |
| 382 | os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g') |
| 383 | fi |
| 384 | export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME |
| 385 | } |
| 386 | |
| 387 | # Translate the OS version values into common nomenclature |
| 388 | # Sets global ``DISTRO`` from the ``os_*`` values |
| 389 | function GetDistro() { |
| 390 | GetOSVersion |
| 391 | if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then |
| 392 | # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective |
| 393 | DISTRO=$os_CODENAME |
| 394 | elif [[ "$os_VENDOR" =~ (Fedora) ]]; then |
| 395 | # For Fedora, just use 'f' and the release |
| 396 | DISTRO="f$os_RELEASE" |
| 397 | elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then |
| 398 | DISTRO="opensuse-$os_RELEASE" |
| 399 | elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then |
| 400 | # For SLE, also use the service pack |
| 401 | if [[ -z "$os_UPDATE" ]]; then |
| 402 | DISTRO="sle${os_RELEASE}" |
| 403 | else |
| 404 | DISTRO="sle${os_RELEASE}sp${os_UPDATE}" |
| 405 | fi |
| 406 | elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then |
| 407 | # Drop the . release as we assume it's compatible |
| 408 | DISTRO="rhel${os_RELEASE::1}" |
| 409 | elif [[ "$os_VENDOR" =~ (XenServer) ]]; then |
| 410 | DISTRO="xs$os_RELEASE" |
| 411 | else |
| 412 | # Catch-all for now is Vendor + Release + Update |
| 413 | DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" |
| 414 | fi |
| 415 | export DISTRO |
| 416 | } |
| 417 | |
| 418 | # Utility function for checking machine architecture |
| 419 | # is_arch arch-type |
| 420 | function is_arch { |
| 421 | ARCH_TYPE=$1 |
| 422 | |
| 423 | [[ "$(uname -m)" == "$ARCH_TYPE" ]] |
| 424 | } |
| 425 | |
| 426 | # Determine if current distribution is a Fedora-based distribution |
| 427 | # (Fedora, RHEL, CentOS, etc). |
| 428 | # is_fedora |
| 429 | function is_fedora { |
| 430 | if [[ -z "$os_VENDOR" ]]; then |
| 431 | GetOSVersion |
| 432 | fi |
| 433 | |
| 434 | [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ] |
| 435 | } |
| 436 | |
| 437 | |
| 438 | # Determine if current distribution is a SUSE-based distribution |
| 439 | # (openSUSE, SLE). |
| 440 | # is_suse |
| 441 | function is_suse { |
| 442 | if [[ -z "$os_VENDOR" ]]; then |
| 443 | GetOSVersion |
| 444 | fi |
| 445 | |
| 446 | [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ] |
| 447 | } |
| 448 | |
| 449 | |
| 450 | # Determine if current distribution is an Ubuntu-based distribution |
| 451 | # It will also detect non-Ubuntu but Debian-based distros |
| 452 | # is_ubuntu |
| 453 | function is_ubuntu { |
| 454 | if [[ -z "$os_PACKAGE" ]]; then |
| 455 | GetOSVersion |
| 456 | fi |
| 457 | [ "$os_PACKAGE" = "deb" ] |
| 458 | } |
| 459 | |
| 460 | |
| 461 | # Git Functions |
| 462 | # ============= |
| 463 | |
Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 464 | # Returns openstack release name for a given branch name |
| 465 | # ``get_release_name_from_branch branch-name`` |
| 466 | function get_release_name_from_branch(){ |
| 467 | local branch=$1 |
| 468 | if [[ $branch =~ "stable/" ]]; then |
| 469 | echo ${branch#*/} |
| 470 | else |
| 471 | echo "master" |
| 472 | fi |
| 473 | } |
| 474 | |
Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 475 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 476 | # be owned by the installation user, we create the directory and change the |
| 477 | # ownership to the proper user. |
| 478 | # Set global RECLONE=yes to simulate a clone when dest-dir exists |
| 479 | # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo |
| 480 | # does not exist (default is False, meaning the repo will be cloned). |
| 481 | # Uses global ``OFFLINE`` |
| 482 | # git_clone remote dest-dir branch |
| 483 | function git_clone { |
| 484 | GIT_REMOTE=$1 |
| 485 | GIT_DEST=$2 |
| 486 | GIT_REF=$3 |
| 487 | RECLONE=$(trueorfalse False $RECLONE) |
| 488 | |
| 489 | if [[ "$OFFLINE" = "True" ]]; then |
| 490 | echo "Running in offline mode, clones already exist" |
| 491 | # print out the results so we know what change was used in the logs |
| 492 | cd $GIT_DEST |
| 493 | git show --oneline | head -1 |
| 494 | return |
| 495 | fi |
| 496 | |
| 497 | if echo $GIT_REF | egrep -q "^refs"; then |
| 498 | # If our branch name is a gerrit style refs/changes/... |
| 499 | if [[ ! -d $GIT_DEST ]]; then |
| 500 | [[ "$ERROR_ON_CLONE" = "True" ]] && \ |
| 501 | die $LINENO "Cloning not allowed in this configuration" |
| 502 | git clone $GIT_REMOTE $GIT_DEST |
| 503 | fi |
| 504 | cd $GIT_DEST |
| 505 | git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD |
| 506 | else |
| 507 | # do a full clone only if the directory doesn't exist |
| 508 | if [[ ! -d $GIT_DEST ]]; then |
| 509 | [[ "$ERROR_ON_CLONE" = "True" ]] && \ |
| 510 | die $LINENO "Cloning not allowed in this configuration" |
| 511 | git clone $GIT_REMOTE $GIT_DEST |
| 512 | cd $GIT_DEST |
| 513 | # This checkout syntax works for both branches and tags |
| 514 | git checkout $GIT_REF |
| 515 | elif [[ "$RECLONE" = "True" ]]; then |
| 516 | # if it does exist then simulate what clone does if asked to RECLONE |
| 517 | cd $GIT_DEST |
| 518 | # set the url to pull from and fetch |
| 519 | git remote set-url origin $GIT_REMOTE |
| 520 | git fetch origin |
| 521 | # remove the existing ignored files (like pyc) as they cause breakage |
| 522 | # (due to the py files having older timestamps than our pyc, so python |
| 523 | # thinks the pyc files are correct using them) |
| 524 | find $GIT_DEST -name '*.pyc' -delete |
| 525 | |
| 526 | # handle GIT_REF accordingly to type (tag, branch) |
| 527 | if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then |
| 528 | git_update_tag $GIT_REF |
| 529 | elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then |
| 530 | git_update_branch $GIT_REF |
| 531 | elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then |
| 532 | git_update_remote_branch $GIT_REF |
| 533 | else |
| 534 | die $LINENO "$GIT_REF is neither branch nor tag" |
| 535 | fi |
| 536 | |
| 537 | fi |
| 538 | fi |
| 539 | |
| 540 | # print out the results so we know what change was used in the logs |
| 541 | cd $GIT_DEST |
| 542 | git show --oneline | head -1 |
| 543 | } |
| 544 | |
| 545 | # git update using reference as a branch. |
| 546 | # git_update_branch ref |
| 547 | function git_update_branch() { |
| 548 | |
| 549 | GIT_BRANCH=$1 |
| 550 | |
| 551 | git checkout -f origin/$GIT_BRANCH |
| 552 | # a local branch might not exist |
| 553 | git branch -D $GIT_BRANCH || true |
| 554 | git checkout -b $GIT_BRANCH |
| 555 | } |
| 556 | |
| 557 | # git update using reference as a branch. |
| 558 | # git_update_remote_branch ref |
| 559 | function git_update_remote_branch() { |
| 560 | |
| 561 | GIT_BRANCH=$1 |
| 562 | |
| 563 | git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH |
| 564 | } |
| 565 | |
| 566 | # git update using reference as a tag. Be careful editing source at that repo |
| 567 | # as working copy will be in a detached mode |
| 568 | # git_update_tag ref |
| 569 | function git_update_tag() { |
| 570 | |
| 571 | GIT_TAG=$1 |
| 572 | |
| 573 | git tag -d $GIT_TAG |
| 574 | # fetching given tag only |
| 575 | git fetch origin tag $GIT_TAG |
| 576 | git checkout -f $GIT_TAG |
| 577 | } |
| 578 | |
| 579 | |
| 580 | # OpenStack Functions |
| 581 | # =================== |
| 582 | |
| 583 | # Get the default value for HOST_IP |
| 584 | # get_default_host_ip fixed_range floating_range host_ip_iface host_ip |
| 585 | function get_default_host_ip() { |
| 586 | local fixed_range=$1 |
| 587 | local floating_range=$2 |
| 588 | local host_ip_iface=$3 |
| 589 | local host_ip=$4 |
| 590 | |
| 591 | # Find the interface used for the default route |
| 592 | host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)} |
| 593 | # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable |
| 594 | if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then |
| 595 | host_ip="" |
| 596 | host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'` |
| 597 | for IP in $host_ips; do |
| 598 | # Attempt to filter out IP addresses that are part of the fixed and |
| 599 | # floating range. Note that this method only works if the ``netaddr`` |
| 600 | # python library is installed. If it is not installed, an error |
| 601 | # will be printed and the first IP from the interface will be used. |
| 602 | # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct |
| 603 | # address. |
| 604 | if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then |
| 605 | host_ip=$IP |
| 606 | break; |
| 607 | fi |
| 608 | done |
| 609 | fi |
| 610 | echo $host_ip |
| 611 | } |
| 612 | |
| 613 | # Grab a numbered field from python prettytable output |
| 614 | # Fields are numbered starting with 1 |
| 615 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. |
| 616 | # get_field field-number |
| 617 | function get_field() { |
| 618 | while read data; do |
| 619 | if [ "$1" -lt 0 ]; then |
| 620 | field="(\$(NF$1))" |
| 621 | else |
| 622 | field="\$$(($1 + 1))" |
| 623 | fi |
| 624 | echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" |
| 625 | done |
| 626 | } |
| 627 | |
| 628 | # Add a policy to a policy.json file |
| 629 | # Do nothing if the policy already exists |
| 630 | # ``policy_add policy_file policy_name policy_permissions`` |
| 631 | function policy_add() { |
| 632 | local policy_file=$1 |
| 633 | local policy_name=$2 |
| 634 | local policy_perm=$3 |
| 635 | |
| 636 | if grep -q ${policy_name} ${policy_file}; then |
| 637 | echo "Policy ${policy_name} already exists in ${policy_file}" |
| 638 | return |
| 639 | fi |
| 640 | |
| 641 | # Add a terminating comma to policy lines without one |
| 642 | # Remove the closing '}' and all lines following to the end-of-file |
| 643 | local tmpfile=$(mktemp) |
| 644 | uniq ${policy_file} | sed -e ' |
| 645 | s/]$/],/ |
| 646 | /^[}]/,$d |
| 647 | ' > ${tmpfile} |
| 648 | |
| 649 | # Append policy and closing brace |
| 650 | echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile} |
| 651 | echo "}" >>${tmpfile} |
| 652 | |
| 653 | mv ${tmpfile} ${policy_file} |
| 654 | } |
| 655 | |
| 656 | |
| 657 | # Package Functions |
| 658 | # ================= |
| 659 | |
| 660 | # _get_package_dir |
| 661 | function _get_package_dir() { |
| 662 | local pkg_dir |
| 663 | if is_ubuntu; then |
| 664 | pkg_dir=$FILES/apts |
| 665 | elif is_fedora; then |
| 666 | pkg_dir=$FILES/rpms |
| 667 | elif is_suse; then |
| 668 | pkg_dir=$FILES/rpms-suse |
| 669 | else |
| 670 | exit_distro_not_supported "list of packages" |
| 671 | fi |
| 672 | echo "$pkg_dir" |
| 673 | } |
| 674 | |
| 675 | # Wrapper for ``apt-get`` to set cache and proxy environment variables |
| 676 | # Uses globals ``OFFLINE``, ``*_proxy`` |
| 677 | # apt_get operation package [package ...] |
| 678 | function apt_get() { |
| 679 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
| 680 | local sudo="sudo" |
| 681 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 682 | $sudo DEBIAN_FRONTEND=noninteractive \ |
| 683 | http_proxy=$http_proxy https_proxy=$https_proxy \ |
| 684 | no_proxy=$no_proxy \ |
| 685 | apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" |
| 686 | } |
| 687 | |
| 688 | # get_packages() collects a list of package names of any type from the |
| 689 | # prerequisite files in ``files/{apts|rpms}``. The list is intended |
| 690 | # to be passed to a package installer such as apt or yum. |
| 691 | # |
| 692 | # Only packages required for the services in 1st argument will be |
| 693 | # included. Two bits of metadata are recognized in the prerequisite files: |
| 694 | # |
| 695 | # - ``# NOPRIME`` defers installation to be performed later in `stack.sh` |
| 696 | # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection |
| 697 | # of the package to the distros listed. The distro names are case insensitive. |
| 698 | function get_packages() { |
| 699 | local services=$@ |
| 700 | local package_dir=$(_get_package_dir) |
| 701 | local file_to_parse |
| 702 | local service |
| 703 | |
| 704 | if [[ -z "$package_dir" ]]; then |
| 705 | echo "No package directory supplied" |
| 706 | return 1 |
| 707 | fi |
| 708 | if [[ -z "$DISTRO" ]]; then |
| 709 | GetDistro |
| 710 | fi |
| 711 | for service in ${services//,/ }; do |
| 712 | # Allow individual services to specify dependencies |
| 713 | if [[ -e ${package_dir}/${service} ]]; then |
| 714 | file_to_parse="${file_to_parse} $service" |
| 715 | fi |
| 716 | # NOTE(sdague) n-api needs glance for now because that's where |
| 717 | # glance client is |
| 718 | if [[ $service == n-api ]]; then |
| 719 | if [[ ! $file_to_parse =~ nova ]]; then |
| 720 | file_to_parse="${file_to_parse} nova" |
| 721 | fi |
| 722 | if [[ ! $file_to_parse =~ glance ]]; then |
| 723 | file_to_parse="${file_to_parse} glance" |
| 724 | fi |
| 725 | elif [[ $service == c-* ]]; then |
| 726 | if [[ ! $file_to_parse =~ cinder ]]; then |
| 727 | file_to_parse="${file_to_parse} cinder" |
| 728 | fi |
| 729 | elif [[ $service == ceilometer-* ]]; then |
| 730 | if [[ ! $file_to_parse =~ ceilometer ]]; then |
| 731 | file_to_parse="${file_to_parse} ceilometer" |
| 732 | fi |
| 733 | elif [[ $service == s-* ]]; then |
| 734 | if [[ ! $file_to_parse =~ swift ]]; then |
| 735 | file_to_parse="${file_to_parse} swift" |
| 736 | fi |
| 737 | elif [[ $service == n-* ]]; then |
| 738 | if [[ ! $file_to_parse =~ nova ]]; then |
| 739 | file_to_parse="${file_to_parse} nova" |
| 740 | fi |
| 741 | elif [[ $service == g-* ]]; then |
| 742 | if [[ ! $file_to_parse =~ glance ]]; then |
| 743 | file_to_parse="${file_to_parse} glance" |
| 744 | fi |
| 745 | elif [[ $service == key* ]]; then |
| 746 | if [[ ! $file_to_parse =~ keystone ]]; then |
| 747 | file_to_parse="${file_to_parse} keystone" |
| 748 | fi |
| 749 | elif [[ $service == q-* ]]; then |
| 750 | if [[ ! $file_to_parse =~ neutron ]]; then |
| 751 | file_to_parse="${file_to_parse} neutron" |
| 752 | fi |
| 753 | fi |
| 754 | done |
| 755 | |
| 756 | for file in ${file_to_parse}; do |
| 757 | local fname=${package_dir}/${file} |
| 758 | local OIFS line package distros distro |
| 759 | [[ -e $fname ]] || continue |
| 760 | |
| 761 | OIFS=$IFS |
| 762 | IFS=$'\n' |
| 763 | for line in $(<${fname}); do |
| 764 | if [[ $line =~ "NOPRIME" ]]; then |
| 765 | continue |
| 766 | fi |
| 767 | |
| 768 | # Assume we want this package |
| 769 | package=${line%#*} |
| 770 | inst_pkg=1 |
| 771 | |
| 772 | # Look for # dist:xxx in comment |
| 773 | if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then |
| 774 | # We are using BASH regexp matching feature. |
| 775 | package=${BASH_REMATCH[1]} |
| 776 | distros=${BASH_REMATCH[2]} |
| 777 | # In bash ${VAR,,} will lowecase VAR |
| 778 | # Look for a match in the distro list |
| 779 | if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then |
| 780 | # If no match then skip this package |
| 781 | inst_pkg=0 |
| 782 | fi |
| 783 | fi |
| 784 | |
| 785 | # Look for # testonly in comment |
| 786 | if [[ $line =~ (.*)#.*testonly.* ]]; then |
| 787 | package=${BASH_REMATCH[1]} |
| 788 | # Are we installing test packages? (test for the default value) |
| 789 | if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then |
| 790 | # If not installing test packages the skip this package |
| 791 | inst_pkg=0 |
| 792 | fi |
| 793 | fi |
| 794 | |
| 795 | if [[ $inst_pkg = 1 ]]; then |
| 796 | echo $package |
| 797 | fi |
| 798 | done |
| 799 | IFS=$OIFS |
| 800 | done |
| 801 | } |
| 802 | |
| 803 | # Distro-agnostic package installer |
| 804 | # install_package package [package ...] |
| 805 | function install_package() { |
| 806 | if is_ubuntu; then |
Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 807 | # if there are transient errors pulling the updates, that's fine. It may |
| 808 | # be secondary repositories that we don't really care about. |
| 809 | [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update || /bin/true |
Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 810 | NO_UPDATE_REPOS=True |
| 811 | |
| 812 | apt_get install "$@" |
| 813 | elif is_fedora; then |
| 814 | yum_install "$@" |
| 815 | elif is_suse; then |
| 816 | zypper_install "$@" |
| 817 | else |
| 818 | exit_distro_not_supported "installing packages" |
| 819 | fi |
| 820 | } |
| 821 | |
| 822 | # Distro-agnostic function to tell if a package is installed |
| 823 | # is_package_installed package [package ...] |
| 824 | function is_package_installed() { |
| 825 | if [[ -z "$@" ]]; then |
| 826 | return 1 |
| 827 | fi |
| 828 | |
| 829 | if [[ -z "$os_PACKAGE" ]]; then |
| 830 | GetOSVersion |
| 831 | fi |
| 832 | |
| 833 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 834 | dpkg -s "$@" > /dev/null 2> /dev/null |
| 835 | elif [[ "$os_PACKAGE" = "rpm" ]]; then |
| 836 | rpm --quiet -q "$@" |
| 837 | else |
| 838 | exit_distro_not_supported "finding if a package is installed" |
| 839 | fi |
| 840 | } |
| 841 | |
| 842 | # Distro-agnostic package uninstaller |
| 843 | # uninstall_package package [package ...] |
| 844 | function uninstall_package() { |
| 845 | if is_ubuntu; then |
| 846 | apt_get purge "$@" |
| 847 | elif is_fedora; then |
| 848 | sudo yum remove -y "$@" |
| 849 | elif is_suse; then |
| 850 | sudo zypper rm "$@" |
| 851 | else |
| 852 | exit_distro_not_supported "uninstalling packages" |
| 853 | fi |
| 854 | } |
| 855 | |
| 856 | # Wrapper for ``yum`` to set proxy environment variables |
| 857 | # Uses globals ``OFFLINE``, ``*_proxy`` |
| 858 | # yum_install package [package ...] |
| 859 | function yum_install() { |
| 860 | [[ "$OFFLINE" = "True" ]] && return |
| 861 | local sudo="sudo" |
| 862 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 863 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ |
| 864 | no_proxy=$no_proxy \ |
| 865 | yum install -y "$@" |
| 866 | } |
| 867 | |
| 868 | # zypper wrapper to set arguments correctly |
| 869 | # zypper_install package [package ...] |
| 870 | function zypper_install() { |
| 871 | [[ "$OFFLINE" = "True" ]] && return |
| 872 | local sudo="sudo" |
| 873 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 874 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ |
| 875 | zypper --non-interactive install --auto-agree-with-licenses "$@" |
| 876 | } |
| 877 | |
| 878 | |
| 879 | # Process Functions |
| 880 | # ================= |
| 881 | |
| 882 | # _run_process() is designed to be backgrounded by run_process() to simulate a |
| 883 | # fork. It includes the dirty work of closing extra filehandles and preparing log |
| 884 | # files to produce the same logs as screen_it(). The log filename is derived |
| 885 | # from the service name and global-and-now-misnamed SCREEN_LOGDIR |
| 886 | # _run_process service "command-line" |
| 887 | function _run_process() { |
| 888 | local service=$1 |
| 889 | local command="$2" |
| 890 | |
| 891 | # Undo logging redirections and close the extra descriptors |
| 892 | exec 1>&3 |
| 893 | exec 2>&3 |
| 894 | exec 3>&- |
| 895 | exec 6>&- |
| 896 | |
| 897 | if [[ -n ${SCREEN_LOGDIR} ]]; then |
| 898 | exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1 |
| 899 | ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log |
| 900 | |
| 901 | # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs. |
| 902 | export PYTHONUNBUFFERED=1 |
| 903 | fi |
| 904 | |
| 905 | exec /bin/bash -c "$command" |
| 906 | die "$service exec failure: $command" |
| 907 | } |
| 908 | |
| 909 | # Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``. |
| 910 | # This is used for ``service_check`` when all the ``screen_it`` are called finished |
| 911 | # init_service_check |
| 912 | function init_service_check() { |
| 913 | SCREEN_NAME=${SCREEN_NAME:-stack} |
| 914 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} |
| 915 | |
| 916 | if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then |
| 917 | mkdir -p "$SERVICE_DIR/$SCREEN_NAME" |
| 918 | fi |
| 919 | |
| 920 | rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure |
| 921 | } |
| 922 | |
| 923 | # Find out if a process exists by partial name. |
| 924 | # is_running name |
| 925 | function is_running() { |
| 926 | local name=$1 |
| 927 | ps auxw | grep -v grep | grep ${name} > /dev/null |
| 928 | RC=$? |
| 929 | # some times I really hate bash reverse binary logic |
| 930 | return $RC |
| 931 | } |
| 932 | |
| 933 | # run_process() launches a child process that closes all file descriptors and |
| 934 | # then exec's the passed in command. This is meant to duplicate the semantics |
| 935 | # of screen_it() without screen. PIDs are written to |
| 936 | # $SERVICE_DIR/$SCREEN_NAME/$service.pid |
| 937 | # run_process service "command-line" |
| 938 | function run_process() { |
| 939 | local service=$1 |
| 940 | local command="$2" |
| 941 | |
| 942 | # Spawn the child process |
| 943 | _run_process "$service" "$command" & |
| 944 | echo $! |
| 945 | } |
| 946 | |
| 947 | # Helper to launch a service in a named screen |
| 948 | # screen_it service "command-line" |
| 949 | function screen_it { |
| 950 | SCREEN_NAME=${SCREEN_NAME:-stack} |
| 951 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} |
| 952 | USE_SCREEN=$(trueorfalse True $USE_SCREEN) |
| 953 | |
| 954 | if is_service_enabled $1; then |
| 955 | # Append the service to the screen rc file |
| 956 | screen_rc "$1" "$2" |
| 957 | |
| 958 | if [[ "$USE_SCREEN" = "True" ]]; then |
| 959 | screen -S $SCREEN_NAME -X screen -t $1 |
| 960 | |
| 961 | if [[ -n ${SCREEN_LOGDIR} ]]; then |
| 962 | screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log |
| 963 | screen -S $SCREEN_NAME -p $1 -X log on |
| 964 | ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log |
| 965 | fi |
| 966 | |
| 967 | # sleep to allow bash to be ready to be send the command - we are |
| 968 | # creating a new window in screen and then sends characters, so if |
| 969 | # bash isn't running by the time we send the command, nothing happens |
| 970 | sleep 1.5 |
| 971 | |
| 972 | NL=`echo -ne '\015'` |
| 973 | # This fun command does the following: |
| 974 | # - the passed server command is backgrounded |
| 975 | # - the pid of the background process is saved in the usual place |
| 976 | # - the server process is brought back to the foreground |
| 977 | # - if the server process exits prematurely the fg command errors |
| 978 | # and a message is written to stdout and the service failure file |
| 979 | # The pid saved can be used in screen_stop() as a process group |
| 980 | # id to kill off all child processes |
| 981 | screen -S $SCREEN_NAME -p $1 -X stuff "$2 & echo \$! >$SERVICE_DIR/$SCREEN_NAME/$1.pid; fg || echo \"$1 failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL" |
| 982 | else |
| 983 | # Spawn directly without screen |
| 984 | run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid |
| 985 | fi |
| 986 | fi |
| 987 | } |
| 988 | |
| 989 | # Screen rc file builder |
| 990 | # screen_rc service "command-line" |
| 991 | function screen_rc { |
| 992 | SCREEN_NAME=${SCREEN_NAME:-stack} |
| 993 | SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc |
| 994 | if [[ ! -e $SCREENRC ]]; then |
| 995 | # Name the screen session |
| 996 | echo "sessionname $SCREEN_NAME" > $SCREENRC |
| 997 | # Set a reasonable statusbar |
| 998 | echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC |
| 999 | # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off |
| 1000 | echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC |
| 1001 | echo "screen -t shell bash" >> $SCREENRC |
| 1002 | fi |
| 1003 | # If this service doesn't already exist in the screenrc file |
| 1004 | if ! grep $1 $SCREENRC 2>&1 > /dev/null; then |
| 1005 | NL=`echo -ne '\015'` |
| 1006 | echo "screen -t $1 bash" >> $SCREENRC |
| 1007 | echo "stuff \"$2$NL\"" >> $SCREENRC |
| 1008 | |
| 1009 | if [[ -n ${SCREEN_LOGDIR} ]]; then |
| 1010 | echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC |
| 1011 | echo "log on" >>$SCREENRC |
| 1012 | fi |
| 1013 | fi |
| 1014 | } |
| 1015 | |
| 1016 | # Stop a service in screen |
| 1017 | # If a PID is available use it, kill the whole process group via TERM |
| 1018 | # If screen is being used kill the screen window; this will catch processes |
| 1019 | # that did not leave a PID behind |
| 1020 | # screen_stop service |
| 1021 | function screen_stop() { |
| 1022 | SCREEN_NAME=${SCREEN_NAME:-stack} |
| 1023 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} |
| 1024 | USE_SCREEN=$(trueorfalse True $USE_SCREEN) |
| 1025 | |
| 1026 | if is_service_enabled $1; then |
| 1027 | # Kill via pid if we have one available |
| 1028 | if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then |
| 1029 | pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid) |
| 1030 | rm $SERVICE_DIR/$SCREEN_NAME/$1.pid |
| 1031 | fi |
| 1032 | if [[ "$USE_SCREEN" = "True" ]]; then |
| 1033 | # Clean up the screen window |
| 1034 | screen -S $SCREEN_NAME -p $1 -X kill |
| 1035 | fi |
| 1036 | fi |
| 1037 | } |
| 1038 | |
| 1039 | # Helper to get the status of each running service |
| 1040 | # service_check |
| 1041 | function service_check() { |
| 1042 | local service |
| 1043 | local failures |
| 1044 | SCREEN_NAME=${SCREEN_NAME:-stack} |
| 1045 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} |
| 1046 | |
| 1047 | |
| 1048 | if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then |
| 1049 | echo "No service status directory found" |
| 1050 | return |
| 1051 | fi |
| 1052 | |
| 1053 | # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME |
| 1054 | failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null` |
| 1055 | |
| 1056 | for service in $failures; do |
| 1057 | service=`basename $service` |
| 1058 | service=${service%.failure} |
| 1059 | echo "Error: Service $service is not running" |
| 1060 | done |
| 1061 | |
| 1062 | if [ -n "$failures" ]; then |
| 1063 | echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh" |
| 1064 | fi |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | # Python Functions |
| 1069 | # ================ |
| 1070 | |
| 1071 | # Get the path to the pip command. |
| 1072 | # get_pip_command |
| 1073 | function get_pip_command() { |
| 1074 | which pip || which pip-python |
| 1075 | |
| 1076 | if [ $? -ne 0 ]; then |
| 1077 | die $LINENO "Unable to find pip; cannot continue" |
| 1078 | fi |
| 1079 | } |
| 1080 | |
| 1081 | # Get the path to the direcotry where python executables are installed. |
| 1082 | # get_python_exec_prefix |
| 1083 | function get_python_exec_prefix() { |
| 1084 | if is_fedora || is_suse; then |
| 1085 | echo "/usr/bin" |
| 1086 | else |
| 1087 | echo "/usr/local/bin" |
| 1088 | fi |
| 1089 | } |
| 1090 | |
| 1091 | # Wrapper for ``pip install`` to set cache and proxy environment variables |
| 1092 | # Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``, |
| 1093 | # ``TRACK_DEPENDS``, ``*_proxy`` |
| 1094 | # pip_install package [package ...] |
| 1095 | function pip_install { |
| 1096 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
| 1097 | if [[ -z "$os_PACKAGE" ]]; then |
| 1098 | GetOSVersion |
| 1099 | fi |
| 1100 | if [[ $TRACK_DEPENDS = True ]]; then |
| 1101 | source $DEST/.venv/bin/activate |
| 1102 | CMD_PIP=$DEST/.venv/bin/pip |
| 1103 | SUDO_PIP="env" |
| 1104 | else |
| 1105 | SUDO_PIP="sudo" |
| 1106 | CMD_PIP=$(get_pip_command) |
| 1107 | fi |
| 1108 | |
| 1109 | # Mirror option not needed anymore because pypi has CDN available, |
| 1110 | # but it's useful in certain circumstances |
| 1111 | PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False} |
| 1112 | if [[ "$PIP_USE_MIRRORS" != "False" ]]; then |
| 1113 | PIP_MIRROR_OPT="--use-mirrors" |
| 1114 | fi |
| 1115 | |
| 1116 | # pip < 1.4 has a bug where it will use an already existing build |
| 1117 | # directory unconditionally. Say an earlier component installs |
| 1118 | # foo v1.1; pip will have built foo's source in |
| 1119 | # /tmp/$USER-pip-build. Even if a later component specifies foo < |
| 1120 | # 1.1, the existing extracted build will be used and cause |
| 1121 | # confusing errors. By creating unique build directories we avoid |
| 1122 | # this problem. See https://github.com/pypa/pip/issues/709 |
| 1123 | local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX) |
| 1124 | |
| 1125 | $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \ |
| 1126 | HTTP_PROXY=$http_proxy \ |
| 1127 | HTTPS_PROXY=$https_proxy \ |
| 1128 | NO_PROXY=$no_proxy \ |
| 1129 | $CMD_PIP install --build=${pip_build_tmp} \ |
| 1130 | $PIP_MIRROR_OPT $@ \ |
| 1131 | && $SUDO_PIP rm -rf ${pip_build_tmp} |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | # Service Functions |
| 1136 | # ================= |
| 1137 | |
| 1138 | # remove extra commas from the input string (i.e. ``ENABLED_SERVICES``) |
| 1139 | # _cleanup_service_list service-list |
| 1140 | function _cleanup_service_list () { |
| 1141 | echo "$1" | sed -e ' |
| 1142 | s/,,/,/g; |
| 1143 | s/^,//; |
| 1144 | s/,$// |
| 1145 | ' |
| 1146 | } |
| 1147 | |
| 1148 | # disable_all_services() removes all current services |
| 1149 | # from ``ENABLED_SERVICES`` to reset the configuration |
| 1150 | # before a minimal installation |
| 1151 | # Uses global ``ENABLED_SERVICES`` |
| 1152 | # disable_all_services |
| 1153 | function disable_all_services() { |
| 1154 | ENABLED_SERVICES="" |
| 1155 | } |
| 1156 | |
| 1157 | # Remove all services starting with '-'. For example, to install all default |
| 1158 | # services except rabbit (rabbit) set in ``localrc``: |
| 1159 | # ENABLED_SERVICES+=",-rabbit" |
| 1160 | # Uses global ``ENABLED_SERVICES`` |
| 1161 | # disable_negated_services |
| 1162 | function disable_negated_services() { |
| 1163 | local tmpsvcs="${ENABLED_SERVICES}" |
| 1164 | local service |
| 1165 | for service in ${tmpsvcs//,/ }; do |
| 1166 | if [[ ${service} == -* ]]; then |
| 1167 | tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g") |
| 1168 | fi |
| 1169 | done |
| 1170 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 1171 | } |
| 1172 | |
| 1173 | # disable_service() removes the services passed as argument to the |
| 1174 | # ``ENABLED_SERVICES`` list, if they are present. |
| 1175 | # |
| 1176 | # For example: |
| 1177 | # disable_service rabbit |
| 1178 | # |
| 1179 | # This function does not know about the special cases |
| 1180 | # for nova, glance, and neutron built into is_service_enabled(). |
| 1181 | # Uses global ``ENABLED_SERVICES`` |
| 1182 | # disable_service service [service ...] |
| 1183 | function disable_service() { |
| 1184 | local tmpsvcs=",${ENABLED_SERVICES}," |
| 1185 | local service |
| 1186 | for service in $@; do |
| 1187 | if is_service_enabled $service; then |
| 1188 | tmpsvcs=${tmpsvcs//,$service,/,} |
| 1189 | fi |
| 1190 | done |
| 1191 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 1192 | } |
| 1193 | |
| 1194 | # enable_service() adds the services passed as argument to the |
| 1195 | # ``ENABLED_SERVICES`` list, if they are not already present. |
| 1196 | # |
| 1197 | # For example: |
| 1198 | # enable_service qpid |
| 1199 | # |
| 1200 | # This function does not know about the special cases |
| 1201 | # for nova, glance, and neutron built into is_service_enabled(). |
| 1202 | # Uses global ``ENABLED_SERVICES`` |
| 1203 | # enable_service service [service ...] |
| 1204 | function enable_service() { |
| 1205 | local tmpsvcs="${ENABLED_SERVICES}" |
| 1206 | for service in $@; do |
| 1207 | if ! is_service_enabled $service; then |
| 1208 | tmpsvcs+=",$service" |
| 1209 | fi |
| 1210 | done |
| 1211 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 1212 | disable_negated_services |
| 1213 | } |
| 1214 | |
| 1215 | # is_service_enabled() checks if the service(s) specified as arguments are |
| 1216 | # enabled by the user in ``ENABLED_SERVICES``. |
| 1217 | # |
| 1218 | # Multiple services specified as arguments are ``OR``'ed together; the test |
| 1219 | # is a short-circuit boolean, i.e it returns on the first match. |
| 1220 | # |
| 1221 | # There are special cases for some 'catch-all' services:: |
| 1222 | # **nova** returns true if any service enabled start with **n-** |
| 1223 | # **cinder** returns true if any service enabled start with **c-** |
| 1224 | # **ceilometer** returns true if any service enabled start with **ceilometer** |
| 1225 | # **glance** returns true if any service enabled start with **g-** |
| 1226 | # **neutron** returns true if any service enabled start with **q-** |
| 1227 | # **swift** returns true if any service enabled start with **s-** |
| 1228 | # **trove** returns true if any service enabled start with **tr-** |
| 1229 | # For backward compatibility if we have **swift** in ENABLED_SERVICES all the |
| 1230 | # **s-** services will be enabled. This will be deprecated in the future. |
| 1231 | # |
| 1232 | # Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``. |
| 1233 | # We also need to make sure to treat **n-cell-region** and **n-cell-child** |
| 1234 | # as enabled in this case. |
| 1235 | # |
| 1236 | # Uses global ``ENABLED_SERVICES`` |
| 1237 | # is_service_enabled service [service ...] |
| 1238 | function is_service_enabled() { |
| 1239 | services=$@ |
| 1240 | for service in ${services}; do |
| 1241 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 1242 | |
| 1243 | # Look for top-level 'enabled' function for this service |
| 1244 | if type is_${service}_enabled >/dev/null 2>&1; then |
| 1245 | # A function exists for this service, use it |
| 1246 | is_${service}_enabled |
| 1247 | return $? |
| 1248 | fi |
| 1249 | |
| 1250 | # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled() |
| 1251 | # are implemented |
| 1252 | |
| 1253 | [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && return 0 |
| 1254 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 |
| 1255 | [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0 |
| 1256 | [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0 |
| 1257 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 |
| 1258 | [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && return 0 |
| 1259 | [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 |
| 1260 | [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && return 0 |
| 1261 | [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0 |
| 1262 | [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0 |
| 1263 | done |
| 1264 | return 1 |
| 1265 | } |
| 1266 | |
| 1267 | # Toggle enable/disable_service for services that must run exclusive of each other |
| 1268 | # $1 The name of a variable containing a space-separated list of services |
| 1269 | # $2 The name of a variable in which to store the enabled service's name |
| 1270 | # $3 The name of the service to enable |
| 1271 | function use_exclusive_service { |
| 1272 | local options=${!1} |
| 1273 | local selection=$3 |
| 1274 | out=$2 |
| 1275 | [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1 |
| 1276 | for opt in $options;do |
| 1277 | [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt |
| 1278 | done |
| 1279 | eval "$out=$selection" |
| 1280 | return 0 |
| 1281 | } |
| 1282 | |
| 1283 | |
Masayuki Igawa | f6368d3 | 2014-02-20 13:31:26 +0900 | [diff] [blame^] | 1284 | # System Functions |
| 1285 | # ================ |
Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1286 | |
| 1287 | # Only run the command if the target file (the last arg) is not on an |
| 1288 | # NFS filesystem. |
| 1289 | function _safe_permission_operation() { |
| 1290 | local args=( $@ ) |
| 1291 | local last |
| 1292 | local sudo_cmd |
| 1293 | local dir_to_check |
| 1294 | |
| 1295 | let last="${#args[*]} - 1" |
| 1296 | |
| 1297 | dir_to_check=${args[$last]} |
| 1298 | if [ ! -d "$dir_to_check" ]; then |
| 1299 | dir_to_check=`dirname "$dir_to_check"` |
| 1300 | fi |
| 1301 | |
| 1302 | if is_nfs_directory "$dir_to_check" ; then |
| 1303 | return 0 |
| 1304 | fi |
| 1305 | |
| 1306 | if [[ $TRACK_DEPENDS = True ]]; then |
| 1307 | sudo_cmd="env" |
| 1308 | else |
| 1309 | sudo_cmd="sudo" |
| 1310 | fi |
| 1311 | |
| 1312 | $sudo_cmd $@ |
| 1313 | } |
| 1314 | |
| 1315 | # Exit 0 if address is in network or 1 if address is not in network |
| 1316 | # ip-range is in CIDR notation: 1.2.3.4/20 |
| 1317 | # address_in_net ip-address ip-range |
| 1318 | function address_in_net() { |
| 1319 | local ip=$1 |
| 1320 | local range=$2 |
| 1321 | local masklen=${range#*/} |
| 1322 | local network=$(maskip ${range%/*} $(cidr2netmask $masklen)) |
| 1323 | local subnet=$(maskip $ip $(cidr2netmask $masklen)) |
| 1324 | [[ $network == $subnet ]] |
| 1325 | } |
| 1326 | |
| 1327 | # Add a user to a group. |
| 1328 | # add_user_to_group user group |
| 1329 | function add_user_to_group() { |
| 1330 | local user=$1 |
| 1331 | local group=$2 |
| 1332 | |
| 1333 | if [[ -z "$os_VENDOR" ]]; then |
| 1334 | GetOSVersion |
| 1335 | fi |
| 1336 | |
| 1337 | # SLE11 and openSUSE 12.2 don't have the usual usermod |
| 1338 | if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then |
| 1339 | sudo usermod -a -G "$group" "$user" |
| 1340 | else |
| 1341 | sudo usermod -A "$group" "$user" |
| 1342 | fi |
| 1343 | } |
| 1344 | |
| 1345 | # Convert CIDR notation to a IPv4 netmask |
| 1346 | # cidr2netmask cidr-bits |
| 1347 | function cidr2netmask() { |
| 1348 | local maskpat="255 255 255 255" |
| 1349 | local maskdgt="254 252 248 240 224 192 128" |
| 1350 | set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3} |
| 1351 | echo ${1-0}.${2-0}.${3-0}.${4-0} |
| 1352 | } |
| 1353 | |
| 1354 | # Gracefully cp only if source file/dir exists |
| 1355 | # cp_it source destination |
| 1356 | function cp_it { |
| 1357 | if [ -e $1 ] || [ -d $1 ]; then |
| 1358 | cp -pRL $1 $2 |
| 1359 | fi |
| 1360 | } |
| 1361 | |
| 1362 | # HTTP and HTTPS proxy servers are supported via the usual environment variables [1] |
| 1363 | # ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in |
| 1364 | # ``localrc`` or on the command line if necessary:: |
| 1365 | # |
| 1366 | # [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html |
| 1367 | # |
| 1368 | # http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh |
| 1369 | |
| 1370 | function export_proxy_variables() { |
| 1371 | if [[ -n "$http_proxy" ]]; then |
| 1372 | export http_proxy=$http_proxy |
| 1373 | fi |
| 1374 | if [[ -n "$https_proxy" ]]; then |
| 1375 | export https_proxy=$https_proxy |
| 1376 | fi |
| 1377 | if [[ -n "$no_proxy" ]]; then |
| 1378 | export no_proxy=$no_proxy |
| 1379 | fi |
| 1380 | } |
| 1381 | |
| 1382 | # Returns true if the directory is on a filesystem mounted via NFS. |
| 1383 | function is_nfs_directory() { |
| 1384 | local mount_type=`stat -f -L -c %T $1` |
| 1385 | test "$mount_type" == "nfs" |
| 1386 | } |
| 1387 | |
| 1388 | # Return the network portion of the given IP address using netmask |
| 1389 | # netmask is in the traditional dotted-quad format |
| 1390 | # maskip ip-address netmask |
| 1391 | function maskip() { |
| 1392 | local ip=$1 |
| 1393 | local mask=$2 |
| 1394 | local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}" |
| 1395 | local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.})) |
| 1396 | echo $subnet |
| 1397 | } |
| 1398 | |
| 1399 | # Service wrapper to restart services |
| 1400 | # restart_service service-name |
| 1401 | function restart_service() { |
| 1402 | if is_ubuntu; then |
| 1403 | sudo /usr/sbin/service $1 restart |
| 1404 | else |
| 1405 | sudo /sbin/service $1 restart |
| 1406 | fi |
| 1407 | } |
| 1408 | |
| 1409 | # Only change permissions of a file or directory if it is not on an |
| 1410 | # NFS filesystem. |
| 1411 | function safe_chmod() { |
| 1412 | _safe_permission_operation chmod $@ |
| 1413 | } |
| 1414 | |
| 1415 | # Only change ownership of a file or directory if it is not on an NFS |
| 1416 | # filesystem. |
| 1417 | function safe_chown() { |
| 1418 | _safe_permission_operation chown $@ |
| 1419 | } |
| 1420 | |
| 1421 | # Service wrapper to start services |
| 1422 | # start_service service-name |
| 1423 | function start_service() { |
| 1424 | if is_ubuntu; then |
| 1425 | sudo /usr/sbin/service $1 start |
| 1426 | else |
| 1427 | sudo /sbin/service $1 start |
| 1428 | fi |
| 1429 | } |
| 1430 | |
| 1431 | # Service wrapper to stop services |
| 1432 | # stop_service service-name |
| 1433 | function stop_service() { |
| 1434 | if is_ubuntu; then |
| 1435 | sudo /usr/sbin/service $1 stop |
| 1436 | else |
| 1437 | sudo /sbin/service $1 stop |
| 1438 | fi |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | # Restore xtrace |
| 1443 | $XTRACE |
| 1444 | |
| 1445 | # Local variables: |
| 1446 | # mode: shell-script |
| 1447 | # End: |