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