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