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