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 | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 76 | # Determine OS Vendor, Release and Update |
| 77 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora |
| 78 | # Returns results in global variables: |
| 79 | # os_VENDOR - vendor name |
| 80 | # os_RELEASE - release |
| 81 | # os_UPDATE - update |
| 82 | # os_PACKAGE - package type |
| 83 | # os_CODENAME - vendor's codename for release |
| 84 | # GetOSVersion |
| 85 | GetOSVersion() { |
| 86 | # Figure out which vendor we are |
| 87 | if [[ -n "`which sw_vers 2>/dev/null`" ]]; then |
| 88 | # OS/X |
| 89 | os_VENDOR=`sw_vers -productName` |
| 90 | os_RELEASE=`sw_vers -productVersion` |
| 91 | os_UPDATE=${os_RELEASE##*.} |
| 92 | os_RELEASE=${os_RELEASE%.*} |
| 93 | os_PACKAGE="" |
| 94 | if [[ "$os_RELEASE" =~ "10.7" ]]; then |
| 95 | os_CODENAME="lion" |
| 96 | elif [[ "$os_RELEASE" =~ "10.6" ]]; then |
| 97 | os_CODENAME="snow leopard" |
| 98 | elif [[ "$os_RELEASE" =~ "10.5" ]]; then |
| 99 | os_CODENAME="leopard" |
| 100 | elif [[ "$os_RELEASE" =~ "10.4" ]]; then |
| 101 | os_CODENAME="tiger" |
| 102 | elif [[ "$os_RELEASE" =~ "10.3" ]]; then |
| 103 | os_CODENAME="panther" |
| 104 | else |
| 105 | os_CODENAME="" |
| 106 | fi |
| 107 | elif [[ -x $(which lsb_release 2>/dev/null) ]]; then |
| 108 | os_VENDOR=$(lsb_release -i -s) |
| 109 | os_RELEASE=$(lsb_release -r -s) |
| 110 | os_UPDATE="" |
| 111 | if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then |
| 112 | os_PACKAGE="deb" |
| 113 | else |
| 114 | os_PACKAGE="rpm" |
| 115 | fi |
| 116 | os_CODENAME=$(lsb_release -c -s) |
| 117 | elif [[ -r /etc/redhat-release ]]; then |
| 118 | # Red Hat Enterprise Linux Server release 5.5 (Tikanga) |
| 119 | # CentOS release 5.5 (Final) |
| 120 | # CentOS Linux release 6.0 (Final) |
| 121 | # Fedora release 16 (Verne) |
| 122 | os_CODENAME="" |
| 123 | for r in "Red Hat" CentOS Fedora; do |
| 124 | os_VENDOR=$r |
| 125 | if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then |
| 126 | ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` |
| 127 | os_CODENAME=${ver#*|} |
| 128 | os_RELEASE=${ver%|*} |
| 129 | os_UPDATE=${os_RELEASE##*.} |
| 130 | os_RELEASE=${os_RELEASE%.*} |
| 131 | break |
| 132 | fi |
| 133 | os_VENDOR="" |
| 134 | done |
| 135 | os_PACKAGE="rpm" |
| 136 | fi |
| 137 | export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME |
| 138 | } |
| 139 | |
| 140 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 141 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 142 | # be owned by the installation user, we create the directory and change the |
| 143 | # ownership to the proper user. |
| 144 | # 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^] | 145 | # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo |
| 146 | # does not exist (default is False, meaning the repo will be cloned). |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 147 | # git_clone remote dest-dir branch |
| 148 | function git_clone { |
| 149 | [[ "$OFFLINE" = "True" ]] && return |
| 150 | |
| 151 | GIT_REMOTE=$1 |
| 152 | GIT_DEST=$2 |
| 153 | GIT_BRANCH=$3 |
| 154 | |
| 155 | if echo $GIT_BRANCH | egrep -q "^refs"; then |
| 156 | # If our branch name is a gerrit style refs/changes/... |
| 157 | if [[ ! -d $GIT_DEST ]]; then |
James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame^] | 158 | [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 159 | git clone $GIT_REMOTE $GIT_DEST |
| 160 | fi |
| 161 | cd $GIT_DEST |
| 162 | git fetch $GIT_REMOTE $GIT_BRANCH && git checkout FETCH_HEAD |
| 163 | else |
| 164 | # do a full clone only if the directory doesn't exist |
| 165 | if [[ ! -d $GIT_DEST ]]; then |
James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame^] | 166 | [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 167 | git clone $GIT_REMOTE $GIT_DEST |
| 168 | cd $GIT_DEST |
| 169 | # This checkout syntax works for both branches and tags |
| 170 | git checkout $GIT_BRANCH |
| 171 | elif [[ "$RECLONE" == "yes" ]]; then |
| 172 | # if it does exist then simulate what clone does if asked to RECLONE |
| 173 | cd $GIT_DEST |
| 174 | # set the url to pull from and fetch |
| 175 | git remote set-url origin $GIT_REMOTE |
| 176 | git fetch origin |
| 177 | # remove the existing ignored files (like pyc) as they cause breakage |
| 178 | # (due to the py files having older timestamps than our pyc, so python |
| 179 | # thinks the pyc files are correct using them) |
| 180 | find $GIT_DEST -name '*.pyc' -delete |
| 181 | git checkout -f origin/$GIT_BRANCH |
| 182 | # a local branch might not exist |
| 183 | git branch -D $GIT_BRANCH || true |
| 184 | git checkout -b $GIT_BRANCH |
| 185 | fi |
| 186 | fi |
| 187 | } |
| 188 | |
| 189 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 190 | # Comment an option in an INI file |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 191 | # inicomment config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 192 | function inicomment() { |
| 193 | local file=$1 |
| 194 | local section=$2 |
| 195 | local option=$3 |
| 196 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file |
| 197 | } |
| 198 | |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 199 | # Uncomment an option in an INI file |
| 200 | # iniuncomment config-file section option |
| 201 | function iniuncomment() { |
| 202 | local file=$1 |
| 203 | local section=$2 |
| 204 | local option=$3 |
| 205 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file |
| 206 | } |
| 207 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 208 | |
| 209 | # Get an option from an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 210 | # iniget config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 211 | function iniget() { |
| 212 | local file=$1 |
| 213 | local section=$2 |
| 214 | local option=$3 |
| 215 | local line |
| 216 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file) |
| 217 | echo ${line#*=} |
| 218 | } |
| 219 | |
| 220 | |
| 221 | # Set an option in an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 222 | # iniset config-file section option value |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 223 | function iniset() { |
| 224 | local file=$1 |
| 225 | local section=$2 |
| 226 | local option=$3 |
| 227 | local value=$4 |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 228 | if ! grep -q "^\[$section\]" $file; then |
| 229 | # Add section at the end |
| 230 | echo -e "\n[$section]" >>$file |
| 231 | fi |
| 232 | if [[ -z "$(iniget $file $section $option)" ]]; then |
| 233 | # Add it |
| 234 | sed -i -e "/^\[$section\]/ a\\ |
| 235 | $option = $value |
| 236 | " $file |
| 237 | else |
| 238 | # Replace it |
| 239 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file |
| 240 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 244 | # is_service_enabled() checks if the service(s) specified as arguments are |
| 245 | # enabled by the user in **ENABLED_SERVICES**. |
| 246 | # |
| 247 | # If there are multiple services specified as arguments the test performs a |
| 248 | # boolean OR or if any of the services specified on the command line |
| 249 | # return true. |
| 250 | # |
| 251 | # There is a special cases for some 'catch-all' services:: |
| 252 | # **nova** returns true if any service enabled start with **n-** |
| 253 | # **glance** returns true if any service enabled start with **g-** |
| 254 | # **quantum** returns true if any service enabled start with **q-** |
| 255 | function is_service_enabled() { |
| 256 | services=$@ |
| 257 | for service in ${services}; do |
| 258 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 259 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 |
Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 260 | [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0 |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 261 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 |
| 262 | [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 |
| 263 | done |
| 264 | return 1 |
| 265 | } |
| 266 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 267 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 268 | # Distro-agnostic package installer |
| 269 | # install_package package [package ...] |
| 270 | function install_package() { |
| 271 | if [[ -z "$os_PACKAGE" ]]; then |
| 272 | GetOSVersion |
| 273 | fi |
| 274 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 275 | apt_get install "$@" |
| 276 | else |
| 277 | yum_install "$@" |
| 278 | fi |
| 279 | } |
| 280 | |
| 281 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 282 | # Test if the named environment variable is set and not zero length |
| 283 | # is_set env-var |
| 284 | function is_set() { |
| 285 | local var=\$"$1" |
| 286 | if eval "[ -z $var ]"; then |
| 287 | return 1 |
| 288 | fi |
| 289 | return 0 |
| 290 | } |
| 291 | |
| 292 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 293 | # pip install wrapper to set cache and proxy environment variables |
| 294 | # pip_install package [package ...] |
| 295 | function pip_install { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 296 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 297 | if [[ -z "$os_PACKAGE" ]]; then |
| 298 | GetOSVersion |
| 299 | fi |
| 300 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 301 | CMD_PIP=/usr/bin/pip |
| 302 | else |
| 303 | CMD_PIP=/usr/bin/pip-python |
| 304 | fi |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 305 | sudo PIP_DOWNLOAD_CACHE=/var/cache/pip \ |
| 306 | HTTP_PROXY=$http_proxy \ |
| 307 | HTTPS_PROXY=$https_proxy \ |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 308 | $CMD_PIP install --use-mirrors $@ |
| 309 | } |
| 310 | |
| 311 | |
| 312 | # Service wrapper to restart services |
| 313 | # restart_service service-name |
| 314 | function restart_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 315 | if [[ -z "$os_PACKAGE" ]]; then |
| 316 | GetOSVersion |
| 317 | fi |
| 318 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 319 | sudo /usr/sbin/service $1 restart |
| 320 | else |
| 321 | sudo /sbin/service $1 restart |
| 322 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 326 | # pip install the dependencies of the package before we do the setup.py |
| 327 | # develop, so that pip and not distutils process the dependency chain |
| 328 | # setup_develop directory |
| 329 | function setup_develop() { |
| 330 | (cd $1; \ |
| 331 | python setup.py egg_info; \ |
| 332 | raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \ |
| 333 | depend_links=$(echo $raw_links | xargs); \ |
| 334 | pip_install -r *-info/requires.txt $depend_links; \ |
| 335 | sudo \ |
| 336 | HTTP_PROXY=$http_proxy \ |
| 337 | HTTPS_PROXY=$https_proxy \ |
| 338 | python setup.py develop \ |
| 339 | ) |
| 340 | } |
| 341 | |
| 342 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 343 | # Service wrapper to start services |
| 344 | # start_service service-name |
| 345 | function start_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 346 | if [[ -z "$os_PACKAGE" ]]; then |
| 347 | GetOSVersion |
| 348 | fi |
| 349 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 350 | sudo /usr/sbin/service $1 start |
| 351 | else |
| 352 | sudo /sbin/service $1 start |
| 353 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
| 357 | # Service wrapper to stop services |
| 358 | # stop_service service-name |
| 359 | function stop_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 360 | if [[ -z "$os_PACKAGE" ]]; then |
| 361 | GetOSVersion |
| 362 | fi |
| 363 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 364 | sudo /usr/sbin/service $1 stop |
| 365 | else |
| 366 | sudo /sbin/service $1 stop |
| 367 | fi |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
| 371 | # Normalize config values to True or False |
| 372 | # VAR=`trueorfalse default-value test-value` |
| 373 | function trueorfalse() { |
| 374 | local default=$1 |
| 375 | local testval=$2 |
| 376 | |
| 377 | [[ -z "$testval" ]] && { echo "$default"; return; } |
| 378 | [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } |
| 379 | [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } |
| 380 | echo "$default" |
| 381 | } |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 382 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 383 | |
| 384 | # yum wrapper to set arguments correctly |
| 385 | # yum_install package [package ...] |
| 386 | function yum_install() { |
| 387 | [[ "$OFFLINE" = "True" ]] && return |
| 388 | local sudo="sudo" |
| 389 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 390 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ |
| 391 | yum install -y "$@" |
| 392 | } |
| 393 | |
| 394 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 395 | # Restore xtrace |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 396 | $XTRACE |