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