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