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 | |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 243 | # git update using reference as a branch. |
| 244 | function git_update_branch() { |
| 245 | |
| 246 | GIT_BRANCH=$1 |
| 247 | |
| 248 | git checkout -f origin/$GIT_BRANCH |
| 249 | # a local branch might not exist |
| 250 | git branch -D $GIT_BRANCH || true |
| 251 | git checkout -b $GIT_BRANCH |
| 252 | } |
| 253 | |
| 254 | |
| 255 | # git update using reference as a tag. Be careful editing source at that repo |
| 256 | # as working copy will be in a detached mode |
| 257 | function git_update_tag() { |
| 258 | |
| 259 | GIT_TAG=$1 |
| 260 | |
| 261 | git tag -d $GIT_TAG |
| 262 | # fetching given tag only |
| 263 | git fetch origin tag $GIT_TAG |
| 264 | git checkout -f $GIT_TAG |
| 265 | } |
| 266 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 267 | |
Dean Troyer | a9e0a48 | 2012-07-09 14:07:23 -0500 | [diff] [blame] | 268 | # Translate the OS version values into common nomenclature |
| 269 | # Sets ``DISTRO`` from the ``os_*`` values |
| 270 | function GetDistro() { |
| 271 | GetOSVersion |
| 272 | if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then |
| 273 | # 'Everyone' refers to Ubuntu releases by the code name adjective |
| 274 | DISTRO=$os_CODENAME |
| 275 | elif [[ "$os_VENDOR" =~ (Fedora) ]]; then |
| 276 | # For Fedora, just use 'f' and the release |
| 277 | DISTRO="f$os_RELEASE" |
| 278 | else |
| 279 | # Catch-all for now is Vendor + Release + Update |
| 280 | DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" |
| 281 | fi |
| 282 | export DISTRO |
| 283 | } |
| 284 | |
| 285 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 286 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 287 | # be owned by the installation user, we create the directory and change the |
| 288 | # ownership to the proper user. |
| 289 | # 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] | 290 | # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo |
| 291 | # does not exist (default is False, meaning the repo will be cloned). |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 292 | # git_clone remote dest-dir branch |
| 293 | function git_clone { |
| 294 | [[ "$OFFLINE" = "True" ]] && return |
| 295 | |
| 296 | GIT_REMOTE=$1 |
| 297 | GIT_DEST=$2 |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 298 | GIT_REF=$3 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 299 | |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 300 | if echo $GIT_REF | egrep -q "^refs"; then |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 301 | # If our branch name is a gerrit style refs/changes/... |
| 302 | if [[ ! -d $GIT_DEST ]]; then |
James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 303 | [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 304 | git clone $GIT_REMOTE $GIT_DEST |
| 305 | fi |
| 306 | cd $GIT_DEST |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 307 | git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 308 | else |
| 309 | # do a full clone only if the directory doesn't exist |
| 310 | if [[ ! -d $GIT_DEST ]]; then |
James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 311 | [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 312 | git clone $GIT_REMOTE $GIT_DEST |
| 313 | cd $GIT_DEST |
| 314 | # This checkout syntax works for both branches and tags |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 315 | git checkout $GIT_REF |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 316 | elif [[ "$RECLONE" == "yes" ]]; then |
| 317 | # if it does exist then simulate what clone does if asked to RECLONE |
| 318 | cd $GIT_DEST |
| 319 | # set the url to pull from and fetch |
| 320 | git remote set-url origin $GIT_REMOTE |
| 321 | git fetch origin |
| 322 | # remove the existing ignored files (like pyc) as they cause breakage |
| 323 | # (due to the py files having older timestamps than our pyc, so python |
| 324 | # thinks the pyc files are correct using them) |
| 325 | find $GIT_DEST -name '*.pyc' -delete |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 326 | |
| 327 | # handle GIT_REF accordingly to type (tag, branch) |
| 328 | if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then |
| 329 | git_update_tag $GIT_REF |
| 330 | elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then |
| 331 | git_update_branch $GIT_REF |
| 332 | else |
| 333 | echo $GIT_REF is neither branch nor tag |
| 334 | exit 1 |
| 335 | fi |
| 336 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 337 | fi |
| 338 | fi |
| 339 | } |
| 340 | |
| 341 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 342 | # Comment an option in an INI file |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 343 | # inicomment config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 344 | function inicomment() { |
| 345 | local file=$1 |
| 346 | local section=$2 |
| 347 | local option=$3 |
| 348 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file |
| 349 | } |
| 350 | |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 351 | # Uncomment an option in an INI file |
| 352 | # iniuncomment config-file section option |
| 353 | function iniuncomment() { |
| 354 | local file=$1 |
| 355 | local section=$2 |
| 356 | local option=$3 |
| 357 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file |
| 358 | } |
| 359 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 360 | |
| 361 | # Get an option from an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 362 | # iniget config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 363 | function iniget() { |
| 364 | local file=$1 |
| 365 | local section=$2 |
| 366 | local option=$3 |
| 367 | local line |
| 368 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file) |
| 369 | echo ${line#*=} |
| 370 | } |
| 371 | |
| 372 | |
| 373 | # Set an option in an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 374 | # iniset config-file section option value |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 375 | function iniset() { |
| 376 | local file=$1 |
| 377 | local section=$2 |
| 378 | local option=$3 |
| 379 | local value=$4 |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 380 | if ! grep -q "^\[$section\]" $file; then |
| 381 | # Add section at the end |
| 382 | echo -e "\n[$section]" >>$file |
| 383 | fi |
| 384 | if [[ -z "$(iniget $file $section $option)" ]]; then |
| 385 | # Add it |
| 386 | sed -i -e "/^\[$section\]/ a\\ |
| 387 | $option = $value |
| 388 | " $file |
| 389 | else |
| 390 | # Replace it |
| 391 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file |
| 392 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 396 | # is_service_enabled() checks if the service(s) specified as arguments are |
| 397 | # enabled by the user in **ENABLED_SERVICES**. |
| 398 | # |
| 399 | # If there are multiple services specified as arguments the test performs a |
| 400 | # boolean OR or if any of the services specified on the command line |
| 401 | # return true. |
| 402 | # |
| 403 | # There is a special cases for some 'catch-all' services:: |
| 404 | # **nova** returns true if any service enabled start with **n-** |
| 405 | # **glance** returns true if any service enabled start with **g-** |
| 406 | # **quantum** returns true if any service enabled start with **q-** |
| 407 | function is_service_enabled() { |
| 408 | services=$@ |
| 409 | for service in ${services}; do |
| 410 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 411 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 |
Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 412 | [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0 |
John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame] | 413 | [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0 |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 414 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 |
| 415 | [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 |
| 416 | done |
| 417 | return 1 |
| 418 | } |
| 419 | |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 420 | # remove extra commas from the input string (ENABLED_SERVICES) |
| 421 | function _cleanup_service_list () { |
Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 422 | echo "$1" | sed -e ' |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 423 | s/,,/,/g; |
| 424 | s/^,//; |
| 425 | s/,$// |
| 426 | ' |
| 427 | } |
| 428 | |
| 429 | # enable_service() adds the services passed as argument to the |
| 430 | # **ENABLED_SERVICES** list, if they are not already present. |
| 431 | # |
| 432 | # For example: |
| 433 | # |
| 434 | # enable_service n-vol |
| 435 | # |
| 436 | # This function does not know about the special cases |
| 437 | # for nova, glance, and quantum built into is_service_enabled(). |
| 438 | function enable_service() { |
| 439 | local tmpsvcs="${ENABLED_SERVICES}" |
| 440 | for service in $@; do |
| 441 | if ! is_service_enabled $service; then |
| 442 | tmpsvcs+=",$service" |
| 443 | fi |
| 444 | done |
| 445 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 446 | disable_negated_services |
| 447 | } |
| 448 | |
| 449 | # disable_service() removes the services passed as argument to the |
| 450 | # **ENABLED_SERVICES** list, if they are present. |
| 451 | # |
| 452 | # For example: |
| 453 | # |
| 454 | # disable_service n-vol |
| 455 | # |
| 456 | # This function does not know about the special cases |
| 457 | # for nova, glance, and quantum built into is_service_enabled(). |
| 458 | function disable_service() { |
| 459 | local tmpsvcs=",${ENABLED_SERVICES}," |
| 460 | local service |
| 461 | for service in $@; do |
| 462 | if is_service_enabled $service; then |
| 463 | tmpsvcs=${tmpsvcs//,$service,/,} |
| 464 | fi |
| 465 | done |
| 466 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 467 | } |
| 468 | |
| 469 | # disable_all_services() removes all current services |
| 470 | # from **ENABLED_SERVICES** to reset the configuration |
| 471 | # before a minimal installation |
| 472 | function disable_all_services() { |
| 473 | ENABLED_SERVICES="" |
| 474 | } |
| 475 | |
| 476 | # We are looking for services with a - at the beginning to force |
| 477 | # excluding those services. For example if you want to install all the default |
| 478 | # services but not nova-volume (n-vol) you can have this set in your localrc : |
| 479 | # ENABLED_SERVICES+=",-n-vol" |
| 480 | function disable_negated_services() { |
| 481 | local tmpsvcs="${ENABLED_SERVICES}" |
| 482 | local service |
| 483 | for service in ${tmpsvcs//,/ }; do |
| 484 | if [[ ${service} == -* ]]; then |
| 485 | tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g") |
| 486 | fi |
| 487 | done |
| 488 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 489 | } |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 490 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 491 | # Distro-agnostic package installer |
| 492 | # install_package package [package ...] |
| 493 | function install_package() { |
| 494 | if [[ -z "$os_PACKAGE" ]]; then |
| 495 | GetOSVersion |
| 496 | fi |
| 497 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 498 | apt_get install "$@" |
| 499 | else |
| 500 | yum_install "$@" |
| 501 | fi |
| 502 | } |
| 503 | |
| 504 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 505 | # Test if the named environment variable is set and not zero length |
| 506 | # is_set env-var |
| 507 | function is_set() { |
| 508 | local var=\$"$1" |
| 509 | if eval "[ -z $var ]"; then |
| 510 | return 1 |
| 511 | fi |
| 512 | return 0 |
| 513 | } |
| 514 | |
| 515 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 516 | # pip install wrapper to set cache and proxy environment variables |
| 517 | # pip_install package [package ...] |
| 518 | function pip_install { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 519 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 520 | if [[ -z "$os_PACKAGE" ]]; then |
| 521 | GetOSVersion |
| 522 | fi |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 523 | if [[ $TRACK_DEPENDS = True ]] ; then |
| 524 | source $DEST/.venv/bin/activate |
| 525 | CMD_PIP=$DEST/.venv/bin/pip |
| 526 | SUDO_PIP="env" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 527 | else |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 528 | SUDO_PIP="sudo" |
| 529 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 530 | CMD_PIP=/usr/bin/pip |
| 531 | else |
| 532 | CMD_PIP=/usr/bin/pip-python |
| 533 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 534 | fi |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 535 | $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \ |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 536 | HTTP_PROXY=$http_proxy \ |
| 537 | HTTPS_PROXY=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 538 | NO_PROXY=$no_proxy \ |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 539 | $CMD_PIP install --use-mirrors $@ |
| 540 | } |
| 541 | |
| 542 | |
| 543 | # Service wrapper to restart services |
| 544 | # restart_service service-name |
| 545 | function restart_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 546 | if [[ -z "$os_PACKAGE" ]]; then |
| 547 | GetOSVersion |
| 548 | fi |
| 549 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 550 | sudo /usr/sbin/service $1 restart |
| 551 | else |
| 552 | sudo /sbin/service $1 restart |
| 553 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 557 | # pip install the dependencies of the package before we do the setup.py |
| 558 | # develop, so that pip and not distutils process the dependency chain |
| 559 | # setup_develop directory |
| 560 | function setup_develop() { |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 561 | if [[ $TRACK_DEPENDS = True ]] ; then |
| 562 | SUDO_CMD="env" |
| 563 | else |
| 564 | SUDO_CMD="sudo" |
| 565 | fi |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 566 | (cd $1; \ |
| 567 | python setup.py egg_info; \ |
| 568 | raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \ |
| 569 | depend_links=$(echo $raw_links | xargs); \ |
| 570 | pip_install -r *-info/requires.txt $depend_links; \ |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 571 | $SUDO_CMD \ |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 572 | HTTP_PROXY=$http_proxy \ |
| 573 | HTTPS_PROXY=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 574 | NO_PROXY=$no_proxy \ |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 575 | python setup.py develop \ |
| 576 | ) |
| 577 | } |
| 578 | |
| 579 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 580 | # Service wrapper to start services |
| 581 | # start_service service-name |
| 582 | function start_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 583 | if [[ -z "$os_PACKAGE" ]]; then |
| 584 | GetOSVersion |
| 585 | fi |
| 586 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 587 | sudo /usr/sbin/service $1 start |
| 588 | else |
| 589 | sudo /sbin/service $1 start |
| 590 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
| 594 | # Service wrapper to stop services |
| 595 | # stop_service service-name |
| 596 | function stop_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 597 | if [[ -z "$os_PACKAGE" ]]; then |
| 598 | GetOSVersion |
| 599 | fi |
| 600 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 601 | sudo /usr/sbin/service $1 stop |
| 602 | else |
| 603 | sudo /sbin/service $1 stop |
| 604 | fi |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | |
| 608 | # Normalize config values to True or False |
| 609 | # VAR=`trueorfalse default-value test-value` |
| 610 | function trueorfalse() { |
| 611 | local default=$1 |
| 612 | local testval=$2 |
| 613 | |
| 614 | [[ -z "$testval" ]] && { echo "$default"; return; } |
| 615 | [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } |
| 616 | [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } |
| 617 | echo "$default" |
| 618 | } |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 619 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 620 | |
Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 621 | # Retrieve an image from a URL and upload into Glance |
| 622 | # Uses the following variables: |
| 623 | # **FILES** must be set to the cache dir |
| 624 | # **GLANCE_HOSTPORT** |
| 625 | # upload_image image-url glance-token |
| 626 | function upload_image() { |
| 627 | local image_url=$1 |
| 628 | local token=$2 |
| 629 | |
| 630 | # Create a directory for the downloaded image tarballs. |
| 631 | mkdir -p $FILES/images |
| 632 | |
| 633 | # Downloads the image (uec ami+aki style), then extracts it. |
| 634 | IMAGE_FNAME=`basename "$image_url"` |
| 635 | if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then |
| 636 | wget -c $image_url -O $FILES/$IMAGE_FNAME |
| 637 | if [[ $? -ne 0 ]]; then |
| 638 | echo "Not found: $image_url" |
| 639 | return |
| 640 | fi |
| 641 | fi |
| 642 | |
| 643 | # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading |
| 644 | if [[ "$image_url" =~ 'openvz' ]]; then |
| 645 | IMAGE="$FILES/${IMAGE_FNAME}" |
| 646 | IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" |
| 647 | 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}" |
| 648 | return |
| 649 | fi |
| 650 | |
| 651 | KERNEL="" |
| 652 | RAMDISK="" |
| 653 | DISK_FORMAT="" |
| 654 | CONTAINER_FORMAT="" |
| 655 | UNPACK="" |
| 656 | case "$IMAGE_FNAME" in |
| 657 | *.tar.gz|*.tgz) |
| 658 | # Extract ami and aki files |
| 659 | [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] && |
| 660 | IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" || |
| 661 | IMAGE_NAME="${IMAGE_FNAME%.tgz}" |
| 662 | xdir="$FILES/images/$IMAGE_NAME" |
| 663 | rm -Rf "$xdir"; |
| 664 | mkdir "$xdir" |
| 665 | tar -zxf $FILES/$IMAGE_FNAME -C "$xdir" |
| 666 | KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do |
| 667 | [ -f "$f" ] && echo "$f" && break; done; true) |
| 668 | RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do |
| 669 | [ -f "$f" ] && echo "$f" && break; done; true) |
| 670 | IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do |
| 671 | [ -f "$f" ] && echo "$f" && break; done; true) |
| 672 | if [[ -z "$IMAGE_NAME" ]]; then |
| 673 | IMAGE_NAME=$(basename "$IMAGE" ".img") |
| 674 | fi |
| 675 | ;; |
| 676 | *.img) |
| 677 | IMAGE="$FILES/$IMAGE_FNAME"; |
| 678 | IMAGE_NAME=$(basename "$IMAGE" ".img") |
| 679 | DISK_FORMAT=raw |
| 680 | CONTAINER_FORMAT=bare |
| 681 | ;; |
| 682 | *.img.gz) |
| 683 | IMAGE="$FILES/${IMAGE_FNAME}" |
| 684 | IMAGE_NAME=$(basename "$IMAGE" ".img.gz") |
| 685 | DISK_FORMAT=raw |
| 686 | CONTAINER_FORMAT=bare |
| 687 | UNPACK=zcat |
| 688 | ;; |
| 689 | *.qcow2) |
| 690 | IMAGE="$FILES/${IMAGE_FNAME}" |
| 691 | IMAGE_NAME=$(basename "$IMAGE" ".qcow2") |
| 692 | DISK_FORMAT=qcow2 |
| 693 | CONTAINER_FORMAT=bare |
| 694 | ;; |
| 695 | *) echo "Do not know what to do with $IMAGE_FNAME"; false;; |
| 696 | esac |
| 697 | |
| 698 | if [ "$CONTAINER_FORMAT" = "bare" ]; then |
| 699 | if [ "$UNPACK" = "zcat" ]; then |
| 700 | 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}") |
| 701 | else |
| 702 | 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}" |
| 703 | fi |
| 704 | else |
| 705 | # Use glance client to add the kernel the root filesystem. |
| 706 | # We parse the results of the first upload to get the glance ID of the |
| 707 | # kernel for use when uploading the root filesystem. |
| 708 | KERNEL_ID=""; RAMDISK_ID=""; |
| 709 | if [ -n "$KERNEL" ]; then |
| 710 | 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) |
| 711 | fi |
| 712 | if [ -n "$RAMDISK" ]; then |
| 713 | 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) |
| 714 | fi |
| 715 | 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}" |
| 716 | fi |
| 717 | } |
| 718 | |
| 719 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 720 | # yum wrapper to set arguments correctly |
| 721 | # yum_install package [package ...] |
| 722 | function yum_install() { |
| 723 | [[ "$OFFLINE" = "True" ]] && return |
| 724 | local sudo="sudo" |
| 725 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 726 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 727 | no_proxy=$no_proxy \ |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 728 | yum install -y "$@" |
| 729 | } |
| 730 | |
| 731 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 732 | # Restore xtrace |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 733 | $XTRACE |