| Sean Dague | e263c82 | 2014-12-05 14:25:28 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 3 | # functions-common - Common functions used by DevStack components | 
|  | 4 | # | 
|  | 5 | # The canonical copy of this file is maintained in the DevStack repo. | 
|  | 6 | # All modifications should be made there and then sync'ed to other repos | 
|  | 7 | # as required. | 
|  | 8 | # | 
|  | 9 | # This file is sorted alphabetically within the function groups. | 
|  | 10 | # | 
|  | 11 | # - Config Functions | 
|  | 12 | # - Control Functions | 
|  | 13 | # - Distro Functions | 
|  | 14 | # - Git Functions | 
|  | 15 | # - OpenStack Functions | 
|  | 16 | # - Package Functions | 
|  | 17 | # - Process Functions | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 18 | # - Service Functions | 
| Masayuki Igawa | f6368d3 | 2014-02-20 13:31:26 +0900 | [diff] [blame] | 19 | # - System Functions | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 20 | # | 
|  | 21 | # The following variables are assumed to be defined by certain functions: | 
|  | 22 | # | 
|  | 23 | # - ``ENABLED_SERVICES`` | 
|  | 24 | # - ``ERROR_ON_CLONE`` | 
|  | 25 | # - ``FILES`` | 
|  | 26 | # - ``OFFLINE`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 27 | # - ``RECLONE`` | 
| Masayuki Igawa | d20f632 | 2014-02-28 09:22:37 +0900 | [diff] [blame] | 28 | # - ``REQUIREMENTS_DIR`` | 
|  | 29 | # - ``STACK_USER`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 30 | # - ``TRACK_DEPENDS`` | 
| Masayuki Igawa | d20f632 | 2014-02-28 09:22:37 +0900 | [diff] [blame] | 31 | # - ``UNDO_REQUIREMENTS`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 32 | # - ``http_proxy``, ``https_proxy``, ``no_proxy`` | 
| Dean Troyer | 3324f19 | 2014-09-18 09:26:39 -0500 | [diff] [blame] | 33 | # | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 34 |  | 
|  | 35 | # Save trace setting | 
|  | 36 | XTRACE=$(set +o | grep xtrace) | 
|  | 37 | set +o xtrace | 
|  | 38 |  | 
| Sean Dague | cc52406 | 2014-10-01 09:06:43 -0400 | [diff] [blame] | 39 | # Global Config Variables | 
|  | 40 | declare -A GITREPO | 
|  | 41 | declare -A GITBRANCH | 
|  | 42 | declare -A GITDIR | 
|  | 43 |  | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 44 | TRACK_DEPENDS=${TRACK_DEPENDS:-False} | 
|  | 45 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 46 |  | 
|  | 47 | # Normalize config values to True or False | 
|  | 48 | # Accepts as False: 0 no No NO false False FALSE | 
|  | 49 | # Accepts as True: 1 yes Yes YES true True TRUE | 
|  | 50 | # VAR=$(trueorfalse default-value test-value) | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 51 | function trueorfalse { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 52 | local xtrace=$(set +o | grep xtrace) | 
|  | 53 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 54 | local default=$1 | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 55 | local literal=$2 | 
| Dean Troyer | a1b82cc | 2015-01-30 13:54:40 -0600 | [diff] [blame] | 56 | local testval=${!literal:-} | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 57 |  | 
|  | 58 | [[ -z "$testval" ]] && { echo "$default"; return; } | 
|  | 59 | [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; } | 
|  | 60 | [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; } | 
|  | 61 | echo "$default" | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 62 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
|  | 65 |  | 
|  | 66 | # Control Functions | 
|  | 67 | # ================= | 
|  | 68 |  | 
|  | 69 | # Prints backtrace info | 
|  | 70 | # filename:lineno:function | 
|  | 71 | # backtrace level | 
|  | 72 | function backtrace { | 
|  | 73 | local level=$1 | 
|  | 74 | local deep=$((${#BASH_SOURCE[@]} - 1)) | 
|  | 75 | echo "[Call Trace]" | 
|  | 76 | while [ $level -le $deep ]; do | 
|  | 77 | echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}" | 
|  | 78 | deep=$((deep - 1)) | 
|  | 79 | done | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | # Prints line number and "message" then exits | 
|  | 83 | # die $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 84 | function die { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 85 | local exitcode=$? | 
|  | 86 | set +o xtrace | 
|  | 87 | local line=$1; shift | 
|  | 88 | if [ $exitcode == 0 ]; then | 
|  | 89 | exitcode=1 | 
|  | 90 | fi | 
|  | 91 | backtrace 2 | 
|  | 92 | err $line "$*" | 
| Dean Troyer | a25a6f6 | 2014-02-24 16:03:41 -0600 | [diff] [blame] | 93 | # Give buffers a second to flush | 
|  | 94 | sleep 1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 95 | exit $exitcode | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | # Checks an environment variable is not set or has length 0 OR if the | 
|  | 99 | # exit code is non-zero and prints "message" and exits | 
|  | 100 | # NOTE: env-var is the variable name without a '$' | 
|  | 101 | # die_if_not_set $LINENO env-var "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 102 | function die_if_not_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 103 | local exitcode=$? | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 104 | local xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 105 | set +o xtrace | 
|  | 106 | local line=$1; shift | 
|  | 107 | local evar=$1; shift | 
|  | 108 | if ! is_set $evar || [ $exitcode != 0 ]; then | 
|  | 109 | die $line "$*" | 
|  | 110 | fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 111 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
|  | 114 | # Prints line number and "message" in error format | 
|  | 115 | # err $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 116 | function err { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 117 | local exitcode=$? | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 118 | local xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 119 | set +o xtrace | 
|  | 120 | local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2" | 
|  | 121 | echo $msg 1>&2; | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 122 | if [[ -n ${LOGDIR} ]]; then | 
|  | 123 | echo $msg >> "${LOGDIR}/error.log" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 124 | fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 125 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 126 | return $exitcode | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | # Checks an environment variable is not set or has length 0 OR if the | 
|  | 130 | # exit code is non-zero and prints "message" | 
|  | 131 | # NOTE: env-var is the variable name without a '$' | 
|  | 132 | # err_if_not_set $LINENO env-var "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 133 | function err_if_not_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 134 | local exitcode=$? | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 135 | local xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 136 | set +o xtrace | 
|  | 137 | local line=$1; shift | 
|  | 138 | local evar=$1; shift | 
|  | 139 | if ! is_set $evar || [ $exitcode != 0 ]; then | 
|  | 140 | err $line "$*" | 
|  | 141 | fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 142 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 143 | return $exitcode | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | # Exit after outputting a message about the distribution not being supported. | 
|  | 147 | # exit_distro_not_supported [optional-string-telling-what-is-missing] | 
|  | 148 | function exit_distro_not_supported { | 
|  | 149 | if [[ -z "$DISTRO" ]]; then | 
|  | 150 | GetDistro | 
|  | 151 | fi | 
|  | 152 |  | 
|  | 153 | if [ $# -gt 0 ]; then | 
|  | 154 | die $LINENO "Support for $DISTRO is incomplete: no support for $@" | 
|  | 155 | else | 
|  | 156 | die $LINENO "Support for $DISTRO is incomplete." | 
|  | 157 | fi | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | # Test if the named environment variable is set and not zero length | 
|  | 161 | # is_set env-var | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 162 | function is_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 163 | local var=\$"$1" | 
|  | 164 | eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | # Prints line number and "message" in warning format | 
|  | 168 | # warn $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 169 | function warn { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 170 | local exitcode=$? | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 171 | local xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 172 | set +o xtrace | 
|  | 173 | local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2" | 
|  | 174 | echo $msg 1>&2; | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 175 | if [[ -n ${LOGDIR} ]]; then | 
|  | 176 | echo $msg >> "${LOGDIR}/error.log" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 177 | fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 178 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 179 | return $exitcode | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 |  | 
|  | 183 | # Distro Functions | 
|  | 184 | # ================ | 
|  | 185 |  | 
|  | 186 | # Determine OS Vendor, Release and Update | 
|  | 187 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora | 
|  | 188 | # Returns results in global variables: | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 189 | # ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc | 
|  | 190 | # ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora) | 
|  | 191 | # ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5`` | 
|  | 192 | # ``os_PACKAGE`` - package type: ``deb`` or ``rpm`` | 
|  | 193 | # ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty`` | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 194 | os_VENDOR="" | 
|  | 195 | os_RELEASE="" | 
|  | 196 | os_UPDATE="" | 
|  | 197 | os_PACKAGE="" | 
|  | 198 | os_CODENAME="" | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 199 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 200 | # GetOSVersion | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 201 | function GetOSVersion { | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 202 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 203 | # Figure out which vendor we are | 
|  | 204 | if [[ -x "`which sw_vers 2>/dev/null`" ]]; then | 
|  | 205 | # OS/X | 
|  | 206 | os_VENDOR=`sw_vers -productName` | 
|  | 207 | os_RELEASE=`sw_vers -productVersion` | 
|  | 208 | os_UPDATE=${os_RELEASE##*.} | 
|  | 209 | os_RELEASE=${os_RELEASE%.*} | 
|  | 210 | os_PACKAGE="" | 
|  | 211 | if [[ "$os_RELEASE" =~ "10.7" ]]; then | 
|  | 212 | os_CODENAME="lion" | 
|  | 213 | elif [[ "$os_RELEASE" =~ "10.6" ]]; then | 
|  | 214 | os_CODENAME="snow leopard" | 
|  | 215 | elif [[ "$os_RELEASE" =~ "10.5" ]]; then | 
|  | 216 | os_CODENAME="leopard" | 
|  | 217 | elif [[ "$os_RELEASE" =~ "10.4" ]]; then | 
|  | 218 | os_CODENAME="tiger" | 
|  | 219 | elif [[ "$os_RELEASE" =~ "10.3" ]]; then | 
|  | 220 | os_CODENAME="panther" | 
|  | 221 | else | 
|  | 222 | os_CODENAME="" | 
|  | 223 | fi | 
|  | 224 | elif [[ -x $(which lsb_release 2>/dev/null) ]]; then | 
|  | 225 | os_VENDOR=$(lsb_release -i -s) | 
|  | 226 | os_RELEASE=$(lsb_release -r -s) | 
|  | 227 | os_UPDATE="" | 
|  | 228 | os_PACKAGE="rpm" | 
|  | 229 | if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then | 
|  | 230 | os_PACKAGE="deb" | 
|  | 231 | elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then | 
|  | 232 | lsb_release -d -s | grep -q openSUSE | 
|  | 233 | if [[ $? -eq 0 ]]; then | 
|  | 234 | os_VENDOR="openSUSE" | 
|  | 235 | fi | 
|  | 236 | elif [[ $os_VENDOR == "openSUSE project" ]]; then | 
|  | 237 | os_VENDOR="openSUSE" | 
|  | 238 | elif [[ $os_VENDOR =~ Red.*Hat ]]; then | 
|  | 239 | os_VENDOR="Red Hat" | 
|  | 240 | fi | 
|  | 241 | os_CODENAME=$(lsb_release -c -s) | 
|  | 242 | elif [[ -r /etc/redhat-release ]]; then | 
|  | 243 | # Red Hat Enterprise Linux Server release 5.5 (Tikanga) | 
|  | 244 | # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo) | 
|  | 245 | # CentOS release 5.5 (Final) | 
|  | 246 | # CentOS Linux release 6.0 (Final) | 
|  | 247 | # Fedora release 16 (Verne) | 
|  | 248 | # XenServer release 6.2.0-70446c (xenenterprise) | 
|  | 249 | os_CODENAME="" | 
|  | 250 | for r in "Red Hat" CentOS Fedora XenServer; do | 
|  | 251 | os_VENDOR=$r | 
|  | 252 | if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then | 
|  | 253 | ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` | 
|  | 254 | os_CODENAME=${ver#*|} | 
|  | 255 | os_RELEASE=${ver%|*} | 
|  | 256 | os_UPDATE=${os_RELEASE##*.} | 
|  | 257 | os_RELEASE=${os_RELEASE%.*} | 
|  | 258 | break | 
|  | 259 | fi | 
|  | 260 | os_VENDOR="" | 
|  | 261 | done | 
|  | 262 | os_PACKAGE="rpm" | 
|  | 263 | elif [[ -r /etc/SuSE-release ]]; then | 
|  | 264 | for r in openSUSE "SUSE Linux"; do | 
|  | 265 | if [[ "$r" = "SUSE Linux" ]]; then | 
|  | 266 | os_VENDOR="SUSE LINUX" | 
|  | 267 | else | 
|  | 268 | os_VENDOR=$r | 
|  | 269 | fi | 
|  | 270 |  | 
|  | 271 | if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then | 
|  | 272 | os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'` | 
|  | 273 | os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'` | 
|  | 274 | os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'` | 
|  | 275 | break | 
|  | 276 | fi | 
|  | 277 | os_VENDOR="" | 
|  | 278 | done | 
|  | 279 | os_PACKAGE="rpm" | 
|  | 280 | # If lsb_release is not installed, we should be able to detect Debian OS | 
|  | 281 | elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then | 
|  | 282 | os_VENDOR="Debian" | 
|  | 283 | os_PACKAGE="deb" | 
|  | 284 | os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}') | 
|  | 285 | os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g') | 
|  | 286 | fi | 
|  | 287 | export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | # Translate the OS version values into common nomenclature | 
|  | 291 | # Sets global ``DISTRO`` from the ``os_*`` values | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 292 | declare DISTRO | 
|  | 293 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 294 | function GetDistro { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 295 | GetOSVersion | 
|  | 296 | if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then | 
|  | 297 | # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective | 
|  | 298 | DISTRO=$os_CODENAME | 
|  | 299 | elif [[ "$os_VENDOR" =~ (Fedora) ]]; then | 
|  | 300 | # For Fedora, just use 'f' and the release | 
|  | 301 | DISTRO="f$os_RELEASE" | 
|  | 302 | elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then | 
|  | 303 | DISTRO="opensuse-$os_RELEASE" | 
|  | 304 | elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then | 
|  | 305 | # For SLE, also use the service pack | 
|  | 306 | if [[ -z "$os_UPDATE" ]]; then | 
|  | 307 | DISTRO="sle${os_RELEASE}" | 
|  | 308 | else | 
|  | 309 | DISTRO="sle${os_RELEASE}sp${os_UPDATE}" | 
|  | 310 | fi | 
| anju Tiwari | 6c639c9 | 2014-07-15 18:11:54 +0530 | [diff] [blame] | 311 | elif [[ "$os_VENDOR" =~ (Red Hat) || \ | 
|  | 312 | "$os_VENDOR" =~ (CentOS) || \ | 
|  | 313 | "$os_VENDOR" =~ (OracleServer) ]]; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 314 | # Drop the . release as we assume it's compatible | 
|  | 315 | DISTRO="rhel${os_RELEASE::1}" | 
|  | 316 | elif [[ "$os_VENDOR" =~ (XenServer) ]]; then | 
|  | 317 | DISTRO="xs$os_RELEASE" | 
|  | 318 | else | 
|  | 319 | # Catch-all for now is Vendor + Release + Update | 
|  | 320 | DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" | 
|  | 321 | fi | 
|  | 322 | export DISTRO | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | # Utility function for checking machine architecture | 
|  | 326 | # is_arch arch-type | 
|  | 327 | function is_arch { | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 328 | [[ "$(uname -m)" == "$1" ]] | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 329 | } | 
|  | 330 |  | 
|  | 331 | # Determine if current distribution is a Fedora-based distribution | 
|  | 332 | # (Fedora, RHEL, CentOS, etc). | 
|  | 333 | # is_fedora | 
|  | 334 | function is_fedora { | 
|  | 335 | if [[ -z "$os_VENDOR" ]]; then | 
|  | 336 | GetOSVersion | 
|  | 337 | fi | 
|  | 338 |  | 
| anju Tiwari | 6c639c9 | 2014-07-15 18:11:54 +0530 | [diff] [blame] | 339 | [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \ | 
|  | 340 | [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ] | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
|  | 343 |  | 
|  | 344 | # Determine if current distribution is a SUSE-based distribution | 
|  | 345 | # (openSUSE, SLE). | 
|  | 346 | # is_suse | 
|  | 347 | function is_suse { | 
|  | 348 | if [[ -z "$os_VENDOR" ]]; then | 
|  | 349 | GetOSVersion | 
|  | 350 | fi | 
|  | 351 |  | 
|  | 352 | [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ] | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 |  | 
|  | 356 | # Determine if current distribution is an Ubuntu-based distribution | 
|  | 357 | # It will also detect non-Ubuntu but Debian-based distros | 
|  | 358 | # is_ubuntu | 
|  | 359 | function is_ubuntu { | 
|  | 360 | if [[ -z "$os_PACKAGE" ]]; then | 
|  | 361 | GetOSVersion | 
|  | 362 | fi | 
|  | 363 | [ "$os_PACKAGE" = "deb" ] | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 |  | 
|  | 367 | # Git Functions | 
|  | 368 | # ============= | 
|  | 369 |  | 
| Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 370 | # Returns openstack release name for a given branch name | 
|  | 371 | # ``get_release_name_from_branch branch-name`` | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 372 | function get_release_name_from_branch { | 
| Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 373 | local branch=$1 | 
| Adam Gandelman | 8f38572 | 2014-10-14 15:50:18 -0700 | [diff] [blame] | 374 | if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then | 
| Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 375 | echo ${branch#*/} | 
|  | 376 | else | 
|  | 377 | echo "master" | 
|  | 378 | fi | 
|  | 379 | } | 
|  | 380 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 381 | # git clone only if directory doesn't exist already.  Since ``DEST`` might not | 
|  | 382 | # be owned by the installation user, we create the directory and change the | 
|  | 383 | # ownership to the proper user. | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 384 | # Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists | 
|  | 385 | # Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 386 | # does not exist (default is False, meaning the repo will be cloned). | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 387 | # Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 388 | # git_clone remote dest-dir branch | 
|  | 389 | function git_clone { | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 390 | local git_remote=$1 | 
|  | 391 | local git_dest=$2 | 
|  | 392 | local git_ref=$3 | 
|  | 393 | local orig_dir=$(pwd) | 
| Jamie Lennox | 51f0de5 | 2014-10-20 16:32:34 +0200 | [diff] [blame] | 394 | local git_clone_flags="" | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 395 |  | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 396 | RECLONE=$(trueorfalse False RECLONE) | 
| Kevin Benton | 59d52f3 | 2015-01-17 11:29:12 -0800 | [diff] [blame] | 397 | if [[ "${GIT_DEPTH}" -gt 0 ]]; then | 
| Jamie Lennox | 51f0de5 | 2014-10-20 16:32:34 +0200 | [diff] [blame] | 398 | git_clone_flags="$git_clone_flags --depth $GIT_DEPTH" | 
|  | 399 | fi | 
|  | 400 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 401 | if [[ "$OFFLINE" = "True" ]]; then | 
|  | 402 | echo "Running in offline mode, clones already exist" | 
|  | 403 | # print out the results so we know what change was used in the logs | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 404 | cd $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 405 | git show --oneline | head -1 | 
| Sean Dague | 64bd016 | 2014-03-12 13:04:22 -0400 | [diff] [blame] | 406 | cd $orig_dir | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 407 | return | 
|  | 408 | fi | 
|  | 409 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 410 | if echo $git_ref | egrep -q "^refs"; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 411 | # If our branch name is a gerrit style refs/changes/... | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 412 | if [[ ! -d $git_dest ]]; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 413 | [[ "$ERROR_ON_CLONE" = "True" ]] && \ | 
|  | 414 | die $LINENO "Cloning not allowed in this configuration" | 
| Jamie Lennox | 51f0de5 | 2014-10-20 16:32:34 +0200 | [diff] [blame] | 415 | git_timed clone $git_clone_flags $git_remote $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 416 | fi | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 417 | cd $git_dest | 
|  | 418 | git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 419 | else | 
|  | 420 | # do a full clone only if the directory doesn't exist | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 421 | if [[ ! -d $git_dest ]]; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 422 | [[ "$ERROR_ON_CLONE" = "True" ]] && \ | 
|  | 423 | die $LINENO "Cloning not allowed in this configuration" | 
| Jamie Lennox | 51f0de5 | 2014-10-20 16:32:34 +0200 | [diff] [blame] | 424 | git_timed clone $git_clone_flags $git_remote $git_dest | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 425 | cd $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 426 | # This checkout syntax works for both branches and tags | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 427 | git checkout $git_ref | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 428 | elif [[ "$RECLONE" = "True" ]]; then | 
|  | 429 | # if it does exist then simulate what clone does if asked to RECLONE | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 430 | cd $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 431 | # set the url to pull from and fetch | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 432 | git remote set-url origin $git_remote | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 433 | git_timed fetch origin | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 434 | # remove the existing ignored files (like pyc) as they cause breakage | 
|  | 435 | # (due to the py files having older timestamps than our pyc, so python | 
|  | 436 | # thinks the pyc files are correct using them) | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 437 | find $git_dest -name '*.pyc' -delete | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 438 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 439 | # handle git_ref accordingly to type (tag, branch) | 
|  | 440 | if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then | 
|  | 441 | git_update_tag $git_ref | 
|  | 442 | elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then | 
|  | 443 | git_update_branch $git_ref | 
|  | 444 | elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then | 
|  | 445 | git_update_remote_branch $git_ref | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 446 | else | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 447 | die $LINENO "$git_ref is neither branch nor tag" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 448 | fi | 
|  | 449 |  | 
|  | 450 | fi | 
|  | 451 | fi | 
|  | 452 |  | 
|  | 453 | # print out the results so we know what change was used in the logs | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 454 | cd $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 455 | git show --oneline | head -1 | 
| Sean Dague | 64bd016 | 2014-03-12 13:04:22 -0400 | [diff] [blame] | 456 | cd $orig_dir | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 457 | } | 
|  | 458 |  | 
| Sean Dague | cc52406 | 2014-10-01 09:06:43 -0400 | [diff] [blame] | 459 | # A variation on git clone that lets us specify a project by it's | 
|  | 460 | # actual name, like oslo.config. This is exceptionally useful in the | 
|  | 461 | # library installation case | 
|  | 462 | function git_clone_by_name { | 
|  | 463 | local name=$1 | 
|  | 464 | local repo=${GITREPO[$name]} | 
|  | 465 | local dir=${GITDIR[$name]} | 
|  | 466 | local branch=${GITBRANCH[$name]} | 
|  | 467 | git_clone $repo $dir $branch | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 |  | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 471 | # git can sometimes get itself infinitely stuck with transient network | 
|  | 472 | # errors or other issues with the remote end.  This wraps git in a | 
|  | 473 | # timeout/retry loop and is intended to watch over non-local git | 
|  | 474 | # processes that might hang.  GIT_TIMEOUT, if set, is passed directly | 
|  | 475 | # to timeout(1); otherwise the default value of 0 maintains the status | 
|  | 476 | # quo of waiting forever. | 
|  | 477 | # usage: git_timed <git-command> | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 478 | function git_timed { | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 479 | local count=0 | 
|  | 480 | local timeout=0 | 
|  | 481 |  | 
|  | 482 | if [[ -n "${GIT_TIMEOUT}" ]]; then | 
|  | 483 | timeout=${GIT_TIMEOUT} | 
|  | 484 | fi | 
|  | 485 |  | 
|  | 486 | until timeout -s SIGINT ${timeout} git "$@"; do | 
|  | 487 | # 124 is timeout(1)'s special return code when it reached the | 
|  | 488 | # timeout; otherwise assume fatal failure | 
|  | 489 | if [[ $? -ne 124 ]]; then | 
|  | 490 | die $LINENO "git call failed: [git $@]" | 
|  | 491 | fi | 
|  | 492 |  | 
|  | 493 | count=$(($count + 1)) | 
|  | 494 | warn "timeout ${count} for git call: [git $@]" | 
|  | 495 | if [ $count -eq 3 ]; then | 
|  | 496 | die $LINENO "Maximum of 3 git retries reached" | 
|  | 497 | fi | 
|  | 498 | sleep 5 | 
|  | 499 | done | 
|  | 500 | } | 
|  | 501 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 502 | # git update using reference as a branch. | 
|  | 503 | # git_update_branch ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 504 | function git_update_branch { | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 505 | local git_branch=$1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 506 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 507 | git checkout -f origin/$git_branch | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 508 | # a local branch might not exist | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 509 | git branch -D $git_branch || true | 
|  | 510 | git checkout -b $git_branch | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 511 | } | 
|  | 512 |  | 
|  | 513 | # git update using reference as a branch. | 
|  | 514 | # git_update_remote_branch ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 515 | function git_update_remote_branch { | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 516 | local git_branch=$1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 517 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 518 | git checkout -b $git_branch -t origin/$git_branch | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 519 | } | 
|  | 520 |  | 
|  | 521 | # git update using reference as a tag. Be careful editing source at that repo | 
|  | 522 | # as working copy will be in a detached mode | 
|  | 523 | # git_update_tag ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 524 | function git_update_tag { | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 525 | local git_tag=$1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 526 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 527 | git tag -d $git_tag | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 528 | # fetching given tag only | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 529 | git_timed fetch origin tag $git_tag | 
|  | 530 | git checkout -f $git_tag | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 531 | } | 
|  | 532 |  | 
|  | 533 |  | 
|  | 534 | # OpenStack Functions | 
|  | 535 | # =================== | 
|  | 536 |  | 
|  | 537 | # Get the default value for HOST_IP | 
|  | 538 | # get_default_host_ip fixed_range floating_range host_ip_iface host_ip | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 539 | function get_default_host_ip { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 540 | local fixed_range=$1 | 
|  | 541 | local floating_range=$2 | 
|  | 542 | local host_ip_iface=$3 | 
|  | 543 | local host_ip=$4 | 
|  | 544 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 545 | # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable | 
|  | 546 | if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then | 
|  | 547 | host_ip="" | 
| Andreas Scheuring | a343027 | 2015-03-09 16:55:32 +0100 | [diff] [blame] | 548 | # Find the interface used for the default route | 
|  | 549 | host_ip_iface=${host_ip_iface:-$(ip route | awk '/default/ {print $5}' | head -1)} | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 550 | local host_ips=$(LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}') | 
|  | 551 | local ip | 
|  | 552 | for ip in $host_ips; do | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 553 | # Attempt to filter out IP addresses that are part of the fixed and | 
|  | 554 | # floating range. Note that this method only works if the ``netaddr`` | 
|  | 555 | # python library is installed. If it is not installed, an error | 
|  | 556 | # will be printed and the first IP from the interface will be used. | 
|  | 557 | # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct | 
|  | 558 | # address. | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 559 | if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then | 
|  | 560 | host_ip=$ip | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 561 | break; | 
|  | 562 | fi | 
|  | 563 | done | 
|  | 564 | fi | 
|  | 565 | echo $host_ip | 
|  | 566 | } | 
|  | 567 |  | 
| Attila Fazekas | f71b500 | 2014-05-28 09:52:22 +0200 | [diff] [blame] | 568 | # Generates hex string from ``size`` byte of pseudo random data | 
|  | 569 | # generate_hex_string size | 
|  | 570 | function generate_hex_string { | 
|  | 571 | local size=$1 | 
|  | 572 | hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom | 
|  | 573 | } | 
|  | 574 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 575 | # Grab a numbered field from python prettytable output | 
|  | 576 | # Fields are numbered starting with 1 | 
|  | 577 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. | 
|  | 578 | # get_field field-number | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 579 | function get_field { | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 580 | local data field | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 581 | while read data; do | 
|  | 582 | if [ "$1" -lt 0 ]; then | 
|  | 583 | field="(\$(NF$1))" | 
|  | 584 | else | 
|  | 585 | field="\$$(($1 + 1))" | 
|  | 586 | fi | 
|  | 587 | echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" | 
|  | 588 | done | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | # Add a policy to a policy.json file | 
|  | 592 | # Do nothing if the policy already exists | 
|  | 593 | # ``policy_add policy_file policy_name policy_permissions`` | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 594 | function policy_add { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 595 | local policy_file=$1 | 
|  | 596 | local policy_name=$2 | 
|  | 597 | local policy_perm=$3 | 
|  | 598 |  | 
|  | 599 | if grep -q ${policy_name} ${policy_file}; then | 
|  | 600 | echo "Policy ${policy_name} already exists in ${policy_file}" | 
|  | 601 | return | 
|  | 602 | fi | 
|  | 603 |  | 
|  | 604 | # Add a terminating comma to policy lines without one | 
|  | 605 | # Remove the closing '}' and all lines following to the end-of-file | 
|  | 606 | local tmpfile=$(mktemp) | 
|  | 607 | uniq ${policy_file} | sed -e ' | 
|  | 608 | s/]$/],/ | 
|  | 609 | /^[}]/,$d | 
|  | 610 | ' > ${tmpfile} | 
|  | 611 |  | 
|  | 612 | # Append policy and closing brace | 
|  | 613 | echo "    \"${policy_name}\": ${policy_perm}" >>${tmpfile} | 
|  | 614 | echo "}" >>${tmpfile} | 
|  | 615 |  | 
|  | 616 | mv ${tmpfile} ${policy_file} | 
|  | 617 | } | 
|  | 618 |  | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 619 | # Gets or creates a domain | 
|  | 620 | # Usage: get_or_create_domain <name> <description> | 
|  | 621 | function get_or_create_domain { | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 622 | local os_url="$KEYSTONE_SERVICE_URI_V3" | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 623 | # Gets domain id | 
|  | 624 | local domain_id=$( | 
|  | 625 | # Gets domain id | 
|  | 626 | openstack --os-token=$OS_TOKEN --os-url=$os_url \ | 
|  | 627 | --os-identity-api-version=3 domain show $1 \ | 
|  | 628 | -f value -c id 2>/dev/null || | 
|  | 629 | # Creates new domain | 
|  | 630 | openstack --os-token=$OS_TOKEN --os-url=$os_url \ | 
|  | 631 | --os-identity-api-version=3 domain create $1 \ | 
|  | 632 | --description "$2" \ | 
|  | 633 | -f value -c id | 
|  | 634 | ) | 
|  | 635 | echo $domain_id | 
|  | 636 | } | 
|  | 637 |  | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 638 | # Gets or creates group | 
|  | 639 | # Usage: get_or_create_group <groupname> [<domain> <description>] | 
|  | 640 | function get_or_create_group { | 
|  | 641 | local domain=${2:+--domain ${2}} | 
|  | 642 | local desc="${3:-}" | 
|  | 643 | local os_url="$KEYSTONE_SERVICE_URI_V3" | 
|  | 644 | # Gets group id | 
|  | 645 | local group_id=$( | 
|  | 646 | # Creates new group with --or-show | 
|  | 647 | openstack --os-token=$OS_TOKEN --os-url=$os_url \ | 
|  | 648 | --os-identity-api-version=3 group create $1 \ | 
|  | 649 | $domain --description "$desc" --or-show \ | 
|  | 650 | -f value -c id | 
|  | 651 | ) | 
|  | 652 | echo $group_id | 
|  | 653 | } | 
|  | 654 |  | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 655 | # Gets or creates user | 
| Jamie Lennox | 18f39bf | 2015-01-28 13:38:32 +1000 | [diff] [blame] | 656 | # Usage: get_or_create_user <username> <password> [<email> [<domain>]] | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 657 | function get_or_create_user { | 
| Jamie Lennox | 18f39bf | 2015-01-28 13:38:32 +1000 | [diff] [blame] | 658 | if [[ ! -z "$3" ]]; then | 
|  | 659 | local email="--email=$3" | 
| Gael Chamoulaud | 6dd8a8b | 2014-07-22 01:12:12 +0200 | [diff] [blame] | 660 | else | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 661 | local email="" | 
| Gael Chamoulaud | 6dd8a8b | 2014-07-22 01:12:12 +0200 | [diff] [blame] | 662 | fi | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 663 | local os_cmd="openstack" | 
|  | 664 | local domain="" | 
| Jamie Lennox | 18f39bf | 2015-01-28 13:38:32 +1000 | [diff] [blame] | 665 | if [[ ! -z "$4" ]]; then | 
|  | 666 | domain="--domain=$4" | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 667 | os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3" | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 668 | fi | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 669 | # Gets user id | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 670 | local user_id=$( | 
| Steve Martinelli | 245daa2 | 2014-11-14 02:17:22 -0500 | [diff] [blame] | 671 | # Creates new user with --or-show | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 672 | $os_cmd user create \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 673 | $1 \ | 
|  | 674 | --password "$2" \ | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 675 | $email \ | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 676 | $domain \ | 
| Steve Martinelli | 245daa2 | 2014-11-14 02:17:22 -0500 | [diff] [blame] | 677 | --or-show \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 678 | -f value -c id | 
|  | 679 | ) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 680 | echo $user_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 681 | } | 
|  | 682 |  | 
|  | 683 | # Gets or creates project | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 684 | # Usage: get_or_create_project <name> [<domain>] | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 685 | function get_or_create_project { | 
|  | 686 | # Gets project id | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 687 | local os_cmd="openstack" | 
|  | 688 | local domain="" | 
|  | 689 | if [[ ! -z "$2" ]]; then | 
|  | 690 | domain="--domain=$2" | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 691 | os_cmd="$os_cmd --os-url=$KEYSTONE_SERVICE_URI_V3 --os-identity-api-version=3" | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 692 | fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 693 | local project_id=$( | 
| Steve Martinelli | 245daa2 | 2014-11-14 02:17:22 -0500 | [diff] [blame] | 694 | # Creates new project with --or-show | 
|  | 695 | $os_cmd project create $1 $domain --or-show -f value -c id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 696 | ) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 697 | echo $project_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 698 | } | 
|  | 699 |  | 
|  | 700 | # Gets or creates role | 
|  | 701 | # Usage: get_or_create_role <name> | 
|  | 702 | function get_or_create_role { | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 703 | local role_id=$( | 
| Steve Martinelli | 245daa2 | 2014-11-14 02:17:22 -0500 | [diff] [blame] | 704 | # Creates role with --or-show | 
|  | 705 | openstack role create $1 --or-show -f value -c id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 706 | ) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 707 | echo $role_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 708 | } | 
|  | 709 |  | 
| Jamie Lennox | 9b215db | 2015-02-10 18:19:57 +1100 | [diff] [blame] | 710 | # Gets or adds user role to project | 
|  | 711 | # Usage: get_or_add_user_project_role <role> <user> <project> | 
|  | 712 | function get_or_add_user_project_role { | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 713 | # Gets user role id | 
| Steve Martinelli | 5541a61 | 2015-01-19 15:58:49 -0500 | [diff] [blame] | 714 | local user_role_id=$(openstack role list \ | 
|  | 715 | --user $2 \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 716 | --project $3 \ | 
|  | 717 | --column "ID" \ | 
|  | 718 | --column "Name" \ | 
|  | 719 | | grep " $1 " | get_field 1) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 720 | if [[ -z "$user_role_id" ]]; then | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 721 | # Adds role to user | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 722 | user_role_id=$(openstack role add \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 723 | $1 \ | 
|  | 724 | --user $2 \ | 
|  | 725 | --project $3 \ | 
|  | 726 | | grep " id " | get_field 2) | 
|  | 727 | fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 728 | echo $user_role_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 729 | } | 
|  | 730 |  | 
|  | 731 | # Gets or creates service | 
|  | 732 | # Usage: get_or_create_service <name> <type> <description> | 
|  | 733 | function get_or_create_service { | 
|  | 734 | # Gets service id | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 735 | local service_id=$( | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 736 | # Gets service id | 
|  | 737 | openstack service show $1 -f value -c id 2>/dev/null || | 
|  | 738 | # Creates new service if not exists | 
|  | 739 | openstack service create \ | 
| Steve Martinelli | 789af5c | 2015-01-19 16:11:44 -0500 | [diff] [blame] | 740 | $2 \ | 
|  | 741 | --name $1 \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 742 | --description="$3" \ | 
|  | 743 | -f value -c id | 
|  | 744 | ) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 745 | echo $service_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 746 | } | 
|  | 747 |  | 
|  | 748 | # Gets or creates endpoint | 
|  | 749 | # Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl> | 
|  | 750 | function get_or_create_endpoint { | 
|  | 751 | # Gets endpoint id | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 752 | local endpoint_id=$(openstack endpoint list \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 753 | --column "ID" \ | 
|  | 754 | --column "Region" \ | 
|  | 755 | --column "Service Name" \ | 
|  | 756 | | grep " $2 " \ | 
|  | 757 | | grep " $1 " | get_field 1) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 758 | if [[ -z "$endpoint_id" ]]; then | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 759 | # Creates new endpoint | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 760 | endpoint_id=$(openstack endpoint create \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 761 | $1 \ | 
|  | 762 | --region $2 \ | 
|  | 763 | --publicurl $3 \ | 
|  | 764 | --adminurl $4 \ | 
|  | 765 | --internalurl $5 \ | 
|  | 766 | | grep " id " | get_field 2) | 
|  | 767 | fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 768 | echo $endpoint_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 769 | } | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 770 |  | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 771 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 772 | # Package Functions | 
|  | 773 | # ================= | 
|  | 774 |  | 
|  | 775 | # _get_package_dir | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 776 | function _get_package_dir { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 777 | local pkg_dir | 
|  | 778 | if is_ubuntu; then | 
| Monty Taylor | 81a016d | 2014-11-15 17:18:13 -0300 | [diff] [blame] | 779 | pkg_dir=$FILES/debs | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 780 | elif is_fedora; then | 
|  | 781 | pkg_dir=$FILES/rpms | 
|  | 782 | elif is_suse; then | 
|  | 783 | pkg_dir=$FILES/rpms-suse | 
|  | 784 | else | 
|  | 785 | exit_distro_not_supported "list of packages" | 
|  | 786 | fi | 
|  | 787 | echo "$pkg_dir" | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | # Wrapper for ``apt-get`` to set cache and proxy environment variables | 
|  | 791 | # Uses globals ``OFFLINE``, ``*_proxy`` | 
|  | 792 | # apt_get operation package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 793 | function apt_get { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 794 | local xtrace=$(set +o | grep xtrace) | 
|  | 795 | set +o xtrace | 
|  | 796 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 797 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return | 
|  | 798 | local sudo="sudo" | 
|  | 799 | [[ "$(id -u)" = "0" ]] && sudo="env" | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 800 |  | 
|  | 801 | $xtrace | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 802 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 803 | $sudo DEBIAN_FRONTEND=noninteractive \ | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 804 | http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \ | 
|  | 805 | no_proxy=${no_proxy:-} \ | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 806 | apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" | 
|  | 807 | } | 
|  | 808 |  | 
|  | 809 | # get_packages() collects a list of package names of any type from the | 
| Monty Taylor | 81a016d | 2014-11-15 17:18:13 -0300 | [diff] [blame] | 810 | # prerequisite files in ``files/{debs|rpms}``.  The list is intended | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 811 | # to be passed to a package installer such as apt or yum. | 
|  | 812 | # | 
|  | 813 | # Only packages required for the services in 1st argument will be | 
|  | 814 | # included.  Two bits of metadata are recognized in the prerequisite files: | 
|  | 815 | # | 
|  | 816 | # - ``# NOPRIME`` defers installation to be performed later in `stack.sh` | 
|  | 817 | # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection | 
|  | 818 | #   of the package to the distros listed.  The distro names are case insensitive. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 819 | function get_packages { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 820 | local xtrace=$(set +o | grep xtrace) | 
|  | 821 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 822 | local services=$@ | 
|  | 823 | local package_dir=$(_get_package_dir) | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 824 | local file_to_parse="" | 
|  | 825 | local service="" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 826 |  | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 827 | INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES) | 
| Flavio Percoco | 5a91c35 | 2014-10-31 18:48:00 +0100 | [diff] [blame] | 828 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 829 | if [[ -z "$package_dir" ]]; then | 
|  | 830 | echo "No package directory supplied" | 
|  | 831 | return 1 | 
|  | 832 | fi | 
|  | 833 | if [[ -z "$DISTRO" ]]; then | 
|  | 834 | GetDistro | 
|  | 835 | fi | 
|  | 836 | for service in ${services//,/ }; do | 
|  | 837 | # Allow individual services to specify dependencies | 
|  | 838 | if [[ -e ${package_dir}/${service} ]]; then | 
|  | 839 | file_to_parse="${file_to_parse} $service" | 
|  | 840 | fi | 
|  | 841 | # NOTE(sdague) n-api needs glance for now because that's where | 
|  | 842 | # glance client is | 
|  | 843 | if [[ $service == n-api ]]; then | 
|  | 844 | if [[ ! $file_to_parse =~ nova ]]; then | 
|  | 845 | file_to_parse="${file_to_parse} nova" | 
|  | 846 | fi | 
|  | 847 | if [[ ! $file_to_parse =~ glance ]]; then | 
|  | 848 | file_to_parse="${file_to_parse} glance" | 
|  | 849 | fi | 
|  | 850 | elif [[ $service == c-* ]]; then | 
|  | 851 | if [[ ! $file_to_parse =~ cinder ]]; then | 
|  | 852 | file_to_parse="${file_to_parse} cinder" | 
|  | 853 | fi | 
|  | 854 | elif [[ $service == ceilometer-* ]]; then | 
|  | 855 | if [[ ! $file_to_parse =~ ceilometer ]]; then | 
|  | 856 | file_to_parse="${file_to_parse} ceilometer" | 
|  | 857 | fi | 
|  | 858 | elif [[ $service == s-* ]]; then | 
|  | 859 | if [[ ! $file_to_parse =~ swift ]]; then | 
|  | 860 | file_to_parse="${file_to_parse} swift" | 
|  | 861 | fi | 
|  | 862 | elif [[ $service == n-* ]]; then | 
|  | 863 | if [[ ! $file_to_parse =~ nova ]]; then | 
|  | 864 | file_to_parse="${file_to_parse} nova" | 
|  | 865 | fi | 
|  | 866 | elif [[ $service == g-* ]]; then | 
|  | 867 | if [[ ! $file_to_parse =~ glance ]]; then | 
|  | 868 | file_to_parse="${file_to_parse} glance" | 
|  | 869 | fi | 
|  | 870 | elif [[ $service == key* ]]; then | 
|  | 871 | if [[ ! $file_to_parse =~ keystone ]]; then | 
|  | 872 | file_to_parse="${file_to_parse} keystone" | 
|  | 873 | fi | 
|  | 874 | elif [[ $service == q-* ]]; then | 
|  | 875 | if [[ ! $file_to_parse =~ neutron ]]; then | 
|  | 876 | file_to_parse="${file_to_parse} neutron" | 
|  | 877 | fi | 
| Adam Gandelman | 539ec43 | 2014-03-18 18:57:43 -0700 | [diff] [blame] | 878 | elif [[ $service == ir-* ]]; then | 
|  | 879 | if [[ ! $file_to_parse =~ ironic ]]; then | 
|  | 880 | file_to_parse="${file_to_parse} ironic" | 
|  | 881 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 882 | fi | 
|  | 883 | done | 
|  | 884 |  | 
|  | 885 | for file in ${file_to_parse}; do | 
|  | 886 | local fname=${package_dir}/${file} | 
|  | 887 | local OIFS line package distros distro | 
|  | 888 | [[ -e $fname ]] || continue | 
|  | 889 |  | 
|  | 890 | OIFS=$IFS | 
|  | 891 | IFS=$'\n' | 
|  | 892 | for line in $(<${fname}); do | 
|  | 893 | if [[ $line =~ "NOPRIME" ]]; then | 
|  | 894 | continue | 
|  | 895 | fi | 
|  | 896 |  | 
|  | 897 | # Assume we want this package | 
|  | 898 | package=${line%#*} | 
|  | 899 | inst_pkg=1 | 
|  | 900 |  | 
|  | 901 | # Look for # dist:xxx in comment | 
|  | 902 | if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then | 
|  | 903 | # We are using BASH regexp matching feature. | 
|  | 904 | package=${BASH_REMATCH[1]} | 
|  | 905 | distros=${BASH_REMATCH[2]} | 
|  | 906 | # In bash ${VAR,,} will lowecase VAR | 
|  | 907 | # Look for a match in the distro list | 
|  | 908 | if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then | 
|  | 909 | # If no match then skip this package | 
|  | 910 | inst_pkg=0 | 
|  | 911 | fi | 
|  | 912 | fi | 
|  | 913 |  | 
|  | 914 | # Look for # testonly in comment | 
|  | 915 | if [[ $line =~ (.*)#.*testonly.* ]]; then | 
|  | 916 | package=${BASH_REMATCH[1]} | 
|  | 917 | # Are we installing test packages? (test for the default value) | 
|  | 918 | if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then | 
|  | 919 | # If not installing test packages the skip this package | 
|  | 920 | inst_pkg=0 | 
|  | 921 | fi | 
|  | 922 | fi | 
|  | 923 |  | 
|  | 924 | if [[ $inst_pkg = 1 ]]; then | 
|  | 925 | echo $package | 
|  | 926 | fi | 
|  | 927 | done | 
|  | 928 | IFS=$OIFS | 
|  | 929 | done | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 930 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 931 | } | 
|  | 932 |  | 
|  | 933 | # Distro-agnostic package installer | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 934 | # Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 935 | # install_package package [package ...] | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 936 | function update_package_repo { | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 937 | NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False} | 
|  | 938 | REPOS_UPDATED=${REPOS_UPDATED:-False} | 
|  | 939 | RETRY_UPDATE=${RETRY_UPDATE:-False} | 
|  | 940 |  | 
| Paul Linchpiner | 9e17974 | 2014-07-13 22:23:00 -0700 | [diff] [blame] | 941 | if [[ "$NO_UPDATE_REPOS" = "True" ]]; then | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 942 | return 0 | 
|  | 943 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 944 |  | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 945 | if is_ubuntu; then | 
|  | 946 | local xtrace=$(set +o | grep xtrace) | 
|  | 947 | set +o xtrace | 
|  | 948 | if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then | 
|  | 949 | # if there are transient errors pulling the updates, that's fine. | 
|  | 950 | # It may be secondary repositories that we don't really care about. | 
|  | 951 | apt_get update  || /bin/true | 
|  | 952 | REPOS_UPDATED=True | 
|  | 953 | fi | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 954 | $xtrace | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 955 | fi | 
|  | 956 | } | 
|  | 957 |  | 
|  | 958 | function real_install_package { | 
|  | 959 | if is_ubuntu; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 960 | apt_get install "$@" | 
|  | 961 | elif is_fedora; then | 
|  | 962 | yum_install "$@" | 
|  | 963 | elif is_suse; then | 
|  | 964 | zypper_install "$@" | 
|  | 965 | else | 
|  | 966 | exit_distro_not_supported "installing packages" | 
|  | 967 | fi | 
|  | 968 | } | 
|  | 969 |  | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 970 | # Distro-agnostic package installer | 
|  | 971 | # install_package package [package ...] | 
|  | 972 | function install_package { | 
|  | 973 | update_package_repo | 
|  | 974 | real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@ | 
|  | 975 | } | 
|  | 976 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 977 | # Distro-agnostic function to tell if a package is installed | 
|  | 978 | # is_package_installed package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 979 | function is_package_installed { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 980 | if [[ -z "$@" ]]; then | 
|  | 981 | return 1 | 
|  | 982 | fi | 
|  | 983 |  | 
|  | 984 | if [[ -z "$os_PACKAGE" ]]; then | 
|  | 985 | GetOSVersion | 
|  | 986 | fi | 
|  | 987 |  | 
|  | 988 | if [[ "$os_PACKAGE" = "deb" ]]; then | 
|  | 989 | dpkg -s "$@" > /dev/null 2> /dev/null | 
|  | 990 | elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
|  | 991 | rpm --quiet -q "$@" | 
|  | 992 | else | 
|  | 993 | exit_distro_not_supported "finding if a package is installed" | 
|  | 994 | fi | 
|  | 995 | } | 
|  | 996 |  | 
|  | 997 | # Distro-agnostic package uninstaller | 
|  | 998 | # uninstall_package package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 999 | function uninstall_package { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1000 | if is_ubuntu; then | 
|  | 1001 | apt_get purge "$@" | 
|  | 1002 | elif is_fedora; then | 
| Ian Wienand | 36298ee | 2015-02-04 10:29:31 +1100 | [diff] [blame] | 1003 | sudo ${YUM:-yum} remove -y "$@" ||: | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1004 | elif is_suse; then | 
|  | 1005 | sudo zypper rm "$@" | 
|  | 1006 | else | 
|  | 1007 | exit_distro_not_supported "uninstalling packages" | 
|  | 1008 | fi | 
|  | 1009 | } | 
|  | 1010 |  | 
|  | 1011 | # Wrapper for ``yum`` to set proxy environment variables | 
| Daniel P. Berrange | 63d25d9 | 2014-12-09 15:21:22 +0000 | [diff] [blame] | 1012 | # Uses globals ``OFFLINE``, ``*_proxy``, ``YUM`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1013 | # yum_install package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1014 | function yum_install { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1015 | [[ "$OFFLINE" = "True" ]] && return | 
|  | 1016 | local sudo="sudo" | 
|  | 1017 | [[ "$(id -u)" = "0" ]] && sudo="env" | 
| Ian Wienand | b27f16d | 2014-02-28 14:29:02 +1100 | [diff] [blame] | 1018 |  | 
|  | 1019 | # The manual check for missing packages is because yum -y assumes | 
|  | 1020 | # missing packages are OK.  See | 
|  | 1021 | # https://bugzilla.redhat.com/show_bug.cgi?id=965567 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1022 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ | 
|  | 1023 | no_proxy=$no_proxy \ | 
| Ian Wienand | 36298ee | 2015-02-04 10:29:31 +1100 | [diff] [blame] | 1024 | ${YUM:-yum} install -y "$@" 2>&1 | \ | 
| Ian Wienand | b27f16d | 2014-02-28 14:29:02 +1100 | [diff] [blame] | 1025 | awk ' | 
|  | 1026 | BEGIN { fail=0 } | 
|  | 1027 | /No package/ { fail=1 } | 
|  | 1028 | { print } | 
|  | 1029 | END { exit fail }' || \ | 
|  | 1030 | die $LINENO "Missing packages detected" | 
|  | 1031 |  | 
|  | 1032 | # also ensure we catch a yum failure | 
|  | 1033 | if [[ ${PIPESTATUS[0]} != 0 ]]; then | 
| Ian Wienand | 36298ee | 2015-02-04 10:29:31 +1100 | [diff] [blame] | 1034 | die $LINENO "${YUM:-yum} install failure" | 
| Ian Wienand | b27f16d | 2014-02-28 14:29:02 +1100 | [diff] [blame] | 1035 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1036 | } | 
|  | 1037 |  | 
|  | 1038 | # zypper wrapper to set arguments correctly | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1039 | # Uses globals ``OFFLINE``, ``*_proxy`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1040 | # zypper_install package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1041 | function zypper_install { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1042 | [[ "$OFFLINE" = "True" ]] && return | 
|  | 1043 | local sudo="sudo" | 
|  | 1044 | [[ "$(id -u)" = "0" ]] && sudo="env" | 
|  | 1045 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ | 
|  | 1046 | zypper --non-interactive install --auto-agree-with-licenses "$@" | 
|  | 1047 | } | 
|  | 1048 |  | 
|  | 1049 |  | 
|  | 1050 | # Process Functions | 
|  | 1051 | # ================= | 
|  | 1052 |  | 
|  | 1053 | # _run_process() is designed to be backgrounded by run_process() to simulate a | 
|  | 1054 | # fork.  It includes the dirty work of closing extra filehandles and preparing log | 
|  | 1055 | # files to produce the same logs as screen_it().  The log filename is derived | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 1056 | # from the service name. | 
|  | 1057 | # Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR`` | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1058 | # If an optional group is provided sg will be used to set the group of | 
|  | 1059 | # the command. | 
|  | 1060 | # _run_process service "command-line" [group] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1061 | function _run_process { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1062 | local service=$1 | 
|  | 1063 | local command="$2" | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1064 | local group=$3 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1065 |  | 
|  | 1066 | # Undo logging redirections and close the extra descriptors | 
|  | 1067 | exec 1>&3 | 
|  | 1068 | exec 2>&3 | 
|  | 1069 | exec 3>&- | 
|  | 1070 | exec 6>&- | 
|  | 1071 |  | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 1072 | local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}" | 
|  | 1073 | if [[ -n ${LOGDIR} ]]; then | 
|  | 1074 | exec 1>&"$real_logfile" 2>&1 | 
|  | 1075 | ln -sf "$real_logfile" ${LOGDIR}/${service}.log | 
|  | 1076 | if [[ -n ${SCREEN_LOGDIR} ]]; then | 
|  | 1077 | # Drop the backward-compat symlink | 
|  | 1078 | ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log | 
|  | 1079 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1080 |  | 
|  | 1081 | # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs. | 
|  | 1082 | export PYTHONUNBUFFERED=1 | 
|  | 1083 | fi | 
|  | 1084 |  | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1085 | # Run under ``setsid`` to force the process to become a session and group leader. | 
|  | 1086 | # The pid saved can be used with pkill -g to get the entire process group. | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1087 | if [[ -n "$group" ]]; then | 
|  | 1088 | setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid | 
|  | 1089 | else | 
|  | 1090 | setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid | 
|  | 1091 | fi | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1092 |  | 
|  | 1093 | # Just silently exit this process | 
|  | 1094 | exit 0 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1095 | } | 
|  | 1096 |  | 
|  | 1097 | # Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``. | 
|  | 1098 | # This is used for ``service_check`` when all the ``screen_it`` are called finished | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1099 | # Uses globals ``SCREEN_NAME``, ``SERVICE_DIR`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1100 | # init_service_check | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1101 | function init_service_check { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1102 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1103 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
|  | 1104 |  | 
|  | 1105 | if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then | 
|  | 1106 | mkdir -p "$SERVICE_DIR/$SCREEN_NAME" | 
|  | 1107 | fi | 
|  | 1108 |  | 
|  | 1109 | rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure | 
|  | 1110 | } | 
|  | 1111 |  | 
|  | 1112 | # Find out if a process exists by partial name. | 
|  | 1113 | # is_running name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1114 | function is_running { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1115 | local name=$1 | 
|  | 1116 | ps auxw | grep -v grep | grep ${name} > /dev/null | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1117 | local exitcode=$? | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1118 | # some times I really hate bash reverse binary logic | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1119 | return $exitcode | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1120 | } | 
|  | 1121 |  | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1122 | # Run a single service under screen or directly | 
|  | 1123 | # If the command includes shell metachatacters (;<>*) it must be run using a shell | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1124 | # If an optional group is provided sg will be used to run the | 
|  | 1125 | # command as that group. | 
|  | 1126 | # run_process service "command-line" [group] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1127 | function run_process { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1128 | local service=$1 | 
|  | 1129 | local command="$2" | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1130 | local group=$3 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1131 |  | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1132 | if is_service_enabled $service; then | 
|  | 1133 | if [[ "$USE_SCREEN" = "True" ]]; then | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1134 | screen_process "$service" "$command" "$group" | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1135 | else | 
|  | 1136 | # Spawn directly without screen | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1137 | _run_process "$service" "$command" "$group" & | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1138 | fi | 
|  | 1139 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1140 | } | 
|  | 1141 |  | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1142 | # Helper to launch a process in a named screen | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 1143 | # Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``, | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1144 | # ``SERVICE_DIR``, ``USE_SCREEN`` | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1145 | # screen_process name "command-line" [group] | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1146 | # Run a command in a shell in a screen window, if an optional group | 
|  | 1147 | # is provided, use sg to set the group of the command. | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1148 | function screen_process { | 
|  | 1149 | local name=$1 | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1150 | local command="$2" | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1151 | local group=$3 | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1152 |  | 
| Sean Dague | ea22a4f | 2014-06-27 15:21:41 -0400 | [diff] [blame] | 1153 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1154 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1155 | USE_SCREEN=$(trueorfalse True USE_SCREEN) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1156 |  | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1157 | # Append the process to the screen rc file | 
|  | 1158 | screen_rc "$name" "$command" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1159 |  | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1160 | screen -S $SCREEN_NAME -X screen -t $name | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1161 |  | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 1162 | local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}" | 
|  | 1163 | echo "LOGDIR: $LOGDIR" | 
|  | 1164 | echo "SCREEN_LOGDIR: $SCREEN_LOGDIR" | 
|  | 1165 | echo "log: $real_logfile" | 
|  | 1166 | if [[ -n ${LOGDIR} ]]; then | 
|  | 1167 | screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile" | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1168 | screen -S $SCREEN_NAME -p $name -X log on | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 1169 | ln -sf "$real_logfile" ${LOGDIR}/${name}.log | 
|  | 1170 | if [[ -n ${SCREEN_LOGDIR} ]]; then | 
|  | 1171 | # Drop the backward-compat symlink | 
|  | 1172 | ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log | 
|  | 1173 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1174 | fi | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1175 |  | 
|  | 1176 | # sleep to allow bash to be ready to be send the command - we are | 
|  | 1177 | # creating a new window in screen and then sends characters, so if | 
|  | 1178 | # bash isn't running by the time we send the command, nothing happens | 
|  | 1179 | sleep 3 | 
|  | 1180 |  | 
|  | 1181 | NL=`echo -ne '\015'` | 
|  | 1182 | # This fun command does the following: | 
|  | 1183 | # - the passed server command is backgrounded | 
|  | 1184 | # - the pid of the background process is saved in the usual place | 
|  | 1185 | # - the server process is brought back to the foreground | 
|  | 1186 | # - if the server process exits prematurely the fg command errors | 
|  | 1187 | # and a message is written to stdout and the process failure file | 
|  | 1188 | # | 
|  | 1189 | # The pid saved can be used in stop_process() as a process group | 
|  | 1190 | # id to kill off all child processes | 
|  | 1191 | if [[ -n "$group" ]]; then | 
|  | 1192 | command="sg $group '$command'" | 
|  | 1193 | fi | 
|  | 1194 | screen -S $SCREEN_NAME -p $name -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${name}.pid; fg || echo \"$name failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${name}.failure\"$NL" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1195 | } | 
|  | 1196 |  | 
|  | 1197 | # Screen rc file builder | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1198 | # Uses globals ``SCREEN_NAME``, ``SCREENRC`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1199 | # screen_rc service "command-line" | 
|  | 1200 | function screen_rc { | 
|  | 1201 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1202 | SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc | 
|  | 1203 | if [[ ! -e $SCREENRC ]]; then | 
|  | 1204 | # Name the screen session | 
|  | 1205 | echo "sessionname $SCREEN_NAME" > $SCREENRC | 
|  | 1206 | # Set a reasonable statusbar | 
|  | 1207 | echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC | 
|  | 1208 | # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off | 
|  | 1209 | echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC | 
|  | 1210 | echo "screen -t shell bash" >> $SCREENRC | 
|  | 1211 | fi | 
|  | 1212 | # If this service doesn't already exist in the screenrc file | 
|  | 1213 | if ! grep $1 $SCREENRC 2>&1 > /dev/null; then | 
|  | 1214 | NL=`echo -ne '\015'` | 
|  | 1215 | echo "screen -t $1 bash" >> $SCREENRC | 
|  | 1216 | echo "stuff \"$2$NL\"" >> $SCREENRC | 
|  | 1217 |  | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 1218 | if [[ -n ${LOGDIR} ]]; then | 
|  | 1219 | echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1220 | echo "log on" >>$SCREENRC | 
|  | 1221 | fi | 
|  | 1222 | fi | 
|  | 1223 | } | 
|  | 1224 |  | 
|  | 1225 | # Stop a service in screen | 
|  | 1226 | # If a PID is available use it, kill the whole process group via TERM | 
|  | 1227 | # If screen is being used kill the screen window; this will catch processes | 
|  | 1228 | # that did not leave a PID behind | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1229 | # Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN`` | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1230 | # screen_stop_service service | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1231 | function screen_stop_service { | 
|  | 1232 | local service=$1 | 
|  | 1233 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1234 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1235 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1236 | USE_SCREEN=$(trueorfalse True USE_SCREEN) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1237 |  | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1238 | if is_service_enabled $service; then | 
|  | 1239 | # Clean up the screen window | 
|  | 1240 | screen -S $SCREEN_NAME -p $service -X kill | 
|  | 1241 | fi | 
|  | 1242 | } | 
|  | 1243 |  | 
|  | 1244 | # Stop a service process | 
|  | 1245 | # If a PID is available use it, kill the whole process group via TERM | 
|  | 1246 | # If screen is being used kill the screen window; this will catch processes | 
|  | 1247 | # that did not leave a PID behind | 
|  | 1248 | # Uses globals ``SERVICE_DIR``, ``USE_SCREEN`` | 
|  | 1249 | # stop_process service | 
|  | 1250 | function stop_process { | 
|  | 1251 | local service=$1 | 
|  | 1252 |  | 
|  | 1253 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1254 | USE_SCREEN=$(trueorfalse True USE_SCREEN) | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1255 |  | 
|  | 1256 | if is_service_enabled $service; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1257 | # Kill via pid if we have one available | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1258 | if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then | 
|  | 1259 | pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid) | 
|  | 1260 | rm $SERVICE_DIR/$SCREEN_NAME/$service.pid | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1261 | fi | 
|  | 1262 | if [[ "$USE_SCREEN" = "True" ]]; then | 
|  | 1263 | # Clean up the screen window | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1264 | screen_stop_service $service | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1265 | fi | 
|  | 1266 | fi | 
|  | 1267 | } | 
|  | 1268 |  | 
|  | 1269 | # Helper to get the status of each running service | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1270 | # Uses globals ``SCREEN_NAME``, ``SERVICE_DIR`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1271 | # service_check | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1272 | function service_check { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1273 | local service | 
|  | 1274 | local failures | 
|  | 1275 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1276 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
|  | 1277 |  | 
|  | 1278 |  | 
|  | 1279 | if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then | 
|  | 1280 | echo "No service status directory found" | 
|  | 1281 | return | 
|  | 1282 | fi | 
|  | 1283 |  | 
|  | 1284 | # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME | 
| Sean Dague | 09bd7c8 | 2014-02-03 08:35:26 +0900 | [diff] [blame] | 1285 | # make this -o errexit safe | 
|  | 1286 | failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1287 |  | 
|  | 1288 | for service in $failures; do | 
|  | 1289 | service=`basename $service` | 
|  | 1290 | service=${service%.failure} | 
|  | 1291 | echo "Error: Service $service is not running" | 
|  | 1292 | done | 
|  | 1293 |  | 
|  | 1294 | if [ -n "$failures" ]; then | 
| Sean Dague | 1237922 | 2014-02-27 17:16:46 -0500 | [diff] [blame] | 1295 | die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1296 | fi | 
|  | 1297 | } | 
|  | 1298 |  | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1299 | # Tail a log file in a screen if USE_SCREEN is true. | 
|  | 1300 | function tail_log { | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1301 | local name=$1 | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1302 | local logfile=$2 | 
|  | 1303 |  | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1304 | USE_SCREEN=$(trueorfalse True USE_SCREEN) | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1305 | if [[ "$USE_SCREEN" = "True" ]]; then | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1306 | screen_process "$name" "sudo tail -f $logfile" | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1307 | fi | 
|  | 1308 | } | 
|  | 1309 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1310 |  | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1311 | # Deprecated Functions | 
|  | 1312 | # -------------------- | 
|  | 1313 |  | 
|  | 1314 | # _old_run_process() is designed to be backgrounded by old_run_process() to simulate a | 
|  | 1315 | # fork.  It includes the dirty work of closing extra filehandles and preparing log | 
|  | 1316 | # files to produce the same logs as screen_it().  The log filename is derived | 
|  | 1317 | # from the service name and global-and-now-misnamed ``SCREEN_LOGDIR`` | 
|  | 1318 | # Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR`` | 
|  | 1319 | # _old_run_process service "command-line" | 
|  | 1320 | function _old_run_process { | 
|  | 1321 | local service=$1 | 
|  | 1322 | local command="$2" | 
|  | 1323 |  | 
|  | 1324 | # Undo logging redirections and close the extra descriptors | 
|  | 1325 | exec 1>&3 | 
|  | 1326 | exec 2>&3 | 
|  | 1327 | exec 3>&- | 
|  | 1328 | exec 6>&- | 
|  | 1329 |  | 
|  | 1330 | if [[ -n ${SCREEN_LOGDIR} ]]; then | 
| Dean Troyer | ad5cc98 | 2014-12-10 16:35:32 -0600 | [diff] [blame] | 1331 | exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1 | 
|  | 1332 | ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1333 |  | 
|  | 1334 | # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs. | 
|  | 1335 | export PYTHONUNBUFFERED=1 | 
|  | 1336 | fi | 
|  | 1337 |  | 
|  | 1338 | exec /bin/bash -c "$command" | 
|  | 1339 | die "$service exec failure: $command" | 
|  | 1340 | } | 
|  | 1341 |  | 
|  | 1342 | # old_run_process() launches a child process that closes all file descriptors and | 
|  | 1343 | # then exec's the passed in command.  This is meant to duplicate the semantics | 
|  | 1344 | # of screen_it() without screen.  PIDs are written to | 
|  | 1345 | # ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process. | 
|  | 1346 | # old_run_process service "command-line" | 
|  | 1347 | function old_run_process { | 
|  | 1348 | local service=$1 | 
|  | 1349 | local command="$2" | 
|  | 1350 |  | 
|  | 1351 | # Spawn the child process | 
|  | 1352 | _old_run_process "$service" "$command" & | 
|  | 1353 | echo $! | 
|  | 1354 | } | 
|  | 1355 |  | 
|  | 1356 | # Compatibility for existing start_XXXX() functions | 
|  | 1357 | # Uses global ``USE_SCREEN`` | 
|  | 1358 | # screen_it service "command-line" | 
|  | 1359 | function screen_it { | 
|  | 1360 | if is_service_enabled $1; then | 
|  | 1361 | # Append the service to the screen rc file | 
|  | 1362 | screen_rc "$1" "$2" | 
|  | 1363 |  | 
|  | 1364 | if [[ "$USE_SCREEN" = "True" ]]; then | 
| Adam Gandelman | 8543a0f | 2014-10-16 17:42:33 -0700 | [diff] [blame] | 1365 | screen_process "$1" "$2" | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1366 | else | 
|  | 1367 | # Spawn directly without screen | 
|  | 1368 | old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid | 
|  | 1369 | fi | 
|  | 1370 | fi | 
|  | 1371 | } | 
|  | 1372 |  | 
|  | 1373 | # Compatibility for existing stop_XXXX() functions | 
|  | 1374 | # Stop a service in screen | 
|  | 1375 | # If a PID is available use it, kill the whole process group via TERM | 
|  | 1376 | # If screen is being used kill the screen window; this will catch processes | 
|  | 1377 | # that did not leave a PID behind | 
|  | 1378 | # screen_stop service | 
|  | 1379 | function screen_stop { | 
|  | 1380 | # Clean up the screen window | 
|  | 1381 | stop_process $1 | 
|  | 1382 | } | 
|  | 1383 |  | 
|  | 1384 |  | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1385 | # Plugin Functions | 
|  | 1386 | # ================= | 
|  | 1387 |  | 
|  | 1388 | DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""} | 
|  | 1389 |  | 
|  | 1390 | # enable_plugin <name> <url> [branch] | 
|  | 1391 | # | 
|  | 1392 | # ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar) | 
|  | 1393 | # ``url`` is a git url | 
|  | 1394 | # ``branch`` is a gitref. If it's not set, defaults to master | 
|  | 1395 | function enable_plugin { | 
|  | 1396 | local name=$1 | 
|  | 1397 | local url=$2 | 
|  | 1398 | local branch=${3:-master} | 
|  | 1399 | DEVSTACK_PLUGINS+=",$name" | 
|  | 1400 | GITREPO[$name]=$url | 
|  | 1401 | GITDIR[$name]=$DEST/$name | 
|  | 1402 | GITBRANCH[$name]=$branch | 
|  | 1403 | } | 
|  | 1404 |  | 
|  | 1405 | # fetch_plugins | 
|  | 1406 | # | 
|  | 1407 | # clones all plugins | 
|  | 1408 | function fetch_plugins { | 
|  | 1409 | local plugins="${DEVSTACK_PLUGINS}" | 
|  | 1410 | local plugin | 
|  | 1411 |  | 
|  | 1412 | # short circuit if nothing to do | 
|  | 1413 | if [[ -z $plugins ]]; then | 
|  | 1414 | return | 
|  | 1415 | fi | 
|  | 1416 |  | 
|  | 1417 | echo "Fetching devstack plugins" | 
|  | 1418 | for plugin in ${plugins//,/ }; do | 
|  | 1419 | git_clone_by_name $plugin | 
|  | 1420 | done | 
|  | 1421 | } | 
|  | 1422 |  | 
|  | 1423 | # load_plugin_settings | 
|  | 1424 | # | 
|  | 1425 | # Load settings from plugins in the order that they were registered | 
|  | 1426 | function load_plugin_settings { | 
|  | 1427 | local plugins="${DEVSTACK_PLUGINS}" | 
|  | 1428 | local plugin | 
|  | 1429 |  | 
|  | 1430 | # short circuit if nothing to do | 
|  | 1431 | if [[ -z $plugins ]]; then | 
|  | 1432 | return | 
|  | 1433 | fi | 
|  | 1434 |  | 
|  | 1435 | echo "Loading plugin settings" | 
|  | 1436 | for plugin in ${plugins//,/ }; do | 
|  | 1437 | local dir=${GITDIR[$plugin]} | 
|  | 1438 | # source any known settings | 
|  | 1439 | if [[ -f $dir/devstack/settings ]]; then | 
|  | 1440 | source $dir/devstack/settings | 
|  | 1441 | fi | 
|  | 1442 | done | 
|  | 1443 | } | 
|  | 1444 |  | 
|  | 1445 | # run_plugins | 
|  | 1446 | # | 
|  | 1447 | # Run the devstack/plugin.sh in all the plugin directories. These are | 
|  | 1448 | # run in registration order. | 
|  | 1449 | function run_plugins { | 
|  | 1450 | local mode=$1 | 
|  | 1451 | local phase=$2 | 
| Bharat Kumar Kobagana | 441ff07 | 2015-01-08 12:26:26 +0530 | [diff] [blame] | 1452 |  | 
|  | 1453 | local plugins="${DEVSTACK_PLUGINS}" | 
|  | 1454 | local plugin | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1455 | for plugin in ${plugins//,/ }; do | 
|  | 1456 | local dir=${GITDIR[$plugin]} | 
|  | 1457 | if [[ -f $dir/devstack/plugin.sh ]]; then | 
|  | 1458 | source $dir/devstack/plugin.sh $mode $phase | 
|  | 1459 | fi | 
|  | 1460 | done | 
|  | 1461 | } | 
|  | 1462 |  | 
|  | 1463 | function run_phase { | 
|  | 1464 | local mode=$1 | 
|  | 1465 | local phase=$2 | 
|  | 1466 | if [[ -d $TOP_DIR/extras.d ]]; then | 
|  | 1467 | for i in $TOP_DIR/extras.d/*.sh; do | 
|  | 1468 | [[ -r $i ]] && source $i $mode $phase | 
|  | 1469 | done | 
|  | 1470 | fi | 
|  | 1471 | # the source phase corresponds to settings loading in plugins | 
|  | 1472 | if [[ "$mode" == "source" ]]; then | 
|  | 1473 | load_plugin_settings | 
|  | 1474 | else | 
|  | 1475 | run_plugins $mode $phase | 
|  | 1476 | fi | 
|  | 1477 | } | 
|  | 1478 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1479 |  | 
|  | 1480 | # Service Functions | 
|  | 1481 | # ================= | 
|  | 1482 |  | 
|  | 1483 | # remove extra commas from the input string (i.e. ``ENABLED_SERVICES``) | 
|  | 1484 | # _cleanup_service_list service-list | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1485 | function _cleanup_service_list { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1486 | echo "$1" | sed -e ' | 
|  | 1487 | s/,,/,/g; | 
|  | 1488 | s/^,//; | 
|  | 1489 | s/,$// | 
|  | 1490 | ' | 
|  | 1491 | } | 
|  | 1492 |  | 
|  | 1493 | # disable_all_services() removes all current services | 
|  | 1494 | # from ``ENABLED_SERVICES`` to reset the configuration | 
|  | 1495 | # before a minimal installation | 
|  | 1496 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1497 | # disable_all_services | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1498 | function disable_all_services { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1499 | ENABLED_SERVICES="" | 
|  | 1500 | } | 
|  | 1501 |  | 
|  | 1502 | # Remove all services starting with '-'.  For example, to install all default | 
|  | 1503 | # services except rabbit (rabbit) set in ``localrc``: | 
|  | 1504 | # ENABLED_SERVICES+=",-rabbit" | 
|  | 1505 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1506 | # disable_negated_services | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1507 | function disable_negated_services { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1508 | local tmpsvcs="${ENABLED_SERVICES}" | 
|  | 1509 | local service | 
|  | 1510 | for service in ${tmpsvcs//,/ }; do | 
|  | 1511 | if [[ ${service} == -* ]]; then | 
|  | 1512 | tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g") | 
|  | 1513 | fi | 
|  | 1514 | done | 
|  | 1515 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
|  | 1516 | } | 
|  | 1517 |  | 
|  | 1518 | # disable_service() removes the services passed as argument to the | 
|  | 1519 | # ``ENABLED_SERVICES`` list, if they are present. | 
|  | 1520 | # | 
|  | 1521 | # For example: | 
|  | 1522 | #   disable_service rabbit | 
|  | 1523 | # | 
|  | 1524 | # This function does not know about the special cases | 
|  | 1525 | # for nova, glance, and neutron built into is_service_enabled(). | 
|  | 1526 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1527 | # disable_service service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1528 | function disable_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1529 | local tmpsvcs=",${ENABLED_SERVICES}," | 
|  | 1530 | local service | 
|  | 1531 | for service in $@; do | 
|  | 1532 | if is_service_enabled $service; then | 
|  | 1533 | tmpsvcs=${tmpsvcs//,$service,/,} | 
|  | 1534 | fi | 
|  | 1535 | done | 
|  | 1536 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
|  | 1537 | } | 
|  | 1538 |  | 
|  | 1539 | # enable_service() adds the services passed as argument to the | 
|  | 1540 | # ``ENABLED_SERVICES`` list, if they are not already present. | 
|  | 1541 | # | 
|  | 1542 | # For example: | 
|  | 1543 | #   enable_service qpid | 
|  | 1544 | # | 
|  | 1545 | # This function does not know about the special cases | 
|  | 1546 | # for nova, glance, and neutron built into is_service_enabled(). | 
|  | 1547 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1548 | # enable_service service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1549 | function enable_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1550 | local tmpsvcs="${ENABLED_SERVICES}" | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1551 | local service | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1552 | for service in $@; do | 
|  | 1553 | if ! is_service_enabled $service; then | 
|  | 1554 | tmpsvcs+=",$service" | 
|  | 1555 | fi | 
|  | 1556 | done | 
|  | 1557 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
|  | 1558 | disable_negated_services | 
|  | 1559 | } | 
|  | 1560 |  | 
|  | 1561 | # is_service_enabled() checks if the service(s) specified as arguments are | 
|  | 1562 | # enabled by the user in ``ENABLED_SERVICES``. | 
|  | 1563 | # | 
|  | 1564 | # Multiple services specified as arguments are ``OR``'ed together; the test | 
|  | 1565 | # is a short-circuit boolean, i.e it returns on the first match. | 
|  | 1566 | # | 
|  | 1567 | # There are special cases for some 'catch-all' services:: | 
|  | 1568 | #   **nova** returns true if any service enabled start with **n-** | 
|  | 1569 | #   **cinder** returns true if any service enabled start with **c-** | 
|  | 1570 | #   **ceilometer** returns true if any service enabled start with **ceilometer** | 
|  | 1571 | #   **glance** returns true if any service enabled start with **g-** | 
|  | 1572 | #   **neutron** returns true if any service enabled start with **q-** | 
|  | 1573 | #   **swift** returns true if any service enabled start with **s-** | 
|  | 1574 | #   **trove** returns true if any service enabled start with **tr-** | 
|  | 1575 | #   For backward compatibility if we have **swift** in ENABLED_SERVICES all the | 
|  | 1576 | #   **s-** services will be enabled. This will be deprecated in the future. | 
|  | 1577 | # | 
|  | 1578 | # Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``. | 
|  | 1579 | # We also need to make sure to treat **n-cell-region** and **n-cell-child** | 
|  | 1580 | # as enabled in this case. | 
|  | 1581 | # | 
|  | 1582 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1583 | # is_service_enabled service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1584 | function is_service_enabled { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1585 | local xtrace=$(set +o | grep xtrace) | 
|  | 1586 | set +o xtrace | 
|  | 1587 | local enabled=1 | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1588 | local services=$@ | 
|  | 1589 | local service | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1590 | for service in ${services}; do | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1591 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1592 |  | 
|  | 1593 | # Look for top-level 'enabled' function for this service | 
|  | 1594 | if type is_${service}_enabled >/dev/null 2>&1; then | 
|  | 1595 | # A function exists for this service, use it | 
|  | 1596 | is_${service}_enabled | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1597 | enabled=$? | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1598 | fi | 
|  | 1599 |  | 
|  | 1600 | # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled() | 
|  | 1601 | #                are implemented | 
|  | 1602 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1603 | [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0 | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1604 | [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0 | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1605 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0 | 
|  | 1606 | [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0 | 
|  | 1607 | [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0 | 
|  | 1608 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0 | 
|  | 1609 | [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0 | 
|  | 1610 | [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0 | 
|  | 1611 | [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0 | 
|  | 1612 | [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0 | 
|  | 1613 | [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1614 | done | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1615 | $xtrace | 
|  | 1616 | return $enabled | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1617 | } | 
|  | 1618 |  | 
|  | 1619 | # Toggle enable/disable_service for services that must run exclusive of each other | 
|  | 1620 | #  $1 The name of a variable containing a space-separated list of services | 
|  | 1621 | #  $2 The name of a variable in which to store the enabled service's name | 
|  | 1622 | #  $3 The name of the service to enable | 
|  | 1623 | function use_exclusive_service { | 
|  | 1624 | local options=${!1} | 
|  | 1625 | local selection=$3 | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1626 | local out=$2 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1627 | [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1 | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1628 | local opt | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1629 | for opt in $options;do | 
|  | 1630 | [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt | 
|  | 1631 | done | 
|  | 1632 | eval "$out=$selection" | 
|  | 1633 | return 0 | 
|  | 1634 | } | 
|  | 1635 |  | 
|  | 1636 |  | 
| Masayuki Igawa | f6368d3 | 2014-02-20 13:31:26 +0900 | [diff] [blame] | 1637 | # System Functions | 
|  | 1638 | # ================ | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1639 |  | 
|  | 1640 | # Only run the command if the target file (the last arg) is not on an | 
|  | 1641 | # NFS filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1642 | function _safe_permission_operation { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1643 | local xtrace=$(set +o | grep xtrace) | 
|  | 1644 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1645 | local args=( $@ ) | 
|  | 1646 | local last | 
|  | 1647 | local sudo_cmd | 
|  | 1648 | local dir_to_check | 
|  | 1649 |  | 
|  | 1650 | let last="${#args[*]} - 1" | 
|  | 1651 |  | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1652 | local dir_to_check=${args[$last]} | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1653 | if [ ! -d "$dir_to_check" ]; then | 
|  | 1654 | dir_to_check=`dirname "$dir_to_check"` | 
|  | 1655 | fi | 
|  | 1656 |  | 
|  | 1657 | if is_nfs_directory "$dir_to_check" ; then | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1658 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1659 | return 0 | 
|  | 1660 | fi | 
|  | 1661 |  | 
|  | 1662 | if [[ $TRACK_DEPENDS = True ]]; then | 
|  | 1663 | sudo_cmd="env" | 
|  | 1664 | else | 
|  | 1665 | sudo_cmd="sudo" | 
|  | 1666 | fi | 
|  | 1667 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1668 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1669 | $sudo_cmd $@ | 
|  | 1670 | } | 
|  | 1671 |  | 
|  | 1672 | # Exit 0 if address is in network or 1 if address is not in network | 
|  | 1673 | # ip-range is in CIDR notation: 1.2.3.4/20 | 
|  | 1674 | # address_in_net ip-address ip-range | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1675 | function address_in_net { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1676 | local ip=$1 | 
|  | 1677 | local range=$2 | 
|  | 1678 | local masklen=${range#*/} | 
|  | 1679 | local network=$(maskip ${range%/*} $(cidr2netmask $masklen)) | 
|  | 1680 | local subnet=$(maskip $ip $(cidr2netmask $masklen)) | 
|  | 1681 | [[ $network == $subnet ]] | 
|  | 1682 | } | 
|  | 1683 |  | 
|  | 1684 | # Add a user to a group. | 
|  | 1685 | # add_user_to_group user group | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1686 | function add_user_to_group { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1687 | local user=$1 | 
|  | 1688 | local group=$2 | 
|  | 1689 |  | 
|  | 1690 | if [[ -z "$os_VENDOR" ]]; then | 
|  | 1691 | GetOSVersion | 
|  | 1692 | fi | 
|  | 1693 |  | 
|  | 1694 | # SLE11 and openSUSE 12.2 don't have the usual usermod | 
|  | 1695 | if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then | 
|  | 1696 | sudo usermod -a -G "$group" "$user" | 
|  | 1697 | else | 
|  | 1698 | sudo usermod -A "$group" "$user" | 
|  | 1699 | fi | 
|  | 1700 | } | 
|  | 1701 |  | 
|  | 1702 | # Convert CIDR notation to a IPv4 netmask | 
|  | 1703 | # cidr2netmask cidr-bits | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1704 | function cidr2netmask { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1705 | local maskpat="255 255 255 255" | 
|  | 1706 | local maskdgt="254 252 248 240 224 192 128" | 
|  | 1707 | set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3} | 
|  | 1708 | echo ${1-0}.${2-0}.${3-0}.${4-0} | 
|  | 1709 | } | 
|  | 1710 |  | 
|  | 1711 | # Gracefully cp only if source file/dir exists | 
|  | 1712 | # cp_it source destination | 
|  | 1713 | function cp_it { | 
|  | 1714 | if [ -e $1 ] || [ -d $1 ]; then | 
|  | 1715 | cp -pRL $1 $2 | 
|  | 1716 | fi | 
|  | 1717 | } | 
|  | 1718 |  | 
|  | 1719 | # HTTP and HTTPS proxy servers are supported via the usual environment variables [1] | 
|  | 1720 | # ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in | 
|  | 1721 | # ``localrc`` or on the command line if necessary:: | 
|  | 1722 | # | 
|  | 1723 | # [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html | 
|  | 1724 | # | 
|  | 1725 | #     http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh | 
|  | 1726 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1727 | function export_proxy_variables { | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1728 | if isset http_proxy ; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1729 | export http_proxy=$http_proxy | 
|  | 1730 | fi | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1731 | if isset https_proxy ; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1732 | export https_proxy=$https_proxy | 
|  | 1733 | fi | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1734 | if isset no_proxy ; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1735 | export no_proxy=$no_proxy | 
|  | 1736 | fi | 
|  | 1737 | } | 
|  | 1738 |  | 
|  | 1739 | # Returns true if the directory is on a filesystem mounted via NFS. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1740 | function is_nfs_directory { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1741 | local mount_type=`stat -f -L -c %T $1` | 
|  | 1742 | test "$mount_type" == "nfs" | 
|  | 1743 | } | 
|  | 1744 |  | 
|  | 1745 | # Return the network portion of the given IP address using netmask | 
|  | 1746 | # netmask is in the traditional dotted-quad format | 
|  | 1747 | # maskip ip-address netmask | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1748 | function maskip { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1749 | local ip=$1 | 
|  | 1750 | local mask=$2 | 
|  | 1751 | local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}" | 
|  | 1752 | local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.})) | 
|  | 1753 | echo $subnet | 
|  | 1754 | } | 
|  | 1755 |  | 
|  | 1756 | # Service wrapper to restart services | 
|  | 1757 | # restart_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1758 | function restart_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1759 | if is_ubuntu; then | 
|  | 1760 | sudo /usr/sbin/service $1 restart | 
|  | 1761 | else | 
|  | 1762 | sudo /sbin/service $1 restart | 
|  | 1763 | fi | 
|  | 1764 | } | 
|  | 1765 |  | 
|  | 1766 | # Only change permissions of a file or directory if it is not on an | 
|  | 1767 | # NFS filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1768 | function safe_chmod { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1769 | _safe_permission_operation chmod $@ | 
|  | 1770 | } | 
|  | 1771 |  | 
|  | 1772 | # Only change ownership of a file or directory if it is not on an NFS | 
|  | 1773 | # filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1774 | function safe_chown { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1775 | _safe_permission_operation chown $@ | 
|  | 1776 | } | 
|  | 1777 |  | 
|  | 1778 | # Service wrapper to start services | 
|  | 1779 | # start_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1780 | function start_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1781 | if is_ubuntu; then | 
|  | 1782 | sudo /usr/sbin/service $1 start | 
|  | 1783 | else | 
|  | 1784 | sudo /sbin/service $1 start | 
|  | 1785 | fi | 
|  | 1786 | } | 
|  | 1787 |  | 
|  | 1788 | # Service wrapper to stop services | 
|  | 1789 | # stop_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1790 | function stop_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1791 | if is_ubuntu; then | 
|  | 1792 | sudo /usr/sbin/service $1 stop | 
|  | 1793 | else | 
|  | 1794 | sudo /sbin/service $1 stop | 
|  | 1795 | fi | 
|  | 1796 | } | 
|  | 1797 |  | 
|  | 1798 |  | 
|  | 1799 | # Restore xtrace | 
|  | 1800 | $XTRACE | 
|  | 1801 |  | 
|  | 1802 | # Local variables: | 
|  | 1803 | # mode: shell-script | 
|  | 1804 | # End: |