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