| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 1 | # functions - Common functions used by DevStack components | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 2 | # | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 3 | # The following variables are assumed to be defined by certain functions: | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 4 | # ``ENABLED_SERVICES`` | 
 | 5 | # ``EROR_ON_CLONE`` | 
 | 6 | # ``FILES`` | 
 | 7 | # ``GLANCE_HOSTPORT`` | 
 | 8 | # ``OFFLINE`` | 
 | 9 | # ``PIP_DOWNLOAD_CACHE`` | 
| Maru Newby | 3a87edd | 2012-10-25 23:01:06 +0000 | [diff] [blame] | 10 | # ``PIP_USE_MIRRORS`` | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 11 | # ``RECLONE`` | 
 | 12 | # ``TRACK_DEPENDS`` | 
 | 13 | # ``http_proxy``, ``https_proxy``, ``no_proxy`` | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 14 |  | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 15 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 16 | # Save trace setting | 
 | 17 | XTRACE=$(set +o | grep xtrace) | 
 | 18 | set +o xtrace | 
 | 19 |  | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 20 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 21 | # Exit 0 if address is in network or 1 if address is not in | 
 | 22 | # network or netaddr library is not installed. | 
 | 23 | # address_in_net ip-address ip-range | 
| Vishvananda Ishaya | c9ad14b | 2012-07-03 20:29:01 +0000 | [diff] [blame] | 24 | function address_in_net() { | 
 | 25 |     python -c " | 
 | 26 | import netaddr | 
 | 27 | import sys | 
 | 28 | sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2')) | 
 | 29 | " | 
 | 30 | } | 
 | 31 |  | 
 | 32 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 33 | # Wrapper for ``apt-get`` to set cache and proxy environment variables | 
 | 34 | # Uses globals ``OFFLINE``, ``*_proxy` | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 35 | # apt_get operation package [package ...] | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 36 | function apt_get() { | 
| Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 37 |     [[ "$OFFLINE" = "True" || -z "$@" ]] && return | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 38 |     local sudo="sudo" | 
 | 39 |     [[ "$(id -u)" = "0" ]] && sudo="env" | 
 | 40 |     $sudo DEBIAN_FRONTEND=noninteractive \ | 
 | 41 |         http_proxy=$http_proxy https_proxy=$https_proxy \ | 
| Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 42 |         no_proxy=$no_proxy \ | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 43 |         apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" | 
 | 44 | } | 
 | 45 |  | 
 | 46 |  | 
 | 47 | # Gracefully cp only if source file/dir exists | 
 | 48 | # cp_it source destination | 
 | 49 | function cp_it { | 
 | 50 |     if [ -e $1 ] || [ -d $1 ]; then | 
 | 51 |         cp -pRL $1 $2 | 
 | 52 |     fi | 
 | 53 | } | 
 | 54 |  | 
 | 55 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 56 | # Prints "message" and exits | 
 | 57 | # die "message" | 
 | 58 | function die() { | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 59 |     local exitcode=$? | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 60 |     set +o xtrace | 
 | 61 |     echo $@ | 
 | 62 |     exit $exitcode | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 63 | } | 
 | 64 |  | 
 | 65 |  | 
 | 66 | # Checks an environment variable is not set or has length 0 OR if the | 
 | 67 | # exit code is non-zero and prints "message" and exits | 
 | 68 | # NOTE: env-var is the variable name without a '$' | 
 | 69 | # die_if_not_set env-var "message" | 
 | 70 | function die_if_not_set() { | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 71 |     ( | 
 | 72 |         local exitcode=$? | 
 | 73 |         set +o xtrace | 
 | 74 |         local evar=$1; shift | 
 | 75 |         if ! is_set $evar || [ $exitcode != 0 ]; then | 
 | 76 |             set +o xtrace | 
 | 77 |             echo $@ | 
 | 78 |             exit -1 | 
 | 79 |         fi | 
 | 80 |     ) | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 81 | } | 
 | 82 |  | 
 | 83 |  | 
 | 84 | # Grab a numbered field from python prettytable output | 
 | 85 | # Fields are numbered starting with 1 | 
 | 86 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. | 
 | 87 | # get_field field-number | 
 | 88 | function get_field() { | 
 | 89 |     while read data; do | 
 | 90 |         if [ "$1" -lt 0 ]; then | 
 | 91 |             field="(\$(NF$1))" | 
 | 92 |         else | 
 | 93 |             field="\$$(($1 + 1))" | 
 | 94 |         fi | 
 | 95 |         echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" | 
 | 96 |     done | 
 | 97 | } | 
 | 98 |  | 
 | 99 |  | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 100 | # get_packages() collects a list of package names of any type from the | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 101 | # prerequisite files in ``files/{apts|rpms}``.  The list is intended | 
 | 102 | # to be passed to a package installer such as apt or yum. | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 103 | # | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 104 | # Only packages required for the services in ``ENABLED_SERVICES`` will be | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 105 | # included.  Two bits of metadata are recognized in the prerequisite files: | 
 | 106 | # - ``# NOPRIME`` defers installation to be performed later in stack.sh | 
 | 107 | # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection | 
 | 108 | #   of the package to the distros listed.  The distro names are case insensitive. | 
 | 109 | # | 
| Vincent Untz | 855c587 | 2012-10-04 13:36:46 +0200 | [diff] [blame] | 110 | # Uses globals ``ENABLED_SERVICES`` | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 111 | # get_packages dir | 
 | 112 | function get_packages() { | 
 | 113 |     local package_dir=$1 | 
 | 114 |     local file_to_parse | 
 | 115 |     local service | 
 | 116 |  | 
 | 117 |     if [[ -z "$package_dir" ]]; then | 
 | 118 |         echo "No package directory supplied" | 
 | 119 |         return 1 | 
 | 120 |     fi | 
 | 121 |     if [[ -z "$DISTRO" ]]; then | 
| Vincent Untz | 855c587 | 2012-10-04 13:36:46 +0200 | [diff] [blame] | 122 |         GetDistro | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 123 |     fi | 
 | 124 |     for service in general ${ENABLED_SERVICES//,/ }; do | 
 | 125 |         # Allow individual services to specify dependencies | 
 | 126 |         if [[ -e ${package_dir}/${service} ]]; then | 
 | 127 |             file_to_parse="${file_to_parse} $service" | 
 | 128 |         fi | 
 | 129 |         # NOTE(sdague) n-api needs glance for now because that's where | 
 | 130 |         # glance client is | 
 | 131 |         if [[ $service == n-api ]]; then | 
 | 132 |             if [[ ! $file_to_parse =~ nova ]]; then | 
 | 133 |                 file_to_parse="${file_to_parse} nova" | 
 | 134 |             fi | 
 | 135 |             if [[ ! $file_to_parse =~ glance ]]; then | 
 | 136 |                 file_to_parse="${file_to_parse} glance" | 
 | 137 |             fi | 
 | 138 |         elif [[ $service == c-* ]]; then | 
 | 139 |             if [[ ! $file_to_parse =~ cinder ]]; then | 
 | 140 |                 file_to_parse="${file_to_parse} cinder" | 
 | 141 |             fi | 
| John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame] | 142 |         elif [[ $service == ceilometer-* ]]; then | 
 | 143 |             if [[ ! $file_to_parse =~ ceilometer ]]; then | 
 | 144 |                 file_to_parse="${file_to_parse} ceilometer" | 
 | 145 |             fi | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 146 |         elif [[ $service == n-* ]]; then | 
 | 147 |             if [[ ! $file_to_parse =~ nova ]]; then | 
 | 148 |                 file_to_parse="${file_to_parse} nova" | 
 | 149 |             fi | 
 | 150 |         elif [[ $service == g-* ]]; then | 
 | 151 |             if [[ ! $file_to_parse =~ glance ]]; then | 
 | 152 |                 file_to_parse="${file_to_parse} glance" | 
 | 153 |             fi | 
 | 154 |         elif [[ $service == key* ]]; then | 
 | 155 |             if [[ ! $file_to_parse =~ keystone ]]; then | 
 | 156 |                 file_to_parse="${file_to_parse} keystone" | 
 | 157 |             fi | 
| Robert Collins | 0a9954f | 2012-11-20 11:34:25 +1300 | [diff] [blame] | 158 |         elif [[ $service == q-* ]]; then | 
 | 159 |             if [[ ! $file_to_parse =~ quantum ]]; then | 
 | 160 |                 file_to_parse="${file_to_parse} quantum" | 
 | 161 |             fi | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 162 |         fi | 
 | 163 |     done | 
 | 164 |  | 
 | 165 |     for file in ${file_to_parse}; do | 
 | 166 |         local fname=${package_dir}/${file} | 
 | 167 |         local OIFS line package distros distro | 
 | 168 |         [[ -e $fname ]] || continue | 
 | 169 |  | 
 | 170 |         OIFS=$IFS | 
 | 171 |         IFS=$'\n' | 
 | 172 |         for line in $(<${fname}); do | 
 | 173 |             if [[ $line =~ "NOPRIME" ]]; then | 
 | 174 |                 continue | 
 | 175 |             fi | 
 | 176 |  | 
 | 177 |             if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then | 
 | 178 |                 # We are using BASH regexp matching feature. | 
 | 179 |                 package=${BASH_REMATCH[1]} | 
 | 180 |                 distros=${BASH_REMATCH[2]} | 
 | 181 |                 # In bash ${VAR,,} will lowecase VAR | 
 | 182 |                 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package | 
 | 183 |                 continue | 
 | 184 |             fi | 
 | 185 |  | 
 | 186 |             echo ${line%#*} | 
 | 187 |         done | 
 | 188 |         IFS=$OIFS | 
 | 189 |     done | 
 | 190 | } | 
 | 191 |  | 
 | 192 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 193 | # Determine OS Vendor, Release and Update | 
 | 194 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora | 
 | 195 | # Returns results in global variables: | 
 | 196 | # os_VENDOR - vendor name | 
 | 197 | # os_RELEASE - release | 
 | 198 | # os_UPDATE - update | 
 | 199 | # os_PACKAGE - package type | 
 | 200 | # os_CODENAME - vendor's codename for release | 
 | 201 | # GetOSVersion | 
 | 202 | GetOSVersion() { | 
 | 203 |     # Figure out which vendor we are | 
 | 204 |     if [[ -n "`which sw_vers 2>/dev/null`" ]]; then | 
 | 205 |         # OS/X | 
 | 206 |         os_VENDOR=`sw_vers -productName` | 
 | 207 |         os_RELEASE=`sw_vers -productVersion` | 
 | 208 |         os_UPDATE=${os_RELEASE##*.} | 
 | 209 |         os_RELEASE=${os_RELEASE%.*} | 
 | 210 |         os_PACKAGE="" | 
 | 211 |         if [[ "$os_RELEASE" =~ "10.7" ]]; then | 
 | 212 |             os_CODENAME="lion" | 
 | 213 |         elif [[ "$os_RELEASE" =~ "10.6" ]]; then | 
 | 214 |             os_CODENAME="snow leopard" | 
 | 215 |         elif [[ "$os_RELEASE" =~ "10.5" ]]; then | 
 | 216 |             os_CODENAME="leopard" | 
 | 217 |         elif [[ "$os_RELEASE" =~ "10.4" ]]; then | 
 | 218 |             os_CODENAME="tiger" | 
 | 219 |         elif [[ "$os_RELEASE" =~ "10.3" ]]; then | 
 | 220 |             os_CODENAME="panther" | 
 | 221 |         else | 
 | 222 |             os_CODENAME="" | 
 | 223 |         fi | 
 | 224 |     elif [[ -x $(which lsb_release 2>/dev/null) ]]; then | 
 | 225 |         os_VENDOR=$(lsb_release -i -s) | 
 | 226 |         os_RELEASE=$(lsb_release -r -s) | 
 | 227 |         os_UPDATE="" | 
 | 228 |         if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then | 
 | 229 |             os_PACKAGE="deb" | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 230 |         elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then | 
 | 231 |             lsb_release -d -s | grep -q openSUSE | 
 | 232 |             if [[ $? -eq 0 ]]; then | 
 | 233 |                 os_VENDOR="openSUSE" | 
 | 234 |             fi | 
 | 235 |             os_PACKAGE="rpm" | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 236 |         else | 
 | 237 |             os_PACKAGE="rpm" | 
 | 238 |         fi | 
 | 239 |         os_CODENAME=$(lsb_release -c -s) | 
 | 240 |     elif [[ -r /etc/redhat-release ]]; then | 
 | 241 |         # Red Hat Enterprise Linux Server release 5.5 (Tikanga) | 
 | 242 |         # CentOS release 5.5 (Final) | 
 | 243 |         # CentOS Linux release 6.0 (Final) | 
 | 244 |         # Fedora release 16 (Verne) | 
 | 245 |         os_CODENAME="" | 
 | 246 |         for r in "Red Hat" CentOS Fedora; do | 
 | 247 |             os_VENDOR=$r | 
 | 248 |             if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then | 
 | 249 |                 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` | 
 | 250 |                 os_CODENAME=${ver#*|} | 
 | 251 |                 os_RELEASE=${ver%|*} | 
 | 252 |                 os_UPDATE=${os_RELEASE##*.} | 
 | 253 |                 os_RELEASE=${os_RELEASE%.*} | 
 | 254 |                 break | 
 | 255 |             fi | 
 | 256 |             os_VENDOR="" | 
 | 257 |         done | 
 | 258 |         os_PACKAGE="rpm" | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 259 |     elif [[ -r /etc/SuSE-release ]]; then | 
 | 260 |         for r in openSUSE "SUSE Linux"; do | 
 | 261 |             if [[ "$r" = "SUSE Linux" ]]; then | 
 | 262 |                 os_VENDOR="SUSE LINUX" | 
 | 263 |             else | 
 | 264 |                 os_VENDOR=$r | 
 | 265 |             fi | 
 | 266 |  | 
 | 267 |             if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then | 
 | 268 |                 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'` | 
 | 269 |                 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'` | 
 | 270 |                 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'` | 
 | 271 |                 break | 
 | 272 |             fi | 
 | 273 |             os_VENDOR="" | 
 | 274 |         done | 
 | 275 |         os_PACKAGE="rpm" | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 276 |     fi | 
 | 277 |     export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME | 
 | 278 | } | 
 | 279 |  | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 280 | # git update using reference as a branch. | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 281 | # git_update_branch ref | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 282 | function git_update_branch() { | 
 | 283 |  | 
 | 284 |     GIT_BRANCH=$1 | 
 | 285 |  | 
 | 286 |     git checkout -f origin/$GIT_BRANCH | 
 | 287 |     # a local branch might not exist | 
 | 288 |     git branch -D $GIT_BRANCH || true | 
 | 289 |     git checkout -b $GIT_BRANCH | 
 | 290 | } | 
 | 291 |  | 
 | 292 |  | 
 | 293 | # git update using reference as a tag. Be careful editing source at that repo | 
 | 294 | # as working copy will be in a detached mode | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 295 | # git_update_tag ref | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 296 | function git_update_tag() { | 
 | 297 |  | 
 | 298 |     GIT_TAG=$1 | 
 | 299 |  | 
 | 300 |     git tag -d $GIT_TAG | 
 | 301 |     # fetching given tag only | 
 | 302 |     git fetch origin tag $GIT_TAG | 
 | 303 |     git checkout -f $GIT_TAG | 
 | 304 | } | 
 | 305 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 306 |  | 
| Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 307 | # git update using reference as a branch. | 
 | 308 | # git_update_remote_branch ref | 
 | 309 | function git_update_remote_branch() { | 
 | 310 |  | 
 | 311 |     GIT_BRANCH=$1 | 
 | 312 |  | 
 | 313 |     git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH | 
 | 314 | } | 
 | 315 |  | 
 | 316 |  | 
| Dean Troyer | a9e0a48 | 2012-07-09 14:07:23 -0500 | [diff] [blame] | 317 | # Translate the OS version values into common nomenclature | 
 | 318 | # Sets ``DISTRO`` from the ``os_*`` values | 
 | 319 | function GetDistro() { | 
 | 320 |     GetOSVersion | 
 | 321 |     if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then | 
 | 322 |         # 'Everyone' refers to Ubuntu releases by the code name adjective | 
 | 323 |         DISTRO=$os_CODENAME | 
 | 324 |     elif [[ "$os_VENDOR" =~ (Fedora) ]]; then | 
 | 325 |         # For Fedora, just use 'f' and the release | 
 | 326 |         DISTRO="f$os_RELEASE" | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 327 |     elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then | 
 | 328 |         DISTRO="opensuse-$os_RELEASE" | 
 | 329 |     elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then | 
 | 330 |         # For SLE, also use the service pack | 
 | 331 |         if [[ -z "$os_UPDATE" ]]; then | 
 | 332 |             DISTRO="sle${os_RELEASE}" | 
 | 333 |         else | 
 | 334 |             DISTRO="sle${os_RELEASE}sp${os_UPDATE}" | 
 | 335 |         fi | 
| Dean Troyer | a9e0a48 | 2012-07-09 14:07:23 -0500 | [diff] [blame] | 336 |     else | 
 | 337 |         # Catch-all for now is Vendor + Release + Update | 
 | 338 |         DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" | 
 | 339 |     fi | 
 | 340 |     export DISTRO | 
 | 341 | } | 
 | 342 |  | 
 | 343 |  | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 344 | # Determine if current distribution is an Ubuntu-based distribution. | 
 | 345 | # It will also detect non-Ubuntu but Debian-based distros; this is not an issue | 
 | 346 | # since Debian and Ubuntu should be compatible. | 
 | 347 | # is_ubuntu | 
 | 348 | function is_ubuntu { | 
 | 349 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 350 |         GetOSVersion | 
 | 351 |     fi | 
 | 352 |  | 
 | 353 |     [ "$os_PACKAGE" = "deb" ] | 
 | 354 | } | 
 | 355 |  | 
 | 356 |  | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 357 | # Determine if current distribution is a SUSE-based distribution | 
 | 358 | # (openSUSE, SLE). | 
 | 359 | # is_suse | 
 | 360 | function is_suse { | 
 | 361 |     if [[ -z "$os_VENDOR" ]]; then | 
 | 362 |         GetOSVersion | 
 | 363 |     fi | 
 | 364 |  | 
| Steve Baker | 1a7bbd2 | 2012-12-03 17:04:02 +1300 | [diff] [blame] | 365 |     [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ] | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 366 | } | 
 | 367 |  | 
 | 368 |  | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 369 | # git clone only if directory doesn't exist already.  Since ``DEST`` might not | 
 | 370 | # be owned by the installation user, we create the directory and change the | 
 | 371 | # ownership to the proper user. | 
 | 372 | # Set global RECLONE=yes to simulate a clone when dest-dir exists | 
| James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 373 | # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo | 
 | 374 | # does not exist (default is False, meaning the repo will be cloned). | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 375 | # Uses global ``OFFLINE`` | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 376 | # git_clone remote dest-dir branch | 
 | 377 | function git_clone { | 
 | 378 |     [[ "$OFFLINE" = "True" ]] && return | 
 | 379 |  | 
 | 380 |     GIT_REMOTE=$1 | 
 | 381 |     GIT_DEST=$2 | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 382 |     GIT_REF=$3 | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 383 |  | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 384 |     if echo $GIT_REF | egrep -q "^refs"; then | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 385 |         # If our branch name is a gerrit style refs/changes/... | 
 | 386 |         if [[ ! -d $GIT_DEST ]]; then | 
| James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 387 |             [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 388 |             git clone $GIT_REMOTE $GIT_DEST | 
 | 389 |         fi | 
 | 390 |         cd $GIT_DEST | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 391 |         git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 392 |     else | 
 | 393 |         # do a full clone only if the directory doesn't exist | 
 | 394 |         if [[ ! -d $GIT_DEST ]]; then | 
| James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 395 |             [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 396 |             git clone $GIT_REMOTE $GIT_DEST | 
 | 397 |             cd $GIT_DEST | 
 | 398 |             # This checkout syntax works for both branches and tags | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 399 |             git checkout $GIT_REF | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 400 |         elif [[ "$RECLONE" == "yes" ]]; then | 
 | 401 |             # if it does exist then simulate what clone does if asked to RECLONE | 
 | 402 |             cd $GIT_DEST | 
 | 403 |             # set the url to pull from and fetch | 
 | 404 |             git remote set-url origin $GIT_REMOTE | 
 | 405 |             git fetch origin | 
 | 406 |             # remove the existing ignored files (like pyc) as they cause breakage | 
 | 407 |             # (due to the py files having older timestamps than our pyc, so python | 
 | 408 |             # thinks the pyc files are correct using them) | 
 | 409 |             find $GIT_DEST -name '*.pyc' -delete | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 410 |  | 
 | 411 |             # handle GIT_REF accordingly to type (tag, branch) | 
 | 412 |             if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then | 
 | 413 |                 git_update_tag $GIT_REF | 
 | 414 |             elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then | 
 | 415 |                 git_update_branch $GIT_REF | 
| Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 416 |             elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then | 
 | 417 |                 git_update_remote_branch $GIT_REF | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 418 |             else | 
 | 419 |                 echo $GIT_REF is neither branch nor tag | 
 | 420 |                 exit 1 | 
 | 421 |             fi | 
 | 422 |  | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 423 |         fi | 
 | 424 |     fi | 
 | 425 | } | 
 | 426 |  | 
 | 427 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 428 | # Comment an option in an INI file | 
| Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 429 | # inicomment config-file section option | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 430 | function inicomment() { | 
 | 431 |     local file=$1 | 
 | 432 |     local section=$2 | 
 | 433 |     local option=$3 | 
| Dean Troyer | afd472c | 2012-11-28 11:54:45 -0600 | [diff] [blame] | 434 |     sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 435 | } | 
 | 436 |  | 
| Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 437 | # Uncomment an option in an INI file | 
 | 438 | # iniuncomment config-file section option | 
 | 439 | function iniuncomment() { | 
 | 440 |     local file=$1 | 
 | 441 |     local section=$2 | 
 | 442 |     local option=$3 | 
| Dean Troyer | afd472c | 2012-11-28 11:54:45 -0600 | [diff] [blame] | 443 |     sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file | 
| Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 444 | } | 
 | 445 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 446 |  | 
 | 447 | # Get an option from an INI file | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 448 | # iniget config-file section option | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 449 | function iniget() { | 
 | 450 |     local file=$1 | 
 | 451 |     local section=$2 | 
 | 452 |     local option=$3 | 
 | 453 |     local line | 
| Dean Troyer | e833562 | 2012-11-27 17:00:11 -0600 | [diff] [blame] | 454 |     line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file) | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 455 |     echo ${line#*=} | 
 | 456 | } | 
 | 457 |  | 
 | 458 |  | 
 | 459 | # Set an option in an INI file | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 460 | # iniset config-file section option value | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 461 | function iniset() { | 
 | 462 |     local file=$1 | 
 | 463 |     local section=$2 | 
 | 464 |     local option=$3 | 
 | 465 |     local value=$4 | 
| Dean Troyer | e833562 | 2012-11-27 17:00:11 -0600 | [diff] [blame] | 466 |     if ! grep -q "^\[ *$section *\]" $file; then | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 467 |         # Add section at the end | 
 | 468 |         echo -e "\n[$section]" >>$file | 
 | 469 |     fi | 
 | 470 |     if [[ -z "$(iniget $file $section $option)" ]]; then | 
 | 471 |         # Add it | 
| Dean Troyer | afd472c | 2012-11-28 11:54:45 -0600 | [diff] [blame] | 472 |         sed -i -e "/^\[ *$section *\]/ a\\ | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 473 | $option = $value | 
 | 474 | " $file | 
 | 475 |     else | 
 | 476 |         # Replace it | 
| Dean Troyer | afd472c | 2012-11-28 11:54:45 -0600 | [diff] [blame] | 477 |         sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 478 |     fi | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 479 | } | 
 | 480 |  | 
 | 481 |  | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 482 | # is_service_enabled() checks if the service(s) specified as arguments are | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 483 | # enabled by the user in ``ENABLED_SERVICES``. | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 484 | # | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 485 | # Multiple services specified as arguments are ``OR``'ed together; the test | 
 | 486 | # is a short-circuit boolean, i.e it returns on the first match. | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 487 | # | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 488 | # There are special cases for some 'catch-all' services:: | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 489 | #   **nova** returns true if any service enabled start with **n-** | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 490 | #   **cinder** returns true if any service enabled start with **c-** | 
 | 491 | #   **ceilometer** returns true if any service enabled start with **ceilometer** | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 492 | #   **glance** returns true if any service enabled start with **g-** | 
 | 493 | #   **quantum** returns true if any service enabled start with **q-** | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 494 | # | 
 | 495 | # Uses global ``ENABLED_SERVICES`` | 
 | 496 | # is_service_enabled service [service ...] | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 497 | function is_service_enabled() { | 
 | 498 |     services=$@ | 
 | 499 |     for service in ${services}; do | 
 | 500 |         [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 | 
 | 501 |         [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 | 
| Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 502 |         [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0 | 
| John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame] | 503 |         [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0 | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 504 |         [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 | 
 | 505 |         [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 | 
 | 506 |     done | 
 | 507 |     return 1 | 
 | 508 | } | 
 | 509 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 510 |  | 
 | 511 | # remove extra commas from the input string (i.e. ``ENABLED_SERVICES``) | 
 | 512 | # _cleanup_service_list service-list | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 513 | function _cleanup_service_list () { | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 514 |     echo "$1" | sed -e ' | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 515 |         s/,,/,/g; | 
 | 516 |         s/^,//; | 
 | 517 |         s/,$// | 
 | 518 |     ' | 
 | 519 | } | 
 | 520 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 521 |  | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 522 | # enable_service() adds the services passed as argument to the | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 523 | # ``ENABLED_SERVICES`` list, if they are not already present. | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 524 | # | 
 | 525 | # For example: | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 526 | #   enable_service qpid | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 527 | # | 
 | 528 | # This function does not know about the special cases | 
 | 529 | # for nova, glance, and quantum built into is_service_enabled(). | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 530 | # Uses global ``ENABLED_SERVICES`` | 
 | 531 | # enable_service service [service ...] | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 532 | function enable_service() { | 
 | 533 |     local tmpsvcs="${ENABLED_SERVICES}" | 
 | 534 |     for service in $@; do | 
 | 535 |         if ! is_service_enabled $service; then | 
 | 536 |             tmpsvcs+=",$service" | 
 | 537 |         fi | 
 | 538 |     done | 
 | 539 |     ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
 | 540 |     disable_negated_services | 
 | 541 | } | 
 | 542 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 543 |  | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 544 | # disable_service() removes the services passed as argument to the | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 545 | # ``ENABLED_SERVICES`` list, if they are present. | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 546 | # | 
 | 547 | # For example: | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 548 | #   disable_service rabbit | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 549 | # | 
 | 550 | # This function does not know about the special cases | 
 | 551 | # for nova, glance, and quantum built into is_service_enabled(). | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 552 | # Uses global ``ENABLED_SERVICES`` | 
 | 553 | # disable_service service [service ...] | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 554 | function disable_service() { | 
 | 555 |     local tmpsvcs=",${ENABLED_SERVICES}," | 
 | 556 |     local service | 
 | 557 |     for service in $@; do | 
 | 558 |         if is_service_enabled $service; then | 
 | 559 |             tmpsvcs=${tmpsvcs//,$service,/,} | 
 | 560 |         fi | 
 | 561 |     done | 
 | 562 |     ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
 | 563 | } | 
 | 564 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 565 |  | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 566 | # disable_all_services() removes all current services | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 567 | # from ``ENABLED_SERVICES`` to reset the configuration | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 568 | # before a minimal installation | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 569 | # Uses global ``ENABLED_SERVICES`` | 
 | 570 | # disable_all_services | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 571 | function disable_all_services() { | 
 | 572 |     ENABLED_SERVICES="" | 
 | 573 | } | 
 | 574 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 575 |  | 
 | 576 | # Remove all services starting with '-'.  For example, to install all default | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 577 | # services except rabbit (rabbit) set in ``localrc``: | 
 | 578 | # ENABLED_SERVICES+=",-rabbit" | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 579 | # Uses global ``ENABLED_SERVICES`` | 
 | 580 | # disable_negated_services | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 581 | function disable_negated_services() { | 
 | 582 |     local tmpsvcs="${ENABLED_SERVICES}" | 
 | 583 |     local service | 
 | 584 |     for service in ${tmpsvcs//,/ }; do | 
 | 585 |         if [[ ${service} == -* ]]; then | 
 | 586 |             tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g") | 
 | 587 |         fi | 
 | 588 |     done | 
 | 589 |     ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
 | 590 | } | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 591 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 592 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 593 | # Distro-agnostic package installer | 
 | 594 | # install_package package [package ...] | 
 | 595 | function install_package() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 596 |     if is_ubuntu; then | 
| Vincent Untz | c0482e6 | 2012-06-12 11:30:43 +0200 | [diff] [blame] | 597 |         [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update | 
 | 598 |         NO_UPDATE_REPOS=True | 
 | 599 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 600 |         apt_get install "$@" | 
 | 601 |     else | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 602 |         if is_suse; then | 
 | 603 |             zypper_install "$@" | 
 | 604 |         else | 
 | 605 |             yum_install "$@" | 
 | 606 |         fi | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 607 |     fi | 
 | 608 | } | 
 | 609 |  | 
 | 610 |  | 
| Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 611 | # Distro-agnostic function to tell if a package is installed | 
 | 612 | # is_package_installed package [package ...] | 
 | 613 | function is_package_installed() { | 
 | 614 |     if [[ -z "$@" ]]; then | 
 | 615 |         return 1 | 
 | 616 |     fi | 
 | 617 |  | 
 | 618 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 619 |         GetOSVersion | 
 | 620 |     fi | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 621 |  | 
| Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 622 |     if [[ "$os_PACKAGE" = "deb" ]]; then | 
 | 623 |         dpkg -l "$@" > /dev/null | 
 | 624 |         return $? | 
 | 625 |     else | 
 | 626 |         rpm --quiet -q "$@" | 
 | 627 |         return $? | 
 | 628 |     fi | 
 | 629 | } | 
 | 630 |  | 
 | 631 |  | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 632 | # Test if the named environment variable is set and not zero length | 
 | 633 | # is_set env-var | 
 | 634 | function is_set() { | 
 | 635 |     local var=\$"$1" | 
| Dean Troyer | e88c0a2 | 2012-11-02 16:59:03 -0500 | [diff] [blame] | 636 |     if eval "[ -z \"$var\" ]"; then | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 637 |         return 1 | 
 | 638 |     fi | 
 | 639 |     return 0 | 
 | 640 | } | 
 | 641 |  | 
 | 642 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 643 | # Wrapper for ``pip install`` to set cache and proxy environment variables | 
| Maru Newby | 3a87edd | 2012-10-25 23:01:06 +0000 | [diff] [blame] | 644 | # Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``, | 
 | 645 | #   ``TRACK_DEPENDS``, ``*_proxy` | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 646 | # pip_install package [package ...] | 
 | 647 | function pip_install { | 
| Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 648 |     [[ "$OFFLINE" = "True" || -z "$@" ]] && return | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 649 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 650 |         GetOSVersion | 
 | 651 |     fi | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 652 |     if [[ $TRACK_DEPENDS = True ]] ; then | 
 | 653 |         source $DEST/.venv/bin/activate | 
 | 654 |         CMD_PIP=$DEST/.venv/bin/pip | 
 | 655 |         SUDO_PIP="env" | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 656 |     else | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 657 |         SUDO_PIP="sudo" | 
| Vincent Untz | 8ec2722 | 2012-11-29 09:25:31 +0100 | [diff] [blame] | 658 |         CMD_PIP=$(get_pip_command) | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 659 |     fi | 
| Maru Newby | 3a87edd | 2012-10-25 23:01:06 +0000 | [diff] [blame] | 660 |     if [[ "$PIP_USE_MIRRORS" != "False" ]]; then | 
 | 661 |         PIP_MIRROR_OPT="--use-mirrors" | 
 | 662 |     fi | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 663 |     $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \ | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 664 |         HTTP_PROXY=$http_proxy \ | 
 | 665 |         HTTPS_PROXY=$https_proxy \ | 
| Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 666 |         NO_PROXY=$no_proxy \ | 
| Maru Newby | 3a87edd | 2012-10-25 23:01:06 +0000 | [diff] [blame] | 667 |         $CMD_PIP install $PIP_MIRROR_OPT $@ | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 668 | } | 
 | 669 |  | 
 | 670 |  | 
 | 671 | # Service wrapper to restart services | 
 | 672 | # restart_service service-name | 
 | 673 | function restart_service() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 674 |     if is_ubuntu; then | 
| Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 675 |         sudo /usr/sbin/service $1 restart | 
 | 676 |     else | 
 | 677 |         sudo /sbin/service $1 restart | 
 | 678 |     fi | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 679 | } | 
 | 680 |  | 
 | 681 |  | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 682 | # Helper to launch a service in a named screen | 
 | 683 | # screen_it service "command-line" | 
 | 684 | function screen_it { | 
 | 685 |     NL=`echo -ne '\015'` | 
 | 686 |     SCREEN_NAME=${SCREEN_NAME:-stack} | 
| jiajun xu | a941424 | 2012-12-06 16:30:57 +0800 | [diff] [blame^] | 687 |     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
 | 688 |  | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 689 |     if is_service_enabled $1; then | 
 | 690 |         # Append the service to the screen rc file | 
 | 691 |         screen_rc "$1" "$2" | 
 | 692 |  | 
 | 693 |         screen -S $SCREEN_NAME -X screen -t $1 | 
 | 694 |         # sleep to allow bash to be ready to be send the command - we are | 
 | 695 |         # creating a new window in screen and then sends characters, so if | 
 | 696 |         # bash isn't running by the time we send the command, nothing happens | 
 | 697 |         sleep 1.5 | 
 | 698 |  | 
 | 699 |         if [[ -n ${SCREEN_LOGDIR} ]]; then | 
 | 700 |             screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log | 
 | 701 |             screen -S $SCREEN_NAME -p $1 -X log on | 
 | 702 |             ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log | 
 | 703 |         fi | 
| jiajun xu | a941424 | 2012-12-06 16:30:57 +0800 | [diff] [blame^] | 704 |         screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL" | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 705 |     fi | 
 | 706 | } | 
 | 707 |  | 
 | 708 |  | 
 | 709 | # Screen rc file builder | 
 | 710 | # screen_rc service "command-line" | 
 | 711 | function screen_rc { | 
 | 712 |     SCREEN_NAME=${SCREEN_NAME:-stack} | 
 | 713 |     SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc | 
 | 714 |     if [[ ! -e $SCREENRC ]]; then | 
 | 715 |         # Name the screen session | 
 | 716 |         echo "sessionname $SCREEN_NAME" > $SCREENRC | 
 | 717 |         # Set a reasonable statusbar | 
 | 718 |         echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC | 
 | 719 |         echo "screen -t shell bash" >> $SCREENRC | 
 | 720 |     fi | 
 | 721 |     # If this service doesn't already exist in the screenrc file | 
 | 722 |     if ! grep $1 $SCREENRC 2>&1 > /dev/null; then | 
 | 723 |         NL=`echo -ne '\015'` | 
 | 724 |         echo "screen -t $1 bash" >> $SCREENRC | 
 | 725 |         echo "stuff \"$2$NL\"" >> $SCREENRC | 
 | 726 |     fi | 
 | 727 | } | 
 | 728 |  | 
| jiajun xu | a941424 | 2012-12-06 16:30:57 +0800 | [diff] [blame^] | 729 | # Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME | 
 | 730 | # This is used for service_check when all the screen_it are called finished | 
 | 731 | # init_service_check | 
 | 732 | function init_service_check() { | 
 | 733 |     SCREEN_NAME=${SCREEN_NAME:-stack} | 
 | 734 |     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
 | 735 |  | 
 | 736 |     if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then | 
 | 737 |         mkdir -p "$SERVICE_DIR/$SCREEN_NAME" | 
 | 738 |     fi | 
 | 739 |  | 
 | 740 |     rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure | 
 | 741 | } | 
 | 742 |  | 
 | 743 | # Helper to get the status of each running service | 
 | 744 | # service_check | 
 | 745 | function service_check() { | 
 | 746 |     local service | 
 | 747 |     local failures | 
 | 748 |     SCREEN_NAME=${SCREEN_NAME:-stack} | 
 | 749 |     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
 | 750 |  | 
 | 751 |  | 
 | 752 |     if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then | 
 | 753 |         echo "No service status directory found" | 
 | 754 |         return | 
 | 755 |     fi | 
 | 756 |  | 
 | 757 |     # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME | 
 | 758 |     failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null` | 
 | 759 |  | 
 | 760 |     for service in $failures; do | 
 | 761 |         service=`basename $service` | 
 | 762 |         service=${service::-8} | 
 | 763 |         echo "Error: Service $service is not running" | 
 | 764 |     done | 
 | 765 |  | 
 | 766 |     if [ -n "$failures" ]; then | 
 | 767 |         echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh" | 
 | 768 |     fi | 
 | 769 | } | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 770 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 771 | # ``pip install`` the dependencies of the package before ``setup.py develop`` | 
 | 772 | # so pip and not distutils processes the dependency chain | 
 | 773 | # Uses globals ``TRACK_DEPENDES``, ``*_proxy` | 
| Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 774 | # setup_develop directory | 
 | 775 | function setup_develop() { | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 776 |     if [[ $TRACK_DEPENDS = True ]] ; then | 
 | 777 |         SUDO_CMD="env" | 
 | 778 |     else | 
 | 779 |         SUDO_CMD="sudo" | 
 | 780 |     fi | 
| Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 781 |     (cd $1; \ | 
 | 782 |         python setup.py egg_info; \ | 
 | 783 |         raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \ | 
 | 784 |         depend_links=$(echo $raw_links | xargs); \ | 
| Dean Troyer | 1a3c9fe | 2012-09-29 17:25:02 -0500 | [diff] [blame] | 785 |         require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \ | 
 | 786 |         pip_install $require_file $depend_links; \ | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 787 |         $SUDO_CMD \ | 
| Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 788 |             HTTP_PROXY=$http_proxy \ | 
 | 789 |             HTTPS_PROXY=$https_proxy \ | 
| Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 790 |             NO_PROXY=$no_proxy \ | 
| Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 791 |             python setup.py develop \ | 
 | 792 |     ) | 
 | 793 | } | 
 | 794 |  | 
 | 795 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 796 | # Service wrapper to start services | 
 | 797 | # start_service service-name | 
 | 798 | function start_service() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 799 |     if is_ubuntu; then | 
| Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 800 |         sudo /usr/sbin/service $1 start | 
 | 801 |     else | 
 | 802 |         sudo /sbin/service $1 start | 
 | 803 |     fi | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 804 | } | 
 | 805 |  | 
 | 806 |  | 
 | 807 | # Service wrapper to stop services | 
 | 808 | # stop_service service-name | 
 | 809 | function stop_service() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 810 |     if is_ubuntu; then | 
| Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 811 |         sudo /usr/sbin/service $1 stop | 
 | 812 |     else | 
 | 813 |         sudo /sbin/service $1 stop | 
 | 814 |     fi | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 815 | } | 
 | 816 |  | 
 | 817 |  | 
 | 818 | # Normalize config values to True or False | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 819 | # Accepts as False: 0 no false False FALSE | 
 | 820 | # Accepts as True: 1 yes true True TRUE | 
 | 821 | # VAR=$(trueorfalse default-value test-value) | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 822 | function trueorfalse() { | 
 | 823 |     local default=$1 | 
 | 824 |     local testval=$2 | 
 | 825 |  | 
 | 826 |     [[ -z "$testval" ]] && { echo "$default"; return; } | 
 | 827 |     [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } | 
 | 828 |     [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } | 
 | 829 |     echo "$default" | 
 | 830 | } | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 831 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 832 |  | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 833 | # Retrieve an image from a URL and upload into Glance | 
 | 834 | # Uses the following variables: | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 835 | #   ``FILES`` must be set to the cache dir | 
 | 836 | #   ``GLANCE_HOSTPORT`` | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 837 | # upload_image image-url glance-token | 
 | 838 | function upload_image() { | 
 | 839 |     local image_url=$1 | 
 | 840 |     local token=$2 | 
 | 841 |  | 
 | 842 |     # Create a directory for the downloaded image tarballs. | 
 | 843 |     mkdir -p $FILES/images | 
 | 844 |  | 
 | 845 |     # Downloads the image (uec ami+aki style), then extracts it. | 
 | 846 |     IMAGE_FNAME=`basename "$image_url"` | 
 | 847 |     if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then | 
 | 848 |         wget -c $image_url -O $FILES/$IMAGE_FNAME | 
 | 849 |         if [[ $? -ne 0 ]]; then | 
 | 850 |             echo "Not found: $image_url" | 
 | 851 |             return | 
 | 852 |         fi | 
 | 853 |     fi | 
 | 854 |  | 
 | 855 |     # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading | 
 | 856 |     if [[ "$image_url" =~ 'openvz' ]]; then | 
 | 857 |         IMAGE="$FILES/${IMAGE_FNAME}" | 
 | 858 |         IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" | 
 | 859 |         glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format ami --disk-format ami < "${IMAGE}" | 
 | 860 |         return | 
 | 861 |     fi | 
 | 862 |  | 
 | 863 |     KERNEL="" | 
 | 864 |     RAMDISK="" | 
 | 865 |     DISK_FORMAT="" | 
 | 866 |     CONTAINER_FORMAT="" | 
 | 867 |     UNPACK="" | 
 | 868 |     case "$IMAGE_FNAME" in | 
 | 869 |         *.tar.gz|*.tgz) | 
 | 870 |             # Extract ami and aki files | 
 | 871 |             [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] && | 
 | 872 |                 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" || | 
 | 873 |                 IMAGE_NAME="${IMAGE_FNAME%.tgz}" | 
 | 874 |             xdir="$FILES/images/$IMAGE_NAME" | 
 | 875 |             rm -Rf "$xdir"; | 
 | 876 |             mkdir "$xdir" | 
 | 877 |             tar -zxf $FILES/$IMAGE_FNAME -C "$xdir" | 
 | 878 |             KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do | 
 | 879 |                      [ -f "$f" ] && echo "$f" && break; done; true) | 
 | 880 |             RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do | 
 | 881 |                      [ -f "$f" ] && echo "$f" && break; done; true) | 
 | 882 |             IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do | 
 | 883 |                      [ -f "$f" ] && echo "$f" && break; done; true) | 
 | 884 |             if [[ -z "$IMAGE_NAME" ]]; then | 
 | 885 |                 IMAGE_NAME=$(basename "$IMAGE" ".img") | 
 | 886 |             fi | 
 | 887 |             ;; | 
 | 888 |         *.img) | 
 | 889 |             IMAGE="$FILES/$IMAGE_FNAME"; | 
 | 890 |             IMAGE_NAME=$(basename "$IMAGE" ".img") | 
| Dean Troyer | 636a3ff | 2012-09-14 11:36:07 -0500 | [diff] [blame] | 891 |             format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }') | 
 | 892 |             if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then | 
 | 893 |                 DISK_FORMAT=$format | 
 | 894 |             else | 
 | 895 |                 DISK_FORMAT=raw | 
 | 896 |             fi | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 897 |             CONTAINER_FORMAT=bare | 
 | 898 |             ;; | 
 | 899 |         *.img.gz) | 
 | 900 |             IMAGE="$FILES/${IMAGE_FNAME}" | 
 | 901 |             IMAGE_NAME=$(basename "$IMAGE" ".img.gz") | 
 | 902 |             DISK_FORMAT=raw | 
 | 903 |             CONTAINER_FORMAT=bare | 
 | 904 |             UNPACK=zcat | 
 | 905 |             ;; | 
 | 906 |         *.qcow2) | 
 | 907 |             IMAGE="$FILES/${IMAGE_FNAME}" | 
 | 908 |             IMAGE_NAME=$(basename "$IMAGE" ".qcow2") | 
 | 909 |             DISK_FORMAT=qcow2 | 
 | 910 |             CONTAINER_FORMAT=bare | 
 | 911 |             ;; | 
 | 912 |         *) echo "Do not know what to do with $IMAGE_FNAME"; false;; | 
 | 913 |     esac | 
 | 914 |  | 
 | 915 |     if [ "$CONTAINER_FORMAT" = "bare" ]; then | 
 | 916 |         if [ "$UNPACK" = "zcat" ]; then | 
 | 917 |             glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}") | 
 | 918 |         else | 
 | 919 |             glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}" | 
 | 920 |         fi | 
 | 921 |     else | 
 | 922 |         # Use glance client to add the kernel the root filesystem. | 
 | 923 |         # We parse the results of the first upload to get the glance ID of the | 
 | 924 |         # kernel for use when uploading the root filesystem. | 
 | 925 |         KERNEL_ID=""; RAMDISK_ID=""; | 
 | 926 |         if [ -n "$KERNEL" ]; then | 
 | 927 |             KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" --public --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2) | 
 | 928 |         fi | 
 | 929 |         if [ -n "$RAMDISK" ]; then | 
 | 930 |             RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" --public --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2) | 
 | 931 |         fi | 
 | 932 |         glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" --public --container-format ami --disk-format ami ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}" | 
 | 933 |     fi | 
 | 934 | } | 
 | 935 |  | 
| Dean Troyer | c1b486a | 2012-11-05 14:26:09 -0600 | [diff] [blame] | 936 | # Set the database backend to use | 
 | 937 | # When called from stackrc/localrc DATABASE_BACKENDS has not been | 
 | 938 | # initialized yet, just save the configuration selection and call back later | 
 | 939 | # to validate it. | 
 | 940 | #  $1 The name of the database backend to use (mysql, postgresql, ...) | 
 | 941 | function use_database { | 
 | 942 |     if [[ -z "$DATABASE_BACKENDS" ]]; then | 
 | 943 |         # The backends haven't initialized yet, just save the selection for now | 
 | 944 |         DATABASE_TYPE=$1 | 
 | 945 |         return | 
 | 946 |     fi | 
 | 947 |     use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1 && return 0 | 
 | 948 |     ret=$? | 
 | 949 |     return $ret | 
 | 950 | } | 
 | 951 |  | 
| Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 952 | # Toggle enable/disable_service for services that must run exclusive of each other | 
 | 953 | #  $1 The name of a variable containing a space-separated list of services | 
 | 954 | #  $2 The name of a variable in which to store the enabled service's name | 
 | 955 | #  $3 The name of the service to enable | 
 | 956 | function use_exclusive_service { | 
 | 957 |     local options=${!1} | 
 | 958 |     local selection=$3 | 
 | 959 |     out=$2 | 
 | 960 |     [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1 | 
 | 961 |     for opt in $options;do | 
 | 962 |         [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt | 
 | 963 |     done | 
 | 964 |     eval "$out=$selection" | 
 | 965 |     return 0 | 
 | 966 | } | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 967 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 968 | # Wrapper for ``yum`` to set proxy environment variables | 
 | 969 | # Uses globals ``OFFLINE``, ``*_proxy` | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 970 | # yum_install package [package ...] | 
 | 971 | function yum_install() { | 
 | 972 |     [[ "$OFFLINE" = "True" ]] && return | 
 | 973 |     local sudo="sudo" | 
 | 974 |     [[ "$(id -u)" = "0" ]] && sudo="env" | 
 | 975 |     $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ | 
| Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 976 |         no_proxy=$no_proxy \ | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 977 |         yum install -y "$@" | 
 | 978 | } | 
 | 979 |  | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 980 | # ping check | 
 | 981 | # Uses globals ``ENABLED_SERVICES`` | 
 | 982 | function ping_check() { | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 983 |     if is_service_enabled quantum; then | 
 | 984 |         _ping_check_quantum  "$1" $2 $3 $4 | 
 | 985 |         return | 
 | 986 |     fi | 
 | 987 |     _ping_check_novanet "$1" $2 $3 $4 | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 988 | } | 
 | 989 |  | 
 | 990 | # ping check for nova | 
 | 991 | # Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK`` | 
 | 992 | function _ping_check_novanet() { | 
 | 993 |     local from_net=$1 | 
 | 994 |     local ip=$2 | 
 | 995 |     local boot_timeout=$3 | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 996 |     local expected=${4:-"True"} | 
 | 997 |     local check_command="" | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 998 |     MULTI_HOST=`trueorfalse False $MULTI_HOST` | 
 | 999 |     if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then | 
 | 1000 |         sleep $boot_timeout | 
 | 1001 |         return | 
 | 1002 |     fi | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1003 |     if [[ "$expected" = "True" ]]; then | 
 | 1004 |         check_command="while ! ping -c1 -w1 $ip; do sleep 1; done" | 
 | 1005 |     else | 
 | 1006 |         check_command="while ping -c1 -w1 $ip; do sleep 1; done" | 
 | 1007 |     fi | 
 | 1008 |     if ! timeout $boot_timeout sh -c "$check_command"; then | 
 | 1009 |         if [[ "$expected" = "True" ]]; then | 
 | 1010 |             echo "[Fail] Couldn't ping server" | 
 | 1011 |         else | 
 | 1012 |             echo "[Fail] Could ping server" | 
 | 1013 |         fi | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1014 |         exit 1 | 
 | 1015 |     fi | 
 | 1016 | } | 
 | 1017 |  | 
 | 1018 | # ssh check | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1019 |  | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1020 | function ssh_check() { | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1021 |     if is_service_enabled quantum; then | 
 | 1022 |         _ssh_check_quantum  "$1" $2 $3 $4 $5 | 
 | 1023 |         return | 
 | 1024 |     fi | 
 | 1025 |     _ssh_check_novanet "$1" $2 $3 $4 $5 | 
 | 1026 | } | 
 | 1027 |  | 
 | 1028 | function _ssh_check_novanet() { | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1029 |     local NET_NAME=$1 | 
 | 1030 |     local KEY_FILE=$2 | 
 | 1031 |     local FLOATING_IP=$3 | 
 | 1032 |     local DEFAULT_INSTANCE_USER=$4 | 
 | 1033 |     local ACTIVE_TIMEOUT=$5 | 
| Dean Troyer | 6931c13 | 2012-11-07 16:51:21 -0600 | [diff] [blame] | 1034 |     local probe_cmd="" | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1035 |     if ! timeout $ACTIVE_TIMEOUT sh -c "while ! ssh -o StrictHostKeyChecking=no -i $KEY_FILE ${DEFAULT_INSTANCE_USER}@$FLOATING_IP echo success ; do sleep 1; done"; then | 
 | 1036 |         echo "server didn't become ssh-able!" | 
 | 1037 |         exit 1 | 
 | 1038 |     fi | 
 | 1039 | } | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 1040 |  | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1041 |  | 
 | 1042 | # zypper wrapper to set arguments correctly | 
 | 1043 | # zypper_install package [package ...] | 
 | 1044 | function zypper_install() { | 
 | 1045 |     [[ "$OFFLINE" = "True" ]] && return | 
 | 1046 |     local sudo="sudo" | 
 | 1047 |     [[ "$(id -u)" = "0" ]] && sudo="env" | 
 | 1048 |     $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ | 
 | 1049 |         zypper --non-interactive install --auto-agree-with-licenses "$@" | 
 | 1050 | } | 
 | 1051 |  | 
 | 1052 |  | 
 | 1053 | # Add a user to a group. | 
 | 1054 | # add_user_to_group user group | 
 | 1055 | function add_user_to_group() { | 
 | 1056 |     local user=$1 | 
 | 1057 |     local group=$2 | 
 | 1058 |  | 
 | 1059 |     if [[ -z "$os_VENDOR" ]]; then | 
 | 1060 |         GetOSVersion | 
 | 1061 |     fi | 
 | 1062 |  | 
 | 1063 |     # SLE11 and openSUSE 12.2 don't have the usual usermod | 
 | 1064 |     if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then | 
 | 1065 |         sudo usermod -a -G "$group" "$user" | 
 | 1066 |     else | 
 | 1067 |         sudo usermod -A "$group" "$user" | 
 | 1068 |     fi | 
 | 1069 | } | 
 | 1070 |  | 
 | 1071 |  | 
 | 1072 | # Get the location of the $module-rootwrap executables, where module is cinder | 
 | 1073 | # or nova. | 
 | 1074 | # get_rootwrap_location module | 
 | 1075 | function get_rootwrap_location() { | 
 | 1076 |     local module=$1 | 
 | 1077 |  | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 1078 |     if is_ubuntu || is_suse; then | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1079 |         echo "/usr/local/bin/$module-rootwrap" | 
 | 1080 |     else | 
 | 1081 |         echo "/usr/bin/$module-rootwrap" | 
 | 1082 |     fi | 
 | 1083 | } | 
 | 1084 |  | 
| Vincent Untz | 8ec2722 | 2012-11-29 09:25:31 +0100 | [diff] [blame] | 1085 | # Get the path to the pip command. | 
 | 1086 | # get_pip_command | 
 | 1087 | function get_pip_command() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 1088 |     if is_ubuntu || is_suse; then | 
| Vincent Untz | 8ec2722 | 2012-11-29 09:25:31 +0100 | [diff] [blame] | 1089 |         echo "/usr/bin/pip" | 
 | 1090 |     else | 
 | 1091 |         echo "/usr/bin/pip-python" | 
 | 1092 |     fi | 
 | 1093 | } | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1094 |  | 
 | 1095 | # Check if qpid can be used on the current distro. | 
 | 1096 | # qpid_is_supported | 
 | 1097 | function qpid_is_supported() { | 
 | 1098 |     if [[ -z "$DISTRO" ]]; then | 
 | 1099 |         GetDistro | 
 | 1100 |     fi | 
 | 1101 |  | 
 | 1102 |     # Qpid was introduced to Ubuntu in precise, disallow it on oneiric; it is | 
 | 1103 |     # not in openSUSE either right now. | 
| Russell Bryant | c2d2f52 | 2012-12-03 10:02:40 -0500 | [diff] [blame] | 1104 |     ( ! ([[ "$DISTRO" = "oneiric" ]] || is_suse) ) | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1105 |     return $? | 
 | 1106 | } | 
 | 1107 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 1108 | # Restore xtrace | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 1109 | $XTRACE | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 1110 |  | 
 | 1111 |  | 
 | 1112 | # Local variables: | 
 | 1113 | # -*- mode: Shell-script -*- | 
| Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 1114 | # End: |