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