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