| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1 | # functions-common - Common functions used by DevStack components | 
|  | 2 | # | 
|  | 3 | # The canonical copy of this file is maintained in the DevStack repo. | 
|  | 4 | # All modifications should be made there and then sync'ed to other repos | 
|  | 5 | # as required. | 
|  | 6 | # | 
|  | 7 | # This file is sorted alphabetically within the function groups. | 
|  | 8 | # | 
|  | 9 | # - Config Functions | 
|  | 10 | # - Control Functions | 
|  | 11 | # - Distro Functions | 
|  | 12 | # - Git Functions | 
|  | 13 | # - OpenStack Functions | 
|  | 14 | # - Package Functions | 
|  | 15 | # - Process Functions | 
|  | 16 | # - Python Functions | 
|  | 17 | # - Service Functions | 
| Masayuki Igawa | f6368d3 | 2014-02-20 13:31:26 +0900 | [diff] [blame] | 18 | # - System Functions | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 19 | # | 
|  | 20 | # The following variables are assumed to be defined by certain functions: | 
|  | 21 | # | 
|  | 22 | # - ``ENABLED_SERVICES`` | 
|  | 23 | # - ``ERROR_ON_CLONE`` | 
|  | 24 | # - ``FILES`` | 
|  | 25 | # - ``OFFLINE`` | 
|  | 26 | # - ``PIP_DOWNLOAD_CACHE`` | 
|  | 27 | # - ``PIP_USE_MIRRORS`` | 
|  | 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`` | 
|  | 34 |  | 
|  | 35 | # Save trace setting | 
|  | 36 | XTRACE=$(set +o | grep xtrace) | 
|  | 37 | set +o xtrace | 
|  | 38 |  | 
|  | 39 |  | 
|  | 40 | # Config Functions | 
|  | 41 | # ================ | 
|  | 42 |  | 
|  | 43 | # Append a new option in an ini file without replacing the old value | 
|  | 44 | # iniadd config-file section option value1 value2 value3 ... | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 45 | function iniadd { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 46 | local xtrace=$(set +o | grep xtrace) | 
|  | 47 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 48 | local file=$1 | 
|  | 49 | local section=$2 | 
|  | 50 | local option=$3 | 
|  | 51 | shift 3 | 
|  | 52 | local values="$(iniget_multiline $file $section $option) $@" | 
|  | 53 | iniset_multiline $file $section $option $values | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 54 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
|  | 57 | # Comment an option in an INI file | 
|  | 58 | # inicomment config-file section option | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 59 | function inicomment { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 60 | local xtrace=$(set +o | grep xtrace) | 
|  | 61 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 62 | local file=$1 | 
|  | 63 | local section=$2 | 
|  | 64 | local option=$3 | 
|  | 65 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file" | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 66 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
|  | 69 | # Get an option from an INI file | 
|  | 70 | # iniget config-file section option | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 71 | function iniget { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 72 | local xtrace=$(set +o | grep xtrace) | 
|  | 73 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 74 | local file=$1 | 
|  | 75 | local section=$2 | 
|  | 76 | local option=$3 | 
|  | 77 | local line | 
|  | 78 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") | 
|  | 79 | echo ${line#*=} | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 80 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 83 | # Get a multiple line option from an INI file | 
|  | 84 | # iniget_multiline config-file section option | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 85 | function iniget_multiline { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 86 | local xtrace=$(set +o | grep xtrace) | 
|  | 87 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 88 | local file=$1 | 
|  | 89 | local section=$2 | 
|  | 90 | local option=$3 | 
|  | 91 | local values | 
|  | 92 | values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file") | 
|  | 93 | echo ${values} | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 94 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
|  | 97 | # Determinate is the given option present in the INI file | 
|  | 98 | # ini_has_option config-file section option | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 99 | function ini_has_option { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 100 | local xtrace=$(set +o | grep xtrace) | 
|  | 101 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 102 | local file=$1 | 
|  | 103 | local section=$2 | 
|  | 104 | local option=$3 | 
|  | 105 | local line | 
|  | 106 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 107 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 108 | [ -n "$line" ] | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | # Set an option in an INI file | 
|  | 112 | # iniset config-file section option value | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 113 | function iniset { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 114 | local xtrace=$(set +o | grep xtrace) | 
|  | 115 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 116 | local file=$1 | 
|  | 117 | local section=$2 | 
|  | 118 | local option=$3 | 
|  | 119 | local value=$4 | 
|  | 120 |  | 
|  | 121 | [[ -z $section || -z $option ]] && return | 
|  | 122 |  | 
|  | 123 | if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then | 
|  | 124 | # Add section at the end | 
|  | 125 | echo -e "\n[$section]" >>"$file" | 
|  | 126 | fi | 
|  | 127 | if ! ini_has_option "$file" "$section" "$option"; then | 
|  | 128 | # Add it | 
|  | 129 | sed -i -e "/^\[$section\]/ a\\ | 
|  | 130 | $option = $value | 
|  | 131 | " "$file" | 
|  | 132 | else | 
|  | 133 | local sep=$(echo -ne "\x01") | 
|  | 134 | # Replace it | 
|  | 135 | sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file" | 
|  | 136 | fi | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 137 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 138 | } | 
|  | 139 |  | 
|  | 140 | # Set a multiple line option in an INI file | 
|  | 141 | # iniset_multiline config-file section option value1 value2 valu3 ... | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 142 | function iniset_multiline { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 143 | local xtrace=$(set +o | grep xtrace) | 
|  | 144 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 145 | local file=$1 | 
|  | 146 | local section=$2 | 
|  | 147 | local option=$3 | 
|  | 148 | shift 3 | 
|  | 149 | local values | 
|  | 150 | for v in $@; do | 
|  | 151 | # The later sed command inserts each new value in the line next to | 
|  | 152 | # the section identifier, which causes the values to be inserted in | 
|  | 153 | # the reverse order. Do a reverse here to keep the original order. | 
|  | 154 | values="$v ${values}" | 
|  | 155 | done | 
|  | 156 | if ! grep -q "^\[$section\]" "$file"; then | 
|  | 157 | # Add section at the end | 
|  | 158 | echo -e "\n[$section]" >>"$file" | 
|  | 159 | else | 
|  | 160 | # Remove old values | 
|  | 161 | sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" | 
|  | 162 | fi | 
|  | 163 | # Add new ones | 
|  | 164 | for v in $values; do | 
|  | 165 | sed -i -e "/^\[$section\]/ a\\ | 
|  | 166 | $option = $v | 
|  | 167 | " "$file" | 
|  | 168 | done | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 169 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
|  | 172 | # Uncomment an option in an INI file | 
|  | 173 | # iniuncomment config-file section option | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 174 | function iniuncomment { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 175 | local xtrace=$(set +o | grep xtrace) | 
|  | 176 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 177 | local file=$1 | 
|  | 178 | local section=$2 | 
|  | 179 | local option=$3 | 
|  | 180 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file" | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 181 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
|  | 184 | # Normalize config values to True or False | 
|  | 185 | # Accepts as False: 0 no No NO false False FALSE | 
|  | 186 | # Accepts as True: 1 yes Yes YES true True TRUE | 
|  | 187 | # VAR=$(trueorfalse default-value test-value) | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 188 | function trueorfalse { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 189 | local xtrace=$(set +o | grep xtrace) | 
|  | 190 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 191 | local default=$1 | 
|  | 192 | local testval=$2 | 
|  | 193 |  | 
|  | 194 | [[ -z "$testval" ]] && { echo "$default"; return; } | 
|  | 195 | [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; } | 
|  | 196 | [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; } | 
|  | 197 | echo "$default" | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 198 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
|  | 201 |  | 
|  | 202 | # Control Functions | 
|  | 203 | # ================= | 
|  | 204 |  | 
|  | 205 | # Prints backtrace info | 
|  | 206 | # filename:lineno:function | 
|  | 207 | # backtrace level | 
|  | 208 | function backtrace { | 
|  | 209 | local level=$1 | 
|  | 210 | local deep=$((${#BASH_SOURCE[@]} - 1)) | 
|  | 211 | echo "[Call Trace]" | 
|  | 212 | while [ $level -le $deep ]; do | 
|  | 213 | echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}" | 
|  | 214 | deep=$((deep - 1)) | 
|  | 215 | done | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | # Prints line number and "message" then exits | 
|  | 219 | # die $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 220 | function die { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 221 | local exitcode=$? | 
|  | 222 | set +o xtrace | 
|  | 223 | local line=$1; shift | 
|  | 224 | if [ $exitcode == 0 ]; then | 
|  | 225 | exitcode=1 | 
|  | 226 | fi | 
|  | 227 | backtrace 2 | 
|  | 228 | err $line "$*" | 
| Dean Troyer | a25a6f6 | 2014-02-24 16:03:41 -0600 | [diff] [blame] | 229 | # Give buffers a second to flush | 
|  | 230 | sleep 1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 231 | exit $exitcode | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | # Checks an environment variable is not set or has length 0 OR if the | 
|  | 235 | # exit code is non-zero and prints "message" and exits | 
|  | 236 | # NOTE: env-var is the variable name without a '$' | 
|  | 237 | # die_if_not_set $LINENO env-var "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 238 | function die_if_not_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 239 | local exitcode=$? | 
|  | 240 | FXTRACE=$(set +o | grep xtrace) | 
|  | 241 | set +o xtrace | 
|  | 242 | local line=$1; shift | 
|  | 243 | local evar=$1; shift | 
|  | 244 | if ! is_set $evar || [ $exitcode != 0 ]; then | 
|  | 245 | die $line "$*" | 
|  | 246 | fi | 
|  | 247 | $FXTRACE | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | # Prints line number and "message" in error format | 
|  | 251 | # err $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 252 | function err { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 253 | local exitcode=$? | 
|  | 254 | errXTRACE=$(set +o | grep xtrace) | 
|  | 255 | set +o xtrace | 
|  | 256 | local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2" | 
|  | 257 | echo $msg 1>&2; | 
|  | 258 | if [[ -n ${SCREEN_LOGDIR} ]]; then | 
|  | 259 | echo $msg >> "${SCREEN_LOGDIR}/error.log" | 
|  | 260 | fi | 
|  | 261 | $errXTRACE | 
|  | 262 | return $exitcode | 
|  | 263 | } | 
|  | 264 |  | 
|  | 265 | # Checks an environment variable is not set or has length 0 OR if the | 
|  | 266 | # exit code is non-zero and prints "message" | 
|  | 267 | # NOTE: env-var is the variable name without a '$' | 
|  | 268 | # err_if_not_set $LINENO env-var "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 269 | function err_if_not_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 270 | local exitcode=$? | 
|  | 271 | errinsXTRACE=$(set +o | grep xtrace) | 
|  | 272 | set +o xtrace | 
|  | 273 | local line=$1; shift | 
|  | 274 | local evar=$1; shift | 
|  | 275 | if ! is_set $evar || [ $exitcode != 0 ]; then | 
|  | 276 | err $line "$*" | 
|  | 277 | fi | 
|  | 278 | $errinsXTRACE | 
|  | 279 | return $exitcode | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | # Exit after outputting a message about the distribution not being supported. | 
|  | 283 | # exit_distro_not_supported [optional-string-telling-what-is-missing] | 
|  | 284 | function exit_distro_not_supported { | 
|  | 285 | if [[ -z "$DISTRO" ]]; then | 
|  | 286 | GetDistro | 
|  | 287 | fi | 
|  | 288 |  | 
|  | 289 | if [ $# -gt 0 ]; then | 
|  | 290 | die $LINENO "Support for $DISTRO is incomplete: no support for $@" | 
|  | 291 | else | 
|  | 292 | die $LINENO "Support for $DISTRO is incomplete." | 
|  | 293 | fi | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | # Test if the named environment variable is set and not zero length | 
|  | 297 | # is_set env-var | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 298 | function is_set { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 299 | local var=\$"$1" | 
|  | 300 | eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | # Prints line number and "message" in warning format | 
|  | 304 | # warn $LINENO "message" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 305 | function warn { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 306 | local exitcode=$? | 
|  | 307 | errXTRACE=$(set +o | grep xtrace) | 
|  | 308 | set +o xtrace | 
|  | 309 | local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2" | 
|  | 310 | echo $msg 1>&2; | 
|  | 311 | if [[ -n ${SCREEN_LOGDIR} ]]; then | 
|  | 312 | echo $msg >> "${SCREEN_LOGDIR}/error.log" | 
|  | 313 | fi | 
|  | 314 | $errXTRACE | 
|  | 315 | return $exitcode | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 |  | 
|  | 319 | # Distro Functions | 
|  | 320 | # ================ | 
|  | 321 |  | 
|  | 322 | # Determine OS Vendor, Release and Update | 
|  | 323 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora | 
|  | 324 | # Returns results in global variables: | 
|  | 325 | # os_VENDOR - vendor name | 
|  | 326 | # os_RELEASE - release | 
|  | 327 | # os_UPDATE - update | 
|  | 328 | # os_PACKAGE - package type | 
|  | 329 | # os_CODENAME - vendor's codename for release | 
|  | 330 | # GetOSVersion | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 331 | function GetOSVersion { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 332 | # Figure out which vendor we are | 
|  | 333 | if [[ -x "`which sw_vers 2>/dev/null`" ]]; then | 
|  | 334 | # OS/X | 
|  | 335 | os_VENDOR=`sw_vers -productName` | 
|  | 336 | os_RELEASE=`sw_vers -productVersion` | 
|  | 337 | os_UPDATE=${os_RELEASE##*.} | 
|  | 338 | os_RELEASE=${os_RELEASE%.*} | 
|  | 339 | os_PACKAGE="" | 
|  | 340 | if [[ "$os_RELEASE" =~ "10.7" ]]; then | 
|  | 341 | os_CODENAME="lion" | 
|  | 342 | elif [[ "$os_RELEASE" =~ "10.6" ]]; then | 
|  | 343 | os_CODENAME="snow leopard" | 
|  | 344 | elif [[ "$os_RELEASE" =~ "10.5" ]]; then | 
|  | 345 | os_CODENAME="leopard" | 
|  | 346 | elif [[ "$os_RELEASE" =~ "10.4" ]]; then | 
|  | 347 | os_CODENAME="tiger" | 
|  | 348 | elif [[ "$os_RELEASE" =~ "10.3" ]]; then | 
|  | 349 | os_CODENAME="panther" | 
|  | 350 | else | 
|  | 351 | os_CODENAME="" | 
|  | 352 | fi | 
|  | 353 | elif [[ -x $(which lsb_release 2>/dev/null) ]]; then | 
|  | 354 | os_VENDOR=$(lsb_release -i -s) | 
|  | 355 | os_RELEASE=$(lsb_release -r -s) | 
|  | 356 | os_UPDATE="" | 
|  | 357 | os_PACKAGE="rpm" | 
|  | 358 | if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then | 
|  | 359 | os_PACKAGE="deb" | 
|  | 360 | elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then | 
|  | 361 | lsb_release -d -s | grep -q openSUSE | 
|  | 362 | if [[ $? -eq 0 ]]; then | 
|  | 363 | os_VENDOR="openSUSE" | 
|  | 364 | fi | 
|  | 365 | elif [[ $os_VENDOR == "openSUSE project" ]]; then | 
|  | 366 | os_VENDOR="openSUSE" | 
|  | 367 | elif [[ $os_VENDOR =~ Red.*Hat ]]; then | 
|  | 368 | os_VENDOR="Red Hat" | 
|  | 369 | fi | 
|  | 370 | os_CODENAME=$(lsb_release -c -s) | 
|  | 371 | elif [[ -r /etc/redhat-release ]]; then | 
|  | 372 | # Red Hat Enterprise Linux Server release 5.5 (Tikanga) | 
|  | 373 | # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo) | 
|  | 374 | # CentOS release 5.5 (Final) | 
|  | 375 | # CentOS Linux release 6.0 (Final) | 
|  | 376 | # Fedora release 16 (Verne) | 
|  | 377 | # XenServer release 6.2.0-70446c (xenenterprise) | 
|  | 378 | os_CODENAME="" | 
|  | 379 | for r in "Red Hat" CentOS Fedora XenServer; do | 
|  | 380 | os_VENDOR=$r | 
|  | 381 | if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then | 
|  | 382 | ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` | 
|  | 383 | os_CODENAME=${ver#*|} | 
|  | 384 | os_RELEASE=${ver%|*} | 
|  | 385 | os_UPDATE=${os_RELEASE##*.} | 
|  | 386 | os_RELEASE=${os_RELEASE%.*} | 
|  | 387 | break | 
|  | 388 | fi | 
|  | 389 | os_VENDOR="" | 
|  | 390 | done | 
|  | 391 | os_PACKAGE="rpm" | 
|  | 392 | elif [[ -r /etc/SuSE-release ]]; then | 
|  | 393 | for r in openSUSE "SUSE Linux"; do | 
|  | 394 | if [[ "$r" = "SUSE Linux" ]]; then | 
|  | 395 | os_VENDOR="SUSE LINUX" | 
|  | 396 | else | 
|  | 397 | os_VENDOR=$r | 
|  | 398 | fi | 
|  | 399 |  | 
|  | 400 | if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then | 
|  | 401 | os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'` | 
|  | 402 | os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'` | 
|  | 403 | os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'` | 
|  | 404 | break | 
|  | 405 | fi | 
|  | 406 | os_VENDOR="" | 
|  | 407 | done | 
|  | 408 | os_PACKAGE="rpm" | 
|  | 409 | # If lsb_release is not installed, we should be able to detect Debian OS | 
|  | 410 | elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then | 
|  | 411 | os_VENDOR="Debian" | 
|  | 412 | os_PACKAGE="deb" | 
|  | 413 | os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}') | 
|  | 414 | os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g') | 
|  | 415 | fi | 
|  | 416 | export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | # Translate the OS version values into common nomenclature | 
|  | 420 | # Sets global ``DISTRO`` from the ``os_*`` values | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 421 | function GetDistro { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 422 | GetOSVersion | 
|  | 423 | if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then | 
|  | 424 | # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective | 
|  | 425 | DISTRO=$os_CODENAME | 
|  | 426 | elif [[ "$os_VENDOR" =~ (Fedora) ]]; then | 
|  | 427 | # For Fedora, just use 'f' and the release | 
|  | 428 | DISTRO="f$os_RELEASE" | 
|  | 429 | elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then | 
|  | 430 | DISTRO="opensuse-$os_RELEASE" | 
|  | 431 | elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then | 
|  | 432 | # For SLE, also use the service pack | 
|  | 433 | if [[ -z "$os_UPDATE" ]]; then | 
|  | 434 | DISTRO="sle${os_RELEASE}" | 
|  | 435 | else | 
|  | 436 | DISTRO="sle${os_RELEASE}sp${os_UPDATE}" | 
|  | 437 | fi | 
| anju Tiwari | 6c639c9 | 2014-07-15 18:11:54 +0530 | [diff] [blame] | 438 | elif [[ "$os_VENDOR" =~ (Red Hat) || \ | 
|  | 439 | "$os_VENDOR" =~ (CentOS) || \ | 
|  | 440 | "$os_VENDOR" =~ (OracleServer) ]]; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 441 | # Drop the . release as we assume it's compatible | 
|  | 442 | DISTRO="rhel${os_RELEASE::1}" | 
|  | 443 | elif [[ "$os_VENDOR" =~ (XenServer) ]]; then | 
|  | 444 | DISTRO="xs$os_RELEASE" | 
|  | 445 | else | 
|  | 446 | # Catch-all for now is Vendor + Release + Update | 
|  | 447 | DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" | 
|  | 448 | fi | 
|  | 449 | export DISTRO | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | # Utility function for checking machine architecture | 
|  | 453 | # is_arch arch-type | 
|  | 454 | function is_arch { | 
|  | 455 | ARCH_TYPE=$1 | 
|  | 456 |  | 
|  | 457 | [[ "$(uname -m)" == "$ARCH_TYPE" ]] | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | # Determine if current distribution is a Fedora-based distribution | 
|  | 461 | # (Fedora, RHEL, CentOS, etc). | 
|  | 462 | # is_fedora | 
|  | 463 | function is_fedora { | 
|  | 464 | if [[ -z "$os_VENDOR" ]]; then | 
|  | 465 | GetOSVersion | 
|  | 466 | fi | 
|  | 467 |  | 
| anju Tiwari | 6c639c9 | 2014-07-15 18:11:54 +0530 | [diff] [blame] | 468 | [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \ | 
|  | 469 | [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ] | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 470 | } | 
|  | 471 |  | 
|  | 472 |  | 
|  | 473 | # Determine if current distribution is a SUSE-based distribution | 
|  | 474 | # (openSUSE, SLE). | 
|  | 475 | # is_suse | 
|  | 476 | function is_suse { | 
|  | 477 | if [[ -z "$os_VENDOR" ]]; then | 
|  | 478 | GetOSVersion | 
|  | 479 | fi | 
|  | 480 |  | 
|  | 481 | [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ] | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 |  | 
|  | 485 | # Determine if current distribution is an Ubuntu-based distribution | 
|  | 486 | # It will also detect non-Ubuntu but Debian-based distros | 
|  | 487 | # is_ubuntu | 
|  | 488 | function is_ubuntu { | 
|  | 489 | if [[ -z "$os_PACKAGE" ]]; then | 
|  | 490 | GetOSVersion | 
|  | 491 | fi | 
|  | 492 | [ "$os_PACKAGE" = "deb" ] | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 |  | 
|  | 496 | # Git Functions | 
|  | 497 | # ============= | 
|  | 498 |  | 
| Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 499 | # Returns openstack release name for a given branch name | 
|  | 500 | # ``get_release_name_from_branch branch-name`` | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 501 | function get_release_name_from_branch { | 
| Dean Troyer | abc7b1d | 2014-02-12 12:09:22 -0600 | [diff] [blame] | 502 | local branch=$1 | 
|  | 503 | if [[ $branch =~ "stable/" ]]; then | 
|  | 504 | echo ${branch#*/} | 
|  | 505 | else | 
|  | 506 | echo "master" | 
|  | 507 | fi | 
|  | 508 | } | 
|  | 509 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 510 | # git clone only if directory doesn't exist already.  Since ``DEST`` might not | 
|  | 511 | # be owned by the installation user, we create the directory and change the | 
|  | 512 | # ownership to the proper user. | 
|  | 513 | # Set global RECLONE=yes to simulate a clone when dest-dir exists | 
|  | 514 | # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo | 
|  | 515 | # does not exist (default is False, meaning the repo will be cloned). | 
|  | 516 | # Uses global ``OFFLINE`` | 
|  | 517 | # git_clone remote dest-dir branch | 
|  | 518 | function git_clone { | 
|  | 519 | GIT_REMOTE=$1 | 
|  | 520 | GIT_DEST=$2 | 
|  | 521 | GIT_REF=$3 | 
|  | 522 | RECLONE=$(trueorfalse False $RECLONE) | 
| Sean Dague | 64bd016 | 2014-03-12 13:04:22 -0400 | [diff] [blame] | 523 | local orig_dir=`pwd` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 524 |  | 
|  | 525 | if [[ "$OFFLINE" = "True" ]]; then | 
|  | 526 | echo "Running in offline mode, clones already exist" | 
|  | 527 | # print out the results so we know what change was used in the logs | 
|  | 528 | cd $GIT_DEST | 
|  | 529 | git show --oneline | head -1 | 
| Sean Dague | 64bd016 | 2014-03-12 13:04:22 -0400 | [diff] [blame] | 530 | cd $orig_dir | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 531 | return | 
|  | 532 | fi | 
|  | 533 |  | 
|  | 534 | if echo $GIT_REF | egrep -q "^refs"; then | 
|  | 535 | # If our branch name is a gerrit style refs/changes/... | 
|  | 536 | if [[ ! -d $GIT_DEST ]]; then | 
|  | 537 | [[ "$ERROR_ON_CLONE" = "True" ]] && \ | 
|  | 538 | die $LINENO "Cloning not allowed in this configuration" | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 539 | git_timed clone $GIT_REMOTE $GIT_DEST | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 540 | fi | 
|  | 541 | cd $GIT_DEST | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 542 | git_timed fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 543 | else | 
|  | 544 | # do a full clone only if the directory doesn't exist | 
|  | 545 | if [[ ! -d $GIT_DEST ]]; then | 
|  | 546 | [[ "$ERROR_ON_CLONE" = "True" ]] && \ | 
|  | 547 | die $LINENO "Cloning not allowed in this configuration" | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 548 | git_timed clone $GIT_REMOTE $GIT_DEST | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 549 | cd $GIT_DEST | 
|  | 550 | # This checkout syntax works for both branches and tags | 
|  | 551 | git checkout $GIT_REF | 
|  | 552 | elif [[ "$RECLONE" = "True" ]]; then | 
|  | 553 | # if it does exist then simulate what clone does if asked to RECLONE | 
|  | 554 | cd $GIT_DEST | 
|  | 555 | # set the url to pull from and fetch | 
|  | 556 | git remote set-url origin $GIT_REMOTE | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 557 | git_timed fetch origin | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 558 | # remove the existing ignored files (like pyc) as they cause breakage | 
|  | 559 | # (due to the py files having older timestamps than our pyc, so python | 
|  | 560 | # thinks the pyc files are correct using them) | 
|  | 561 | find $GIT_DEST -name '*.pyc' -delete | 
|  | 562 |  | 
|  | 563 | # handle GIT_REF accordingly to type (tag, branch) | 
|  | 564 | if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then | 
|  | 565 | git_update_tag $GIT_REF | 
|  | 566 | elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then | 
|  | 567 | git_update_branch $GIT_REF | 
|  | 568 | elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then | 
|  | 569 | git_update_remote_branch $GIT_REF | 
|  | 570 | else | 
|  | 571 | die $LINENO "$GIT_REF is neither branch nor tag" | 
|  | 572 | fi | 
|  | 573 |  | 
|  | 574 | fi | 
|  | 575 | fi | 
|  | 576 |  | 
|  | 577 | # print out the results so we know what change was used in the logs | 
|  | 578 | cd $GIT_DEST | 
|  | 579 | git show --oneline | head -1 | 
| Sean Dague | 64bd016 | 2014-03-12 13:04:22 -0400 | [diff] [blame] | 580 | cd $orig_dir | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 581 | } | 
|  | 582 |  | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 583 | # git can sometimes get itself infinitely stuck with transient network | 
|  | 584 | # errors or other issues with the remote end.  This wraps git in a | 
|  | 585 | # timeout/retry loop and is intended to watch over non-local git | 
|  | 586 | # processes that might hang.  GIT_TIMEOUT, if set, is passed directly | 
|  | 587 | # to timeout(1); otherwise the default value of 0 maintains the status | 
|  | 588 | # quo of waiting forever. | 
|  | 589 | # usage: git_timed <git-command> | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 590 | function git_timed { | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 591 | local count=0 | 
|  | 592 | local timeout=0 | 
|  | 593 |  | 
|  | 594 | if [[ -n "${GIT_TIMEOUT}" ]]; then | 
|  | 595 | timeout=${GIT_TIMEOUT} | 
|  | 596 | fi | 
|  | 597 |  | 
|  | 598 | until timeout -s SIGINT ${timeout} git "$@"; do | 
|  | 599 | # 124 is timeout(1)'s special return code when it reached the | 
|  | 600 | # timeout; otherwise assume fatal failure | 
|  | 601 | if [[ $? -ne 124 ]]; then | 
|  | 602 | die $LINENO "git call failed: [git $@]" | 
|  | 603 | fi | 
|  | 604 |  | 
|  | 605 | count=$(($count + 1)) | 
|  | 606 | warn "timeout ${count} for git call: [git $@]" | 
|  | 607 | if [ $count -eq 3 ]; then | 
|  | 608 | die $LINENO "Maximum of 3 git retries reached" | 
|  | 609 | fi | 
|  | 610 | sleep 5 | 
|  | 611 | done | 
|  | 612 | } | 
|  | 613 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 614 | # git update using reference as a branch. | 
|  | 615 | # git_update_branch ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 616 | function git_update_branch { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 617 |  | 
|  | 618 | GIT_BRANCH=$1 | 
|  | 619 |  | 
|  | 620 | git checkout -f origin/$GIT_BRANCH | 
|  | 621 | # a local branch might not exist | 
|  | 622 | git branch -D $GIT_BRANCH || true | 
|  | 623 | git checkout -b $GIT_BRANCH | 
|  | 624 | } | 
|  | 625 |  | 
|  | 626 | # git update using reference as a branch. | 
|  | 627 | # git_update_remote_branch ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 628 | function git_update_remote_branch { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 629 |  | 
|  | 630 | GIT_BRANCH=$1 | 
|  | 631 |  | 
|  | 632 | git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | # git update using reference as a tag. Be careful editing source at that repo | 
|  | 636 | # as working copy will be in a detached mode | 
|  | 637 | # git_update_tag ref | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 638 | function git_update_tag { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 639 |  | 
|  | 640 | GIT_TAG=$1 | 
|  | 641 |  | 
|  | 642 | git tag -d $GIT_TAG | 
|  | 643 | # fetching given tag only | 
| Ian Wienand | d53ad0b | 2014-02-20 13:55:13 +1100 | [diff] [blame] | 644 | git_timed fetch origin tag $GIT_TAG | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 645 | git checkout -f $GIT_TAG | 
|  | 646 | } | 
|  | 647 |  | 
|  | 648 |  | 
|  | 649 | # OpenStack Functions | 
|  | 650 | # =================== | 
|  | 651 |  | 
|  | 652 | # Get the default value for HOST_IP | 
|  | 653 | # 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] | 654 | function get_default_host_ip { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 655 | local fixed_range=$1 | 
|  | 656 | local floating_range=$2 | 
|  | 657 | local host_ip_iface=$3 | 
|  | 658 | local host_ip=$4 | 
|  | 659 |  | 
|  | 660 | # Find the interface used for the default route | 
|  | 661 | host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)} | 
|  | 662 | # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable | 
|  | 663 | if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then | 
|  | 664 | host_ip="" | 
|  | 665 | host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}'` | 
|  | 666 | for IP in $host_ips; do | 
|  | 667 | # Attempt to filter out IP addresses that are part of the fixed and | 
|  | 668 | # floating range. Note that this method only works if the ``netaddr`` | 
|  | 669 | # python library is installed. If it is not installed, an error | 
|  | 670 | # will be printed and the first IP from the interface will be used. | 
|  | 671 | # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct | 
|  | 672 | # address. | 
|  | 673 | if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then | 
|  | 674 | host_ip=$IP | 
|  | 675 | break; | 
|  | 676 | fi | 
|  | 677 | done | 
|  | 678 | fi | 
|  | 679 | echo $host_ip | 
|  | 680 | } | 
|  | 681 |  | 
|  | 682 | # Grab a numbered field from python prettytable output | 
|  | 683 | # Fields are numbered starting with 1 | 
|  | 684 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. | 
|  | 685 | # get_field field-number | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 686 | function get_field { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 687 | while read data; do | 
|  | 688 | if [ "$1" -lt 0 ]; then | 
|  | 689 | field="(\$(NF$1))" | 
|  | 690 | else | 
|  | 691 | field="\$$(($1 + 1))" | 
|  | 692 | fi | 
|  | 693 | echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" | 
|  | 694 | done | 
|  | 695 | } | 
|  | 696 |  | 
|  | 697 | # Add a policy to a policy.json file | 
|  | 698 | # Do nothing if the policy already exists | 
|  | 699 | # ``policy_add policy_file policy_name policy_permissions`` | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 700 | function policy_add { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 701 | local policy_file=$1 | 
|  | 702 | local policy_name=$2 | 
|  | 703 | local policy_perm=$3 | 
|  | 704 |  | 
|  | 705 | if grep -q ${policy_name} ${policy_file}; then | 
|  | 706 | echo "Policy ${policy_name} already exists in ${policy_file}" | 
|  | 707 | return | 
|  | 708 | fi | 
|  | 709 |  | 
|  | 710 | # Add a terminating comma to policy lines without one | 
|  | 711 | # Remove the closing '}' and all lines following to the end-of-file | 
|  | 712 | local tmpfile=$(mktemp) | 
|  | 713 | uniq ${policy_file} | sed -e ' | 
|  | 714 | s/]$/],/ | 
|  | 715 | /^[}]/,$d | 
|  | 716 | ' > ${tmpfile} | 
|  | 717 |  | 
|  | 718 | # Append policy and closing brace | 
|  | 719 | echo "    \"${policy_name}\": ${policy_perm}" >>${tmpfile} | 
|  | 720 | echo "}" >>${tmpfile} | 
|  | 721 |  | 
|  | 722 | mv ${tmpfile} ${policy_file} | 
|  | 723 | } | 
|  | 724 |  | 
| Bartosz Górski | 0abde39 | 2014-02-28 14:15:19 +0100 | [diff] [blame] | 725 | # Gets or creates user | 
|  | 726 | # Usage: get_or_create_user <username> <password> <project> <email> | 
|  | 727 | function get_or_create_user { | 
|  | 728 | # Gets user id | 
|  | 729 | USER_ID=$( | 
|  | 730 | # Gets user id | 
|  | 731 | openstack user show $1 -f value -c id 2>/dev/null || | 
|  | 732 | # Creates new user | 
|  | 733 | openstack user create \ | 
|  | 734 | $1 \ | 
|  | 735 | --password "$2" \ | 
|  | 736 | --project $3 \ | 
|  | 737 | --email $4 \ | 
|  | 738 | -f value -c id | 
|  | 739 | ) | 
|  | 740 | echo $USER_ID | 
|  | 741 | } | 
|  | 742 |  | 
|  | 743 | # Gets or creates project | 
|  | 744 | # Usage: get_or_create_project <name> | 
|  | 745 | function get_or_create_project { | 
|  | 746 | # Gets project id | 
|  | 747 | PROJECT_ID=$( | 
|  | 748 | # Gets project id | 
|  | 749 | openstack project show $1 -f value -c id 2>/dev/null || | 
|  | 750 | # Creates new project if not exists | 
|  | 751 | openstack project create $1 -f value -c id | 
|  | 752 | ) | 
|  | 753 | echo $PROJECT_ID | 
|  | 754 | } | 
|  | 755 |  | 
|  | 756 | # Gets or creates role | 
|  | 757 | # Usage: get_or_create_role <name> | 
|  | 758 | function get_or_create_role { | 
|  | 759 | ROLE_ID=$( | 
|  | 760 | # Gets role id | 
|  | 761 | openstack role show $1 -f value -c id 2>/dev/null || | 
|  | 762 | # Creates role if not exists | 
|  | 763 | openstack role create $1 -f value -c id | 
|  | 764 | ) | 
|  | 765 | echo $ROLE_ID | 
|  | 766 | } | 
|  | 767 |  | 
|  | 768 | # Gets or adds user role | 
|  | 769 | # Usage: get_or_add_user_role <role> <user> <project> | 
|  | 770 | function get_or_add_user_role { | 
|  | 771 | # Gets user role id | 
|  | 772 | USER_ROLE_ID=$(openstack user role list \ | 
|  | 773 | $2 \ | 
|  | 774 | --project $3 \ | 
|  | 775 | --column "ID" \ | 
|  | 776 | --column "Name" \ | 
|  | 777 | | grep " $1 " | get_field 1) | 
|  | 778 | if [[ -z "$USER_ROLE_ID" ]]; then | 
|  | 779 | # Adds role to user | 
|  | 780 | USER_ROLE_ID=$(openstack role add \ | 
|  | 781 | $1 \ | 
|  | 782 | --user $2 \ | 
|  | 783 | --project $3 \ | 
|  | 784 | | grep " id " | get_field 2) | 
|  | 785 | fi | 
|  | 786 | echo $USER_ROLE_ID | 
|  | 787 | } | 
|  | 788 |  | 
|  | 789 | # Gets or creates service | 
|  | 790 | # Usage: get_or_create_service <name> <type> <description> | 
|  | 791 | function get_or_create_service { | 
|  | 792 | # Gets service id | 
|  | 793 | SERVICE_ID=$( | 
|  | 794 | # Gets service id | 
|  | 795 | openstack service show $1 -f value -c id 2>/dev/null || | 
|  | 796 | # Creates new service if not exists | 
|  | 797 | openstack service create \ | 
|  | 798 | $1 \ | 
|  | 799 | --type=$2 \ | 
|  | 800 | --description="$3" \ | 
|  | 801 | -f value -c id | 
|  | 802 | ) | 
|  | 803 | echo $SERVICE_ID | 
|  | 804 | } | 
|  | 805 |  | 
|  | 806 | # Gets or creates endpoint | 
|  | 807 | # Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl> | 
|  | 808 | function get_or_create_endpoint { | 
|  | 809 | # Gets endpoint id | 
|  | 810 | ENDPOINT_ID=$(openstack endpoint list \ | 
|  | 811 | --column "ID" \ | 
|  | 812 | --column "Region" \ | 
|  | 813 | --column "Service Name" \ | 
|  | 814 | | grep " $2 " \ | 
|  | 815 | | grep " $1 " | get_field 1) | 
|  | 816 | if [[ -z "$ENDPOINT_ID" ]]; then | 
|  | 817 | # Creates new endpoint | 
|  | 818 | ENDPOINT_ID=$(openstack endpoint create \ | 
|  | 819 | $1 \ | 
|  | 820 | --region $2 \ | 
|  | 821 | --publicurl $3 \ | 
|  | 822 | --adminurl $4 \ | 
|  | 823 | --internalurl $5 \ | 
|  | 824 | | grep " id " | get_field 2) | 
|  | 825 | fi | 
|  | 826 | echo $ENDPOINT_ID | 
|  | 827 | } | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 828 |  | 
|  | 829 | # Package Functions | 
|  | 830 | # ================= | 
|  | 831 |  | 
|  | 832 | # _get_package_dir | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 833 | function _get_package_dir { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 834 | local pkg_dir | 
|  | 835 | if is_ubuntu; then | 
|  | 836 | pkg_dir=$FILES/apts | 
|  | 837 | elif is_fedora; then | 
|  | 838 | pkg_dir=$FILES/rpms | 
|  | 839 | elif is_suse; then | 
|  | 840 | pkg_dir=$FILES/rpms-suse | 
|  | 841 | else | 
|  | 842 | exit_distro_not_supported "list of packages" | 
|  | 843 | fi | 
|  | 844 | echo "$pkg_dir" | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | # Wrapper for ``apt-get`` to set cache and proxy environment variables | 
|  | 848 | # Uses globals ``OFFLINE``, ``*_proxy`` | 
|  | 849 | # apt_get operation package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 850 | function apt_get { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 851 | local xtrace=$(set +o | grep xtrace) | 
|  | 852 | set +o xtrace | 
|  | 853 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 854 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return | 
|  | 855 | local sudo="sudo" | 
|  | 856 | [[ "$(id -u)" = "0" ]] && sudo="env" | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 857 |  | 
|  | 858 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 859 | $sudo DEBIAN_FRONTEND=noninteractive \ | 
|  | 860 | http_proxy=$http_proxy https_proxy=$https_proxy \ | 
|  | 861 | no_proxy=$no_proxy \ | 
|  | 862 | apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" | 
|  | 863 | } | 
|  | 864 |  | 
|  | 865 | # get_packages() collects a list of package names of any type from the | 
|  | 866 | # prerequisite files in ``files/{apts|rpms}``.  The list is intended | 
|  | 867 | # to be passed to a package installer such as apt or yum. | 
|  | 868 | # | 
|  | 869 | # Only packages required for the services in 1st argument will be | 
|  | 870 | # included.  Two bits of metadata are recognized in the prerequisite files: | 
|  | 871 | # | 
|  | 872 | # - ``# NOPRIME`` defers installation to be performed later in `stack.sh` | 
|  | 873 | # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection | 
|  | 874 | #   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] | 875 | function get_packages { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 876 | local xtrace=$(set +o | grep xtrace) | 
|  | 877 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 878 | local services=$@ | 
|  | 879 | local package_dir=$(_get_package_dir) | 
|  | 880 | local file_to_parse | 
|  | 881 | local service | 
|  | 882 |  | 
|  | 883 | if [[ -z "$package_dir" ]]; then | 
|  | 884 | echo "No package directory supplied" | 
|  | 885 | return 1 | 
|  | 886 | fi | 
|  | 887 | if [[ -z "$DISTRO" ]]; then | 
|  | 888 | GetDistro | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 889 | echo "Found Distro $DISTRO" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 890 | fi | 
|  | 891 | for service in ${services//,/ }; do | 
|  | 892 | # Allow individual services to specify dependencies | 
|  | 893 | if [[ -e ${package_dir}/${service} ]]; then | 
|  | 894 | file_to_parse="${file_to_parse} $service" | 
|  | 895 | fi | 
|  | 896 | # NOTE(sdague) n-api needs glance for now because that's where | 
|  | 897 | # glance client is | 
|  | 898 | if [[ $service == n-api ]]; then | 
|  | 899 | if [[ ! $file_to_parse =~ nova ]]; then | 
|  | 900 | file_to_parse="${file_to_parse} nova" | 
|  | 901 | fi | 
|  | 902 | if [[ ! $file_to_parse =~ glance ]]; then | 
|  | 903 | file_to_parse="${file_to_parse} glance" | 
|  | 904 | fi | 
|  | 905 | elif [[ $service == c-* ]]; then | 
|  | 906 | if [[ ! $file_to_parse =~ cinder ]]; then | 
|  | 907 | file_to_parse="${file_to_parse} cinder" | 
|  | 908 | fi | 
|  | 909 | elif [[ $service == ceilometer-* ]]; then | 
|  | 910 | if [[ ! $file_to_parse =~ ceilometer ]]; then | 
|  | 911 | file_to_parse="${file_to_parse} ceilometer" | 
|  | 912 | fi | 
|  | 913 | elif [[ $service == s-* ]]; then | 
|  | 914 | if [[ ! $file_to_parse =~ swift ]]; then | 
|  | 915 | file_to_parse="${file_to_parse} swift" | 
|  | 916 | fi | 
|  | 917 | elif [[ $service == n-* ]]; then | 
|  | 918 | if [[ ! $file_to_parse =~ nova ]]; then | 
|  | 919 | file_to_parse="${file_to_parse} nova" | 
|  | 920 | fi | 
|  | 921 | elif [[ $service == g-* ]]; then | 
|  | 922 | if [[ ! $file_to_parse =~ glance ]]; then | 
|  | 923 | file_to_parse="${file_to_parse} glance" | 
|  | 924 | fi | 
|  | 925 | elif [[ $service == key* ]]; then | 
|  | 926 | if [[ ! $file_to_parse =~ keystone ]]; then | 
|  | 927 | file_to_parse="${file_to_parse} keystone" | 
|  | 928 | fi | 
|  | 929 | elif [[ $service == q-* ]]; then | 
|  | 930 | if [[ ! $file_to_parse =~ neutron ]]; then | 
|  | 931 | file_to_parse="${file_to_parse} neutron" | 
|  | 932 | fi | 
| Adam Gandelman | 539ec43 | 2014-03-18 18:57:43 -0700 | [diff] [blame] | 933 | elif [[ $service == ir-* ]]; then | 
|  | 934 | if [[ ! $file_to_parse =~ ironic ]]; then | 
|  | 935 | file_to_parse="${file_to_parse} ironic" | 
|  | 936 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 937 | fi | 
|  | 938 | done | 
|  | 939 |  | 
|  | 940 | for file in ${file_to_parse}; do | 
|  | 941 | local fname=${package_dir}/${file} | 
|  | 942 | local OIFS line package distros distro | 
|  | 943 | [[ -e $fname ]] || continue | 
|  | 944 |  | 
|  | 945 | OIFS=$IFS | 
|  | 946 | IFS=$'\n' | 
|  | 947 | for line in $(<${fname}); do | 
|  | 948 | if [[ $line =~ "NOPRIME" ]]; then | 
|  | 949 | continue | 
|  | 950 | fi | 
|  | 951 |  | 
|  | 952 | # Assume we want this package | 
|  | 953 | package=${line%#*} | 
|  | 954 | inst_pkg=1 | 
|  | 955 |  | 
|  | 956 | # Look for # dist:xxx in comment | 
|  | 957 | if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then | 
|  | 958 | # We are using BASH regexp matching feature. | 
|  | 959 | package=${BASH_REMATCH[1]} | 
|  | 960 | distros=${BASH_REMATCH[2]} | 
|  | 961 | # In bash ${VAR,,} will lowecase VAR | 
|  | 962 | # Look for a match in the distro list | 
|  | 963 | if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then | 
|  | 964 | # If no match then skip this package | 
|  | 965 | inst_pkg=0 | 
|  | 966 | fi | 
|  | 967 | fi | 
|  | 968 |  | 
|  | 969 | # Look for # testonly in comment | 
|  | 970 | if [[ $line =~ (.*)#.*testonly.* ]]; then | 
|  | 971 | package=${BASH_REMATCH[1]} | 
|  | 972 | # Are we installing test packages? (test for the default value) | 
|  | 973 | if [[ $INSTALL_TESTONLY_PACKAGES = "False" ]]; then | 
|  | 974 | # If not installing test packages the skip this package | 
|  | 975 | inst_pkg=0 | 
|  | 976 | fi | 
|  | 977 | fi | 
|  | 978 |  | 
|  | 979 | if [[ $inst_pkg = 1 ]]; then | 
|  | 980 | echo $package | 
|  | 981 | fi | 
|  | 982 | done | 
|  | 983 | IFS=$OIFS | 
|  | 984 | done | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 985 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 986 | } | 
|  | 987 |  | 
|  | 988 | # Distro-agnostic package installer | 
|  | 989 | # install_package package [package ...] | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 990 | function update_package_repo { | 
|  | 991 | if [[ "NO_UPDATE_REPOS" = "True" ]]; then | 
|  | 992 | return 0 | 
|  | 993 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 994 |  | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 995 | if is_ubuntu; then | 
|  | 996 | local xtrace=$(set +o | grep xtrace) | 
|  | 997 | set +o xtrace | 
|  | 998 | if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then | 
|  | 999 | # if there are transient errors pulling the updates, that's fine. | 
|  | 1000 | # It may be secondary repositories that we don't really care about. | 
|  | 1001 | apt_get update  || /bin/true | 
|  | 1002 | REPOS_UPDATED=True | 
|  | 1003 | fi | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1004 | $xtrace | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 1005 | fi | 
|  | 1006 | } | 
|  | 1007 |  | 
|  | 1008 | function real_install_package { | 
|  | 1009 | if is_ubuntu; then | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1010 | apt_get install "$@" | 
|  | 1011 | elif is_fedora; then | 
|  | 1012 | yum_install "$@" | 
|  | 1013 | elif is_suse; then | 
|  | 1014 | zypper_install "$@" | 
|  | 1015 | else | 
|  | 1016 | exit_distro_not_supported "installing packages" | 
|  | 1017 | fi | 
|  | 1018 | } | 
|  | 1019 |  | 
| Monty Taylor | 5cc6d2c | 2014-06-06 08:45:16 -0400 | [diff] [blame] | 1020 | # Distro-agnostic package installer | 
|  | 1021 | # install_package package [package ...] | 
|  | 1022 | function install_package { | 
|  | 1023 | update_package_repo | 
|  | 1024 | real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@ | 
|  | 1025 | } | 
|  | 1026 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1027 | # Distro-agnostic function to tell if a package is installed | 
|  | 1028 | # is_package_installed package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1029 | function is_package_installed { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1030 | if [[ -z "$@" ]]; then | 
|  | 1031 | return 1 | 
|  | 1032 | fi | 
|  | 1033 |  | 
|  | 1034 | if [[ -z "$os_PACKAGE" ]]; then | 
|  | 1035 | GetOSVersion | 
|  | 1036 | fi | 
|  | 1037 |  | 
|  | 1038 | if [[ "$os_PACKAGE" = "deb" ]]; then | 
|  | 1039 | dpkg -s "$@" > /dev/null 2> /dev/null | 
|  | 1040 | elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
|  | 1041 | rpm --quiet -q "$@" | 
|  | 1042 | else | 
|  | 1043 | exit_distro_not_supported "finding if a package is installed" | 
|  | 1044 | fi | 
|  | 1045 | } | 
|  | 1046 |  | 
|  | 1047 | # Distro-agnostic package uninstaller | 
|  | 1048 | # uninstall_package package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1049 | function uninstall_package { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1050 | if is_ubuntu; then | 
|  | 1051 | apt_get purge "$@" | 
|  | 1052 | elif is_fedora; then | 
|  | 1053 | sudo yum remove -y "$@" | 
|  | 1054 | elif is_suse; then | 
|  | 1055 | sudo zypper rm "$@" | 
|  | 1056 | else | 
|  | 1057 | exit_distro_not_supported "uninstalling packages" | 
|  | 1058 | fi | 
|  | 1059 | } | 
|  | 1060 |  | 
|  | 1061 | # Wrapper for ``yum`` to set proxy environment variables | 
|  | 1062 | # Uses globals ``OFFLINE``, ``*_proxy`` | 
|  | 1063 | # yum_install package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1064 | function yum_install { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1065 | [[ "$OFFLINE" = "True" ]] && return | 
|  | 1066 | local sudo="sudo" | 
|  | 1067 | [[ "$(id -u)" = "0" ]] && sudo="env" | 
| Ian Wienand | b27f16d | 2014-02-28 14:29:02 +1100 | [diff] [blame] | 1068 |  | 
|  | 1069 | # The manual check for missing packages is because yum -y assumes | 
|  | 1070 | # missing packages are OK.  See | 
|  | 1071 | # https://bugzilla.redhat.com/show_bug.cgi?id=965567 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1072 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ | 
|  | 1073 | no_proxy=$no_proxy \ | 
| Ian Wienand | b27f16d | 2014-02-28 14:29:02 +1100 | [diff] [blame] | 1074 | yum install -y "$@" 2>&1 | \ | 
|  | 1075 | awk ' | 
|  | 1076 | BEGIN { fail=0 } | 
|  | 1077 | /No package/ { fail=1 } | 
|  | 1078 | { print } | 
|  | 1079 | END { exit fail }' || \ | 
|  | 1080 | die $LINENO "Missing packages detected" | 
|  | 1081 |  | 
|  | 1082 | # also ensure we catch a yum failure | 
|  | 1083 | if [[ ${PIPESTATUS[0]} != 0 ]]; then | 
|  | 1084 | die $LINENO "Yum install failure" | 
|  | 1085 | fi | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1086 | } | 
|  | 1087 |  | 
|  | 1088 | # zypper wrapper to set arguments correctly | 
|  | 1089 | # zypper_install package [package ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1090 | function zypper_install { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1091 | [[ "$OFFLINE" = "True" ]] && return | 
|  | 1092 | local sudo="sudo" | 
|  | 1093 | [[ "$(id -u)" = "0" ]] && sudo="env" | 
|  | 1094 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ | 
|  | 1095 | zypper --non-interactive install --auto-agree-with-licenses "$@" | 
|  | 1096 | } | 
|  | 1097 |  | 
|  | 1098 |  | 
|  | 1099 | # Process Functions | 
|  | 1100 | # ================= | 
|  | 1101 |  | 
|  | 1102 | # _run_process() is designed to be backgrounded by run_process() to simulate a | 
|  | 1103 | # fork.  It includes the dirty work of closing extra filehandles and preparing log | 
|  | 1104 | # files to produce the same logs as screen_it().  The log filename is derived | 
|  | 1105 | # from the service name and global-and-now-misnamed SCREEN_LOGDIR | 
|  | 1106 | # _run_process service "command-line" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1107 | function _run_process { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1108 | local service=$1 | 
|  | 1109 | local command="$2" | 
|  | 1110 |  | 
|  | 1111 | # Undo logging redirections and close the extra descriptors | 
|  | 1112 | exec 1>&3 | 
|  | 1113 | exec 2>&3 | 
|  | 1114 | exec 3>&- | 
|  | 1115 | exec 6>&- | 
|  | 1116 |  | 
|  | 1117 | if [[ -n ${SCREEN_LOGDIR} ]]; then | 
|  | 1118 | exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1 | 
|  | 1119 | ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log | 
|  | 1120 |  | 
|  | 1121 | # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs. | 
|  | 1122 | export PYTHONUNBUFFERED=1 | 
|  | 1123 | fi | 
|  | 1124 |  | 
|  | 1125 | exec /bin/bash -c "$command" | 
|  | 1126 | die "$service exec failure: $command" | 
|  | 1127 | } | 
|  | 1128 |  | 
|  | 1129 | # Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``. | 
|  | 1130 | # This is used for ``service_check`` when all the ``screen_it`` are called finished | 
|  | 1131 | # init_service_check | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1132 | function init_service_check { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1133 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1134 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
|  | 1135 |  | 
|  | 1136 | if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then | 
|  | 1137 | mkdir -p "$SERVICE_DIR/$SCREEN_NAME" | 
|  | 1138 | fi | 
|  | 1139 |  | 
|  | 1140 | rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure | 
|  | 1141 | } | 
|  | 1142 |  | 
|  | 1143 | # Find out if a process exists by partial name. | 
|  | 1144 | # is_running name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1145 | function is_running { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1146 | local name=$1 | 
|  | 1147 | ps auxw | grep -v grep | grep ${name} > /dev/null | 
|  | 1148 | RC=$? | 
|  | 1149 | # some times I really hate bash reverse binary logic | 
|  | 1150 | return $RC | 
|  | 1151 | } | 
|  | 1152 |  | 
|  | 1153 | # run_process() launches a child process that closes all file descriptors and | 
|  | 1154 | # then exec's the passed in command.  This is meant to duplicate the semantics | 
|  | 1155 | # of screen_it() without screen.  PIDs are written to | 
|  | 1156 | # $SERVICE_DIR/$SCREEN_NAME/$service.pid | 
|  | 1157 | # run_process service "command-line" | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1158 | function run_process { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1159 | local service=$1 | 
|  | 1160 | local command="$2" | 
|  | 1161 |  | 
|  | 1162 | # Spawn the child process | 
|  | 1163 | _run_process "$service" "$command" & | 
|  | 1164 | echo $! | 
|  | 1165 | } | 
|  | 1166 |  | 
|  | 1167 | # Helper to launch a service in a named screen | 
|  | 1168 | # screen_it service "command-line" | 
|  | 1169 | function screen_it { | 
| Sean Dague | ea22a4f | 2014-06-27 15:21:41 -0400 | [diff] [blame] | 1170 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1171 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
|  | 1172 | USE_SCREEN=$(trueorfalse True $USE_SCREEN) | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1173 |  | 
|  | 1174 | if is_service_enabled $1; then | 
|  | 1175 | # Append the service to the screen rc file | 
| Sean Dague | ea22a4f | 2014-06-27 15:21:41 -0400 | [diff] [blame] | 1176 | screen_rc "$1" "$2" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1177 |  | 
| Sean Dague | ea22a4f | 2014-06-27 15:21:41 -0400 | [diff] [blame] | 1178 | if [[ "$USE_SCREEN" = "True" ]]; then | 
|  | 1179 | screen -S $SCREEN_NAME -X screen -t $1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1180 |  | 
| Sean Dague | ea22a4f | 2014-06-27 15:21:41 -0400 | [diff] [blame] | 1181 | if [[ -n ${SCREEN_LOGDIR} ]]; then | 
|  | 1182 | screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log | 
|  | 1183 | screen -S $SCREEN_NAME -p $1 -X log on | 
|  | 1184 | ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log | 
|  | 1185 | fi | 
|  | 1186 |  | 
|  | 1187 | # sleep to allow bash to be ready to be send the command - we are | 
|  | 1188 | # creating a new window in screen and then sends characters, so if | 
|  | 1189 | # bash isn't running by the time we send the command, nothing happens | 
|  | 1190 | sleep 3 | 
|  | 1191 |  | 
|  | 1192 | NL=`echo -ne '\015'` | 
|  | 1193 | # This fun command does the following: | 
|  | 1194 | # - the passed server command is backgrounded | 
|  | 1195 | # - the pid of the background process is saved in the usual place | 
|  | 1196 | # - the server process is brought back to the foreground | 
|  | 1197 | # - if the server process exits prematurely the fg command errors | 
|  | 1198 | #   and a message is written to stdout and the service failure file | 
|  | 1199 | # The pid saved can be used in screen_stop() as a process group | 
|  | 1200 | # id to kill off all child processes | 
|  | 1201 | screen -S $SCREEN_NAME -p $1 -X stuff "$2 & echo \$! >$SERVICE_DIR/$SCREEN_NAME/$1.pid; fg || echo \"$1 failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL" | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1202 | else | 
|  | 1203 | # Spawn directly without screen | 
| Sean Dague | ea22a4f | 2014-06-27 15:21:41 -0400 | [diff] [blame] | 1204 | run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1205 | fi | 
|  | 1206 | fi | 
|  | 1207 | } | 
|  | 1208 |  | 
|  | 1209 | # Screen rc file builder | 
|  | 1210 | # screen_rc service "command-line" | 
|  | 1211 | function screen_rc { | 
|  | 1212 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1213 | SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc | 
|  | 1214 | if [[ ! -e $SCREENRC ]]; then | 
|  | 1215 | # Name the screen session | 
|  | 1216 | echo "sessionname $SCREEN_NAME" > $SCREENRC | 
|  | 1217 | # Set a reasonable statusbar | 
|  | 1218 | echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC | 
|  | 1219 | # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off | 
|  | 1220 | echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC | 
|  | 1221 | echo "screen -t shell bash" >> $SCREENRC | 
|  | 1222 | fi | 
|  | 1223 | # If this service doesn't already exist in the screenrc file | 
|  | 1224 | if ! grep $1 $SCREENRC 2>&1 > /dev/null; then | 
|  | 1225 | NL=`echo -ne '\015'` | 
|  | 1226 | echo "screen -t $1 bash" >> $SCREENRC | 
|  | 1227 | echo "stuff \"$2$NL\"" >> $SCREENRC | 
|  | 1228 |  | 
|  | 1229 | if [[ -n ${SCREEN_LOGDIR} ]]; then | 
|  | 1230 | echo "logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log" >>$SCREENRC | 
|  | 1231 | echo "log on" >>$SCREENRC | 
|  | 1232 | fi | 
|  | 1233 | fi | 
|  | 1234 | } | 
|  | 1235 |  | 
|  | 1236 | # Stop a service in screen | 
|  | 1237 | # If a PID is available use it, kill the whole process group via TERM | 
|  | 1238 | # If screen is being used kill the screen window; this will catch processes | 
|  | 1239 | # that did not leave a PID behind | 
|  | 1240 | # screen_stop service | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1241 | function screen_stop { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1242 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1243 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
|  | 1244 | USE_SCREEN=$(trueorfalse True $USE_SCREEN) | 
|  | 1245 |  | 
|  | 1246 | if is_service_enabled $1; then | 
|  | 1247 | # Kill via pid if we have one available | 
|  | 1248 | if [[ -r $SERVICE_DIR/$SCREEN_NAME/$1.pid ]]; then | 
|  | 1249 | pkill -TERM -P -$(cat $SERVICE_DIR/$SCREEN_NAME/$1.pid) | 
|  | 1250 | rm $SERVICE_DIR/$SCREEN_NAME/$1.pid | 
|  | 1251 | fi | 
|  | 1252 | if [[ "$USE_SCREEN" = "True" ]]; then | 
|  | 1253 | # Clean up the screen window | 
|  | 1254 | screen -S $SCREEN_NAME -p $1 -X kill | 
|  | 1255 | fi | 
|  | 1256 | fi | 
|  | 1257 | } | 
|  | 1258 |  | 
|  | 1259 | # Helper to get the status of each running service | 
|  | 1260 | # service_check | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1261 | function service_check { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1262 | local service | 
|  | 1263 | local failures | 
|  | 1264 | SCREEN_NAME=${SCREEN_NAME:-stack} | 
|  | 1265 | SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
|  | 1266 |  | 
|  | 1267 |  | 
|  | 1268 | if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then | 
|  | 1269 | echo "No service status directory found" | 
|  | 1270 | return | 
|  | 1271 | fi | 
|  | 1272 |  | 
|  | 1273 | # 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] | 1274 | # make this -o errexit safe | 
|  | 1275 | failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true` | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1276 |  | 
|  | 1277 | for service in $failures; do | 
|  | 1278 | service=`basename $service` | 
|  | 1279 | service=${service%.failure} | 
|  | 1280 | echo "Error: Service $service is not running" | 
|  | 1281 | done | 
|  | 1282 |  | 
|  | 1283 | if [ -n "$failures" ]; then | 
| Sean Dague | 1237922 | 2014-02-27 17:16:46 -0500 | [diff] [blame] | 1284 | 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] | 1285 | fi | 
|  | 1286 | } | 
|  | 1287 |  | 
|  | 1288 |  | 
|  | 1289 | # Python Functions | 
|  | 1290 | # ================ | 
|  | 1291 |  | 
|  | 1292 | # Get the path to the pip command. | 
|  | 1293 | # get_pip_command | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1294 | function get_pip_command { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1295 | which pip || which pip-python | 
|  | 1296 |  | 
|  | 1297 | if [ $? -ne 0 ]; then | 
|  | 1298 | die $LINENO "Unable to find pip; cannot continue" | 
|  | 1299 | fi | 
|  | 1300 | } | 
|  | 1301 |  | 
|  | 1302 | # Get the path to the direcotry where python executables are installed. | 
|  | 1303 | # get_python_exec_prefix | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1304 | function get_python_exec_prefix { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1305 | if is_fedora || is_suse; then | 
|  | 1306 | echo "/usr/bin" | 
|  | 1307 | else | 
|  | 1308 | echo "/usr/local/bin" | 
|  | 1309 | fi | 
|  | 1310 | } | 
|  | 1311 |  | 
|  | 1312 | # Wrapper for ``pip install`` to set cache and proxy environment variables | 
|  | 1313 | # Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``, | 
|  | 1314 | # ``TRACK_DEPENDS``, ``*_proxy`` | 
|  | 1315 | # pip_install package [package ...] | 
|  | 1316 | function pip_install { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1317 | local xtrace=$(set +o | grep xtrace) | 
|  | 1318 | set +o xtrace | 
|  | 1319 | if [[ "$OFFLINE" = "True" || -z "$@" ]]; then | 
|  | 1320 | $xtrace | 
|  | 1321 | return | 
|  | 1322 | fi | 
|  | 1323 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1324 | if [[ -z "$os_PACKAGE" ]]; then | 
|  | 1325 | GetOSVersion | 
|  | 1326 | fi | 
|  | 1327 | if [[ $TRACK_DEPENDS = True ]]; then | 
|  | 1328 | source $DEST/.venv/bin/activate | 
|  | 1329 | CMD_PIP=$DEST/.venv/bin/pip | 
|  | 1330 | SUDO_PIP="env" | 
|  | 1331 | else | 
|  | 1332 | SUDO_PIP="sudo" | 
|  | 1333 | CMD_PIP=$(get_pip_command) | 
|  | 1334 | fi | 
|  | 1335 |  | 
|  | 1336 | # Mirror option not needed anymore because pypi has CDN available, | 
|  | 1337 | # but it's useful in certain circumstances | 
|  | 1338 | PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False} | 
|  | 1339 | if [[ "$PIP_USE_MIRRORS" != "False" ]]; then | 
|  | 1340 | PIP_MIRROR_OPT="--use-mirrors" | 
|  | 1341 | fi | 
|  | 1342 |  | 
|  | 1343 | # pip < 1.4 has a bug where it will use an already existing build | 
|  | 1344 | # directory unconditionally.  Say an earlier component installs | 
|  | 1345 | # foo v1.1; pip will have built foo's source in | 
|  | 1346 | # /tmp/$USER-pip-build.  Even if a later component specifies foo < | 
|  | 1347 | # 1.1, the existing extracted build will be used and cause | 
|  | 1348 | # confusing errors.  By creating unique build directories we avoid | 
|  | 1349 | # this problem. See https://github.com/pypa/pip/issues/709 | 
|  | 1350 | local pip_build_tmp=$(mktemp --tmpdir -d pip-build.XXXXX) | 
|  | 1351 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1352 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1353 | $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \ | 
| Yves-Gwenael Bourhis | d79a8ac | 2014-04-14 14:49:07 +0200 | [diff] [blame] | 1354 | http_proxy=$http_proxy \ | 
|  | 1355 | https_proxy=$https_proxy \ | 
|  | 1356 | no_proxy=$no_proxy \ | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1357 | $CMD_PIP install --build=${pip_build_tmp} \ | 
|  | 1358 | $PIP_MIRROR_OPT $@ \ | 
|  | 1359 | && $SUDO_PIP rm -rf ${pip_build_tmp} | 
|  | 1360 | } | 
|  | 1361 |  | 
| Sean Dague | 099e5e3 | 2014-03-31 10:35:43 -0400 | [diff] [blame] | 1362 | # this should be used if you want to install globally, all libraries should | 
|  | 1363 | # use this, especially *oslo* ones | 
|  | 1364 | function setup_install { | 
|  | 1365 | local project_dir=$1 | 
|  | 1366 | setup_package_with_req_sync $project_dir | 
|  | 1367 | } | 
|  | 1368 |  | 
|  | 1369 | # this should be used for projects which run services, like all services | 
|  | 1370 | function setup_develop { | 
|  | 1371 | local project_dir=$1 | 
|  | 1372 | setup_package_with_req_sync $project_dir -e | 
|  | 1373 | } | 
|  | 1374 |  | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1375 | # ``pip install -e`` the package, which processes the dependencies | 
|  | 1376 | # using pip before running `setup.py develop` | 
|  | 1377 | # | 
|  | 1378 | # Updates the dependencies in project_dir from the | 
|  | 1379 | # openstack/requirements global list before installing anything. | 
|  | 1380 | # | 
|  | 1381 | # Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS`` | 
|  | 1382 | # setup_develop directory | 
| Sean Dague | 099e5e3 | 2014-03-31 10:35:43 -0400 | [diff] [blame] | 1383 | function setup_package_with_req_sync { | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1384 | local project_dir=$1 | 
| Sean Dague | 099e5e3 | 2014-03-31 10:35:43 -0400 | [diff] [blame] | 1385 | local flags=$2 | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1386 |  | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1387 | # Don't update repo if local changes exist | 
|  | 1388 | # Don't use buggy "git diff --quiet" | 
| Dean Troyer | 83b6c99 | 2014-02-27 12:41:28 -0600 | [diff] [blame] | 1389 | # ``errexit`` requires us to trap the exit code when the repo is changed | 
|  | 1390 | 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] | 1391 |  | 
| YAMAMOTO Takashi | 3b1f2e4 | 2014-02-24 20:30:07 +0900 | [diff] [blame] | 1392 | if [[ $update_requirements != "changed" ]]; then | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1393 | (cd $REQUIREMENTS_DIR; \ | 
|  | 1394 | $SUDO_CMD python update.py $project_dir) | 
|  | 1395 | fi | 
|  | 1396 |  | 
| Sean Dague | 099e5e3 | 2014-03-31 10:35:43 -0400 | [diff] [blame] | 1397 | setup_package $project_dir $flags | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1398 |  | 
|  | 1399 | # We've just gone and possibly modified the user's source tree in an | 
|  | 1400 | # automated way, which is considered bad form if it's a development | 
|  | 1401 | # tree because we've screwed up their next git checkin. So undo it. | 
|  | 1402 | # | 
|  | 1403 | # However... there are some circumstances, like running in the gate | 
|  | 1404 | # where we really really want the overridden version to stick. So provide | 
|  | 1405 | # a variable that tells us whether or not we should UNDO the requirements | 
|  | 1406 | # changes (this will be set to False in the OpenStack ci gate) | 
|  | 1407 | if [ $UNDO_REQUIREMENTS = "True" ]; then | 
| YAMAMOTO Takashi | 3b1f2e4 | 2014-02-24 20:30:07 +0900 | [diff] [blame] | 1408 | if [[ $update_requirements != "changed" ]]; then | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1409 | (cd $project_dir && git reset --hard) | 
|  | 1410 | fi | 
|  | 1411 | fi | 
|  | 1412 | } | 
|  | 1413 |  | 
|  | 1414 | # ``pip install -e`` the package, which processes the dependencies | 
|  | 1415 | # using pip before running `setup.py develop` | 
|  | 1416 | # Uses globals ``STACK_USER`` | 
|  | 1417 | # setup_develop_no_requirements_update directory | 
| Sean Dague | 099e5e3 | 2014-03-31 10:35:43 -0400 | [diff] [blame] | 1418 | function setup_package { | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1419 | local project_dir=$1 | 
| Sean Dague | 099e5e3 | 2014-03-31 10:35:43 -0400 | [diff] [blame] | 1420 | local flags=$2 | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1421 |  | 
| Sean Dague | 099e5e3 | 2014-03-31 10:35:43 -0400 | [diff] [blame] | 1422 | pip_install $flags $project_dir | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1423 | # ensure that further actions can do things like setup.py sdist | 
| Sean Dague | 099e5e3 | 2014-03-31 10:35:43 -0400 | [diff] [blame] | 1424 | if [[ "$flags" == "-e" ]]; then | 
|  | 1425 | safe_chown -R $STACK_USER $1/*.egg-info | 
|  | 1426 | fi | 
| Dean Troyer | af616d9 | 2014-02-17 12:57:55 -0600 | [diff] [blame] | 1427 | } | 
|  | 1428 |  | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1429 |  | 
|  | 1430 | # Service Functions | 
|  | 1431 | # ================= | 
|  | 1432 |  | 
|  | 1433 | # remove extra commas from the input string (i.e. ``ENABLED_SERVICES``) | 
|  | 1434 | # _cleanup_service_list service-list | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1435 | function _cleanup_service_list { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1436 | echo "$1" | sed -e ' | 
|  | 1437 | s/,,/,/g; | 
|  | 1438 | s/^,//; | 
|  | 1439 | s/,$// | 
|  | 1440 | ' | 
|  | 1441 | } | 
|  | 1442 |  | 
|  | 1443 | # disable_all_services() removes all current services | 
|  | 1444 | # from ``ENABLED_SERVICES`` to reset the configuration | 
|  | 1445 | # before a minimal installation | 
|  | 1446 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1447 | # disable_all_services | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1448 | function disable_all_services { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1449 | ENABLED_SERVICES="" | 
|  | 1450 | } | 
|  | 1451 |  | 
|  | 1452 | # Remove all services starting with '-'.  For example, to install all default | 
|  | 1453 | # services except rabbit (rabbit) set in ``localrc``: | 
|  | 1454 | # ENABLED_SERVICES+=",-rabbit" | 
|  | 1455 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1456 | # disable_negated_services | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1457 | function disable_negated_services { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1458 | local tmpsvcs="${ENABLED_SERVICES}" | 
|  | 1459 | local service | 
|  | 1460 | for service in ${tmpsvcs//,/ }; do | 
|  | 1461 | if [[ ${service} == -* ]]; then | 
|  | 1462 | tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g") | 
|  | 1463 | fi | 
|  | 1464 | done | 
|  | 1465 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
|  | 1466 | } | 
|  | 1467 |  | 
|  | 1468 | # disable_service() removes the services passed as argument to the | 
|  | 1469 | # ``ENABLED_SERVICES`` list, if they are present. | 
|  | 1470 | # | 
|  | 1471 | # For example: | 
|  | 1472 | #   disable_service rabbit | 
|  | 1473 | # | 
|  | 1474 | # This function does not know about the special cases | 
|  | 1475 | # for nova, glance, and neutron built into is_service_enabled(). | 
|  | 1476 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1477 | # disable_service service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1478 | function disable_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1479 | local tmpsvcs=",${ENABLED_SERVICES}," | 
|  | 1480 | local service | 
|  | 1481 | for service in $@; do | 
|  | 1482 | if is_service_enabled $service; then | 
|  | 1483 | tmpsvcs=${tmpsvcs//,$service,/,} | 
|  | 1484 | fi | 
|  | 1485 | done | 
|  | 1486 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
|  | 1487 | } | 
|  | 1488 |  | 
|  | 1489 | # enable_service() adds the services passed as argument to the | 
|  | 1490 | # ``ENABLED_SERVICES`` list, if they are not already present. | 
|  | 1491 | # | 
|  | 1492 | # For example: | 
|  | 1493 | #   enable_service qpid | 
|  | 1494 | # | 
|  | 1495 | # This function does not know about the special cases | 
|  | 1496 | # for nova, glance, and neutron built into is_service_enabled(). | 
|  | 1497 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1498 | # enable_service service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1499 | function enable_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1500 | local tmpsvcs="${ENABLED_SERVICES}" | 
|  | 1501 | for service in $@; do | 
|  | 1502 | if ! is_service_enabled $service; then | 
|  | 1503 | tmpsvcs+=",$service" | 
|  | 1504 | fi | 
|  | 1505 | done | 
|  | 1506 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
|  | 1507 | disable_negated_services | 
|  | 1508 | } | 
|  | 1509 |  | 
|  | 1510 | # is_service_enabled() checks if the service(s) specified as arguments are | 
|  | 1511 | # enabled by the user in ``ENABLED_SERVICES``. | 
|  | 1512 | # | 
|  | 1513 | # Multiple services specified as arguments are ``OR``'ed together; the test | 
|  | 1514 | # is a short-circuit boolean, i.e it returns on the first match. | 
|  | 1515 | # | 
|  | 1516 | # There are special cases for some 'catch-all' services:: | 
|  | 1517 | #   **nova** returns true if any service enabled start with **n-** | 
|  | 1518 | #   **cinder** returns true if any service enabled start with **c-** | 
|  | 1519 | #   **ceilometer** returns true if any service enabled start with **ceilometer** | 
|  | 1520 | #   **glance** returns true if any service enabled start with **g-** | 
|  | 1521 | #   **neutron** returns true if any service enabled start with **q-** | 
|  | 1522 | #   **swift** returns true if any service enabled start with **s-** | 
|  | 1523 | #   **trove** returns true if any service enabled start with **tr-** | 
|  | 1524 | #   For backward compatibility if we have **swift** in ENABLED_SERVICES all the | 
|  | 1525 | #   **s-** services will be enabled. This will be deprecated in the future. | 
|  | 1526 | # | 
|  | 1527 | # Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``. | 
|  | 1528 | # We also need to make sure to treat **n-cell-region** and **n-cell-child** | 
|  | 1529 | # as enabled in this case. | 
|  | 1530 | # | 
|  | 1531 | # Uses global ``ENABLED_SERVICES`` | 
|  | 1532 | # is_service_enabled service [service ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1533 | function is_service_enabled { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1534 | local xtrace=$(set +o | grep xtrace) | 
|  | 1535 | set +o xtrace | 
|  | 1536 | local enabled=1 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1537 | services=$@ | 
|  | 1538 | for service in ${services}; do | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1539 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1540 |  | 
|  | 1541 | # Look for top-level 'enabled' function for this service | 
|  | 1542 | if type is_${service}_enabled >/dev/null 2>&1; then | 
|  | 1543 | # A function exists for this service, use it | 
|  | 1544 | is_${service}_enabled | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1545 | enabled=$? | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1546 | fi | 
|  | 1547 |  | 
|  | 1548 | # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled() | 
|  | 1549 | #                are implemented | 
|  | 1550 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1551 | [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0 | 
|  | 1552 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0 | 
|  | 1553 | [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && enabled=0 | 
|  | 1554 | [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0 | 
|  | 1555 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0 | 
|  | 1556 | [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0 | 
|  | 1557 | [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0 | 
|  | 1558 | [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0 | 
|  | 1559 | [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0 | 
|  | 1560 | [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0 | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1561 | done | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1562 | $xtrace | 
|  | 1563 | return $enabled | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1564 | } | 
|  | 1565 |  | 
|  | 1566 | # Toggle enable/disable_service for services that must run exclusive of each other | 
|  | 1567 | #  $1 The name of a variable containing a space-separated list of services | 
|  | 1568 | #  $2 The name of a variable in which to store the enabled service's name | 
|  | 1569 | #  $3 The name of the service to enable | 
|  | 1570 | function use_exclusive_service { | 
|  | 1571 | local options=${!1} | 
|  | 1572 | local selection=$3 | 
|  | 1573 | out=$2 | 
|  | 1574 | [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1 | 
|  | 1575 | for opt in $options;do | 
|  | 1576 | [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt | 
|  | 1577 | done | 
|  | 1578 | eval "$out=$selection" | 
|  | 1579 | return 0 | 
|  | 1580 | } | 
|  | 1581 |  | 
|  | 1582 |  | 
| Masayuki Igawa | f6368d3 | 2014-02-20 13:31:26 +0900 | [diff] [blame] | 1583 | # System Functions | 
|  | 1584 | # ================ | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1585 |  | 
|  | 1586 | # Only run the command if the target file (the last arg) is not on an | 
|  | 1587 | # NFS filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1588 | function _safe_permission_operation { | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1589 | local xtrace=$(set +o | grep xtrace) | 
|  | 1590 | set +o xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1591 | local args=( $@ ) | 
|  | 1592 | local last | 
|  | 1593 | local sudo_cmd | 
|  | 1594 | local dir_to_check | 
|  | 1595 |  | 
|  | 1596 | let last="${#args[*]} - 1" | 
|  | 1597 |  | 
|  | 1598 | dir_to_check=${args[$last]} | 
|  | 1599 | if [ ! -d "$dir_to_check" ]; then | 
|  | 1600 | dir_to_check=`dirname "$dir_to_check"` | 
|  | 1601 | fi | 
|  | 1602 |  | 
|  | 1603 | if is_nfs_directory "$dir_to_check" ; then | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1604 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1605 | return 0 | 
|  | 1606 | fi | 
|  | 1607 |  | 
|  | 1608 | if [[ $TRACK_DEPENDS = True ]]; then | 
|  | 1609 | sudo_cmd="env" | 
|  | 1610 | else | 
|  | 1611 | sudo_cmd="sudo" | 
|  | 1612 | fi | 
|  | 1613 |  | 
| Sean Dague | 45917cc | 2014-02-24 16:09:14 -0500 | [diff] [blame] | 1614 | $xtrace | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1615 | $sudo_cmd $@ | 
|  | 1616 | } | 
|  | 1617 |  | 
|  | 1618 | # Exit 0 if address is in network or 1 if address is not in network | 
|  | 1619 | # ip-range is in CIDR notation: 1.2.3.4/20 | 
|  | 1620 | # address_in_net ip-address ip-range | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1621 | function address_in_net { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1622 | local ip=$1 | 
|  | 1623 | local range=$2 | 
|  | 1624 | local masklen=${range#*/} | 
|  | 1625 | local network=$(maskip ${range%/*} $(cidr2netmask $masklen)) | 
|  | 1626 | local subnet=$(maskip $ip $(cidr2netmask $masklen)) | 
|  | 1627 | [[ $network == $subnet ]] | 
|  | 1628 | } | 
|  | 1629 |  | 
|  | 1630 | # Add a user to a group. | 
|  | 1631 | # add_user_to_group user group | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1632 | function add_user_to_group { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1633 | local user=$1 | 
|  | 1634 | local group=$2 | 
|  | 1635 |  | 
|  | 1636 | if [[ -z "$os_VENDOR" ]]; then | 
|  | 1637 | GetOSVersion | 
|  | 1638 | fi | 
|  | 1639 |  | 
|  | 1640 | # SLE11 and openSUSE 12.2 don't have the usual usermod | 
|  | 1641 | if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then | 
|  | 1642 | sudo usermod -a -G "$group" "$user" | 
|  | 1643 | else | 
|  | 1644 | sudo usermod -A "$group" "$user" | 
|  | 1645 | fi | 
|  | 1646 | } | 
|  | 1647 |  | 
|  | 1648 | # Convert CIDR notation to a IPv4 netmask | 
|  | 1649 | # cidr2netmask cidr-bits | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1650 | function cidr2netmask { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1651 | local maskpat="255 255 255 255" | 
|  | 1652 | local maskdgt="254 252 248 240 224 192 128" | 
|  | 1653 | set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3} | 
|  | 1654 | echo ${1-0}.${2-0}.${3-0}.${4-0} | 
|  | 1655 | } | 
|  | 1656 |  | 
|  | 1657 | # Gracefully cp only if source file/dir exists | 
|  | 1658 | # cp_it source destination | 
|  | 1659 | function cp_it { | 
|  | 1660 | if [ -e $1 ] || [ -d $1 ]; then | 
|  | 1661 | cp -pRL $1 $2 | 
|  | 1662 | fi | 
|  | 1663 | } | 
|  | 1664 |  | 
|  | 1665 | # HTTP and HTTPS proxy servers are supported via the usual environment variables [1] | 
|  | 1666 | # ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in | 
|  | 1667 | # ``localrc`` or on the command line if necessary:: | 
|  | 1668 | # | 
|  | 1669 | # [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html | 
|  | 1670 | # | 
|  | 1671 | #     http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh | 
|  | 1672 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1673 | function export_proxy_variables { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1674 | if [[ -n "$http_proxy" ]]; then | 
|  | 1675 | export http_proxy=$http_proxy | 
|  | 1676 | fi | 
|  | 1677 | if [[ -n "$https_proxy" ]]; then | 
|  | 1678 | export https_proxy=$https_proxy | 
|  | 1679 | fi | 
|  | 1680 | if [[ -n "$no_proxy" ]]; then | 
|  | 1681 | export no_proxy=$no_proxy | 
|  | 1682 | fi | 
|  | 1683 | } | 
|  | 1684 |  | 
|  | 1685 | # Returns true if the directory is on a filesystem mounted via NFS. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1686 | function is_nfs_directory { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1687 | local mount_type=`stat -f -L -c %T $1` | 
|  | 1688 | test "$mount_type" == "nfs" | 
|  | 1689 | } | 
|  | 1690 |  | 
|  | 1691 | # Return the network portion of the given IP address using netmask | 
|  | 1692 | # netmask is in the traditional dotted-quad format | 
|  | 1693 | # maskip ip-address netmask | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1694 | function maskip { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1695 | local ip=$1 | 
|  | 1696 | local mask=$2 | 
|  | 1697 | local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}" | 
|  | 1698 | local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.})) | 
|  | 1699 | echo $subnet | 
|  | 1700 | } | 
|  | 1701 |  | 
|  | 1702 | # Service wrapper to restart services | 
|  | 1703 | # restart_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1704 | function restart_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1705 | if is_ubuntu; then | 
|  | 1706 | sudo /usr/sbin/service $1 restart | 
|  | 1707 | else | 
|  | 1708 | sudo /sbin/service $1 restart | 
|  | 1709 | fi | 
|  | 1710 | } | 
|  | 1711 |  | 
|  | 1712 | # Only change permissions of a file or directory if it is not on an | 
|  | 1713 | # NFS filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1714 | function safe_chmod { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1715 | _safe_permission_operation chmod $@ | 
|  | 1716 | } | 
|  | 1717 |  | 
|  | 1718 | # Only change ownership of a file or directory if it is not on an NFS | 
|  | 1719 | # filesystem. | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1720 | function safe_chown { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1721 | _safe_permission_operation chown $@ | 
|  | 1722 | } | 
|  | 1723 |  | 
|  | 1724 | # Service wrapper to start services | 
|  | 1725 | # start_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1726 | function start_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1727 | if is_ubuntu; then | 
|  | 1728 | sudo /usr/sbin/service $1 start | 
|  | 1729 | else | 
|  | 1730 | sudo /sbin/service $1 start | 
|  | 1731 | fi | 
|  | 1732 | } | 
|  | 1733 |  | 
|  | 1734 | # Service wrapper to stop services | 
|  | 1735 | # stop_service service-name | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 1736 | function stop_service { | 
| Dean Troyer | dff49a2 | 2014-01-30 15:37:40 -0600 | [diff] [blame] | 1737 | if is_ubuntu; then | 
|  | 1738 | sudo /usr/sbin/service $1 stop | 
|  | 1739 | else | 
|  | 1740 | sudo /sbin/service $1 stop | 
|  | 1741 | fi | 
|  | 1742 | } | 
|  | 1743 |  | 
|  | 1744 |  | 
|  | 1745 | # Restore xtrace | 
|  | 1746 | $XTRACE | 
|  | 1747 |  | 
|  | 1748 | # Local variables: | 
|  | 1749 | # mode: shell-script | 
|  | 1750 | # End: |