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