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