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