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