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