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