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