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