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