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