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