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