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