Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 1 | # -*- mode: Shell-script -*- |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 2 | # functions - Common functions used by DevStack components |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 3 | # |
| 4 | # ENABLED_SERVICES is used by is_service_enabled() |
| 5 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 6 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 7 | # Save trace setting |
| 8 | XTRACE=$(set +o | grep xtrace) |
| 9 | set +o xtrace |
| 10 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 11 | |
Vishvananda Ishaya | c9ad14b | 2012-07-03 20:29:01 +0000 | [diff] [blame] | 12 | # Exit 0 if address is in network or 1 if |
| 13 | # address is not in network or netaddr library |
| 14 | # is not installed. |
| 15 | function address_in_net() { |
| 16 | python -c " |
| 17 | import netaddr |
| 18 | import sys |
| 19 | sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2')) |
| 20 | " |
| 21 | } |
| 22 | |
| 23 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 24 | # apt-get wrapper to set arguments correctly |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 25 | # apt_get operation package [package ...] |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 26 | function apt_get() { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 27 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 28 | local sudo="sudo" |
| 29 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 30 | $sudo DEBIAN_FRONTEND=noninteractive \ |
| 31 | http_proxy=$http_proxy https_proxy=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 32 | no_proxy=$no_proxy \ |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 33 | apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" |
| 34 | } |
| 35 | |
| 36 | |
| 37 | # Gracefully cp only if source file/dir exists |
| 38 | # cp_it source destination |
| 39 | function cp_it { |
| 40 | if [ -e $1 ] || [ -d $1 ]; then |
| 41 | cp -pRL $1 $2 |
| 42 | fi |
| 43 | } |
| 44 | |
| 45 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 46 | # Prints "message" and exits |
| 47 | # die "message" |
| 48 | function die() { |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 49 | local exitcode=$? |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 50 | set +o xtrace |
| 51 | echo $@ |
| 52 | exit $exitcode |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | # Checks an environment variable is not set or has length 0 OR if the |
| 57 | # exit code is non-zero and prints "message" and exits |
| 58 | # NOTE: env-var is the variable name without a '$' |
| 59 | # die_if_not_set env-var "message" |
| 60 | function die_if_not_set() { |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 61 | ( |
| 62 | local exitcode=$? |
| 63 | set +o xtrace |
| 64 | local evar=$1; shift |
| 65 | if ! is_set $evar || [ $exitcode != 0 ]; then |
| 66 | set +o xtrace |
| 67 | echo $@ |
| 68 | exit -1 |
| 69 | fi |
| 70 | ) |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | # Grab a numbered field from python prettytable output |
| 75 | # Fields are numbered starting with 1 |
| 76 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. |
| 77 | # get_field field-number |
| 78 | function get_field() { |
| 79 | while read data; do |
| 80 | if [ "$1" -lt 0 ]; then |
| 81 | field="(\$(NF$1))" |
| 82 | else |
| 83 | field="\$$(($1 + 1))" |
| 84 | fi |
| 85 | echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" |
| 86 | done |
| 87 | } |
| 88 | |
| 89 | |
Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 90 | # get_packages() collects a list of package names of any type from the |
| 91 | # prerequisite files in ``files/{apts|pips}``. The list is intended |
| 92 | # to be passed to a package installer such as apt or pip. |
| 93 | # |
| 94 | # Only packages required for the services in ENABLED_SERVICES will be |
| 95 | # included. Two bits of metadata are recognized in the prerequisite files: |
| 96 | # - ``# NOPRIME`` defers installation to be performed later in stack.sh |
| 97 | # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection |
| 98 | # of the package to the distros listed. The distro names are case insensitive. |
| 99 | # |
| 100 | # get_packages dir |
| 101 | function get_packages() { |
| 102 | local package_dir=$1 |
| 103 | local file_to_parse |
| 104 | local service |
| 105 | |
| 106 | if [[ -z "$package_dir" ]]; then |
| 107 | echo "No package directory supplied" |
| 108 | return 1 |
| 109 | fi |
| 110 | if [[ -z "$DISTRO" ]]; then |
| 111 | echo "No distro set in DISTRO" |
| 112 | return 1 |
| 113 | fi |
| 114 | for service in general ${ENABLED_SERVICES//,/ }; do |
| 115 | # Allow individual services to specify dependencies |
| 116 | if [[ -e ${package_dir}/${service} ]]; then |
| 117 | file_to_parse="${file_to_parse} $service" |
| 118 | fi |
| 119 | # NOTE(sdague) n-api needs glance for now because that's where |
| 120 | # glance client is |
| 121 | if [[ $service == n-api ]]; then |
| 122 | if [[ ! $file_to_parse =~ nova ]]; then |
| 123 | file_to_parse="${file_to_parse} nova" |
| 124 | fi |
| 125 | if [[ ! $file_to_parse =~ glance ]]; then |
| 126 | file_to_parse="${file_to_parse} glance" |
| 127 | fi |
| 128 | elif [[ $service == c-* ]]; then |
| 129 | if [[ ! $file_to_parse =~ cinder ]]; then |
| 130 | file_to_parse="${file_to_parse} cinder" |
| 131 | fi |
John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame^] | 132 | elif [[ $service == ceilometer-* ]]; then |
| 133 | if [[ ! $file_to_parse =~ ceilometer ]]; then |
| 134 | file_to_parse="${file_to_parse} ceilometer" |
| 135 | fi |
Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 136 | elif [[ $service == n-* ]]; then |
| 137 | if [[ ! $file_to_parse =~ nova ]]; then |
| 138 | file_to_parse="${file_to_parse} nova" |
| 139 | fi |
| 140 | elif [[ $service == g-* ]]; then |
| 141 | if [[ ! $file_to_parse =~ glance ]]; then |
| 142 | file_to_parse="${file_to_parse} glance" |
| 143 | fi |
| 144 | elif [[ $service == key* ]]; then |
| 145 | if [[ ! $file_to_parse =~ keystone ]]; then |
| 146 | file_to_parse="${file_to_parse} keystone" |
| 147 | fi |
| 148 | fi |
| 149 | done |
| 150 | |
| 151 | for file in ${file_to_parse}; do |
| 152 | local fname=${package_dir}/${file} |
| 153 | local OIFS line package distros distro |
| 154 | [[ -e $fname ]] || continue |
| 155 | |
| 156 | OIFS=$IFS |
| 157 | IFS=$'\n' |
| 158 | for line in $(<${fname}); do |
| 159 | if [[ $line =~ "NOPRIME" ]]; then |
| 160 | continue |
| 161 | fi |
| 162 | |
| 163 | if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then |
| 164 | # We are using BASH regexp matching feature. |
| 165 | package=${BASH_REMATCH[1]} |
| 166 | distros=${BASH_REMATCH[2]} |
| 167 | # In bash ${VAR,,} will lowecase VAR |
| 168 | [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package |
| 169 | continue |
| 170 | fi |
| 171 | |
| 172 | echo ${line%#*} |
| 173 | done |
| 174 | IFS=$OIFS |
| 175 | done |
| 176 | } |
| 177 | |
| 178 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 179 | # Determine OS Vendor, Release and Update |
| 180 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora |
| 181 | # Returns results in global variables: |
| 182 | # os_VENDOR - vendor name |
| 183 | # os_RELEASE - release |
| 184 | # os_UPDATE - update |
| 185 | # os_PACKAGE - package type |
| 186 | # os_CODENAME - vendor's codename for release |
| 187 | # GetOSVersion |
| 188 | GetOSVersion() { |
| 189 | # Figure out which vendor we are |
| 190 | if [[ -n "`which sw_vers 2>/dev/null`" ]]; then |
| 191 | # OS/X |
| 192 | os_VENDOR=`sw_vers -productName` |
| 193 | os_RELEASE=`sw_vers -productVersion` |
| 194 | os_UPDATE=${os_RELEASE##*.} |
| 195 | os_RELEASE=${os_RELEASE%.*} |
| 196 | os_PACKAGE="" |
| 197 | if [[ "$os_RELEASE" =~ "10.7" ]]; then |
| 198 | os_CODENAME="lion" |
| 199 | elif [[ "$os_RELEASE" =~ "10.6" ]]; then |
| 200 | os_CODENAME="snow leopard" |
| 201 | elif [[ "$os_RELEASE" =~ "10.5" ]]; then |
| 202 | os_CODENAME="leopard" |
| 203 | elif [[ "$os_RELEASE" =~ "10.4" ]]; then |
| 204 | os_CODENAME="tiger" |
| 205 | elif [[ "$os_RELEASE" =~ "10.3" ]]; then |
| 206 | os_CODENAME="panther" |
| 207 | else |
| 208 | os_CODENAME="" |
| 209 | fi |
| 210 | elif [[ -x $(which lsb_release 2>/dev/null) ]]; then |
| 211 | os_VENDOR=$(lsb_release -i -s) |
| 212 | os_RELEASE=$(lsb_release -r -s) |
| 213 | os_UPDATE="" |
| 214 | if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then |
| 215 | os_PACKAGE="deb" |
| 216 | else |
| 217 | os_PACKAGE="rpm" |
| 218 | fi |
| 219 | os_CODENAME=$(lsb_release -c -s) |
| 220 | elif [[ -r /etc/redhat-release ]]; then |
| 221 | # Red Hat Enterprise Linux Server release 5.5 (Tikanga) |
| 222 | # CentOS release 5.5 (Final) |
| 223 | # CentOS Linux release 6.0 (Final) |
| 224 | # Fedora release 16 (Verne) |
| 225 | os_CODENAME="" |
| 226 | for r in "Red Hat" CentOS Fedora; do |
| 227 | os_VENDOR=$r |
| 228 | if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then |
| 229 | ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` |
| 230 | os_CODENAME=${ver#*|} |
| 231 | os_RELEASE=${ver%|*} |
| 232 | os_UPDATE=${os_RELEASE##*.} |
| 233 | os_RELEASE=${os_RELEASE%.*} |
| 234 | break |
| 235 | fi |
| 236 | os_VENDOR="" |
| 237 | done |
| 238 | os_PACKAGE="rpm" |
| 239 | fi |
| 240 | export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME |
| 241 | } |
| 242 | |
| 243 | |
Dean Troyer | a9e0a48 | 2012-07-09 14:07:23 -0500 | [diff] [blame] | 244 | # Translate the OS version values into common nomenclature |
| 245 | # Sets ``DISTRO`` from the ``os_*`` values |
| 246 | function GetDistro() { |
| 247 | GetOSVersion |
| 248 | if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then |
| 249 | # 'Everyone' refers to Ubuntu releases by the code name adjective |
| 250 | DISTRO=$os_CODENAME |
| 251 | elif [[ "$os_VENDOR" =~ (Fedora) ]]; then |
| 252 | # For Fedora, just use 'f' and the release |
| 253 | DISTRO="f$os_RELEASE" |
| 254 | else |
| 255 | # Catch-all for now is Vendor + Release + Update |
| 256 | DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" |
| 257 | fi |
| 258 | export DISTRO |
| 259 | } |
| 260 | |
| 261 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 262 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 263 | # be owned by the installation user, we create the directory and change the |
| 264 | # ownership to the proper user. |
| 265 | # 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] | 266 | # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo |
| 267 | # does not exist (default is False, meaning the repo will be cloned). |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 268 | # git_clone remote dest-dir branch |
| 269 | function git_clone { |
| 270 | [[ "$OFFLINE" = "True" ]] && return |
| 271 | |
| 272 | GIT_REMOTE=$1 |
| 273 | GIT_DEST=$2 |
| 274 | GIT_BRANCH=$3 |
| 275 | |
| 276 | if echo $GIT_BRANCH | egrep -q "^refs"; then |
| 277 | # If our branch name is a gerrit style refs/changes/... |
| 278 | if [[ ! -d $GIT_DEST ]]; then |
James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 279 | [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 280 | git clone $GIT_REMOTE $GIT_DEST |
| 281 | fi |
| 282 | cd $GIT_DEST |
| 283 | git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD |
| 284 | else |
| 285 | # do a full clone only if the directory doesn't exist |
| 286 | if [[ ! -d $GIT_DEST ]]; then |
James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 287 | [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 288 | git clone $GIT_REMOTE $GIT_DEST |
| 289 | cd $GIT_DEST |
| 290 | # This checkout syntax works for both branches and tags |
| 291 | git checkout $GIT_BRANCH |
| 292 | elif [[ "$RECLONE" == "yes" ]]; then |
| 293 | # if it does exist then simulate what clone does if asked to RECLONE |
| 294 | cd $GIT_DEST |
| 295 | # set the url to pull from and fetch |
| 296 | git remote set-url origin $GIT_REMOTE |
| 297 | git fetch origin |
| 298 | # remove the existing ignored files (like pyc) as they cause breakage |
| 299 | # (due to the py files having older timestamps than our pyc, so python |
| 300 | # thinks the pyc files are correct using them) |
| 301 | find $GIT_DEST -name '*.pyc' -delete |
| 302 | git checkout -f origin/$GIT_BRANCH |
| 303 | # a local branch might not exist |
| 304 | git branch -D $GIT_BRANCH || true |
| 305 | git checkout -b $GIT_BRANCH |
| 306 | fi |
| 307 | fi |
| 308 | } |
| 309 | |
| 310 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 311 | # Comment an option in an INI file |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 312 | # inicomment config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 313 | function inicomment() { |
| 314 | local file=$1 |
| 315 | local section=$2 |
| 316 | local option=$3 |
| 317 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file |
| 318 | } |
| 319 | |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 320 | # Uncomment an option in an INI file |
| 321 | # iniuncomment config-file section option |
| 322 | function iniuncomment() { |
| 323 | local file=$1 |
| 324 | local section=$2 |
| 325 | local option=$3 |
| 326 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file |
| 327 | } |
| 328 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 329 | |
| 330 | # Get an option from an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 331 | # iniget config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 332 | function iniget() { |
| 333 | local file=$1 |
| 334 | local section=$2 |
| 335 | local option=$3 |
| 336 | local line |
| 337 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file) |
| 338 | echo ${line#*=} |
| 339 | } |
| 340 | |
| 341 | |
| 342 | # Set an option in an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 343 | # iniset config-file section option value |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 344 | function iniset() { |
| 345 | local file=$1 |
| 346 | local section=$2 |
| 347 | local option=$3 |
| 348 | local value=$4 |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 349 | if ! grep -q "^\[$section\]" $file; then |
| 350 | # Add section at the end |
| 351 | echo -e "\n[$section]" >>$file |
| 352 | fi |
| 353 | if [[ -z "$(iniget $file $section $option)" ]]; then |
| 354 | # Add it |
| 355 | sed -i -e "/^\[$section\]/ a\\ |
| 356 | $option = $value |
| 357 | " $file |
| 358 | else |
| 359 | # Replace it |
| 360 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file |
| 361 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 365 | # is_service_enabled() checks if the service(s) specified as arguments are |
| 366 | # enabled by the user in **ENABLED_SERVICES**. |
| 367 | # |
| 368 | # If there are multiple services specified as arguments the test performs a |
| 369 | # boolean OR or if any of the services specified on the command line |
| 370 | # return true. |
| 371 | # |
| 372 | # There is a special cases for some 'catch-all' services:: |
| 373 | # **nova** returns true if any service enabled start with **n-** |
| 374 | # **glance** returns true if any service enabled start with **g-** |
| 375 | # **quantum** returns true if any service enabled start with **q-** |
| 376 | function is_service_enabled() { |
| 377 | services=$@ |
| 378 | for service in ${services}; do |
| 379 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 380 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 |
Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 381 | [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0 |
John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame^] | 382 | [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0 |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 383 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 |
| 384 | [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 |
| 385 | done |
| 386 | return 1 |
| 387 | } |
| 388 | |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 389 | # remove extra commas from the input string (ENABLED_SERVICES) |
| 390 | function _cleanup_service_list () { |
| 391 | echo "$1" | sed -e ' |
| 392 | s/,,/,/g; |
| 393 | s/^,//; |
| 394 | s/,$// |
| 395 | ' |
| 396 | } |
| 397 | |
| 398 | # enable_service() adds the services passed as argument to the |
| 399 | # **ENABLED_SERVICES** list, if they are not already present. |
| 400 | # |
| 401 | # For example: |
| 402 | # |
| 403 | # enable_service n-vol |
| 404 | # |
| 405 | # This function does not know about the special cases |
| 406 | # for nova, glance, and quantum built into is_service_enabled(). |
| 407 | function enable_service() { |
| 408 | local tmpsvcs="${ENABLED_SERVICES}" |
| 409 | for service in $@; do |
| 410 | if ! is_service_enabled $service; then |
| 411 | tmpsvcs+=",$service" |
| 412 | fi |
| 413 | done |
| 414 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 415 | disable_negated_services |
| 416 | } |
| 417 | |
| 418 | # disable_service() removes the services passed as argument to the |
| 419 | # **ENABLED_SERVICES** list, if they are present. |
| 420 | # |
| 421 | # For example: |
| 422 | # |
| 423 | # disable_service n-vol |
| 424 | # |
| 425 | # This function does not know about the special cases |
| 426 | # for nova, glance, and quantum built into is_service_enabled(). |
| 427 | function disable_service() { |
| 428 | local tmpsvcs=",${ENABLED_SERVICES}," |
| 429 | local service |
| 430 | for service in $@; do |
| 431 | if is_service_enabled $service; then |
| 432 | tmpsvcs=${tmpsvcs//,$service,/,} |
| 433 | fi |
| 434 | done |
| 435 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 436 | } |
| 437 | |
| 438 | # disable_all_services() removes all current services |
| 439 | # from **ENABLED_SERVICES** to reset the configuration |
| 440 | # before a minimal installation |
| 441 | function disable_all_services() { |
| 442 | ENABLED_SERVICES="" |
| 443 | } |
| 444 | |
| 445 | # We are looking for services with a - at the beginning to force |
| 446 | # excluding those services. For example if you want to install all the default |
| 447 | # services but not nova-volume (n-vol) you can have this set in your localrc : |
| 448 | # ENABLED_SERVICES+=",-n-vol" |
| 449 | function disable_negated_services() { |
| 450 | local tmpsvcs="${ENABLED_SERVICES}" |
| 451 | local service |
| 452 | for service in ${tmpsvcs//,/ }; do |
| 453 | if [[ ${service} == -* ]]; then |
| 454 | tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g") |
| 455 | fi |
| 456 | done |
| 457 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 458 | } |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 459 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 460 | # Distro-agnostic package installer |
| 461 | # install_package package [package ...] |
| 462 | function install_package() { |
| 463 | if [[ -z "$os_PACKAGE" ]]; then |
| 464 | GetOSVersion |
| 465 | fi |
| 466 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 467 | apt_get install "$@" |
| 468 | else |
| 469 | yum_install "$@" |
| 470 | fi |
| 471 | } |
| 472 | |
| 473 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 474 | # Test if the named environment variable is set and not zero length |
| 475 | # is_set env-var |
| 476 | function is_set() { |
| 477 | local var=\$"$1" |
| 478 | if eval "[ -z $var ]"; then |
| 479 | return 1 |
| 480 | fi |
| 481 | return 0 |
| 482 | } |
| 483 | |
| 484 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 485 | # pip install wrapper to set cache and proxy environment variables |
| 486 | # pip_install package [package ...] |
| 487 | function pip_install { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 488 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 489 | if [[ -z "$os_PACKAGE" ]]; then |
| 490 | GetOSVersion |
| 491 | fi |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 492 | if [[ $TRACK_DEPENDS = True ]] ; then |
| 493 | source $DEST/.venv/bin/activate |
| 494 | CMD_PIP=$DEST/.venv/bin/pip |
| 495 | SUDO_PIP="env" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 496 | else |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 497 | SUDO_PIP="sudo" |
| 498 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 499 | CMD_PIP=/usr/bin/pip |
| 500 | else |
| 501 | CMD_PIP=/usr/bin/pip-python |
| 502 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 503 | fi |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 504 | $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \ |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 505 | HTTP_PROXY=$http_proxy \ |
| 506 | HTTPS_PROXY=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 507 | NO_PROXY=$no_proxy \ |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 508 | $CMD_PIP install --use-mirrors $@ |
| 509 | } |
| 510 | |
| 511 | |
| 512 | # Service wrapper to restart services |
| 513 | # restart_service service-name |
| 514 | function restart_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 515 | if [[ -z "$os_PACKAGE" ]]; then |
| 516 | GetOSVersion |
| 517 | fi |
| 518 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 519 | sudo /usr/sbin/service $1 restart |
| 520 | else |
| 521 | sudo /sbin/service $1 restart |
| 522 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 526 | # pip install the dependencies of the package before we do the setup.py |
| 527 | # develop, so that pip and not distutils process the dependency chain |
| 528 | # setup_develop directory |
| 529 | function setup_develop() { |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 530 | if [[ $TRACK_DEPENDS = True ]] ; then |
| 531 | SUDO_CMD="env" |
| 532 | else |
| 533 | SUDO_CMD="sudo" |
| 534 | fi |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 535 | (cd $1; \ |
| 536 | python setup.py egg_info; \ |
| 537 | raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \ |
| 538 | depend_links=$(echo $raw_links | xargs); \ |
| 539 | pip_install -r *-info/requires.txt $depend_links; \ |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 540 | $SUDO_CMD \ |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 541 | HTTP_PROXY=$http_proxy \ |
| 542 | HTTPS_PROXY=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 543 | NO_PROXY=$no_proxy \ |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 544 | python setup.py develop \ |
| 545 | ) |
| 546 | } |
| 547 | |
| 548 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 549 | # Service wrapper to start services |
| 550 | # start_service service-name |
| 551 | function start_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 552 | if [[ -z "$os_PACKAGE" ]]; then |
| 553 | GetOSVersion |
| 554 | fi |
| 555 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 556 | sudo /usr/sbin/service $1 start |
| 557 | else |
| 558 | sudo /sbin/service $1 start |
| 559 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | |
| 563 | # Service wrapper to stop services |
| 564 | # stop_service service-name |
| 565 | function stop_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 566 | if [[ -z "$os_PACKAGE" ]]; then |
| 567 | GetOSVersion |
| 568 | fi |
| 569 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 570 | sudo /usr/sbin/service $1 stop |
| 571 | else |
| 572 | sudo /sbin/service $1 stop |
| 573 | fi |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
| 577 | # Normalize config values to True or False |
| 578 | # VAR=`trueorfalse default-value test-value` |
| 579 | function trueorfalse() { |
| 580 | local default=$1 |
| 581 | local testval=$2 |
| 582 | |
| 583 | [[ -z "$testval" ]] && { echo "$default"; return; } |
| 584 | [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } |
| 585 | [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } |
| 586 | echo "$default" |
| 587 | } |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 588 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 589 | |
| 590 | # yum wrapper to set arguments correctly |
| 591 | # yum_install package [package ...] |
| 592 | function yum_install() { |
| 593 | [[ "$OFFLINE" = "True" ]] && return |
| 594 | local sudo="sudo" |
| 595 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 596 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 597 | no_proxy=$no_proxy \ |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 598 | yum install -y "$@" |
| 599 | } |
| 600 | |
| 601 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 602 | # Restore xtrace |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 603 | $XTRACE |