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