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