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