| 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 | 
| Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 35 | _XTRACE_FUNCTIONS_COMMON=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 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 | 
| Sean Dague | afef8bf | 2017-03-06 14:07:23 -0500 | [diff] [blame] | 40 | declare -r -g _DEVSTACK_FUNCTIONS_COMMON=1 | 
| Ian Wienand | 4ffb454 | 2015-06-30 11:00:32 +1000 | [diff] [blame] | 41 |  | 
| Sean Dague | cc52406 | 2014-10-01 09:06:43 -0400 | [diff] [blame] | 42 | # Global Config Variables | 
| Sean Dague | afef8bf | 2017-03-06 14:07:23 -0500 | [diff] [blame] | 43 | declare -A -g GITREPO | 
 | 44 | declare -A -g GITBRANCH | 
 | 45 | declare -A -g GITDIR | 
| Sean Dague | cc52406 | 2014-10-01 09:06:43 -0400 | [diff] [blame] | 46 |  | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 47 | TRACK_DEPENDS=${TRACK_DEPENDS:-False} | 
| Dinesh Bhor | ef60f2b | 2017-09-05 14:40:32 +0530 | [diff] [blame] | 48 | KILL_PATH="$(which kill)" | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 49 |  | 
| Dean Troyer | 6816234 | 2015-05-13 15:41:03 -0500 | [diff] [blame] | 50 | # Save these variables to .stackenv | 
 | 51 | STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \ | 
| Sean Dague | 0effa1a | 2017-04-18 15:16:37 -0400 | [diff] [blame] | 52 |     KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \ | 
 | 53 |     LOGFILE OS_CACERT SERVICE_HOST STACK_USER TLS_IP \ | 
| Brian Haley | 5d04db2 | 2015-06-16 13:14:31 -0400 | [diff] [blame] | 54 |     HOST_IPV6 SERVICE_IP_VERSION" | 
| Dean Troyer | 6816234 | 2015-05-13 15:41:03 -0500 | [diff] [blame] | 55 |  | 
 | 56 |  | 
 | 57 | # Saves significant environment variables to .stackenv for later use | 
 | 58 | # Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to | 
 | 59 | # function, the rest are simply saved and do not cause problems if they are undefined. | 
 | 60 | # save_stackenv [tag] | 
 | 61 | function save_stackenv { | 
 | 62 |     local tag=${1:-""} | 
 | 63 |     # Save some values we generated for later use | 
 | 64 |     time_stamp=$(date "+$TIMESTAMP_FORMAT") | 
 | 65 |     echo "# $time_stamp $tag" >$TOP_DIR/.stackenv | 
 | 66 |     for i in $STACK_ENV_VARS; do | 
 | 67 |         echo $i=${!i} >>$TOP_DIR/.stackenv | 
 | 68 |     done | 
 | 69 | } | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 70 |  | 
| Monty Taylor | 7224eec | 2015-09-19 11:26:18 -0400 | [diff] [blame] | 71 | # Update/create user clouds.yaml file. | 
 | 72 | # clouds.yaml will have | 
 | 73 | # - A `devstack` entry for the `demo` user for the `demo` project. | 
 | 74 | # - A `devstack-admin` entry for the `admin` user for the `admin` project. | 
 | 75 | # write_clouds_yaml | 
 | 76 | function write_clouds_yaml { | 
| Monty Taylor | ee9bb76 | 2015-10-19 15:16:18 -0400 | [diff] [blame] | 77 |     # The location is a variable to allow for easier refactoring later to make it | 
 | 78 |     # overridable. There is currently no usecase where doing so makes sense, so | 
 | 79 |     # it's not currently configurable. | 
| Monty Taylor | 7224eec | 2015-09-19 11:26:18 -0400 | [diff] [blame] | 80 |  | 
| Monty Taylor | ee9bb76 | 2015-10-19 15:16:18 -0400 | [diff] [blame] | 81 |     CLOUDS_YAML=/etc/openstack/clouds.yaml | 
 | 82 |  | 
 | 83 |     sudo mkdir -p $(dirname $CLOUDS_YAML) | 
| Lenny Verkhovsky | 313ddae | 2015-10-20 11:26:34 +0300 | [diff] [blame] | 84 |     sudo chown -R $STACK_USER /etc/openstack | 
| Lenny Verkhovsky | 313ddae | 2015-10-20 11:26:34 +0300 | [diff] [blame] | 85 |  | 
| Monty Taylor | ee9bb76 | 2015-10-19 15:16:18 -0400 | [diff] [blame] | 86 |     CA_CERT_ARG='' | 
 | 87 |     if [ -f "$SSL_BUNDLE_FILE" ]; then | 
 | 88 |         CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE" | 
 | 89 |     fi | 
| Sean Dague | c67d22e | 2016-02-02 05:51:14 -0500 | [diff] [blame] | 90 |     # demo -> devstack | 
| Davanum Srinivas | 51ecf0a | 2017-01-05 16:11:17 -0500 | [diff] [blame] | 91 |     $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \ | 
| Monty Taylor | ee9bb76 | 2015-10-19 15:16:18 -0400 | [diff] [blame] | 92 |         --file $CLOUDS_YAML \ | 
 | 93 |         --os-cloud devstack \ | 
 | 94 |         --os-region-name $REGION_NAME \ | 
 | 95 |         --os-identity-api-version 3 \ | 
 | 96 |         $CA_CERT_ARG \ | 
| Monty Taylor | e43f60b | 2017-04-27 18:27:36 -0500 | [diff] [blame] | 97 |         --os-auth-url $KEYSTONE_SERVICE_URI \ | 
| Monty Taylor | ee9bb76 | 2015-10-19 15:16:18 -0400 | [diff] [blame] | 98 |         --os-username demo \ | 
 | 99 |         --os-password $ADMIN_PASSWORD \ | 
 | 100 |         --os-project-name demo | 
| Sean Dague | c67d22e | 2016-02-02 05:51:14 -0500 | [diff] [blame] | 101 |  | 
 | 102 |     # alt_demo -> devstack-alt | 
| Davanum Srinivas | 51ecf0a | 2017-01-05 16:11:17 -0500 | [diff] [blame] | 103 |     $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \ | 
| Sean Dague | c67d22e | 2016-02-02 05:51:14 -0500 | [diff] [blame] | 104 |         --file $CLOUDS_YAML \ | 
| Clark Boylan | e20cb43 | 2016-02-05 12:00:18 -0800 | [diff] [blame] | 105 |         --os-cloud devstack-alt \ | 
| Sean Dague | c67d22e | 2016-02-02 05:51:14 -0500 | [diff] [blame] | 106 |         --os-region-name $REGION_NAME \ | 
 | 107 |         --os-identity-api-version 3 \ | 
 | 108 |         $CA_CERT_ARG \ | 
| Monty Taylor | e43f60b | 2017-04-27 18:27:36 -0500 | [diff] [blame] | 109 |         --os-auth-url $KEYSTONE_SERVICE_URI \ | 
| Sean Dague | c67d22e | 2016-02-02 05:51:14 -0500 | [diff] [blame] | 110 |         --os-username alt_demo \ | 
 | 111 |         --os-password $ADMIN_PASSWORD \ | 
 | 112 |         --os-project-name alt_demo | 
 | 113 |  | 
 | 114 |     # admin -> devstack-admin | 
| Davanum Srinivas | 51ecf0a | 2017-01-05 16:11:17 -0500 | [diff] [blame] | 115 |     $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \ | 
| Monty Taylor | ee9bb76 | 2015-10-19 15:16:18 -0400 | [diff] [blame] | 116 |         --file $CLOUDS_YAML \ | 
 | 117 |         --os-cloud devstack-admin \ | 
 | 118 |         --os-region-name $REGION_NAME \ | 
 | 119 |         --os-identity-api-version 3 \ | 
 | 120 |         $CA_CERT_ARG \ | 
| Monty Taylor | e43f60b | 2017-04-27 18:27:36 -0500 | [diff] [blame] | 121 |         --os-auth-url $KEYSTONE_SERVICE_URI \ | 
| Monty Taylor | ee9bb76 | 2015-10-19 15:16:18 -0400 | [diff] [blame] | 122 |         --os-username admin \ | 
 | 123 |         --os-password $ADMIN_PASSWORD \ | 
 | 124 |         --os-project-name admin | 
| Monty Taylor | 74379df | 2016-01-21 10:20:08 -0500 | [diff] [blame] | 125 |  | 
 | 126 |     # CLean up any old clouds.yaml files we had laying around | 
| ZhiQiang Fan | 0b4a009 | 2016-04-12 20:26:33 +0800 | [diff] [blame] | 127 |     rm -f $(eval echo ~"$STACK_USER")/.config/openstack/clouds.yaml | 
| Monty Taylor | 7224eec | 2015-09-19 11:26:18 -0400 | [diff] [blame] | 128 | } | 
 | 129 |  | 
| Ian Wienand | e82bac0 | 2015-08-25 14:29:08 +1000 | [diff] [blame] | 130 | # trueorfalse <True|False> <VAR> | 
 | 131 | # | 
 | 132 | # Normalize config-value provided in variable VAR to either "True" or | 
 | 133 | # "False".  If VAR is unset (i.e. $VAR evaluates as empty), the value | 
 | 134 | # of the second argument will be used as the default value. | 
 | 135 | # | 
 | 136 | #  Accepts as False: 0 no  No  NO  false False FALSE | 
 | 137 | #  Accepts as True:  1 yes Yes YES true  True  TRUE | 
 | 138 | # | 
 | 139 | # usage: | 
 | 140 | #  VAL=$(trueorfalse False VAL) | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 141 | function trueorfalse { | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 142 |     local xtrace | 
 | 143 |     xtrace=$(set +o | grep xtrace) | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 144 |     set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 145 |  | 
| Mahito OGURA | 98f59aa | 2015-05-11 18:02:34 +0900 | [diff] [blame] | 146 |     local default=$1 | 
| Ian Wienand | e82bac0 | 2015-08-25 14:29:08 +1000 | [diff] [blame] | 147 |  | 
 | 148 |     if [ -z $2 ]; then | 
 | 149 |         die $LINENO "variable to normalize required" | 
 | 150 |     fi | 
| Mahito OGURA | 98f59aa | 2015-05-11 18:02:34 +0900 | [diff] [blame] | 151 |     local testval=${!2:-} | 
 | 152 |  | 
 | 153 |     case "$testval" in | 
 | 154 |         "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;; | 
 | 155 |         "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;; | 
 | 156 |         * )                                       echo "$default" ;; | 
 | 157 |     esac | 
 | 158 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 159 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 160 | } | 
 | 161 |  | 
| Attila Fazekas | 1bd7959 | 2015-02-24 14:06:56 +0100 | [diff] [blame] | 162 | function isset { | 
 | 163 |     [[ -v "$1" ]] | 
 | 164 | } | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 165 |  | 
| Dean Troyer | 6816234 | 2015-05-13 15:41:03 -0500 | [diff] [blame] | 166 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 167 | # Control Functions | 
 | 168 | # ================= | 
 | 169 |  | 
 | 170 | # Prints backtrace info | 
 | 171 | # filename:lineno:function | 
 | 172 | # backtrace level | 
 | 173 | function backtrace { | 
 | 174 |     local level=$1 | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 175 |     local deep | 
 | 176 |     deep=$((${#BASH_SOURCE[@]} - 1)) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 177 |     echo "[Call Trace]" | 
 | 178 |     while [ $level -le $deep ]; do | 
 | 179 |         echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}" | 
 | 180 |         deep=$((deep - 1)) | 
 | 181 |     done | 
 | 182 | } | 
 | 183 |  | 
 | 184 | # Prints line number and "message" then exits | 
 | 185 | # die $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 186 | function die { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 187 |     local exitcode=$? | 
 | 188 |     set +o xtrace | 
 | 189 |     local line=$1; shift | 
 | 190 |     if [ $exitcode == 0 ]; then | 
 | 191 |         exitcode=1 | 
 | 192 |     fi | 
 | 193 |     backtrace 2 | 
 | 194 |     err $line "$*" | 
| Dean Troyer | a25a6f6 | 2014-02-24 16:03:41 -0600 | [diff] [blame] | 195 |     # Give buffers a second to flush | 
 | 196 |     sleep 1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 197 |     exit $exitcode | 
 | 198 | } | 
 | 199 |  | 
 | 200 | # Checks an environment variable is not set or has length 0 OR if the | 
 | 201 | # exit code is non-zero and prints "message" and exits | 
 | 202 | # NOTE: env-var is the variable name without a '$' | 
 | 203 | # die_if_not_set $LINENO env-var "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 204 | function die_if_not_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 205 |     local exitcode=$? | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 206 |     local xtrace | 
 | 207 |     xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 208 |     set +o xtrace | 
 | 209 |     local line=$1; shift | 
 | 210 |     local evar=$1; shift | 
 | 211 |     if ! is_set $evar || [ $exitcode != 0 ]; then | 
 | 212 |         die $line "$*" | 
 | 213 |     fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 214 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 215 | } | 
 | 216 |  | 
| Sean Dague | 1de9e33 | 2015-10-07 08:46:13 -0400 | [diff] [blame] | 217 | function deprecated { | 
 | 218 |     local text=$1 | 
 | 219 |     DEPRECATED_TEXT+="\n$text" | 
| YAMAMOTO Takashi | 8b1bbd6 | 2016-12-01 22:29:12 +0900 | [diff] [blame] | 220 |     echo "WARNING: $text" >&2 | 
| Sean Dague | 1de9e33 | 2015-10-07 08:46:13 -0400 | [diff] [blame] | 221 | } | 
 | 222 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 223 | # Prints line number and "message" in error format | 
 | 224 | # err $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 225 | function err { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 226 |     local exitcode=$? | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 227 |     local xtrace | 
 | 228 |     xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 229 |     set +o xtrace | 
 | 230 |     local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2" | 
 | 231 |     echo $msg 1>&2; | 
| Dean Troyer | dde41d0 | 2014-12-09 17:47:57 -0600 | [diff] [blame] | 232 |     if [[ -n ${LOGDIR} ]]; then | 
 | 233 |         echo $msg >> "${LOGDIR}/error.log" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 234 |     fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 235 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 236 |     return $exitcode | 
 | 237 | } | 
 | 238 |  | 
 | 239 | # Checks an environment variable is not set or has length 0 OR if the | 
 | 240 | # exit code is non-zero and prints "message" | 
 | 241 | # NOTE: env-var is the variable name without a '$' | 
 | 242 | # err_if_not_set $LINENO env-var "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 243 | function err_if_not_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 244 |     local exitcode=$? | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 245 |     local xtrace | 
 | 246 |     xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 247 |     set +o xtrace | 
 | 248 |     local line=$1; shift | 
 | 249 |     local evar=$1; shift | 
 | 250 |     if ! is_set $evar || [ $exitcode != 0 ]; then | 
 | 251 |         err $line "$*" | 
 | 252 |     fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 253 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 254 |     return $exitcode | 
 | 255 | } | 
 | 256 |  | 
 | 257 | # Exit after outputting a message about the distribution not being supported. | 
 | 258 | # exit_distro_not_supported [optional-string-telling-what-is-missing] | 
 | 259 | function exit_distro_not_supported { | 
 | 260 |     if [[ -z "$DISTRO" ]]; then | 
 | 261 |         GetDistro | 
 | 262 |     fi | 
 | 263 |  | 
 | 264 |     if [ $# -gt 0 ]; then | 
 | 265 |         die $LINENO "Support for $DISTRO is incomplete: no support for $@" | 
 | 266 |     else | 
 | 267 |         die $LINENO "Support for $DISTRO is incomplete." | 
 | 268 |     fi | 
 | 269 | } | 
 | 270 |  | 
 | 271 | # Test if the named environment variable is set and not zero length | 
 | 272 | # is_set env-var | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 273 | function is_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 274 |     local var=\$"$1" | 
 | 275 |     eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this | 
 | 276 | } | 
 | 277 |  | 
 | 278 | # Prints line number and "message" in warning format | 
 | 279 | # warn $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 280 | function warn { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 281 |     local exitcode=$? | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 282 |     local xtrace | 
 | 283 |     xtrace=$(set +o | grep xtrace) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 284 |     set +o xtrace | 
 | 285 |     local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2" | 
| Sean Dague | e4af929 | 2015-04-28 08:57:57 -0400 | [diff] [blame] | 286 |     echo $msg | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 287 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 288 |     return $exitcode | 
 | 289 | } | 
 | 290 |  | 
 | 291 |  | 
 | 292 | # Distro Functions | 
 | 293 | # ================ | 
 | 294 |  | 
 | 295 | # Determine OS Vendor, Release and Update | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 296 |  | 
 | 297 | # | 
 | 298 | # NOTE : For portability, you almost certainly do not want to use | 
 | 299 | # these variables directly!  The "is_*" functions defined below this | 
 | 300 | # bundle up compatible platforms under larger umbrellas that we have | 
 | 301 | # determinted are compatible enough (e.g. is_ubuntu covers Ubuntu & | 
 | 302 | # Debian, is_fedora covers RPM-based distros).  Higher-level functions | 
 | 303 | # such as "install_package" further abstract things in better ways. | 
 | 304 | # | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 305 | # ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc | 
| Matt Riedemann | ff10ac3 | 2017-02-13 12:44:24 -0500 | [diff] [blame] | 306 | # ``os_RELEASE`` - major release: ``16.04`` (Ubuntu), ``23`` (Fedora) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 307 | # ``os_PACKAGE`` - package type: ``deb`` or ``rpm`` | 
| Matt Riedemann | ff10ac3 | 2017-02-13 12:44:24 -0500 | [diff] [blame] | 308 | # ``os_CODENAME`` - vendor's codename for release: ``xenial`` | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 309 |  | 
| Sean Dague | afef8bf | 2017-03-06 14:07:23 -0500 | [diff] [blame] | 310 | declare -g os_VENDOR os_RELEASE os_PACKAGE os_CODENAME | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 311 |  | 
 | 312 | # Make a *best effort* attempt to install lsb_release packages for the | 
 | 313 | # user if not available.  Note can't use generic install_package* | 
 | 314 | # because they depend on this! | 
 | 315 | function _ensure_lsb_release { | 
| Thomas Bechtold | dab3901 | 2016-03-03 12:11:15 +0100 | [diff] [blame] | 316 |     if [[ -x $(command -v lsb_release 2>/dev/null) ]]; then | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 317 |         return | 
 | 318 |     fi | 
 | 319 |  | 
| Thomas Bechtold | dab3901 | 2016-03-03 12:11:15 +0100 | [diff] [blame] | 320 |     if [[ -x $(command -v apt-get 2>/dev/null) ]]; then | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 321 |         sudo apt-get install -y lsb-release | 
| Thomas Bechtold | dab3901 | 2016-03-03 12:11:15 +0100 | [diff] [blame] | 322 |     elif [[ -x $(command -v zypper 2>/dev/null) ]]; then | 
| Dirk Mueller | 5ef8a12 | 2017-09-25 13:08:51 +0200 | [diff] [blame] | 323 |         sudo zypper -n install lsb-release | 
| Thomas Bechtold | dab3901 | 2016-03-03 12:11:15 +0100 | [diff] [blame] | 324 |     elif [[ -x $(command -v dnf 2>/dev/null) ]]; then | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 325 |         sudo dnf install -y redhat-lsb-core | 
| Thomas Bechtold | dab3901 | 2016-03-03 12:11:15 +0100 | [diff] [blame] | 326 |     elif [[ -x $(command -v yum 2>/dev/null) ]]; then | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 327 |         # all rh patforms (fedora, centos, rhel) have this pkg | 
 | 328 |         sudo yum install -y redhat-lsb-core | 
 | 329 |     else | 
 | 330 |         die $LINENO "Unable to find or auto-install lsb_release" | 
 | 331 |     fi | 
 | 332 | } | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 333 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 334 | # GetOSVersion | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 335 | #  Set the following variables: | 
 | 336 | #  - os_RELEASE | 
 | 337 | #  - os_CODENAME | 
 | 338 | #  - os_VENDOR | 
 | 339 | #  - os_PACKAGE | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 340 | function GetOSVersion { | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 341 |     # We only support distros that provide a sane lsb_release | 
 | 342 |     _ensure_lsb_release | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 343 |  | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 344 |     os_RELEASE=$(lsb_release -r -s) | 
 | 345 |     os_CODENAME=$(lsb_release -c -s) | 
 | 346 |     os_VENDOR=$(lsb_release -i -s) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 347 |  | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 348 |     if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 349 |         os_PACKAGE="deb" | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 350 |     else | 
 | 351 |         os_PACKAGE="rpm" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 352 |     fi | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 353 |  | 
 | 354 |     typeset -xr os_VENDOR | 
 | 355 |     typeset -xr os_RELEASE | 
 | 356 |     typeset -xr os_PACKAGE | 
 | 357 |     typeset -xr os_CODENAME | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 358 | } | 
 | 359 |  | 
 | 360 | # Translate the OS version values into common nomenclature | 
 | 361 | # Sets global ``DISTRO`` from the ``os_*`` values | 
| Sean Dague | afef8bf | 2017-03-06 14:07:23 -0500 | [diff] [blame] | 362 | declare -g DISTRO | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 363 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 364 | function GetDistro { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 365 |     GetOSVersion | 
| ptoohill1 | 9600665 | 2016-02-15 16:07:50 -0600 | [diff] [blame] | 366 |     if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) || \ | 
 | 367 |             "$os_VENDOR" =~ (LinuxMint) ]]; then | 
 | 368 |         # 'Everyone' refers to Ubuntu / Debian / Mint releases by | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 369 |         # the code name adjective | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 370 |         DISTRO=$os_CODENAME | 
 | 371 |     elif [[ "$os_VENDOR" =~ (Fedora) ]]; then | 
 | 372 |         # For Fedora, just use 'f' and the release | 
 | 373 |         DISTRO="f$os_RELEASE" | 
 | 374 |     elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then | 
 | 375 |         DISTRO="opensuse-$os_RELEASE" | 
| Dirk Mueller | 4404f68 | 2018-03-02 00:37:58 +0100 | [diff] [blame] | 376 |         # Tumbleweed uses "n/a" as a codename, and the release is a datestring | 
 | 377 |         # like 20180218, so not very useful. | 
 | 378 |         [ "$os_CODENAME" = "n/a" ] && DISTRO="opensuse-tumbleweed" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 379 |     elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 380 |         # just use major release | 
 | 381 |         DISTRO="sle${os_RELEASE%.*}" | 
 | 382 |     elif [[ "$os_VENDOR" =~ (Red.*Hat) || \ | 
| anju Tiwari | 6c639c9 | 2014-07-15 18:11:54 +0530 | [diff] [blame] | 383 |         "$os_VENDOR" =~ (CentOS) || \ | 
| Mike Trimm | 1da4e79 | 2016-04-27 11:40:19 -0500 | [diff] [blame] | 384 |         "$os_VENDOR" =~ (Scientific) || \ | 
| Fernando Ribeiro | 4e6f8ca | 2016-04-12 23:34:03 -0300 | [diff] [blame] | 385 |         "$os_VENDOR" =~ (OracleServer) || \ | 
| Evgeny Antyshev | 718512c | 2016-03-03 14:47:58 +0000 | [diff] [blame] | 386 |         "$os_VENDOR" =~ (Virtuozzo) ]]; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 387 |         # Drop the . release as we assume it's compatible | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 388 |         # XXX re-evaluate when we get RHEL10 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 389 |         DISTRO="rhel${os_RELEASE::1}" | 
 | 390 |     elif [[ "$os_VENDOR" =~ (XenServer) ]]; then | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 391 |         DISTRO="xs${os_RELEASE%.*}" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 392 |     else | 
| Ian Wienand | ed92e43 | 2016-02-16 10:56:56 +1100 | [diff] [blame] | 393 |         # We can't make a good choice here.  Setting a sensible DISTRO | 
 | 394 |         # is part of the problem, but not the major issue -- we really | 
 | 395 |         # only use DISTRO in the code as a fine-filter. | 
 | 396 |         # | 
 | 397 |         # The bigger problem is categorising the system into one of | 
 | 398 |         # our two big categories as Ubuntu/Debian-ish or | 
 | 399 |         # Fedora/CentOS-ish. | 
 | 400 |         # | 
 | 401 |         # The setting of os_PACKAGE above is only set to "deb" based | 
 | 402 |         # on a hard-coded list of vendor names ... thus we will | 
 | 403 |         # default to thinking unknown distros are RPM based | 
 | 404 |         # (ie. is_ubuntu does not match).  But the platform will then | 
 | 405 |         # also not match in is_fedora, because that also has a list of | 
 | 406 |         # names. | 
 | 407 |         # | 
 | 408 |         # So, if you are reading this, getting your distro supported | 
 | 409 |         # is really about making sure it matches correctly in these | 
 | 410 |         # functions.  Then you can choose a sensible way to construct | 
 | 411 |         # DISTRO based on your distros release approach. | 
 | 412 |         die $LINENO "Unable to determine DISTRO, can not continue." | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 413 |     fi | 
| Ian Wienand | 7710e7f | 2014-08-27 16:15:32 +1000 | [diff] [blame] | 414 |     typeset -xr DISTRO | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 415 | } | 
 | 416 |  | 
 | 417 | # Utility function for checking machine architecture | 
 | 418 | # is_arch arch-type | 
 | 419 | function is_arch { | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 420 |     [[ "$(uname -m)" == "$1" ]] | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 421 | } | 
 | 422 |  | 
| Wiekus Beukes | ec47bc1 | 2015-03-19 08:20:38 -0700 | [diff] [blame] | 423 | # Determine if current distribution is an Oracle distribution | 
 | 424 | # is_oraclelinux | 
 | 425 | function is_oraclelinux { | 
 | 426 |     if [[ -z "$os_VENDOR" ]]; then | 
 | 427 |         GetOSVersion | 
 | 428 |     fi | 
 | 429 |  | 
| Fernando Ribeiro | 4e6f8ca | 2016-04-12 23:34:03 -0300 | [diff] [blame] | 430 |     [ "$os_VENDOR" = "OracleServer" ] | 
| Wiekus Beukes | ec47bc1 | 2015-03-19 08:20:38 -0700 | [diff] [blame] | 431 | } | 
 | 432 |  | 
 | 433 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 434 | # Determine if current distribution is a Fedora-based distribution | 
 | 435 | # (Fedora, RHEL, CentOS, etc). | 
 | 436 | # is_fedora | 
 | 437 | function is_fedora { | 
 | 438 |     if [[ -z "$os_VENDOR" ]]; then | 
 | 439 |         GetOSVersion | 
 | 440 |     fi | 
 | 441 |  | 
| anju Tiwari | 6c639c9 | 2014-07-15 18:11:54 +0530 | [diff] [blame] | 442 |     [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \ | 
| tengqm | 78d3739 | 2016-03-15 23:08:00 -0400 | [diff] [blame] | 443 |         [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \ | 
| Fernando Ribeiro | 4e6f8ca | 2016-04-12 23:34:03 -0300 | [diff] [blame] | 444 |         [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ] || \ | 
| Markus Zoeller | b8335ee | 2017-09-26 08:32:50 +0200 | [diff] [blame] | 445 |         [ "$os_VENDOR" = "Virtuozzo" ] | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 446 | } | 
 | 447 |  | 
 | 448 |  | 
 | 449 | # Determine if current distribution is a SUSE-based distribution | 
 | 450 | # (openSUSE, SLE). | 
 | 451 | # is_suse | 
 | 452 | function is_suse { | 
 | 453 |     if [[ -z "$os_VENDOR" ]]; then | 
 | 454 |         GetOSVersion | 
 | 455 |     fi | 
 | 456 |  | 
| Ralf Haferkamp | 37dddb7 | 2016-03-02 15:25:57 +0100 | [diff] [blame] | 457 |     [[ "$os_VENDOR" =~ (openSUSE) || "$os_VENDOR" == "SUSE LINUX" ]] | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 458 | } | 
 | 459 |  | 
 | 460 |  | 
 | 461 | # Determine if current distribution is an Ubuntu-based distribution | 
 | 462 | # It will also detect non-Ubuntu but Debian-based distros | 
 | 463 | # is_ubuntu | 
 | 464 | function is_ubuntu { | 
 | 465 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 466 |         GetOSVersion | 
 | 467 |     fi | 
 | 468 |     [ "$os_PACKAGE" = "deb" ] | 
 | 469 | } | 
 | 470 |  | 
 | 471 |  | 
 | 472 | # Git Functions | 
 | 473 | # ============= | 
 | 474 |  | 
| Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 475 | # Returns openstack release name for a given branch name | 
 | 476 | # ``get_release_name_from_branch branch-name`` | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 477 | function get_release_name_from_branch { | 
| Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 478 |     local branch=$1 | 
| Adam Gandelman | 8f38572 | 2014-10-14 15:50:18 -0700 | [diff] [blame] | 479 |     if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then | 
| Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 480 |         echo ${branch#*/} | 
 | 481 |     else | 
 | 482 |         echo "master" | 
 | 483 |     fi | 
 | 484 | } | 
 | 485 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 486 | # git clone only if directory doesn't exist already.  Since ``DEST`` might not | 
 | 487 | # be owned by the installation user, we create the directory and change the | 
 | 488 | # ownership to the proper user. | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 489 | # Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists | 
 | 490 | # 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] | 491 | # does not exist (default is False, meaning the repo will be cloned). | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 492 | # Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 493 | # git_clone remote dest-dir branch | 
 | 494 | function git_clone { | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 495 |     local git_remote=$1 | 
 | 496 |     local git_dest=$2 | 
 | 497 |     local git_ref=$3 | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 498 |     local orig_dir | 
 | 499 |     orig_dir=$(pwd) | 
| Jamie Lennox | 51f0de5 | 2014-10-20 16:32:34 +0200 | [diff] [blame] | 500 |     local git_clone_flags="" | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 501 |  | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 502 |     RECLONE=$(trueorfalse False RECLONE) | 
| Kevin Benton | 59d52f3 | 2015-01-17 11:29:12 -0800 | [diff] [blame] | 503 |     if [[ "${GIT_DEPTH}" -gt 0 ]]; then | 
| Jamie Lennox | 51f0de5 | 2014-10-20 16:32:34 +0200 | [diff] [blame] | 504 |         git_clone_flags="$git_clone_flags --depth $GIT_DEPTH" | 
 | 505 |     fi | 
 | 506 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 507 |     if [[ "$OFFLINE" = "True" ]]; then | 
 | 508 |         echo "Running in offline mode, clones already exist" | 
 | 509 |         # 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] | 510 |         cd $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 511 |         git show --oneline | head -1 | 
| Sean Dague | 64bd016 | 2014-03-12 13:04:22 -0400 | [diff] [blame] | 512 |         cd $orig_dir | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 513 |         return | 
 | 514 |     fi | 
 | 515 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 516 |     if echo $git_ref | egrep -q "^refs"; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 517 |         # If our branch name is a gerrit style refs/changes/... | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 518 |         if [[ ! -d $git_dest ]]; then | 
| James E. Blair | ebe63d8 | 2015-09-24 07:43:50 -0700 | [diff] [blame] | 519 |             if [[ "$ERROR_ON_CLONE" = "True" ]]; then | 
 | 520 |                 echo "The $git_dest project was not found; if this is a gate job, add" | 
| James E. Blair | 35a0c57 | 2017-09-10 15:37:56 -0700 | [diff] [blame] | 521 |                 echo "the project to 'required-projects' in the job definition." | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 522 |                 die $LINENO "Cloning not allowed in this configuration" | 
| James E. Blair | ebe63d8 | 2015-09-24 07:43:50 -0700 | [diff] [blame] | 523 |             fi | 
| Jamie Lennox | 51f0de5 | 2014-10-20 16:32:34 +0200 | [diff] [blame] | 524 |             git_timed clone $git_clone_flags $git_remote $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 525 |         fi | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 526 |         cd $git_dest | 
 | 527 |         git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 528 |     else | 
 | 529 |         # do a full clone only if the directory doesn't exist | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 530 |         if [[ ! -d $git_dest ]]; then | 
| James E. Blair | ebe63d8 | 2015-09-24 07:43:50 -0700 | [diff] [blame] | 531 |             if [[ "$ERROR_ON_CLONE" = "True" ]]; then | 
 | 532 |                 echo "The $git_dest project was not found; if this is a gate job, add" | 
 | 533 |                 echo "the project to the \$PROJECTS variable in the job definition." | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 534 |                 die $LINENO "Cloning not allowed in this configuration" | 
| James E. Blair | ebe63d8 | 2015-09-24 07:43:50 -0700 | [diff] [blame] | 535 |             fi | 
| Jakub Wachowski | 90742fc | 2016-11-18 14:28:47 +0100 | [diff] [blame] | 536 |             # '--branch' can also take tags | 
 | 537 |             git_timed clone $git_clone_flags $git_remote $git_dest --branch $git_ref | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 538 |         elif [[ "$RECLONE" = "True" ]]; then | 
 | 539 |             # 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] | 540 |             cd $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 541 |             # set the url to pull from and fetch | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 542 |             git remote set-url origin $git_remote | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 543 |             git_timed fetch origin | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 544 |             # remove the existing ignored files (like pyc) as they cause breakage | 
 | 545 |             # (due to the py files having older timestamps than our pyc, so python | 
 | 546 |             # thinks the pyc files are correct using them) | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 547 |             find $git_dest -name '*.pyc' -delete | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 548 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 549 |             # handle git_ref accordingly to type (tag, branch) | 
 | 550 |             if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then | 
 | 551 |                 git_update_tag $git_ref | 
 | 552 |             elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then | 
 | 553 |                 git_update_branch $git_ref | 
 | 554 |             elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then | 
 | 555 |                 git_update_remote_branch $git_ref | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 556 |             else | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 557 |                 die $LINENO "$git_ref is neither branch nor tag" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 558 |             fi | 
 | 559 |  | 
 | 560 |         fi | 
 | 561 |     fi | 
 | 562 |  | 
 | 563 |     # 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] | 564 |     cd $git_dest | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 565 |     git show --oneline | head -1 | 
| Sean Dague | 64bd016 | 2014-03-12 13:04:22 -0400 | [diff] [blame] | 566 |     cd $orig_dir | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 567 | } | 
 | 568 |  | 
| Sean Dague | cc52406 | 2014-10-01 09:06:43 -0400 | [diff] [blame] | 569 | # A variation on git clone that lets us specify a project by it's | 
 | 570 | # actual name, like oslo.config. This is exceptionally useful in the | 
 | 571 | # library installation case | 
 | 572 | function git_clone_by_name { | 
 | 573 |     local name=$1 | 
 | 574 |     local repo=${GITREPO[$name]} | 
 | 575 |     local dir=${GITDIR[$name]} | 
 | 576 |     local branch=${GITBRANCH[$name]} | 
 | 577 |     git_clone $repo $dir $branch | 
 | 578 | } | 
 | 579 |  | 
 | 580 |  | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 581 | # git can sometimes get itself infinitely stuck with transient network | 
 | 582 | # errors or other issues with the remote end.  This wraps git in a | 
 | 583 | # timeout/retry loop and is intended to watch over non-local git | 
 | 584 | # processes that might hang.  GIT_TIMEOUT, if set, is passed directly | 
 | 585 | # to timeout(1); otherwise the default value of 0 maintains the status | 
 | 586 | # quo of waiting forever. | 
 | 587 | # usage: git_timed <git-command> | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 588 | function git_timed { | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 589 |     local count=0 | 
 | 590 |     local timeout=0 | 
 | 591 |  | 
 | 592 |     if [[ -n "${GIT_TIMEOUT}" ]]; then | 
 | 593 |         timeout=${GIT_TIMEOUT} | 
 | 594 |     fi | 
 | 595 |  | 
| Atsushi SAKAI | 2ca8af4 | 2015-12-08 15:36:13 +0900 | [diff] [blame] | 596 |     time_start "git_timed" | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 597 |     until timeout -s SIGINT ${timeout} git "$@"; do | 
 | 598 |         # 124 is timeout(1)'s special return code when it reached the | 
 | 599 |         # timeout; otherwise assume fatal failure | 
 | 600 |         if [[ $? -ne 124 ]]; then | 
 | 601 |             die $LINENO "git call failed: [git $@]" | 
 | 602 |         fi | 
 | 603 |  | 
 | 604 |         count=$(($count + 1)) | 
| Sean Dague | e4af929 | 2015-04-28 08:57:57 -0400 | [diff] [blame] | 605 |         warn $LINENO "timeout ${count} for git call: [git $@]" | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 606 |         if [ $count -eq 3 ]; then | 
 | 607 |             die $LINENO "Maximum of 3 git retries reached" | 
 | 608 |         fi | 
 | 609 |         sleep 5 | 
 | 610 |     done | 
| Atsushi SAKAI | 2ca8af4 | 2015-12-08 15:36:13 +0900 | [diff] [blame] | 611 |     time_stop "git_timed" | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 612 | } | 
 | 613 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 614 | # git update using reference as a branch. | 
 | 615 | # git_update_branch ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 616 | function git_update_branch { | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 617 |     local git_branch=$1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 618 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 619 |     git checkout -f origin/$git_branch | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 620 |     # a local branch might not exist | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 621 |     git branch -D $git_branch || true | 
 | 622 |     git checkout -b $git_branch | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 623 | } | 
 | 624 |  | 
 | 625 | # git update using reference as a branch. | 
 | 626 | # git_update_remote_branch ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 627 | function git_update_remote_branch { | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 628 |     local git_branch=$1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 629 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 630 |     git checkout -b $git_branch -t origin/$git_branch | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 631 | } | 
 | 632 |  | 
 | 633 | # git update using reference as a tag. Be careful editing source at that repo | 
 | 634 | # as working copy will be in a detached mode | 
 | 635 | # git_update_tag ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 636 | function git_update_tag { | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 637 |     local git_tag=$1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 638 |  | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 639 |     git tag -d $git_tag | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 640 |     # fetching given tag only | 
| Dean Troyer | 50cda69 | 2014-07-25 11:57:20 -0500 | [diff] [blame] | 641 |     git_timed fetch origin tag $git_tag | 
 | 642 |     git checkout -f $git_tag | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 643 | } | 
 | 644 |  | 
 | 645 |  | 
 | 646 | # OpenStack Functions | 
 | 647 | # =================== | 
 | 648 |  | 
 | 649 | # Get the default value for HOST_IP | 
 | 650 | # 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] | 651 | function get_default_host_ip { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 652 |     local fixed_range=$1 | 
 | 653 |     local floating_range=$2 | 
 | 654 |     local host_ip_iface=$3 | 
 | 655 |     local host_ip=$4 | 
| Brian Haley | 180f5eb | 2015-06-16 13:14:31 -0400 | [diff] [blame] | 656 |     local af=$5 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 657 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 658 |     # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable | 
 | 659 |     if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then | 
 | 660 |         host_ip="" | 
| Andreas Scheuring | a343027 | 2015-03-09 16:55:32 +0100 | [diff] [blame] | 661 |         # Find the interface used for the default route | 
| Brian Haley | 180f5eb | 2015-06-16 13:14:31 -0400 | [diff] [blame] | 662 |         host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)} | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 663 |         local host_ips | 
 | 664 |         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] | 665 |         local ip | 
 | 666 |         for ip in $host_ips; do | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 667 |             # Attempt to filter out IP addresses that are part of the fixed and | 
 | 668 |             # floating range. Note that this method only works if the ``netaddr`` | 
 | 669 |             # python library is installed. If it is not installed, an error | 
 | 670 |             # will be printed and the first IP from the interface will be used. | 
 | 671 |             # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct | 
 | 672 |             # address. | 
| Brian Haley | 180f5eb | 2015-06-16 13:14:31 -0400 | [diff] [blame] | 673 |             if [[ "$af" == "inet6" ]]; then | 
 | 674 |                 host_ip=$ip | 
 | 675 |                 break; | 
 | 676 |             fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 677 |             if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then | 
 | 678 |                 host_ip=$ip | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 679 |                 break; | 
 | 680 |             fi | 
 | 681 |         done | 
 | 682 |     fi | 
 | 683 |     echo $host_ip | 
 | 684 | } | 
 | 685 |  | 
| Attila Fazekas | f71b500 | 2014-05-28 09:52:22 +0200 | [diff] [blame] | 686 | # Generates hex string from ``size`` byte of pseudo random data | 
 | 687 | # generate_hex_string size | 
 | 688 | function generate_hex_string { | 
 | 689 |     local size=$1 | 
 | 690 |     hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom | 
 | 691 | } | 
 | 692 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 693 | # Grab a numbered field from python prettytable output | 
 | 694 | # Fields are numbered starting with 1 | 
 | 695 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. | 
 | 696 | # get_field field-number | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 697 | function get_field { | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 698 |     local data field | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 699 |     while read data; do | 
 | 700 |         if [ "$1" -lt 0 ]; then | 
 | 701 |             field="(\$(NF$1))" | 
 | 702 |         else | 
 | 703 |             field="\$$(($1 + 1))" | 
 | 704 |         fi | 
 | 705 |         echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" | 
 | 706 |     done | 
 | 707 | } | 
 | 708 |  | 
| yuntongjin | f26deea | 2015-02-28 10:50:34 +0800 | [diff] [blame] | 709 | # install default policy | 
 | 710 | # copy over a default policy.json and policy.d for projects | 
 | 711 | function install_default_policy { | 
 | 712 |     local project=$1 | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 713 |     local project_uc | 
 | 714 |     project_uc=$(echo $1|tr a-z A-Z) | 
| yuntongjin | f26deea | 2015-02-28 10:50:34 +0800 | [diff] [blame] | 715 |     local conf_dir="${project_uc}_CONF_DIR" | 
 | 716 |     # eval conf dir to get the variable | 
 | 717 |     conf_dir="${!conf_dir}" | 
 | 718 |     local project_dir="${project_uc}_DIR" | 
 | 719 |     # eval project dir to get the variable | 
 | 720 |     project_dir="${!project_dir}" | 
 | 721 |     local sample_conf_dir="${project_dir}/etc/${project}" | 
 | 722 |     local sample_policy_dir="${project_dir}/etc/${project}/policy.d" | 
 | 723 |  | 
 | 724 |     # first copy any policy.json | 
 | 725 |     cp -p $sample_conf_dir/policy.json $conf_dir | 
 | 726 |     # then optionally copy over policy.d | 
 | 727 |     if [[ -d $sample_policy_dir ]]; then | 
 | 728 |         cp -r $sample_policy_dir $conf_dir/policy.d | 
 | 729 |     fi | 
 | 730 | } | 
 | 731 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 732 | # Add a policy to a policy.json file | 
 | 733 | # Do nothing if the policy already exists | 
 | 734 | # ``policy_add policy_file policy_name policy_permissions`` | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 735 | function policy_add { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 736 |     local policy_file=$1 | 
 | 737 |     local policy_name=$2 | 
 | 738 |     local policy_perm=$3 | 
 | 739 |  | 
 | 740 |     if grep -q ${policy_name} ${policy_file}; then | 
 | 741 |         echo "Policy ${policy_name} already exists in ${policy_file}" | 
 | 742 |         return | 
 | 743 |     fi | 
 | 744 |  | 
 | 745 |     # Add a terminating comma to policy lines without one | 
 | 746 |     # Remove the closing '}' and all lines following to the end-of-file | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 747 |     local tmpfile | 
 | 748 |     tmpfile=$(mktemp) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 749 |     uniq ${policy_file} | sed -e ' | 
 | 750 |         s/]$/],/ | 
 | 751 |         /^[}]/,$d | 
 | 752 |     ' > ${tmpfile} | 
 | 753 |  | 
 | 754 |     # Append policy and closing brace | 
 | 755 |     echo "    \"${policy_name}\": ${policy_perm}" >>${tmpfile} | 
 | 756 |     echo "}" >>${tmpfile} | 
 | 757 |  | 
 | 758 |     mv ${tmpfile} ${policy_file} | 
 | 759 | } | 
 | 760 |  | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 761 | # Gets or creates a domain | 
 | 762 | # Usage: get_or_create_domain <name> <description> | 
 | 763 | function get_or_create_domain { | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 764 |     local domain_id | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 765 |     # Gets domain id | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 766 |     domain_id=$( | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 767 |         # Gets domain id | 
| Steve Martinelli | 050a0d5 | 2015-09-06 22:03:54 +0000 | [diff] [blame] | 768 |         openstack domain show $1 \ | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 769 |             -f value -c id 2>/dev/null || | 
 | 770 |         # Creates new domain | 
| Steve Martinelli | 050a0d5 | 2015-09-06 22:03:54 +0000 | [diff] [blame] | 771 |         openstack domain create $1 \ | 
| Alistair Coles | 24779f6 | 2014-10-15 18:57:59 +0100 | [diff] [blame] | 772 |             --description "$2" \ | 
 | 773 |             -f value -c id | 
 | 774 |     ) | 
 | 775 |     echo $domain_id | 
 | 776 | } | 
 | 777 |  | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 778 | # Gets or creates group | 
| Jamie Lennox | 9d7e776 | 2015-05-29 01:08:53 +0000 | [diff] [blame] | 779 | # Usage: get_or_create_group <groupname> <domain> [<description>] | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 780 | function get_or_create_group { | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 781 |     local desc="${3:-}" | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 782 |     local group_id | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 783 |     # Gets group id | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 784 |     group_id=$( | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 785 |         # Creates new group with --or-show | 
| Steve Martinelli | 050a0d5 | 2015-09-06 22:03:54 +0000 | [diff] [blame] | 786 |         openstack group create $1 \ | 
| Jamie Lennox | 9d7e776 | 2015-05-29 01:08:53 +0000 | [diff] [blame] | 787 |             --domain $2 --description "$desc" --or-show \ | 
| Steve Martinelli | b74e01c | 2014-12-18 01:35:35 -0500 | [diff] [blame] | 788 |             -f value -c id | 
 | 789 |     ) | 
 | 790 |     echo $group_id | 
 | 791 | } | 
 | 792 |  | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 793 | # Gets or creates user | 
| Jamie Lennox | 9d7e776 | 2015-05-29 01:08:53 +0000 | [diff] [blame] | 794 | # Usage: get_or_create_user <username> <password> <domain> [<email>] | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 795 | function get_or_create_user { | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 796 |     local user_id | 
| Jamie Lennox | 9d7e776 | 2015-05-29 01:08:53 +0000 | [diff] [blame] | 797 |     if [[ ! -z "$4" ]]; then | 
 | 798 |         local email="--email=$4" | 
| Gael Chamoulaud | 6dd8a8b | 2014-07-22 01:12:12 +0200 | [diff] [blame] | 799 |     else | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 800 |         local email="" | 
| Gael Chamoulaud | 6dd8a8b | 2014-07-22 01:12:12 +0200 | [diff] [blame] | 801 |     fi | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 802 |     # Gets user id | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 803 |     user_id=$( | 
| Steve Martinelli | 245daa2 | 2014-11-14 02:17:22 -0500 | [diff] [blame] | 804 |         # Creates new user with --or-show | 
| Jamie Lennox | 9d7e776 | 2015-05-29 01:08:53 +0000 | [diff] [blame] | 805 |         openstack user create \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 806 |             $1 \ | 
 | 807 |             --password "$2" \ | 
| Jamie Lennox | 9d7e776 | 2015-05-29 01:08:53 +0000 | [diff] [blame] | 808 |             --domain=$3 \ | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 809 |             $email \ | 
| Steve Martinelli | 245daa2 | 2014-11-14 02:17:22 -0500 | [diff] [blame] | 810 |             --or-show \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 811 |             -f value -c id | 
 | 812 |     ) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 813 |     echo $user_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 814 | } | 
 | 815 |  | 
 | 816 | # Gets or creates project | 
| Jamie Lennox | b632c9e | 2015-05-28 23:36:15 +0000 | [diff] [blame] | 817 | # Usage: get_or_create_project <name> <domain> | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 818 | function get_or_create_project { | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 819 |     local project_id | 
 | 820 |     project_id=$( | 
| Steve Martinelli | 245daa2 | 2014-11-14 02:17:22 -0500 | [diff] [blame] | 821 |         # Creates new project with --or-show | 
| Steve Martinelli | 050a0d5 | 2015-09-06 22:03:54 +0000 | [diff] [blame] | 822 |         openstack project create $1 \ | 
| Jamie Lennox | b632c9e | 2015-05-28 23:36:15 +0000 | [diff] [blame] | 823 |             --domain=$2 \ | 
 | 824 |             --or-show -f value -c id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 825 |     ) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 826 |     echo $project_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 827 | } | 
 | 828 |  | 
 | 829 | # Gets or creates role | 
 | 830 | # Usage: get_or_create_role <name> | 
 | 831 | function get_or_create_role { | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 832 |     local role_id | 
 | 833 |     role_id=$( | 
| Steve Martinelli | 245daa2 | 2014-11-14 02:17:22 -0500 | [diff] [blame] | 834 |         # Creates role with --or-show | 
| Jamie Lennox | 72ce6ac | 2015-07-02 09:19:01 +1000 | [diff] [blame] | 835 |         openstack role create $1 \ | 
| Jamie Lennox | 72ce6ac | 2015-07-02 09:19:01 +1000 | [diff] [blame] | 836 |             --or-show -f value -c id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 837 |     ) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 838 |     echo $role_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 839 | } | 
 | 840 |  | 
| Jamie Lennox | cbcbd8f | 2016-01-21 16:08:14 -0600 | [diff] [blame] | 841 | # Returns the domain parts of a function call if present | 
 | 842 | # Usage: _get_domain_args [<user_domain> <project_domain>] | 
 | 843 | function _get_domain_args { | 
 | 844 |     local domain | 
 | 845 |     domain="" | 
 | 846 |  | 
 | 847 |     if [[ -n "$1" ]]; then | 
 | 848 |         domain="$domain --user-domain $1" | 
 | 849 |     fi | 
 | 850 |     if [[ -n "$2" ]]; then | 
 | 851 |         domain="$domain --project-domain $2" | 
 | 852 |     fi | 
 | 853 |  | 
 | 854 |     echo $domain | 
 | 855 | } | 
 | 856 |  | 
| Jamie Lennox | 9b215db | 2015-02-10 18:19:57 +1100 | [diff] [blame] | 857 | # Gets or adds user role to project | 
| Jamie Lennox | cbcbd8f | 2016-01-21 16:08:14 -0600 | [diff] [blame] | 858 | # Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>] | 
| Jamie Lennox | 9b215db | 2015-02-10 18:19:57 +1100 | [diff] [blame] | 859 | function get_or_add_user_project_role { | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 860 |     local user_role_id | 
| Jamie Lennox | cbcbd8f | 2016-01-21 16:08:14 -0600 | [diff] [blame] | 861 |  | 
 | 862 |     domain_args=$(_get_domain_args $4 $5) | 
 | 863 |  | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 864 |     # Gets user role id | 
| Mike Perez | c271b3e | 2016-10-03 16:00:33 -0700 | [diff] [blame] | 865 |     user_role_id=$(openstack role assignment list \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 866 |         --role $1 \ | 
| Steve Martinelli | 5541a61 | 2015-01-19 15:58:49 -0500 | [diff] [blame] | 867 |         --user $2 \ | 
| Jamie Lennox | 72ce6ac | 2015-07-02 09:19:01 +1000 | [diff] [blame] | 868 |         --project $3 \ | 
| Jamie Lennox | cbcbd8f | 2016-01-21 16:08:14 -0600 | [diff] [blame] | 869 |         $domain_args \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 870 |         | grep '^|\s[a-f0-9]\+' | get_field 1) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 871 |     if [[ -z "$user_role_id" ]]; then | 
| Roxana Gherle | 50821be | 2015-09-22 10:52:46 -0700 | [diff] [blame] | 872 |         # Adds role to user and get it | 
 | 873 |         openstack role add $1 \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 874 |             --user $2 \ | 
| Jamie Lennox | cbcbd8f | 2016-01-21 16:08:14 -0600 | [diff] [blame] | 875 |             --project $3 \ | 
 | 876 |             $domain_args | 
| Mike Perez | c271b3e | 2016-10-03 16:00:33 -0700 | [diff] [blame] | 877 |         user_role_id=$(openstack role assignment list \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 878 |             --role $1 \ | 
| Roxana Gherle | 50821be | 2015-09-22 10:52:46 -0700 | [diff] [blame] | 879 |             --user $2 \ | 
| Roxana Gherle | 50821be | 2015-09-22 10:52:46 -0700 | [diff] [blame] | 880 |             --project $3 \ | 
| Jamie Lennox | cbcbd8f | 2016-01-21 16:08:14 -0600 | [diff] [blame] | 881 |             $domain_args \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 882 |             | grep '^|\s[a-f0-9]\+' | get_field 1) | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 883 |     fi | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 884 |     echo $user_role_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 885 | } | 
 | 886 |  | 
| Steve Martinelli | 4ce859a | 2015-12-20 01:27:30 -0500 | [diff] [blame] | 887 | # Gets or adds user role to domain | 
 | 888 | # Usage: get_or_add_user_domain_role <role> <user> <domain> | 
 | 889 | function get_or_add_user_domain_role { | 
 | 890 |     local user_role_id | 
 | 891 |     # Gets user role id | 
| Mike Perez | c271b3e | 2016-10-03 16:00:33 -0700 | [diff] [blame] | 892 |     user_role_id=$(openstack role assignment list \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 893 |         --role $1 \ | 
| Steve Martinelli | 4ce859a | 2015-12-20 01:27:30 -0500 | [diff] [blame] | 894 |         --user $2 \ | 
| Steve Martinelli | 4ce859a | 2015-12-20 01:27:30 -0500 | [diff] [blame] | 895 |         --domain $3 \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 896 |         | grep '^|\s[a-f0-9]\+' | get_field 1) | 
| Steve Martinelli | 4ce859a | 2015-12-20 01:27:30 -0500 | [diff] [blame] | 897 |     if [[ -z "$user_role_id" ]]; then | 
 | 898 |         # Adds role to user and get it | 
 | 899 |         openstack role add $1 \ | 
 | 900 |             --user $2 \ | 
 | 901 |             --domain $3 | 
| Mike Perez | c271b3e | 2016-10-03 16:00:33 -0700 | [diff] [blame] | 902 |         user_role_id=$(openstack role assignment list \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 903 |             --role $1 \ | 
| Steve Martinelli | 4ce859a | 2015-12-20 01:27:30 -0500 | [diff] [blame] | 904 |             --user $2 \ | 
| Steve Martinelli | 4ce859a | 2015-12-20 01:27:30 -0500 | [diff] [blame] | 905 |             --domain $3 \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 906 |             | grep '^|\s[a-f0-9]\+' | get_field 1) | 
| Steve Martinelli | 4ce859a | 2015-12-20 01:27:30 -0500 | [diff] [blame] | 907 |     fi | 
 | 908 |     echo $user_role_id | 
 | 909 | } | 
 | 910 |  | 
| Steve Martinelli | 4599fd1 | 2015-03-12 21:30:58 -0400 | [diff] [blame] | 911 | # Gets or adds group role to project | 
 | 912 | # Usage: get_or_add_group_project_role <role> <group> <project> | 
 | 913 | function get_or_add_group_project_role { | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 914 |     local group_role_id | 
| Steve Martinelli | 4599fd1 | 2015-03-12 21:30:58 -0400 | [diff] [blame] | 915 |     # Gets group role id | 
| Mike Perez | c271b3e | 2016-10-03 16:00:33 -0700 | [diff] [blame] | 916 |     group_role_id=$(openstack role assignment list \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 917 |         --role $1 \ | 
| Steve Martinelli | 4599fd1 | 2015-03-12 21:30:58 -0400 | [diff] [blame] | 918 |         --group $2 \ | 
 | 919 |         --project $3 \ | 
| Mike Perez | c271b3e | 2016-10-03 16:00:33 -0700 | [diff] [blame] | 920 |         -f value) | 
| Steve Martinelli | 4599fd1 | 2015-03-12 21:30:58 -0400 | [diff] [blame] | 921 |     if [[ -z "$group_role_id" ]]; then | 
| Jamie Lennox | 72ce6ac | 2015-07-02 09:19:01 +1000 | [diff] [blame] | 922 |         # Adds role to group and get it | 
 | 923 |         openstack role add $1 \ | 
| Jamie Lennox | 72ce6ac | 2015-07-02 09:19:01 +1000 | [diff] [blame] | 924 |             --group $2 \ | 
 | 925 |             --project $3 | 
| Mike Perez | c271b3e | 2016-10-03 16:00:33 -0700 | [diff] [blame] | 926 |         group_role_id=$(openstack role assignment list \ | 
| Clark Boylan | 3e9b562 | 2017-06-14 15:29:47 -0700 | [diff] [blame] | 927 |             --role $1 \ | 
| Steve Martinelli | 4599fd1 | 2015-03-12 21:30:58 -0400 | [diff] [blame] | 928 |             --group $2 \ | 
 | 929 |             --project $3 \ | 
| Mike Perez | c271b3e | 2016-10-03 16:00:33 -0700 | [diff] [blame] | 930 |             -f value) | 
| Steve Martinelli | 4599fd1 | 2015-03-12 21:30:58 -0400 | [diff] [blame] | 931 |     fi | 
 | 932 |     echo $group_role_id | 
 | 933 | } | 
 | 934 |  | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 935 | # Gets or creates service | 
 | 936 | # Usage: get_or_create_service <name> <type> <description> | 
 | 937 | function get_or_create_service { | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 938 |     local service_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 939 |     # Gets service id | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 940 |     service_id=$( | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 941 |         # Gets service id | 
| Jamie Lennox | aedb8b9 | 2015-07-02 17:39:07 +1000 | [diff] [blame] | 942 |         openstack service show $2 -f value -c id 2>/dev/null || | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 943 |         # Creates new service if not exists | 
 | 944 |         openstack service create \ | 
| Steve Martinelli | 789af5c | 2015-01-19 16:11:44 -0500 | [diff] [blame] | 945 |             $2 \ | 
 | 946 |             --name $1 \ | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 947 |             --description="$3" \ | 
 | 948 |             -f value -c id | 
 | 949 |     ) | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 950 |     echo $service_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 951 | } | 
 | 952 |  | 
| Jamie Lennox | b17ad75 | 2015-05-29 06:04:47 +0000 | [diff] [blame] | 953 | # Create an endpoint with a specific interface | 
| Matt Riedemann | ae4578b | 2016-04-23 01:45:40 +0000 | [diff] [blame] | 954 | # Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region> | 
 | 955 | function _get_or_create_endpoint_with_interface { | 
| Ian Wienand | 11d276c | 2015-07-02 09:34:34 +1000 | [diff] [blame] | 956 |     local endpoint_id | 
| Matt Riedemann | ae4578b | 2016-04-23 01:45:40 +0000 | [diff] [blame] | 957 |     endpoint_id=$(openstack endpoint list \ | 
 | 958 |         --service $1 \ | 
 | 959 |         --interface $2 \ | 
 | 960 |         --region $4 \ | 
 | 961 |         -c ID -f value) | 
 | 962 |     if [[ -z "$endpoint_id" ]]; then | 
 | 963 |         # Creates new endpoint | 
 | 964 |         endpoint_id=$(openstack endpoint create \ | 
 | 965 |             $1 $2 $3 --region $4 -f value -c id) | 
 | 966 |     fi | 
 | 967 |  | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 968 |     echo $endpoint_id | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 969 | } | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 970 |  | 
| Sean Dague | 7d1ec43 | 2016-04-22 09:19:10 -0400 | [diff] [blame] | 971 | # Gets or creates endpoint | 
| Sean Dague | 11eb201 | 2017-02-13 16:16:59 -0500 | [diff] [blame] | 972 | # Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl] | 
| Matt Riedemann | ae4578b | 2016-04-23 01:45:40 +0000 | [diff] [blame] | 973 | function get_or_create_endpoint { | 
| Jamie Lennox | b17ad75 | 2015-05-29 06:04:47 +0000 | [diff] [blame] | 974 |     # NOTE(jamielennnox): when converting to v3 endpoint creation we go from | 
 | 975 |     # creating one endpoint with multiple urls to multiple endpoints each with | 
 | 976 |     # a different interface.  To maintain the existing function interface we | 
| Matt Riedemann | ae4578b | 2016-04-23 01:45:40 +0000 | [diff] [blame] | 977 |     # create 3 endpoints and return the id of the public one. In reality | 
| Jamie Lennox | b17ad75 | 2015-05-29 06:04:47 +0000 | [diff] [blame] | 978 |     # returning the public id will not make a lot of difference as there are no | 
 | 979 |     # scenarios currently that use the returned id. Ideally this behaviour | 
 | 980 |     # should be pushed out to the service setups and let them create the | 
 | 981 |     # endpoints they need. | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 982 |     local public_id | 
| Matt Riedemann | ae4578b | 2016-04-23 01:45:40 +0000 | [diff] [blame] | 983 |     public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2) | 
| Sean Dague | 11eb201 | 2017-02-13 16:16:59 -0500 | [diff] [blame] | 984 |     # only create admin/internal urls if provided content for them | 
 | 985 |     if [[ -n "$4" ]]; then | 
 | 986 |         _get_or_create_endpoint_with_interface $1 admin $4 $2 | 
 | 987 |     fi | 
 | 988 |     if [[ -n "$5" ]]; then | 
 | 989 |         _get_or_create_endpoint_with_interface $1 internal $5 $2 | 
 | 990 |     fi | 
| Jamie Lennox | b17ad75 | 2015-05-29 06:04:47 +0000 | [diff] [blame] | 991 |     # return the public id to indicate success, and this is the endpoint most likely wanted | 
 | 992 |     echo $public_id | 
 | 993 | } | 
 | 994 |  | 
 | 995 | # Get a URL from the identity service | 
 | 996 | # Usage: get_endpoint_url <service> <interface> | 
 | 997 | function get_endpoint_url { | 
 | 998 |     echo $(openstack endpoint list \ | 
 | 999 |             --service $1 --interface $2 \ | 
| Jamie Lennox | b17ad75 | 2015-05-29 06:04:47 +0000 | [diff] [blame] | 1000 |             -c URL -f value) | 
 | 1001 | } | 
 | 1002 |  | 
| Jim Rollenhagen | 4736707 | 2015-12-10 14:24:00 +0000 | [diff] [blame] | 1003 | # check if we are using ironic with hardware | 
 | 1004 | # TODO(jroll) this is a kludge left behind when ripping ironic code | 
 | 1005 | # out of tree, as it is used by nova and neutron. | 
 | 1006 | # figure out a way to refactor nova/neutron code to eliminate this | 
 | 1007 | function is_ironic_hardware { | 
| Lucas Alvares Gomes | 8a4dea2 | 2016-02-18 15:52:22 +0000 | [diff] [blame] | 1008 |     is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0 | 
| Jim Rollenhagen | 4736707 | 2015-12-10 14:24:00 +0000 | [diff] [blame] | 1009 |     return 1 | 
 | 1010 | } | 
 | 1011 |  | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1012 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1013 | # Package Functions | 
 | 1014 | # ================= | 
 | 1015 |  | 
 | 1016 | # _get_package_dir | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1017 | function _get_package_dir { | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1018 |     local base_dir=$1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1019 |     local pkg_dir | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1020 |  | 
 | 1021 |     if [[ -z "$base_dir" ]]; then | 
 | 1022 |         base_dir=$FILES | 
 | 1023 |     fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1024 |     if is_ubuntu; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1025 |         pkg_dir=$base_dir/debs | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1026 |     elif is_fedora; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1027 |         pkg_dir=$base_dir/rpms | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1028 |     elif is_suse; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1029 |         pkg_dir=$base_dir/rpms-suse | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1030 |     else | 
 | 1031 |         exit_distro_not_supported "list of packages" | 
 | 1032 |     fi | 
 | 1033 |     echo "$pkg_dir" | 
 | 1034 | } | 
 | 1035 |  | 
| Sean Dague | 88ee8ce | 2015-12-02 07:47:31 -0500 | [diff] [blame] | 1036 | # Wrapper for ``apt-get update`` to try multiple times on the update | 
 | 1037 | # to address bad package mirrors (which happen all the time). | 
 | 1038 | function apt_get_update { | 
 | 1039 |     # only do this once per run | 
 | 1040 |     if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then | 
 | 1041 |         return | 
 | 1042 |     fi | 
 | 1043 |  | 
 | 1044 |     # bail if we are offline | 
 | 1045 |     [[ "$OFFLINE" = "True" ]] && return | 
 | 1046 |  | 
 | 1047 |     local sudo="sudo" | 
 | 1048 |     [[ "$(id -u)" = "0" ]] && sudo="env" | 
 | 1049 |  | 
 | 1050 |     # time all the apt operations | 
 | 1051 |     time_start "apt-get-update" | 
 | 1052 |  | 
 | 1053 |     local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} " | 
 | 1054 |     local update_cmd="$sudo $proxies apt-get update" | 
 | 1055 |     if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then | 
 | 1056 |         die $LINENO "Failed to update apt repos, we're dead now" | 
 | 1057 |     fi | 
 | 1058 |  | 
 | 1059 |     REPOS_UPDATED=True | 
 | 1060 |     # stop the clock | 
 | 1061 |     time_stop "apt-get-update" | 
 | 1062 | } | 
 | 1063 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1064 | # Wrapper for ``apt-get`` to set cache and proxy environment variables | 
 | 1065 | # Uses globals ``OFFLINE``, ``*_proxy`` | 
 | 1066 | # apt_get operation package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1067 | function apt_get { | 
| Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 1068 |     local xtrace result | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 1069 |     xtrace=$(set +o | grep xtrace) | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1070 |     set +o xtrace | 
 | 1071 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1072 |     [[ "$OFFLINE" = "True" || -z "$@" ]] && return | 
 | 1073 |     local sudo="sudo" | 
 | 1074 |     [[ "$(id -u)" = "0" ]] && sudo="env" | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1075 |  | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 1076 |     # time all the apt operations | 
 | 1077 |     time_start "apt-get" | 
 | 1078 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1079 |     $xtrace | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1080 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1081 |     $sudo DEBIAN_FRONTEND=noninteractive \ | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1082 |         http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \ | 
 | 1083 |         no_proxy=${no_proxy:-} \ | 
| John L. Villalovos | f568c3a | 2016-01-07 19:10:50 -0800 | [diff] [blame] | 1084 |         apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null | 
| Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 1085 |     result=$? | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 1086 |  | 
 | 1087 |     # stop the clock | 
 | 1088 |     time_stop "apt-get" | 
| Federico Ressi | e208d06 | 2015-11-21 11:15:39 +0000 | [diff] [blame] | 1089 |     return $result | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1090 | } | 
 | 1091 |  | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1092 | function _parse_package_files { | 
 | 1093 |     local files_to_parse=$@ | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1094 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1095 |     if [[ -z "$DISTRO" ]]; then | 
 | 1096 |         GetDistro | 
 | 1097 |     fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1098 |  | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1099 |     for fname in ${files_to_parse}; do | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1100 |         local OIFS line package distros distro | 
 | 1101 |         [[ -e $fname ]] || continue | 
 | 1102 |  | 
 | 1103 |         OIFS=$IFS | 
 | 1104 |         IFS=$'\n' | 
 | 1105 |         for line in $(<${fname}); do | 
 | 1106 |             if [[ $line =~ "NOPRIME" ]]; then | 
 | 1107 |                 continue | 
 | 1108 |             fi | 
 | 1109 |  | 
| Ian Wienand | 1ff75ff | 2016-02-19 14:28:37 +1100 | [diff] [blame] | 1110 |             # Assume we want this package; free-form | 
 | 1111 |             # comments allowed after a # | 
 | 1112 |             package=${line%%#*} | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1113 |             inst_pkg=1 | 
 | 1114 |  | 
 | 1115 |             # Look for # dist:xxx in comment | 
 | 1116 |             if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then | 
 | 1117 |                 # We are using BASH regexp matching feature. | 
 | 1118 |                 package=${BASH_REMATCH[1]} | 
 | 1119 |                 distros=${BASH_REMATCH[2]} | 
| Atsushi SAKAI | 33c9a67 | 2015-11-12 19:50:00 +0900 | [diff] [blame] | 1120 |                 # In bash ${VAR,,} will lowercase VAR | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1121 |                 # Look for a match in the distro list | 
 | 1122 |                 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then | 
 | 1123 |                     # If no match then skip this package | 
 | 1124 |                     inst_pkg=0 | 
 | 1125 |                 fi | 
 | 1126 |             fi | 
 | 1127 |  | 
| David Rabel | 682e0ab | 2017-03-17 19:19:00 +0100 | [diff] [blame] | 1128 |             # Look for # not:xxx in comment | 
 | 1129 |             if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then | 
 | 1130 |                 # We are using BASH regexp matching feature. | 
 | 1131 |                 package=${BASH_REMATCH[1]} | 
 | 1132 |                 distros=${BASH_REMATCH[2]} | 
 | 1133 |                 # In bash ${VAR,,} will lowercase VAR | 
 | 1134 |                 # Look for a match in the distro list | 
 | 1135 |                 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then | 
 | 1136 |                     # If match then skip this package | 
 | 1137 |                     inst_pkg=0 | 
 | 1138 |                 fi | 
 | 1139 |             fi | 
 | 1140 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1141 |             if [[ $inst_pkg = 1 ]]; then | 
 | 1142 |                 echo $package | 
 | 1143 |             fi | 
 | 1144 |         done | 
 | 1145 |         IFS=$OIFS | 
 | 1146 |     done | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1147 | } | 
 | 1148 |  | 
 | 1149 | # get_packages() collects a list of package names of any type from the | 
 | 1150 | # prerequisite files in ``files/{debs|rpms}``.  The list is intended | 
 | 1151 | # to be passed to a package installer such as apt or yum. | 
 | 1152 | # | 
 | 1153 | # Only packages required for the services in 1st argument will be | 
 | 1154 | # included.  Two bits of metadata are recognized in the prerequisite files: | 
 | 1155 | # | 
 | 1156 | # - ``# NOPRIME`` defers installation to be performed later in `stack.sh` | 
 | 1157 | # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection | 
 | 1158 | #   of the package to the distros listed.  The distro names are case insensitive. | 
| David Rabel | 682e0ab | 2017-03-17 19:19:00 +0100 | [diff] [blame] | 1159 | # - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection | 
 | 1160 | #   of the package to the distros not listed. The distro names are case insensitive. | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1161 | function get_packages { | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 1162 |     local xtrace | 
 | 1163 |     xtrace=$(set +o | grep xtrace) | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1164 |     set +o xtrace | 
 | 1165 |     local services=$@ | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 1166 |     local package_dir | 
 | 1167 |     package_dir=$(_get_package_dir) | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1168 |     local file_to_parse="" | 
 | 1169 |     local service="" | 
 | 1170 |  | 
| Ian Wienand | 7d515b5 | 2015-11-09 15:04:32 +1100 | [diff] [blame] | 1171 |     if [ $# -ne 1 ]; then | 
 | 1172 |         die $LINENO "get_packages takes a single, comma-separated argument" | 
 | 1173 |     fi | 
 | 1174 |  | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1175 |     if [[ -z "$package_dir" ]]; then | 
 | 1176 |         echo "No package directory supplied" | 
 | 1177 |         return 1 | 
 | 1178 |     fi | 
 | 1179 |     for service in ${services//,/ }; do | 
 | 1180 |         # Allow individual services to specify dependencies | 
 | 1181 |         if [[ -e ${package_dir}/${service} ]]; then | 
 | 1182 |             file_to_parse="${file_to_parse} ${package_dir}/${service}" | 
 | 1183 |         fi | 
 | 1184 |         # NOTE(sdague) n-api needs glance for now because that's where | 
 | 1185 |         # glance client is | 
 | 1186 |         if [[ $service == n-api ]]; then | 
| Ryan Hsu | 6f3f310 | 2015-03-19 16:26:45 -0700 | [diff] [blame] | 1187 |             if [[ ! $file_to_parse =~ $package_dir/nova ]]; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1188 |                 file_to_parse="${file_to_parse} ${package_dir}/nova" | 
 | 1189 |             fi | 
| Ryan Hsu | 6f3f310 | 2015-03-19 16:26:45 -0700 | [diff] [blame] | 1190 |             if [[ ! $file_to_parse =~ $package_dir/glance ]]; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1191 |                 file_to_parse="${file_to_parse} ${package_dir}/glance" | 
 | 1192 |             fi | 
 | 1193 |         elif [[ $service == c-* ]]; then | 
| Ryan Hsu | 6f3f310 | 2015-03-19 16:26:45 -0700 | [diff] [blame] | 1194 |             if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1195 |                 file_to_parse="${file_to_parse} ${package_dir}/cinder" | 
 | 1196 |             fi | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1197 |         elif [[ $service == s-* ]]; then | 
| Ryan Hsu | 6f3f310 | 2015-03-19 16:26:45 -0700 | [diff] [blame] | 1198 |             if [[ ! $file_to_parse =~ $package_dir/swift ]]; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1199 |                 file_to_parse="${file_to_parse} ${package_dir}/swift" | 
 | 1200 |             fi | 
 | 1201 |         elif [[ $service == n-* ]]; then | 
| Ryan Hsu | 6f3f310 | 2015-03-19 16:26:45 -0700 | [diff] [blame] | 1202 |             if [[ ! $file_to_parse =~ $package_dir/nova ]]; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1203 |                 file_to_parse="${file_to_parse} ${package_dir}/nova" | 
 | 1204 |             fi | 
 | 1205 |         elif [[ $service == g-* ]]; then | 
| Ryan Hsu | 6f3f310 | 2015-03-19 16:26:45 -0700 | [diff] [blame] | 1206 |             if [[ ! $file_to_parse =~ $package_dir/glance ]]; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1207 |                 file_to_parse="${file_to_parse} ${package_dir}/glance" | 
 | 1208 |             fi | 
 | 1209 |         elif [[ $service == key* ]]; then | 
| Ryan Hsu | 6f3f310 | 2015-03-19 16:26:45 -0700 | [diff] [blame] | 1210 |             if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1211 |                 file_to_parse="${file_to_parse} ${package_dir}/keystone" | 
 | 1212 |             fi | 
| Ihar Hrachyshka | 387aadd | 2017-09-14 09:25:59 -0600 | [diff] [blame] | 1213 |         elif [[ $service == q-* || $service == neutron-* ]]; then | 
 | 1214 |             if [[ ! $file_to_parse =~ $package_dir/neutron-common ]]; then | 
 | 1215 |                 file_to_parse="${file_to_parse} ${package_dir}/neutron-common" | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1216 |             fi | 
 | 1217 |         elif [[ $service == ir-* ]]; then | 
| Ryan Hsu | 6f3f310 | 2015-03-19 16:26:45 -0700 | [diff] [blame] | 1218 |             if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1219 |                 file_to_parse="${file_to_parse} ${package_dir}/ironic" | 
 | 1220 |             fi | 
 | 1221 |         fi | 
 | 1222 |     done | 
 | 1223 |     echo "$(_parse_package_files $file_to_parse)" | 
 | 1224 |     $xtrace | 
 | 1225 | } | 
 | 1226 |  | 
 | 1227 | # get_plugin_packages() collects a list of package names of any type from a | 
 | 1228 | # plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``.  The | 
 | 1229 | # list is intended to be passed to a package installer such as apt or yum. | 
 | 1230 | # | 
 | 1231 | # Only packages required for enabled and collected plugins will included. | 
 | 1232 | # | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 1233 | # 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] | 1234 | # in these prerequisite files, see get_packages() for more info. | 
 | 1235 | function get_plugin_packages { | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 1236 |     local xtrace | 
 | 1237 |     xtrace=$(set +o | grep xtrace) | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1238 |     set +o xtrace | 
 | 1239 |     local files_to_parse="" | 
 | 1240 |     local package_dir="" | 
 | 1241 |     for plugin in ${DEVSTACK_PLUGINS//,/ }; do | 
| Ian Wienand | 7ae9729 | 2016-02-16 14:50:53 +1100 | [diff] [blame] | 1242 |         package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)" | 
| Dmitry Tantsur | 5979f47 | 2016-01-03 18:08:14 +0100 | [diff] [blame] | 1243 |         files_to_parse+=" $package_dir/$plugin" | 
| Adam Gandelman | 7ca90cd | 2015-03-04 17:25:07 -0800 | [diff] [blame] | 1244 |     done | 
 | 1245 |     echo "$(_parse_package_files $files_to_parse)" | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1246 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1247 | } | 
 | 1248 |  | 
 | 1249 | # Distro-agnostic package installer | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1250 | # Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1251 | # install_package package [package ...] | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 1252 | function update_package_repo { | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1253 |     NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False} | 
 | 1254 |     REPOS_UPDATED=${REPOS_UPDATED:-False} | 
 | 1255 |     RETRY_UPDATE=${RETRY_UPDATE:-False} | 
 | 1256 |  | 
| Paul Linchpiner | 9e17974 | 2014-07-13 22:23:00 -0700 | [diff] [blame] | 1257 |     if [[ "$NO_UPDATE_REPOS" = "True" ]]; then | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 1258 |         return 0 | 
 | 1259 |     fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1260 |  | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 1261 |     if is_ubuntu; then | 
| Sean Dague | 88ee8ce | 2015-12-02 07:47:31 -0500 | [diff] [blame] | 1262 |         apt_get_update | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 1263 |     fi | 
 | 1264 | } | 
 | 1265 |  | 
 | 1266 | function real_install_package { | 
 | 1267 |     if is_ubuntu; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1268 |         apt_get install "$@" | 
 | 1269 |     elif is_fedora; then | 
 | 1270 |         yum_install "$@" | 
 | 1271 |     elif is_suse; then | 
 | 1272 |         zypper_install "$@" | 
 | 1273 |     else | 
 | 1274 |         exit_distro_not_supported "installing packages" | 
 | 1275 |     fi | 
 | 1276 | } | 
 | 1277 |  | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 1278 | # Distro-agnostic package installer | 
 | 1279 | # install_package package [package ...] | 
 | 1280 | function install_package { | 
 | 1281 |     update_package_repo | 
| Federico Ressi | ecc1f41 | 2015-12-28 15:14:13 +0100 | [diff] [blame] | 1282 |     if ! real_install_package "$@"; then | 
 | 1283 |         RETRY_UPDATE=True update_package_repo && real_install_package "$@" | 
 | 1284 |     fi | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 1285 | } | 
 | 1286 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1287 | # Distro-agnostic function to tell if a package is installed | 
 | 1288 | # is_package_installed package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1289 | function is_package_installed { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1290 |     if [[ -z "$@" ]]; then | 
 | 1291 |         return 1 | 
 | 1292 |     fi | 
 | 1293 |  | 
 | 1294 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 1295 |         GetOSVersion | 
 | 1296 |     fi | 
 | 1297 |  | 
 | 1298 |     if [[ "$os_PACKAGE" = "deb" ]]; then | 
 | 1299 |         dpkg -s "$@" > /dev/null 2> /dev/null | 
 | 1300 |     elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
 | 1301 |         rpm --quiet -q "$@" | 
 | 1302 |     else | 
 | 1303 |         exit_distro_not_supported "finding if a package is installed" | 
 | 1304 |     fi | 
 | 1305 | } | 
 | 1306 |  | 
 | 1307 | # Distro-agnostic package uninstaller | 
 | 1308 | # uninstall_package package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1309 | function uninstall_package { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1310 |     if is_ubuntu; then | 
 | 1311 |         apt_get purge "$@" | 
 | 1312 |     elif is_fedora; then | 
| Ian Wienand | 36298ee | 2015-02-04 10:29:31 +1100 | [diff] [blame] | 1313 |         sudo ${YUM:-yum} remove -y "$@" ||: | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1314 |     elif is_suse; then | 
| Gary W. Smith | 56b3912 | 2016-11-16 22:03:43 -0800 | [diff] [blame] | 1315 |         sudo zypper remove -y "$@" ||: | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1316 |     else | 
 | 1317 |         exit_distro_not_supported "uninstalling packages" | 
 | 1318 |     fi | 
 | 1319 | } | 
 | 1320 |  | 
 | 1321 | # Wrapper for ``yum`` to set proxy environment variables | 
| Daniel P. Berrange | 63d25d9 | 2014-12-09 15:21:22 +0000 | [diff] [blame] | 1322 | # Uses globals ``OFFLINE``, ``*_proxy``, ``YUM`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1323 | # yum_install package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1324 | function yum_install { | 
| Federico Ressi | 5dcbf7a | 2016-01-13 12:17:32 +0000 | [diff] [blame] | 1325 |     local result parse_yum_result | 
 | 1326 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1327 |     [[ "$OFFLINE" = "True" ]] && return | 
| Federico Ressi | 5dcbf7a | 2016-01-13 12:17:32 +0000 | [diff] [blame] | 1328 |  | 
 | 1329 |     time_start "yum_install" | 
 | 1330 |  | 
| Ian Wienand | a470540 | 2016-09-23 15:08:48 +1000 | [diff] [blame] | 1331 |     # This is a bit tricky, because yum -y assumes missing or failed | 
 | 1332 |     # packages are OK (see [1]).  We want devstack to stop if we are | 
 | 1333 |     # installing missing packages. | 
 | 1334 |     # | 
 | 1335 |     # Thus we manually match on the output (stack.sh runs in a fixed | 
 | 1336 |     # locale, so lang shouldn't change). | 
 | 1337 |     # | 
 | 1338 |     # If yum returns !0, we echo the result as "YUM_FAILED" and return | 
 | 1339 |     # that from the awk (we're subverting -e with this trick). | 
 | 1340 |     # Otherwise we use awk to look for failure strings and return "2" | 
 | 1341 |     # to indicate a terminal failure. | 
 | 1342 |     # | 
 | 1343 |     # [1] https://bugzilla.redhat.com/show_bug.cgi?id=965567 | 
 | 1344 |     parse_yum_result='              \ | 
 | 1345 |         BEGIN { result=0 }          \ | 
 | 1346 |         /^YUM_FAILED/ { result=$2 } \ | 
 | 1347 |         /^No package/ { result=2 }  \ | 
 | 1348 |         /^Failed:/    { result=2 }  \ | 
 | 1349 |         //{ print }                 \ | 
| Federico Ressi | 5dcbf7a | 2016-01-13 12:17:32 +0000 | [diff] [blame] | 1350 |         END { exit result }' | 
| Federico Ressi | 5dcbf7a | 2016-01-13 12:17:32 +0000 | [diff] [blame] | 1351 |     (sudo_with_proxies "${YUM:-yum}" install -y "$@" 2>&1 || echo YUM_FAILED $?) \ | 
| Ian Wienand | 1b1cc8c | 2016-03-01 13:14:39 +1100 | [diff] [blame] | 1352 |         | awk "$parse_yum_result" && result=$? || result=$? | 
| Federico Ressi | 5dcbf7a | 2016-01-13 12:17:32 +0000 | [diff] [blame] | 1353 |  | 
 | 1354 |     time_stop "yum_install" | 
 | 1355 |  | 
| Ian Wienand | 1b1cc8c | 2016-03-01 13:14:39 +1100 | [diff] [blame] | 1356 |     # if we return 1, then the wrapper functions will run an update | 
 | 1357 |     # and try installing the package again as a defense against bad | 
 | 1358 |     # mirrors.  This can hide failures, especially when we have | 
 | 1359 |     # packages that are in the "Failed:" section because their rpm | 
 | 1360 |     # install scripts failed to run correctly (in this case, the | 
 | 1361 |     # package looks installed, so when the retry happens we just think | 
 | 1362 |     # the package is OK, and incorrectly continue on). | 
 | 1363 |     if [ "$result" == 2 ]; then | 
 | 1364 |         die "Detected fatal package install failure" | 
 | 1365 |     fi | 
 | 1366 |  | 
| Federico Ressi | 5dcbf7a | 2016-01-13 12:17:32 +0000 | [diff] [blame] | 1367 |     return "$result" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1368 | } | 
 | 1369 |  | 
 | 1370 | # zypper wrapper to set arguments correctly | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1371 | # Uses globals ``OFFLINE``, ``*_proxy`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1372 | # zypper_install package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1373 | function zypper_install { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1374 |     [[ "$OFFLINE" = "True" ]] && return | 
 | 1375 |     local sudo="sudo" | 
 | 1376 |     [[ "$(id -u)" = "0" ]] && sudo="env" | 
| Ian Wienand | fdf00f2 | 2015-03-13 11:50:02 +1100 | [diff] [blame] | 1377 |     $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \ | 
 | 1378 |         no_proxy="${no_proxy:-}" \ | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1379 |         zypper --non-interactive install --auto-agree-with-licenses "$@" | 
 | 1380 | } | 
 | 1381 |  | 
| Sean Dague | 5edae54 | 2017-03-21 20:50:24 -0400 | [diff] [blame] | 1382 | function write_user_unit_file { | 
 | 1383 |     local service=$1 | 
 | 1384 |     local command="$2" | 
 | 1385 |     local group=$3 | 
 | 1386 |     local user=$4 | 
 | 1387 |     local extra="" | 
 | 1388 |     if [[ -n "$group" ]]; then | 
 | 1389 |         extra="Group=$group" | 
 | 1390 |     fi | 
 | 1391 |     local unitfile="$SYSTEMD_DIR/$service" | 
 | 1392 |     mkdir -p $SYSTEMD_DIR | 
 | 1393 |  | 
 | 1394 |     iniset -sudo $unitfile "Unit" "Description" "Devstack $service" | 
 | 1395 |     iniset -sudo $unitfile "Service" "User" "$user" | 
 | 1396 |     iniset -sudo $unitfile "Service" "ExecStart" "$command" | 
| Matthew Treinish | 477a962 | 2017-08-04 11:09:26 -0400 | [diff] [blame] | 1397 |     iniset -sudo $unitfile "Service" "KillMode" "process" | 
| Michał Dulko | 3b815a3 | 2017-11-14 16:04:51 +0100 | [diff] [blame] | 1398 |     iniset -sudo $unitfile "Service" "TimeoutStopSec" "300" | 
| Dinesh Bhor | ef60f2b | 2017-09-05 14:40:32 +0530 | [diff] [blame] | 1399 |     iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID" | 
| Sean Dague | 5edae54 | 2017-03-21 20:50:24 -0400 | [diff] [blame] | 1400 |     if [[ -n "$group" ]]; then | 
 | 1401 |         iniset -sudo $unitfile "Service" "Group" "$group" | 
 | 1402 |     fi | 
 | 1403 |     iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target" | 
 | 1404 |  | 
 | 1405 |     # changes to existing units sometimes need a refresh | 
 | 1406 |     $SYSTEMCTL daemon-reload | 
 | 1407 | } | 
 | 1408 |  | 
| Matthew Treinish | 9c5ffd8 | 2017-03-29 16:47:57 -0400 | [diff] [blame] | 1409 | function write_uwsgi_user_unit_file { | 
 | 1410 |     local service=$1 | 
 | 1411 |     local command="$2" | 
 | 1412 |     local group=$3 | 
 | 1413 |     local user=$4 | 
 | 1414 |     local unitfile="$SYSTEMD_DIR/$service" | 
 | 1415 |     mkdir -p $SYSTEMD_DIR | 
 | 1416 |  | 
 | 1417 |     iniset -sudo $unitfile "Unit" "Description" "Devstack $service" | 
| Sean Dague | f41bf2a | 2017-05-05 07:50:52 -0400 | [diff] [blame] | 1418 |     iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service" | 
| Matthew Treinish | 9c5ffd8 | 2017-03-29 16:47:57 -0400 | [diff] [blame] | 1419 |     iniset -sudo $unitfile "Service" "User" "$user" | 
 | 1420 |     iniset -sudo $unitfile "Service" "ExecStart" "$command" | 
| Dinesh Bhor | ef60f2b | 2017-09-05 14:40:32 +0530 | [diff] [blame] | 1421 |     iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID" | 
| Matthew Treinish | 9c5ffd8 | 2017-03-29 16:47:57 -0400 | [diff] [blame] | 1422 |     iniset -sudo $unitfile "Service" "Type" "notify" | 
| Matthew Treinish | 477a962 | 2017-08-04 11:09:26 -0400 | [diff] [blame] | 1423 |     iniset -sudo $unitfile "Service" "KillMode" "process" | 
| gong yong sheng | 07b3bc2 | 2017-06-05 14:02:28 +0800 | [diff] [blame] | 1424 |     iniset -sudo $unitfile "Service" "Restart" "always" | 
| Matthew Treinish | 9c5ffd8 | 2017-03-29 16:47:57 -0400 | [diff] [blame] | 1425 |     iniset -sudo $unitfile "Service" "NotifyAccess" "all" | 
 | 1426 |     iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100" | 
 | 1427 |  | 
 | 1428 |     if [[ -n "$group" ]]; then | 
 | 1429 |         iniset -sudo $unitfile "Service" "Group" "$group" | 
 | 1430 |     fi | 
 | 1431 |     iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target" | 
 | 1432 |  | 
 | 1433 |     # changes to existing units sometimes need a refresh | 
 | 1434 |     $SYSTEMCTL daemon-reload | 
 | 1435 | } | 
 | 1436 |  | 
| Sean Dague | 148d58c | 2017-05-04 13:08:25 -0400 | [diff] [blame] | 1437 | function _common_systemd_pitfalls { | 
 | 1438 |     local cmd=$1 | 
 | 1439 |     # do some sanity checks on $cmd to see things we don't expect to work | 
 | 1440 |  | 
 | 1441 |     if [[ "$cmd" =~ "sudo" ]]; then | 
 | 1442 |         local msg=<<EOF | 
 | 1443 | You are trying to use run_process with sudo, this is not going to work under systemd. | 
 | 1444 |  | 
 | 1445 | If you need to run a service as a user other than $STACK_USER call it with: | 
 | 1446 |  | 
 | 1447 |    run_process \$name \$cmd \$group \$user | 
 | 1448 | EOF | 
 | 1449 |         die $LINENO $msg | 
 | 1450 |     fi | 
 | 1451 |  | 
 | 1452 |     if [[ ! "$cmd" =~ ^/ ]]; then | 
 | 1453 |         local msg=<<EOF | 
 | 1454 | The cmd="$cmd" does not start with an absolute path. It will fail to | 
 | 1455 | start under systemd. | 
 | 1456 |  | 
 | 1457 | Please update your run_process stanza to have an absolute path. | 
 | 1458 | EOF | 
 | 1459 |         die $LINENO $msg | 
 | 1460 |     fi | 
 | 1461 |  | 
 | 1462 | } | 
 | 1463 |  | 
| YAMAMOTO Takashi | c087c71 | 2017-06-15 12:10:45 +0000 | [diff] [blame] | 1464 | # Helper function to build a basic unit file and run it under systemd. | 
 | 1465 | function _run_under_systemd { | 
| Sean Dague | 5edae54 | 2017-03-21 20:50:24 -0400 | [diff] [blame] | 1466 |     local service=$1 | 
 | 1467 |     local command="$2" | 
 | 1468 |     local cmd=$command | 
| Sean Dague | 148d58c | 2017-05-04 13:08:25 -0400 | [diff] [blame] | 1469 |     # sanity check the command | 
 | 1470 |     _common_systemd_pitfalls "$cmd" | 
 | 1471 |  | 
| Sean Dague | 5edae54 | 2017-03-21 20:50:24 -0400 | [diff] [blame] | 1472 |     local systemd_service="devstack@$service.service" | 
 | 1473 |     local group=$3 | 
 | 1474 |     local user=${4:-$STACK_USER} | 
| Matthew Treinish | 9c5ffd8 | 2017-03-29 16:47:57 -0400 | [diff] [blame] | 1475 |     if [[ "$command" =~ "uwsgi" ]] ; then | 
 | 1476 |         write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user" | 
 | 1477 |     else | 
 | 1478 |         write_user_unit_file $systemd_service "$cmd" "$group" "$user" | 
 | 1479 |     fi | 
| YAMAMOTO Takashi | c087c71 | 2017-06-15 12:10:45 +0000 | [diff] [blame] | 1480 |  | 
 | 1481 |     $SYSTEMCTL enable $systemd_service | 
 | 1482 |     $SYSTEMCTL start $systemd_service | 
| Sean Dague | 5edae54 | 2017-03-21 20:50:24 -0400 | [diff] [blame] | 1483 | } | 
 | 1484 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1485 | # Find out if a process exists by partial name. | 
 | 1486 | # is_running name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1487 | function is_running { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1488 |     local name=$1 | 
 | 1489 |     ps auxw | grep -v grep | grep ${name} > /dev/null | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1490 |     local exitcode=$? | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1491 |     # some times I really hate bash reverse binary logic | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1492 |     return $exitcode | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1493 | } | 
 | 1494 |  | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1495 | # Run a single service under screen or directly | 
 | 1496 | # 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] | 1497 | # If an optional group is provided sg will be used to run the | 
 | 1498 | # command as that group. | 
| Sean Dague | 148d58c | 2017-05-04 13:08:25 -0400 | [diff] [blame] | 1499 | # run_process service "command-line" [group] [user] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1500 | function run_process { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1501 |     local service=$1 | 
 | 1502 |     local command="$2" | 
| Chris Dent | 2f27a0e | 2014-09-09 13:46:02 +0100 | [diff] [blame] | 1503 |     local group=$3 | 
| Sean Dague | 5edae54 | 2017-03-21 20:50:24 -0400 | [diff] [blame] | 1504 |     local user=$4 | 
| Brant Knudson | edc11c2 | 2015-12-14 15:32:05 -0600 | [diff] [blame] | 1505 |  | 
| Sean Dague | 5edae54 | 2017-03-21 20:50:24 -0400 | [diff] [blame] | 1506 |     local name=$service | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1507 |  | 
| Atsushi SAKAI | 2ca8af4 | 2015-12-08 15:36:13 +0900 | [diff] [blame] | 1508 |     time_start "run_process" | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1509 |     if is_service_enabled $service; then | 
| Sean Dague | cdba1b3 | 2017-08-30 11:11:06 -0400 | [diff] [blame] | 1510 |         _run_under_systemd "$name" "$command" "$group" "$user" | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1511 |     fi | 
| Atsushi SAKAI | 2ca8af4 | 2015-12-08 15:36:13 +0900 | [diff] [blame] | 1512 |     time_stop "run_process" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1513 | } | 
 | 1514 |  | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1515 | # Stop a service process | 
 | 1516 | # If a PID is available use it, kill the whole process group via TERM | 
 | 1517 | # If screen is being used kill the screen window; this will catch processes | 
 | 1518 | # that did not leave a PID behind | 
| Radoslav Gerganov | 2b97a81 | 2017-10-10 16:51:12 +0300 | [diff] [blame] | 1519 | # Uses globals ``SERVICE_DIR`` | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1520 | # stop_process service | 
 | 1521 | function stop_process { | 
 | 1522 |     local service=$1 | 
 | 1523 |  | 
 | 1524 |     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1525 |  | 
 | 1526 |     if is_service_enabled $service; then | 
| Sean Dague | 803acff | 2017-05-01 10:52:38 -0400 | [diff] [blame] | 1527 |         # Only do this for units which appear enabled, this also | 
 | 1528 |         # catches units that don't really exist for cases like | 
 | 1529 |         # keystone without a failure. | 
 | 1530 |         if $SYSTEMCTL is-enabled devstack@$service.service; then | 
 | 1531 |             $SYSTEMCTL stop devstack@$service.service | 
 | 1532 |             $SYSTEMCTL disable devstack@$service.service | 
| Sean Dague | 5edae54 | 2017-03-21 20:50:24 -0400 | [diff] [blame] | 1533 |         fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1534 |     fi | 
 | 1535 | } | 
 | 1536 |  | 
| Sean Dague | cdba1b3 | 2017-08-30 11:11:06 -0400 | [diff] [blame] | 1537 | # use systemctl to check service status | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1538 | function service_check { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1539 |     local service | 
| Sean Dague | cdba1b3 | 2017-08-30 11:11:06 -0400 | [diff] [blame] | 1540 |     for service in ${ENABLED_SERVICES//,/ }; do | 
 | 1541 |         # because some things got renamed like key => keystone | 
 | 1542 |         if $SYSTEMCTL is-enabled devstack@$service.service; then | 
 | 1543 |             # no-pager is needed because otherwise status dumps to a | 
 | 1544 |             # pager when in interactive mode, which will stop a manual | 
 | 1545 |             # devstack run. | 
 | 1546 |             $SYSTEMCTL status devstack@$service.service --no-pager | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1547 |         fi | 
| Sean Dague | cdba1b3 | 2017-08-30 11:11:06 -0400 | [diff] [blame] | 1548 |     done | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1549 | } | 
 | 1550 |  | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1551 |  | 
| Sean Dague | cdba1b3 | 2017-08-30 11:11:06 -0400 | [diff] [blame] | 1552 | function tail_log { | 
 | 1553 |     deprecated "With the removal of screen support, tail_log is deprecated and will be removed after Queens" | 
 | 1554 | } | 
| Dean Troyer | 3159a82 | 2014-08-27 14:13:58 -0500 | [diff] [blame] | 1555 |  | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1556 | # Plugin Functions | 
 | 1557 | # ================= | 
 | 1558 |  | 
 | 1559 | DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""} | 
 | 1560 |  | 
 | 1561 | # enable_plugin <name> <url> [branch] | 
 | 1562 | # | 
 | 1563 | # ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar) | 
 | 1564 | # ``url`` is a git url | 
 | 1565 | # ``branch`` is a gitref. If it's not set, defaults to master | 
 | 1566 | function enable_plugin { | 
 | 1567 |     local name=$1 | 
 | 1568 |     local url=$2 | 
 | 1569 |     local branch=${3:-master} | 
| Omer Anson | 5158486 | 2017-08-24 17:47:37 +0300 | [diff] [blame] | 1570 |     if is_plugin_enabled $name; then | 
| John L. Villalovos | 21d84c2 | 2016-11-11 15:26:11 -0800 | [diff] [blame] | 1571 |         die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}" | 
 | 1572 |     fi | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1573 |     DEVSTACK_PLUGINS+=",$name" | 
 | 1574 |     GITREPO[$name]=$url | 
 | 1575 |     GITDIR[$name]=$DEST/$name | 
 | 1576 |     GITBRANCH[$name]=$branch | 
 | 1577 | } | 
 | 1578 |  | 
| Omer Anson | 5158486 | 2017-08-24 17:47:37 +0300 | [diff] [blame] | 1579 | # is_plugin_enabled <name> | 
 | 1580 | # | 
 | 1581 | # Check if the plugin was enabled, e.g. using enable_plugin | 
 | 1582 | # | 
 | 1583 | # ``name`` The name with which the plugin was enabled | 
 | 1584 | function is_plugin_enabled { | 
 | 1585 |     local name=$1 | 
 | 1586 |     if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then | 
 | 1587 |         return 0 | 
 | 1588 |     fi | 
 | 1589 |     return 1 | 
 | 1590 | } | 
 | 1591 |  | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1592 | # fetch_plugins | 
 | 1593 | # | 
 | 1594 | # clones all plugins | 
 | 1595 | function fetch_plugins { | 
 | 1596 |     local plugins="${DEVSTACK_PLUGINS}" | 
 | 1597 |     local plugin | 
 | 1598 |  | 
 | 1599 |     # short circuit if nothing to do | 
 | 1600 |     if [[ -z $plugins ]]; then | 
 | 1601 |         return | 
 | 1602 |     fi | 
 | 1603 |  | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 1604 |     echo "Fetching DevStack plugins" | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1605 |     for plugin in ${plugins//,/ }; do | 
 | 1606 |         git_clone_by_name $plugin | 
 | 1607 |     done | 
 | 1608 | } | 
 | 1609 |  | 
 | 1610 | # load_plugin_settings | 
 | 1611 | # | 
 | 1612 | # Load settings from plugins in the order that they were registered | 
 | 1613 | function load_plugin_settings { | 
 | 1614 |     local plugins="${DEVSTACK_PLUGINS}" | 
 | 1615 |     local plugin | 
 | 1616 |  | 
 | 1617 |     # short circuit if nothing to do | 
 | 1618 |     if [[ -z $plugins ]]; then | 
 | 1619 |         return | 
 | 1620 |     fi | 
 | 1621 |  | 
 | 1622 |     echo "Loading plugin settings" | 
 | 1623 |     for plugin in ${plugins//,/ }; do | 
 | 1624 |         local dir=${GITDIR[$plugin]} | 
 | 1625 |         # source any known settings | 
 | 1626 |         if [[ -f $dir/devstack/settings ]]; then | 
 | 1627 |             source $dir/devstack/settings | 
 | 1628 |         fi | 
 | 1629 |     done | 
 | 1630 | } | 
 | 1631 |  | 
| Sean Dague | 6e275e1 | 2015-03-26 05:54:28 -0400 | [diff] [blame] | 1632 | # plugin_override_defaults | 
 | 1633 | # | 
 | 1634 | # Run an extremely early setting phase for plugins that allows default | 
 | 1635 | # overriding of services. | 
 | 1636 | function plugin_override_defaults { | 
 | 1637 |     local plugins="${DEVSTACK_PLUGINS}" | 
 | 1638 |     local plugin | 
 | 1639 |  | 
 | 1640 |     # short circuit if nothing to do | 
 | 1641 |     if [[ -z $plugins ]]; then | 
 | 1642 |         return | 
 | 1643 |     fi | 
 | 1644 |  | 
 | 1645 |     echo "Overriding Configuration Defaults" | 
 | 1646 |     for plugin in ${plugins//,/ }; do | 
 | 1647 |         local dir=${GITDIR[$plugin]} | 
 | 1648 |         # source any overrides | 
 | 1649 |         if [[ -f $dir/devstack/override-defaults ]]; then | 
 | 1650 |             # be really verbose that an override is happening, as it | 
 | 1651 |             # may not be obvious if things fail later. | 
| Atsushi SAKAI | 33c9a67 | 2015-11-12 19:50:00 +0900 | [diff] [blame] | 1652 |             echo "$plugin has overridden the following defaults" | 
| Sean Dague | 6e275e1 | 2015-03-26 05:54:28 -0400 | [diff] [blame] | 1653 |             cat $dir/devstack/override-defaults | 
 | 1654 |             source $dir/devstack/override-defaults | 
 | 1655 |         fi | 
 | 1656 |     done | 
 | 1657 | } | 
 | 1658 |  | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1659 | # run_plugins | 
 | 1660 | # | 
 | 1661 | # Run the devstack/plugin.sh in all the plugin directories. These are | 
 | 1662 | # run in registration order. | 
 | 1663 | function run_plugins { | 
 | 1664 |     local mode=$1 | 
 | 1665 |     local phase=$2 | 
| Bharat Kumar Kobagana | 441ff07 | 2015-01-08 12:26:26 +0530 | [diff] [blame] | 1666 |  | 
 | 1667 |     local plugins="${DEVSTACK_PLUGINS}" | 
 | 1668 |     local plugin | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1669 |     for plugin in ${plugins//,/ }; do | 
 | 1670 |         local dir=${GITDIR[$plugin]} | 
 | 1671 |         if [[ -f $dir/devstack/plugin.sh ]]; then | 
 | 1672 |             source $dir/devstack/plugin.sh $mode $phase | 
 | 1673 |         fi | 
 | 1674 |     done | 
 | 1675 | } | 
 | 1676 |  | 
 | 1677 | function run_phase { | 
 | 1678 |     local mode=$1 | 
 | 1679 |     local phase=$2 | 
 | 1680 |     if [[ -d $TOP_DIR/extras.d ]]; then | 
| Dmitry Tantsur | 64be321 | 2015-10-12 13:10:24 +0200 | [diff] [blame] | 1681 |         local extra_plugin_file_name | 
 | 1682 |         for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do | 
| Sean Dague | 41d0110 | 2015-12-03 08:12:23 -0500 | [diff] [blame] | 1683 |             # NOTE(sdague): only process extras.d for the 3 explicitly | 
 | 1684 |             # white listed elements in tree. We want these to move out | 
 | 1685 |             # over time as well, but they are in tree, so we need to | 
 | 1686 |             # manage that. | 
| Matt Riedemann | c9f6327 | 2016-08-30 17:21:30 -0400 | [diff] [blame] | 1687 |             local exceptions="80-tempest.sh" | 
| Ian Wienand | 5cdee8d | 2015-10-19 14:17:18 +1100 | [diff] [blame] | 1688 |             local extra | 
 | 1689 |             extra=$(basename $extra_plugin_file_name) | 
| Sean Dague | 1de9e33 | 2015-10-07 08:46:13 -0400 | [diff] [blame] | 1690 |             if [[ ! ( $exceptions =~ "$extra" ) ]]; then | 
| Sean Dague | 41d0110 | 2015-12-03 08:12:23 -0500 | [diff] [blame] | 1691 |                 warn "use of extras.d is no longer supported" | 
 | 1692 |                 warn "processing of project $extra is skipped" | 
 | 1693 |             else | 
 | 1694 |                 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase | 
| Sean Dague | 1de9e33 | 2015-10-07 08:46:13 -0400 | [diff] [blame] | 1695 |             fi | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1696 |         done | 
 | 1697 |     fi | 
 | 1698 |     # the source phase corresponds to settings loading in plugins | 
 | 1699 |     if [[ "$mode" == "source" ]]; then | 
 | 1700 |         load_plugin_settings | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1701 |         verify_disabled_services | 
| Sean Dague | 6e275e1 | 2015-03-26 05:54:28 -0400 | [diff] [blame] | 1702 |     elif [[ "$mode" == "override_defaults" ]]; then | 
 | 1703 |         plugin_override_defaults | 
| Sean Dague | 2c65e71 | 2014-12-18 09:44:56 -0500 | [diff] [blame] | 1704 |     else | 
 | 1705 |         run_plugins $mode $phase | 
 | 1706 |     fi | 
 | 1707 | } | 
 | 1708 |  | 
| James E. Blair | c5853ac | 2017-11-21 09:44:42 -0800 | [diff] [blame] | 1709 | # define_plugin <name> | 
 | 1710 | # | 
 | 1711 | # This function is a no-op.  It allows a plugin to define its name So | 
 | 1712 | # that other plugins may reference it by name.  It should generally be | 
 | 1713 | # the last component of the canonical git repo name.  E.g., | 
 | 1714 | # openstack/devstack-foo should use "devstack-foo" as the name here. | 
 | 1715 | # | 
 | 1716 | # This function is currently a noop, but the value may still be used | 
 | 1717 | # by external tools (as in plugin_requires) and may be used by | 
 | 1718 | # devstack in the future. | 
 | 1719 | # | 
 | 1720 | # ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar) | 
 | 1721 | function define_plugin { | 
 | 1722 |     : | 
 | 1723 | } | 
 | 1724 |  | 
 | 1725 | # plugin_requires <name> <other> | 
 | 1726 | # | 
 | 1727 | # This function is a no-op.  It is currently used by external tools | 
 | 1728 | # (such as the devstack module for Ansible) to automatically generate | 
 | 1729 | # local.conf files.  It is not currently used by devstack itself to | 
 | 1730 | # resolve dependencies. | 
 | 1731 | # | 
 | 1732 | # ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar) | 
 | 1733 | # ``other`` is the name of another plugin | 
 | 1734 | function plugin_requires { | 
 | 1735 |     : | 
 | 1736 | } | 
 | 1737 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1738 |  | 
 | 1739 | # Service Functions | 
 | 1740 | # ================= | 
 | 1741 |  | 
 | 1742 | # remove extra commas from the input string (i.e. ``ENABLED_SERVICES``) | 
 | 1743 | # _cleanup_service_list service-list | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1744 | function _cleanup_service_list { | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1745 |     local xtrace | 
 | 1746 |     xtrace=$(set +o | grep xtrace) | 
 | 1747 |     set +o xtrace | 
 | 1748 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1749 |     echo "$1" | sed -e ' | 
 | 1750 |         s/,,/,/g; | 
 | 1751 |         s/^,//; | 
 | 1752 |         s/,$// | 
 | 1753 |     ' | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1754 |  | 
 | 1755 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1756 | } | 
 | 1757 |  | 
 | 1758 | # disable_all_services() removes all current services | 
 | 1759 | # from ``ENABLED_SERVICES`` to reset the configuration | 
 | 1760 | # before a minimal installation | 
 | 1761 | # Uses global ``ENABLED_SERVICES`` | 
 | 1762 | # disable_all_services | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1763 | function disable_all_services { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1764 |     ENABLED_SERVICES="" | 
 | 1765 | } | 
 | 1766 |  | 
 | 1767 | # Remove all services starting with '-'.  For example, to install all default | 
 | 1768 | # services except rabbit (rabbit) set in ``localrc``: | 
 | 1769 | # ENABLED_SERVICES+=",-rabbit" | 
 | 1770 | # Uses global ``ENABLED_SERVICES`` | 
 | 1771 | # disable_negated_services | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1772 | function disable_negated_services { | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1773 |     local xtrace | 
 | 1774 |     xtrace=$(set +o | grep xtrace) | 
 | 1775 |     set +o xtrace | 
 | 1776 |  | 
| Ian Wienand | 2796a82 | 2015-04-15 08:59:04 +1000 | [diff] [blame] | 1777 |     local to_remove="" | 
 | 1778 |     local remaining="" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1779 |     local service | 
| Ian Wienand | 2796a82 | 2015-04-15 08:59:04 +1000 | [diff] [blame] | 1780 |  | 
 | 1781 |     # build up list of services that should be removed; i.e. they | 
 | 1782 |     # begin with "-" | 
 | 1783 |     for service in ${ENABLED_SERVICES//,/ }; do | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1784 |         if [[ ${service} == -* ]]; then | 
| Ian Wienand | 2796a82 | 2015-04-15 08:59:04 +1000 | [diff] [blame] | 1785 |             to_remove+=",${service#-}" | 
 | 1786 |         else | 
 | 1787 |             remaining+=",${service}" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1788 |         fi | 
 | 1789 |     done | 
| Ian Wienand | 2796a82 | 2015-04-15 08:59:04 +1000 | [diff] [blame] | 1790 |  | 
 | 1791 |     # go through the service list.  if this service appears in the "to | 
 | 1792 |     # be removed" list, drop it | 
| fumihiko kakuma | 8606c98 | 2015-04-13 09:55:06 +0900 | [diff] [blame] | 1793 |     ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove") | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1794 |  | 
 | 1795 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1796 | } | 
 | 1797 |  | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1798 | # disable_service() prepares the services passed as argument to be | 
 | 1799 | # removed from the ``ENABLED_SERVICES`` list, if they are present. | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1800 | # | 
 | 1801 | # For example: | 
 | 1802 | #   disable_service rabbit | 
 | 1803 | # | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1804 | # Uses global ``DISABLED_SERVICES`` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1805 | # disable_service service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1806 | function disable_service { | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1807 |     local xtrace | 
 | 1808 |     xtrace=$(set +o | grep xtrace) | 
 | 1809 |     set +o xtrace | 
 | 1810 |  | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1811 |     local disabled_svcs="${DISABLED_SERVICES}" | 
 | 1812 |     local enabled_svcs=",${ENABLED_SERVICES}," | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1813 |     local service | 
 | 1814 |     for service in $@; do | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1815 |         disabled_svcs+=",$service" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1816 |         if is_service_enabled $service; then | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1817 |             enabled_svcs=${enabled_svcs//,$service,/,} | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1818 |         fi | 
 | 1819 |     done | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1820 |     DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs") | 
 | 1821 |     ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs") | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1822 |  | 
 | 1823 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1824 | } | 
 | 1825 |  | 
 | 1826 | # enable_service() adds the services passed as argument to the | 
 | 1827 | # ``ENABLED_SERVICES`` list, if they are not already present. | 
 | 1828 | # | 
 | 1829 | # For example: | 
| Sean Dague | 37eca48 | 2015-06-16 07:19:22 -0400 | [diff] [blame] | 1830 | #   enable_service q-svc | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1831 | # | 
 | 1832 | # This function does not know about the special cases | 
 | 1833 | # for nova, glance, and neutron built into is_service_enabled(). | 
 | 1834 | # Uses global ``ENABLED_SERVICES`` | 
 | 1835 | # enable_service service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1836 | function enable_service { | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1837 |     local xtrace | 
 | 1838 |     xtrace=$(set +o | grep xtrace) | 
 | 1839 |     set +o xtrace | 
 | 1840 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1841 |     local tmpsvcs="${ENABLED_SERVICES}" | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1842 |     local service | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1843 |     for service in $@; do | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1844 |         if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then | 
 | 1845 |             warn $LINENO "Attempt to enable_service ${service} when it has been disabled" | 
 | 1846 |             continue | 
 | 1847 |         fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1848 |         if ! is_service_enabled $service; then | 
 | 1849 |             tmpsvcs+=",$service" | 
 | 1850 |         fi | 
 | 1851 |     done | 
 | 1852 |     ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
 | 1853 |     disable_negated_services | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1854 |  | 
 | 1855 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1856 | } | 
 | 1857 |  | 
 | 1858 | # is_service_enabled() checks if the service(s) specified as arguments are | 
 | 1859 | # enabled by the user in ``ENABLED_SERVICES``. | 
 | 1860 | # | 
 | 1861 | # Multiple services specified as arguments are ``OR``'ed together; the test | 
 | 1862 | # is a short-circuit boolean, i.e it returns on the first match. | 
 | 1863 | # | 
 | 1864 | # There are special cases for some 'catch-all' services:: | 
 | 1865 | #   **nova** returns true if any service enabled start with **n-** | 
 | 1866 | #   **cinder** returns true if any service enabled start with **c-** | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1867 | #   **glance** returns true if any service enabled start with **g-** | 
 | 1868 | #   **neutron** returns true if any service enabled start with **q-** | 
 | 1869 | #   **swift** returns true if any service enabled start with **s-** | 
 | 1870 | #   **trove** returns true if any service enabled start with **tr-** | 
 | 1871 | #   For backward compatibility if we have **swift** in ENABLED_SERVICES all the | 
 | 1872 | #   **s-** services will be enabled. This will be deprecated in the future. | 
 | 1873 | # | 
 | 1874 | # Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``. | 
 | 1875 | # We also need to make sure to treat **n-cell-region** and **n-cell-child** | 
 | 1876 | # as enabled in this case. | 
 | 1877 | # | 
 | 1878 | # Uses global ``ENABLED_SERVICES`` | 
 | 1879 | # is_service_enabled service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1880 | function is_service_enabled { | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 1881 |     local xtrace | 
 | 1882 |     xtrace=$(set +o | grep xtrace) | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1883 |     set +o xtrace | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1884 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1885 |     local enabled=1 | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1886 |     local services=$@ | 
 | 1887 |     local service | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1888 |     for service in ${services}; do | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1889 |         [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1890 |  | 
 | 1891 |         # Look for top-level 'enabled' function for this service | 
 | 1892 |         if type is_${service}_enabled >/dev/null 2>&1; then | 
 | 1893 |             # A function exists for this service, use it | 
| Christian Schwede | 3db0aad | 2015-09-10 11:15:39 +0000 | [diff] [blame] | 1894 |             is_${service}_enabled && enabled=0 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1895 |         fi | 
 | 1896 |  | 
 | 1897 |         # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled() | 
 | 1898 |         #                are implemented | 
 | 1899 |  | 
| John Kasperski | 7b3ae53 | 2016-04-24 22:56:04 -0500 | [diff] [blame] | 1900 |         [[ ${service} == n-cell-* && ,${ENABLED_SERVICES} =~ ,"n-cell" ]] && enabled=0 | 
 | 1901 |         [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0 | 
 | 1902 |         [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0 | 
 | 1903 |         [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0 | 
 | 1904 |         [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0 | 
 | 1905 |         [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0 | 
 | 1906 |         [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0 | 
 | 1907 |         [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1908 |     done | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1909 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1910 |     $xtrace | 
 | 1911 |     return $enabled | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1912 | } | 
 | 1913 |  | 
| fumihiko kakuma | 8606c98 | 2015-04-13 09:55:06 +0900 | [diff] [blame] | 1914 | # remove specified list from the input string | 
 | 1915 | # remove_disabled_services service-list remove-list | 
 | 1916 | function remove_disabled_services { | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1917 |     local xtrace | 
 | 1918 |     xtrace=$(set +o | grep xtrace) | 
 | 1919 |     set +o xtrace | 
 | 1920 |  | 
| fumihiko kakuma | 8606c98 | 2015-04-13 09:55:06 +0900 | [diff] [blame] | 1921 |     local service_list=$1 | 
 | 1922 |     local remove_list=$2 | 
 | 1923 |     local service | 
 | 1924 |     local enabled="" | 
 | 1925 |  | 
 | 1926 |     for service in ${service_list//,/ }; do | 
 | 1927 |         local remove | 
 | 1928 |         local add=1 | 
 | 1929 |         for remove in ${remove_list//,/ }; do | 
 | 1930 |             if [[ ${remove} == ${service} ]]; then | 
 | 1931 |                 add=0 | 
 | 1932 |                 break | 
 | 1933 |             fi | 
 | 1934 |         done | 
 | 1935 |         if [[ $add == 1 ]]; then | 
 | 1936 |             enabled="${enabled},$service" | 
 | 1937 |         fi | 
 | 1938 |     done | 
| Ian Wienand | 8043bfa | 2015-10-14 14:53:18 +1100 | [diff] [blame] | 1939 |  | 
 | 1940 |     $xtrace | 
 | 1941 |  | 
| fumihiko kakuma | 8606c98 | 2015-04-13 09:55:06 +0900 | [diff] [blame] | 1942 |     _cleanup_service_list "$enabled" | 
 | 1943 | } | 
 | 1944 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1945 | # Toggle enable/disable_service for services that must run exclusive of each other | 
 | 1946 | #  $1 The name of a variable containing a space-separated list of services | 
 | 1947 | #  $2 The name of a variable in which to store the enabled service's name | 
 | 1948 | #  $3 The name of the service to enable | 
 | 1949 | function use_exclusive_service { | 
 | 1950 |     local options=${!1} | 
 | 1951 |     local selection=$3 | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1952 |     local out=$2 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1953 |     [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1 | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1954 |     local opt | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1955 |     for opt in $options;do | 
 | 1956 |         [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt | 
 | 1957 |     done | 
 | 1958 |     eval "$out=$selection" | 
 | 1959 |     return 0 | 
 | 1960 | } | 
 | 1961 |  | 
| Chris Dent | c6d4701 | 2015-10-09 14:57:05 +0000 | [diff] [blame] | 1962 | # Make sure that nothing has manipulated ENABLED_SERVICES in a way | 
 | 1963 | # that conflicts with prior calls to disable_service. | 
 | 1964 | # Uses global ``ENABLED_SERVICES`` | 
 | 1965 | function verify_disabled_services { | 
 | 1966 |     local service | 
 | 1967 |     for service in ${ENABLED_SERVICES//,/ }; do | 
 | 1968 |         if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then | 
 | 1969 |             die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'" | 
 | 1970 |         fi | 
 | 1971 |     done | 
 | 1972 | } | 
 | 1973 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1974 |  | 
| Masayuki Igawa | f6368d3 | 2014-02-20 13:31:26 +0900 | [diff] [blame] | 1975 | # System Functions | 
 | 1976 | # ================ | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1977 |  | 
 | 1978 | # Only run the command if the target file (the last arg) is not on an | 
 | 1979 | # NFS filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1980 | function _safe_permission_operation { | 
| Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 1981 |     local xtrace | 
 | 1982 |     xtrace=$(set +o | grep xtrace) | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1983 |     set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1984 |     local args=( $@ ) | 
 | 1985 |     local last | 
 | 1986 |     local sudo_cmd | 
 | 1987 |     local dir_to_check | 
 | 1988 |  | 
 | 1989 |     let last="${#args[*]} - 1" | 
 | 1990 |  | 
| Dean Troyer | d5dfa4c | 2014-07-25 11:13:11 -0500 | [diff] [blame] | 1991 |     local dir_to_check=${args[$last]} | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1992 |     if [ ! -d "$dir_to_check" ]; then | 
 | 1993 |         dir_to_check=`dirname "$dir_to_check"` | 
 | 1994 |     fi | 
 | 1995 |  | 
 | 1996 |     if is_nfs_directory "$dir_to_check" ; then | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1997 |         $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1998 |         return 0 | 
 | 1999 |     fi | 
 | 2000 |  | 
 | 2001 |     if [[ $TRACK_DEPENDS = True ]]; then | 
 | 2002 |         sudo_cmd="env" | 
 | 2003 |     else | 
 | 2004 |         sudo_cmd="sudo" | 
 | 2005 |     fi | 
 | 2006 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 2007 |     $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2008 |     $sudo_cmd $@ | 
 | 2009 | } | 
 | 2010 |  | 
 | 2011 | # Exit 0 if address is in network or 1 if address is not in network | 
 | 2012 | # ip-range is in CIDR notation: 1.2.3.4/20 | 
 | 2013 | # address_in_net ip-address ip-range | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2014 | function address_in_net { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2015 |     local ip=$1 | 
 | 2016 |     local range=$2 | 
 | 2017 |     local masklen=${range#*/} | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 2018 |     local network | 
 | 2019 |     network=$(maskip ${range%/*} $(cidr2netmask $masklen)) | 
 | 2020 |     local subnet | 
 | 2021 |     subnet=$(maskip $ip $(cidr2netmask $masklen)) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2022 |     [[ $network == $subnet ]] | 
 | 2023 | } | 
 | 2024 |  | 
 | 2025 | # Add a user to a group. | 
 | 2026 | # add_user_to_group user group | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2027 | function add_user_to_group { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2028 |     local user=$1 | 
 | 2029 |     local group=$2 | 
 | 2030 |  | 
| Thomas Bechtold | a858085 | 2015-05-31 00:04:33 +0200 | [diff] [blame] | 2031 |     sudo usermod -a -G "$group" "$user" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2032 | } | 
 | 2033 |  | 
 | 2034 | # Convert CIDR notation to a IPv4 netmask | 
 | 2035 | # cidr2netmask cidr-bits | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2036 | function cidr2netmask { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2037 |     local maskpat="255 255 255 255" | 
 | 2038 |     local maskdgt="254 252 248 240 224 192 128" | 
 | 2039 |     set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3} | 
 | 2040 |     echo ${1-0}.${2-0}.${3-0}.${4-0} | 
 | 2041 | } | 
 | 2042 |  | 
| Ian Cordasco | 69e3c0a | 2016-09-26 12:53:14 -0500 | [diff] [blame] | 2043 | # Check if this is a valid ipv4 address string | 
 | 2044 | function is_ipv4_address { | 
 | 2045 |     local address=$1 | 
| Jens Harbott | 7617ac2 | 2017-09-19 17:43:48 +0000 | [diff] [blame] | 2046 |     local regex='([0-9]{1,3}\.){3}[0-9]{1,3}' | 
| Ian Cordasco | 69e3c0a | 2016-09-26 12:53:14 -0500 | [diff] [blame] | 2047 |     # TODO(clarkb) make this more robust | 
 | 2048 |     if [[ "$address" =~ $regex ]] ; then | 
 | 2049 |         return 0 | 
 | 2050 |     else | 
 | 2051 |         return 1 | 
 | 2052 |     fi | 
 | 2053 | } | 
 | 2054 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2055 | # Gracefully cp only if source file/dir exists | 
 | 2056 | # cp_it source destination | 
 | 2057 | function cp_it { | 
 | 2058 |     if [ -e $1 ] || [ -d $1 ]; then | 
 | 2059 |         cp -pRL $1 $2 | 
 | 2060 |     fi | 
 | 2061 | } | 
 | 2062 |  | 
 | 2063 | # HTTP and HTTPS proxy servers are supported via the usual environment variables [1] | 
 | 2064 | # ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in | 
 | 2065 | # ``localrc`` or on the command line if necessary:: | 
 | 2066 | # | 
 | 2067 | # [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html | 
 | 2068 | # | 
 | 2069 | #     http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh | 
 | 2070 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2071 | function export_proxy_variables { | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 2072 |     if isset http_proxy ; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2073 |         export http_proxy=$http_proxy | 
 | 2074 |     fi | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 2075 |     if isset https_proxy ; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2076 |         export https_proxy=$https_proxy | 
 | 2077 |     fi | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 2078 |     if isset no_proxy ; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2079 |         export no_proxy=$no_proxy | 
 | 2080 |     fi | 
 | 2081 | } | 
 | 2082 |  | 
 | 2083 | # Returns true if the directory is on a filesystem mounted via NFS. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2084 | function is_nfs_directory { | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 2085 |     local mount_type | 
 | 2086 |     mount_type=`stat -f -L -c %T $1` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2087 |     test "$mount_type" == "nfs" | 
 | 2088 | } | 
 | 2089 |  | 
 | 2090 | # Return the network portion of the given IP address using netmask | 
 | 2091 | # netmask is in the traditional dotted-quad format | 
 | 2092 | # maskip ip-address netmask | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2093 | function maskip { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2094 |     local ip=$1 | 
 | 2095 |     local mask=$2 | 
 | 2096 |     local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}" | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 2097 |     local subnet | 
 | 2098 |     subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.})) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2099 |     echo $subnet | 
 | 2100 | } | 
 | 2101 |  | 
| Michael Turek | 7938d83 | 2016-04-12 14:55:21 -0400 | [diff] [blame] | 2102 | function is_provider_network { | 
 | 2103 |     if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then | 
 | 2104 |         return 0 | 
 | 2105 |     fi | 
 | 2106 |     return 1 | 
 | 2107 | } | 
 | 2108 |  | 
 | 2109 |  | 
| Ian Wienand | 6d213df | 2017-08-22 16:05:16 +1000 | [diff] [blame] | 2110 | # Return just the <major>.<minor> for the given python interpreter | 
 | 2111 | function _get_python_version { | 
 | 2112 |     local interp=$1 | 
 | 2113 |     local version | 
| Sean Dague | 4324f4e | 2017-09-14 12:59:25 -0600 | [diff] [blame] | 2114 |     # disable erroring out here, otherwise if python 3 doesn't exist we fail hard. | 
| Javier Pena | 6bd4924 | 2017-09-15 15:55:00 +0200 | [diff] [blame] | 2115 |     if [[ -x $(which $interp 2> /dev/null) ]]; then | 
| Sean Dague | 4324f4e | 2017-09-14 12:59:25 -0600 | [diff] [blame] | 2116 |         version=$($interp -c 'import sys; print("%s.%s" % sys.version_info[0:2])') | 
 | 2117 |     fi | 
| Ian Wienand | 6d213df | 2017-08-22 16:05:16 +1000 | [diff] [blame] | 2118 |     echo ${version} | 
 | 2119 | } | 
 | 2120 |  | 
| Chris Dent | 3a2c86a | 2015-05-12 13:41:25 +0000 | [diff] [blame] | 2121 | # Return the current python as "python<major>.<minor>" | 
 | 2122 | function python_version { | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 2123 |     local python_version | 
| Ian Wienand | 6d213df | 2017-08-22 16:05:16 +1000 | [diff] [blame] | 2124 |     python_version=$(_get_python_version python2) | 
| Chris Dent | 3a2c86a | 2015-05-12 13:41:25 +0000 | [diff] [blame] | 2125 |     echo "python${python_version}" | 
 | 2126 | } | 
 | 2127 |  | 
| Ian Wienand | 6d213df | 2017-08-22 16:05:16 +1000 | [diff] [blame] | 2128 | function python3_version { | 
 | 2129 |     local python3_version | 
 | 2130 |     python3_version=$(_get_python_version python3) | 
| Doug Hellmann | 0417858 | 2018-06-12 15:37:00 -0400 | [diff] [blame^] | 2131 |     echo "python${python3_version}" | 
| Ian Wienand | 6d213df | 2017-08-22 16:05:16 +1000 | [diff] [blame] | 2132 | } | 
 | 2133 |  | 
 | 2134 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2135 | # Service wrapper to restart services | 
 | 2136 | # restart_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2137 | function restart_service { | 
| Kashyap Chamarthy | 247e446 | 2016-05-20 13:34:41 +0200 | [diff] [blame] | 2138 |     if [ -x /bin/systemctl ]; then | 
 | 2139 |         sudo /bin/systemctl restart $1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2140 |     else | 
| Kashyap Chamarthy | 247e446 | 2016-05-20 13:34:41 +0200 | [diff] [blame] | 2141 |         sudo service $1 restart | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2142 |     fi | 
| Kashyap Chamarthy | 247e446 | 2016-05-20 13:34:41 +0200 | [diff] [blame] | 2143 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2144 | } | 
 | 2145 |  | 
 | 2146 | # Only change permissions of a file or directory if it is not on an | 
 | 2147 | # NFS filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2148 | function safe_chmod { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2149 |     _safe_permission_operation chmod $@ | 
 | 2150 | } | 
 | 2151 |  | 
 | 2152 | # Only change ownership of a file or directory if it is not on an NFS | 
 | 2153 | # filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2154 | function safe_chown { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2155 |     _safe_permission_operation chown $@ | 
 | 2156 | } | 
 | 2157 |  | 
 | 2158 | # Service wrapper to start services | 
 | 2159 | # start_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2160 | function start_service { | 
| Kashyap Chamarthy | 247e446 | 2016-05-20 13:34:41 +0200 | [diff] [blame] | 2161 |     if [ -x /bin/systemctl ]; then | 
 | 2162 |         sudo /bin/systemctl start $1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2163 |     else | 
| Kashyap Chamarthy | 247e446 | 2016-05-20 13:34:41 +0200 | [diff] [blame] | 2164 |         sudo service $1 start | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2165 |     fi | 
 | 2166 | } | 
 | 2167 |  | 
 | 2168 | # Service wrapper to stop services | 
 | 2169 | # stop_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 2170 | function stop_service { | 
| Kashyap Chamarthy | 247e446 | 2016-05-20 13:34:41 +0200 | [diff] [blame] | 2171 |     if [ -x /bin/systemctl ]; then | 
 | 2172 |         sudo /bin/systemctl stop $1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2173 |     else | 
| Kashyap Chamarthy | 247e446 | 2016-05-20 13:34:41 +0200 | [diff] [blame] | 2174 |         sudo service $1 stop | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2175 |     fi | 
 | 2176 | } | 
 | 2177 |  | 
| Attila Fazekas | bcaadd6 | 2016-11-23 12:43:02 +0100 | [diff] [blame] | 2178 | # Service wrapper to reload services | 
 | 2179 | # If the service was not in running state it will start it | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 2180 | # reload_service service-name | 
 | 2181 | function reload_service { | 
 | 2182 |     if [ -x /bin/systemctl ]; then | 
| Attila Fazekas | bcaadd6 | 2016-11-23 12:43:02 +0100 | [diff] [blame] | 2183 |         sudo /bin/systemctl reload-or-restart $1 | 
| Gregory Haynes | 4b49e40 | 2016-08-31 18:19:51 -0700 | [diff] [blame] | 2184 |     else | 
 | 2185 |         sudo service $1 reload | 
 | 2186 |     fi | 
 | 2187 | } | 
 | 2188 |  | 
| Sean Dague | 442e4e9 | 2015-06-24 13:24:02 -0400 | [diff] [blame] | 2189 | # Test with a finite retry loop. | 
 | 2190 | # | 
 | 2191 | function test_with_retry { | 
 | 2192 |     local testcmd=$1 | 
 | 2193 |     local failmsg=$2 | 
 | 2194 |     local until=${3:-10} | 
 | 2195 |     local sleep=${4:-0.5} | 
 | 2196 |  | 
| Atsushi SAKAI | 2ca8af4 | 2015-12-08 15:36:13 +0900 | [diff] [blame] | 2197 |     time_start "test_with_retry" | 
| Sean Dague | 442e4e9 | 2015-06-24 13:24:02 -0400 | [diff] [blame] | 2198 |     if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then | 
 | 2199 |         die $LINENO "$failmsg" | 
 | 2200 |     fi | 
| Atsushi SAKAI | 2ca8af4 | 2015-12-08 15:36:13 +0900 | [diff] [blame] | 2201 |     time_stop "test_with_retry" | 
| Sean Dague | 442e4e9 | 2015-06-24 13:24:02 -0400 | [diff] [blame] | 2202 | } | 
 | 2203 |  | 
| Federico Ressi | 5dcbf7a | 2016-01-13 12:17:32 +0000 | [diff] [blame] | 2204 | # Like sudo but forwarding http_proxy https_proxy no_proxy environment vars. | 
 | 2205 | # If it is run as superuser then sudo is replaced by env. | 
 | 2206 | # | 
 | 2207 | function sudo_with_proxies { | 
 | 2208 |     local sudo | 
 | 2209 |  | 
 | 2210 |     [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo" | 
 | 2211 |  | 
 | 2212 |     $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\ | 
 | 2213 |         no_proxy="${no_proxy:-}" "$@" | 
 | 2214 | } | 
 | 2215 |  | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2216 | # Timing infrastructure - figure out where large blocks of time are | 
 | 2217 | # used in DevStack | 
 | 2218 | # | 
 | 2219 | # The timing infrastructure for DevStack is about collecting buckets | 
 | 2220 | # of time that are spend in some subtask. For instance, that might be | 
 | 2221 | # 'apt', 'pip', 'osc', even database migrations. We do this by a pair | 
 | 2222 | # of functions: time_start / time_stop. | 
 | 2223 | # | 
 | 2224 | # These take a single parameter: $name - which specifies the name of | 
 | 2225 | # the bucket to be accounted against. time_totals function spits out | 
 | 2226 | # the results. | 
 | 2227 | # | 
 | 2228 | # Resolution is only in whole seconds, so should be used for long | 
 | 2229 | # running activities. | 
 | 2230 |  | 
| Sean Dague | afef8bf | 2017-03-06 14:07:23 -0500 | [diff] [blame] | 2231 | declare -A -g _TIME_TOTAL | 
 | 2232 | declare -A -g _TIME_START | 
 | 2233 | declare -r -g _TIME_BEGIN=$(date +%s) | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2234 |  | 
 | 2235 | # time_start $name | 
 | 2236 | # | 
 | 2237 | # starts the clock for a timer by name. Errors if that clock is | 
 | 2238 | # already started. | 
 | 2239 | function time_start { | 
 | 2240 |     local name=$1 | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2241 |     local start_time=${_TIME_START[$name]} | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2242 |     if [[ -n "$start_time" ]]; then | 
 | 2243 |         die $LINENO "Trying to start the clock on $name, but it's already been started" | 
 | 2244 |     fi | 
| Sean Dague | 85cf293 | 2017-03-27 15:35:13 -0400 | [diff] [blame] | 2245 |     _TIME_START[$name]=$(date +%s%3N) | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2246 | } | 
 | 2247 |  | 
 | 2248 | # time_stop $name | 
 | 2249 | # | 
 | 2250 | # stops the clock for a timer by name, and accumulate that time in the | 
 | 2251 | # global counter for that name. Errors if that clock had not | 
 | 2252 | # previously been started. | 
 | 2253 | function time_stop { | 
| Ian Wienand | 5cdee8d | 2015-10-19 14:17:18 +1100 | [diff] [blame] | 2254 |     local name | 
 | 2255 |     local end_time | 
| Rob Crittenden | 38245da | 2016-05-26 17:55:14 -0400 | [diff] [blame] | 2256 |     local elapsed_time | 
| Ian Wienand | 5cdee8d | 2015-10-19 14:17:18 +1100 | [diff] [blame] | 2257 |     local total | 
 | 2258 |     local start_time | 
 | 2259 |  | 
 | 2260 |     name=$1 | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2261 |     start_time=${_TIME_START[$name]} | 
| Ian Wienand | 5cdee8d | 2015-10-19 14:17:18 +1100 | [diff] [blame] | 2262 |  | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2263 |     if [[ -z "$start_time" ]]; then | 
 | 2264 |         die $LINENO "Trying to stop the clock on $name, but it was never started" | 
 | 2265 |     fi | 
| Sean Dague | 85cf293 | 2017-03-27 15:35:13 -0400 | [diff] [blame] | 2266 |     end_time=$(date +%s%3N) | 
| Ian Wienand | 5cdee8d | 2015-10-19 14:17:18 +1100 | [diff] [blame] | 2267 |     elapsed_time=$(($end_time - $start_time)) | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2268 |     total=${_TIME_TOTAL[$name]:-0} | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2269 |     # reset the clock so we can start it in the future | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2270 |     _TIME_START[$name]="" | 
 | 2271 |     _TIME_TOTAL[$name]=$(($total + $elapsed_time)) | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2272 | } | 
 | 2273 |  | 
| Sean Dague | 85cf293 | 2017-03-27 15:35:13 -0400 | [diff] [blame] | 2274 | function oscwrap { | 
 | 2275 |     local out | 
 | 2276 |     local rc | 
 | 2277 |     local start | 
 | 2278 |     local end | 
 | 2279 |     # Cannot use timer_start and timer_stop as we run in subshells | 
 | 2280 |     # and those rely on modifying vars in the same process (which cannot | 
 | 2281 |     # happen from a subshell. | 
 | 2282 |     start=$(date +%s%3N) | 
 | 2283 |     out=$(command openstack "$@") | 
 | 2284 |     rc=$? | 
 | 2285 |     end=$(date +%s%3N) | 
 | 2286 |     echo $((end - start)) >> $OSCWRAP_TIMER_FILE | 
 | 2287 |  | 
 | 2288 |     echo "$out" | 
 | 2289 |     return $rc | 
 | 2290 | } | 
 | 2291 |  | 
 | 2292 | function install_oscwrap { | 
 | 2293 |     # File to accumulate our timing data | 
 | 2294 |     OSCWRAP_TIMER_FILE=$(mktemp) | 
 | 2295 |     # Bash by default doesn't expand aliases, allow it for the aliases | 
 | 2296 |     # we want to whitelist. | 
 | 2297 |     shopt -s expand_aliases | 
 | 2298 |     # Remove all aliases that might be expanded to preserve old unexpanded | 
 | 2299 |     # behavior | 
 | 2300 |     unalias -a | 
 | 2301 |     # Add only the alias we want for openstack | 
 | 2302 |     alias openstack=oscwrap | 
 | 2303 | } | 
 | 2304 |  | 
 | 2305 | function cleanup_oscwrap { | 
 | 2306 |     local total=0 | 
| Stephen Finucane | ffd0047 | 2018-01-18 15:12:29 +0000 | [diff] [blame] | 2307 |     total=$(cat $OSCWRAP_TIMER_FILE | $PYTHON -c "import sys; print(sum(int(l) for l in sys.stdin))") | 
| Sean Dague | 85cf293 | 2017-03-27 15:35:13 -0400 | [diff] [blame] | 2308 |     _TIME_TOTAL["osc"]=$total | 
 | 2309 |     rm $OSCWRAP_TIMER_FILE | 
 | 2310 | } | 
 | 2311 |  | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2312 | # time_totals | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2313 | #  Print out total time summary | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2314 | function time_totals { | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2315 |     local elapsed_time | 
 | 2316 |     local end_time | 
| John L. Villalovos | 5ad4e58 | 2017-09-07 15:33:57 -0700 | [diff] [blame] | 2317 |     local len=20 | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2318 |     local xtrace | 
| John L. Villalovos | 5ad4e58 | 2017-09-07 15:33:57 -0700 | [diff] [blame] | 2319 |     local unaccounted_time | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2320 |  | 
 | 2321 |     end_time=$(date +%s) | 
 | 2322 |     elapsed_time=$(($end_time - $_TIME_BEGIN)) | 
| John L. Villalovos | 5ad4e58 | 2017-09-07 15:33:57 -0700 | [diff] [blame] | 2323 |     unaccounted_time=$elapsed_time | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2324 |  | 
 | 2325 |     # pad 1st column this far | 
 | 2326 |     for t in ${!_TIME_TOTAL[*]}; do | 
 | 2327 |         if [[ ${#t} -gt $len ]]; then | 
 | 2328 |             len=${#t} | 
 | 2329 |         fi | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2330 |     done | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2331 |  | 
| Sean Dague | 85cf293 | 2017-03-27 15:35:13 -0400 | [diff] [blame] | 2332 |     cleanup_oscwrap | 
 | 2333 |  | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2334 |     xtrace=$(set +o | grep xtrace) | 
 | 2335 |     set +o xtrace | 
 | 2336 |  | 
 | 2337 |     echo | 
 | 2338 |     echo "=========================" | 
 | 2339 |     echo "DevStack Component Timing" | 
| John L. Villalovos | 5ad4e58 | 2017-09-07 15:33:57 -0700 | [diff] [blame] | 2340 |     echo " (times are in seconds)  " | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2341 |     echo "=========================" | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2342 |     for t in ${!_TIME_TOTAL[*]}; do | 
 | 2343 |         local v=${_TIME_TOTAL[$t]} | 
| Sean Dague | 85cf293 | 2017-03-27 15:35:13 -0400 | [diff] [blame] | 2344 |         # because we're recording in milliseconds | 
 | 2345 |         v=$(($v / 1000)) | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2346 |         printf "%-${len}s %3d\n" "$t" "$v" | 
| John L. Villalovos | 5ad4e58 | 2017-09-07 15:33:57 -0700 | [diff] [blame] | 2347 |         unaccounted_time=$(($unaccounted_time - $v)) | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2348 |     done | 
| John L. Villalovos | 5ad4e58 | 2017-09-07 15:33:57 -0700 | [diff] [blame] | 2349 |     echo "-------------------------" | 
 | 2350 |     printf "%-${len}s %3d\n" "Unaccounted time" "$unaccounted_time" | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2351 |     echo "=========================" | 
| John L. Villalovos | 5ad4e58 | 2017-09-07 15:33:57 -0700 | [diff] [blame] | 2352 |     printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time" | 
| Ian Wienand | 908a3a9 | 2016-03-29 14:47:09 +1100 | [diff] [blame] | 2353 |  | 
 | 2354 |     $xtrace | 
| Sean Dague | 95c33d5 | 2015-10-07 11:05:59 -0400 | [diff] [blame] | 2355 | } | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2356 |  | 
 | 2357 | # Restore xtrace | 
| Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 2358 | $_XTRACE_FUNCTIONS_COMMON | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 2359 |  | 
 | 2360 | # Local variables: | 
 | 2361 | # mode: shell-script | 
 | 2362 | # End: |