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 | # |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 3 | # The following variables are assumed to be defined by certain functions: |
| 4 | # ``DISTRO`` |
| 5 | # ``ENABLED_SERVICES`` |
| 6 | # ``EROR_ON_CLONE`` |
| 7 | # ``FILES`` |
| 8 | # ``GLANCE_HOSTPORT`` |
| 9 | # ``OFFLINE`` |
| 10 | # ``PIP_DOWNLOAD_CACHE`` |
| 11 | # ``RECLONE`` |
| 12 | # ``TRACK_DEPENDS`` |
| 13 | # ``http_proxy``, ``https_proxy``, ``no_proxy`` |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 14 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 15 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 16 | # Save trace setting |
| 17 | XTRACE=$(set +o | grep xtrace) |
| 18 | set +o xtrace |
| 19 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 20 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 21 | # Exit 0 if address is in network or 1 if address is not in |
| 22 | # network or netaddr library is not installed. |
| 23 | # address_in_net ip-address ip-range |
Vishvananda Ishaya | c9ad14b | 2012-07-03 20:29:01 +0000 | [diff] [blame] | 24 | function address_in_net() { |
| 25 | python -c " |
| 26 | import netaddr |
| 27 | import sys |
| 28 | sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2')) |
| 29 | " |
| 30 | } |
| 31 | |
| 32 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 33 | # Wrapper for ``apt-get`` to set cache and proxy environment variables |
| 34 | # Uses globals ``OFFLINE``, ``*_proxy` |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 35 | # apt_get operation package [package ...] |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 36 | function apt_get() { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 37 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 38 | local sudo="sudo" |
| 39 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 40 | $sudo DEBIAN_FRONTEND=noninteractive \ |
| 41 | http_proxy=$http_proxy https_proxy=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 42 | no_proxy=$no_proxy \ |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 43 | apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" |
| 44 | } |
| 45 | |
| 46 | |
| 47 | # Gracefully cp only if source file/dir exists |
| 48 | # cp_it source destination |
| 49 | function cp_it { |
| 50 | if [ -e $1 ] || [ -d $1 ]; then |
| 51 | cp -pRL $1 $2 |
| 52 | fi |
| 53 | } |
| 54 | |
| 55 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 56 | # Prints "message" and exits |
| 57 | # die "message" |
| 58 | function die() { |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 59 | local exitcode=$? |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 60 | set +o xtrace |
| 61 | echo $@ |
| 62 | exit $exitcode |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
| 66 | # Checks an environment variable is not set or has length 0 OR if the |
| 67 | # exit code is non-zero and prints "message" and exits |
| 68 | # NOTE: env-var is the variable name without a '$' |
| 69 | # die_if_not_set env-var "message" |
| 70 | function die_if_not_set() { |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 71 | ( |
| 72 | local exitcode=$? |
| 73 | set +o xtrace |
| 74 | local evar=$1; shift |
| 75 | if ! is_set $evar || [ $exitcode != 0 ]; then |
| 76 | set +o xtrace |
| 77 | echo $@ |
| 78 | exit -1 |
| 79 | fi |
| 80 | ) |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
| 84 | # Grab a numbered field from python prettytable output |
| 85 | # Fields are numbered starting with 1 |
| 86 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. |
| 87 | # get_field field-number |
| 88 | function get_field() { |
| 89 | while read data; do |
| 90 | if [ "$1" -lt 0 ]; then |
| 91 | field="(\$(NF$1))" |
| 92 | else |
| 93 | field="\$$(($1 + 1))" |
| 94 | fi |
| 95 | echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" |
| 96 | done |
| 97 | } |
| 98 | |
| 99 | |
Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 100 | # get_packages() collects a list of package names of any type from the |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 101 | # prerequisite files in ``files/{apts|rpms}``. The list is intended |
| 102 | # to be passed to a package installer such as apt or yum. |
Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 103 | # |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 104 | # Only packages required for the services in ``ENABLED_SERVICES`` will be |
Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 105 | # included. Two bits of metadata are recognized in the prerequisite files: |
| 106 | # - ``# NOPRIME`` defers installation to be performed later in stack.sh |
| 107 | # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection |
| 108 | # of the package to the distros listed. The distro names are case insensitive. |
| 109 | # |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 110 | # Uses globals ``DISTRO``, ``ENABLED_SERVICES`` |
Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 111 | # get_packages dir |
| 112 | function get_packages() { |
| 113 | local package_dir=$1 |
| 114 | local file_to_parse |
| 115 | local service |
| 116 | |
| 117 | if [[ -z "$package_dir" ]]; then |
| 118 | echo "No package directory supplied" |
| 119 | return 1 |
| 120 | fi |
| 121 | if [[ -z "$DISTRO" ]]; then |
| 122 | echo "No distro set in DISTRO" |
| 123 | return 1 |
| 124 | fi |
| 125 | for service in general ${ENABLED_SERVICES//,/ }; do |
| 126 | # Allow individual services to specify dependencies |
| 127 | if [[ -e ${package_dir}/${service} ]]; then |
| 128 | file_to_parse="${file_to_parse} $service" |
| 129 | fi |
| 130 | # NOTE(sdague) n-api needs glance for now because that's where |
| 131 | # glance client is |
| 132 | if [[ $service == n-api ]]; then |
| 133 | if [[ ! $file_to_parse =~ nova ]]; then |
| 134 | file_to_parse="${file_to_parse} nova" |
| 135 | fi |
| 136 | if [[ ! $file_to_parse =~ glance ]]; then |
| 137 | file_to_parse="${file_to_parse} glance" |
| 138 | fi |
| 139 | elif [[ $service == c-* ]]; then |
| 140 | if [[ ! $file_to_parse =~ cinder ]]; then |
| 141 | file_to_parse="${file_to_parse} cinder" |
| 142 | fi |
John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame] | 143 | elif [[ $service == ceilometer-* ]]; then |
| 144 | if [[ ! $file_to_parse =~ ceilometer ]]; then |
| 145 | file_to_parse="${file_to_parse} ceilometer" |
| 146 | fi |
Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 147 | elif [[ $service == n-* ]]; then |
| 148 | if [[ ! $file_to_parse =~ nova ]]; then |
| 149 | file_to_parse="${file_to_parse} nova" |
| 150 | fi |
| 151 | elif [[ $service == g-* ]]; then |
| 152 | if [[ ! $file_to_parse =~ glance ]]; then |
| 153 | file_to_parse="${file_to_parse} glance" |
| 154 | fi |
| 155 | elif [[ $service == key* ]]; then |
| 156 | if [[ ! $file_to_parse =~ keystone ]]; then |
| 157 | file_to_parse="${file_to_parse} keystone" |
| 158 | fi |
| 159 | fi |
| 160 | done |
| 161 | |
| 162 | for file in ${file_to_parse}; do |
| 163 | local fname=${package_dir}/${file} |
| 164 | local OIFS line package distros distro |
| 165 | [[ -e $fname ]] || continue |
| 166 | |
| 167 | OIFS=$IFS |
| 168 | IFS=$'\n' |
| 169 | for line in $(<${fname}); do |
| 170 | if [[ $line =~ "NOPRIME" ]]; then |
| 171 | continue |
| 172 | fi |
| 173 | |
| 174 | if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then |
| 175 | # We are using BASH regexp matching feature. |
| 176 | package=${BASH_REMATCH[1]} |
| 177 | distros=${BASH_REMATCH[2]} |
| 178 | # In bash ${VAR,,} will lowecase VAR |
| 179 | [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package |
| 180 | continue |
| 181 | fi |
| 182 | |
| 183 | echo ${line%#*} |
| 184 | done |
| 185 | IFS=$OIFS |
| 186 | done |
| 187 | } |
| 188 | |
| 189 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 190 | # Determine OS Vendor, Release and Update |
| 191 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora |
| 192 | # Returns results in global variables: |
| 193 | # os_VENDOR - vendor name |
| 194 | # os_RELEASE - release |
| 195 | # os_UPDATE - update |
| 196 | # os_PACKAGE - package type |
| 197 | # os_CODENAME - vendor's codename for release |
| 198 | # GetOSVersion |
| 199 | GetOSVersion() { |
| 200 | # Figure out which vendor we are |
| 201 | if [[ -n "`which sw_vers 2>/dev/null`" ]]; then |
| 202 | # OS/X |
| 203 | os_VENDOR=`sw_vers -productName` |
| 204 | os_RELEASE=`sw_vers -productVersion` |
| 205 | os_UPDATE=${os_RELEASE##*.} |
| 206 | os_RELEASE=${os_RELEASE%.*} |
| 207 | os_PACKAGE="" |
| 208 | if [[ "$os_RELEASE" =~ "10.7" ]]; then |
| 209 | os_CODENAME="lion" |
| 210 | elif [[ "$os_RELEASE" =~ "10.6" ]]; then |
| 211 | os_CODENAME="snow leopard" |
| 212 | elif [[ "$os_RELEASE" =~ "10.5" ]]; then |
| 213 | os_CODENAME="leopard" |
| 214 | elif [[ "$os_RELEASE" =~ "10.4" ]]; then |
| 215 | os_CODENAME="tiger" |
| 216 | elif [[ "$os_RELEASE" =~ "10.3" ]]; then |
| 217 | os_CODENAME="panther" |
| 218 | else |
| 219 | os_CODENAME="" |
| 220 | fi |
| 221 | elif [[ -x $(which lsb_release 2>/dev/null) ]]; then |
| 222 | os_VENDOR=$(lsb_release -i -s) |
| 223 | os_RELEASE=$(lsb_release -r -s) |
| 224 | os_UPDATE="" |
| 225 | if [[ "Debian,Ubuntu" =~ $os_VENDOR ]]; then |
| 226 | os_PACKAGE="deb" |
| 227 | else |
| 228 | os_PACKAGE="rpm" |
| 229 | fi |
| 230 | os_CODENAME=$(lsb_release -c -s) |
| 231 | elif [[ -r /etc/redhat-release ]]; then |
| 232 | # Red Hat Enterprise Linux Server release 5.5 (Tikanga) |
| 233 | # CentOS release 5.5 (Final) |
| 234 | # CentOS Linux release 6.0 (Final) |
| 235 | # Fedora release 16 (Verne) |
| 236 | os_CODENAME="" |
| 237 | for r in "Red Hat" CentOS Fedora; do |
| 238 | os_VENDOR=$r |
| 239 | if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then |
| 240 | ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` |
| 241 | os_CODENAME=${ver#*|} |
| 242 | os_RELEASE=${ver%|*} |
| 243 | os_UPDATE=${os_RELEASE##*.} |
| 244 | os_RELEASE=${os_RELEASE%.*} |
| 245 | break |
| 246 | fi |
| 247 | os_VENDOR="" |
| 248 | done |
| 249 | os_PACKAGE="rpm" |
| 250 | fi |
| 251 | export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME |
| 252 | } |
| 253 | |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 254 | # git update using reference as a branch. |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 255 | # git_update_branch ref |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 256 | function git_update_branch() { |
| 257 | |
| 258 | GIT_BRANCH=$1 |
| 259 | |
| 260 | git checkout -f origin/$GIT_BRANCH |
| 261 | # a local branch might not exist |
| 262 | git branch -D $GIT_BRANCH || true |
| 263 | git checkout -b $GIT_BRANCH |
| 264 | } |
| 265 | |
| 266 | |
| 267 | # git update using reference as a tag. Be careful editing source at that repo |
| 268 | # as working copy will be in a detached mode |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 269 | # git_update_tag ref |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 270 | function git_update_tag() { |
| 271 | |
| 272 | GIT_TAG=$1 |
| 273 | |
| 274 | git tag -d $GIT_TAG |
| 275 | # fetching given tag only |
| 276 | git fetch origin tag $GIT_TAG |
| 277 | git checkout -f $GIT_TAG |
| 278 | } |
| 279 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 280 | |
Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 281 | # git update using reference as a branch. |
| 282 | # git_update_remote_branch ref |
| 283 | function git_update_remote_branch() { |
| 284 | |
| 285 | GIT_BRANCH=$1 |
| 286 | |
| 287 | git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH |
| 288 | } |
| 289 | |
| 290 | |
Dean Troyer | a9e0a48 | 2012-07-09 14:07:23 -0500 | [diff] [blame] | 291 | # Translate the OS version values into common nomenclature |
| 292 | # Sets ``DISTRO`` from the ``os_*`` values |
| 293 | function GetDistro() { |
| 294 | GetOSVersion |
| 295 | if [[ "$os_VENDOR" =~ (Ubuntu) ]]; then |
| 296 | # 'Everyone' refers to Ubuntu releases by the code name adjective |
| 297 | DISTRO=$os_CODENAME |
| 298 | elif [[ "$os_VENDOR" =~ (Fedora) ]]; then |
| 299 | # For Fedora, just use 'f' and the release |
| 300 | DISTRO="f$os_RELEASE" |
| 301 | else |
| 302 | # Catch-all for now is Vendor + Release + Update |
| 303 | DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" |
| 304 | fi |
| 305 | export DISTRO |
| 306 | } |
| 307 | |
| 308 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 309 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 310 | # be owned by the installation user, we create the directory and change the |
| 311 | # ownership to the proper user. |
| 312 | # 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] | 313 | # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo |
| 314 | # does not exist (default is False, meaning the repo will be cloned). |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 315 | # Uses global ``OFFLINE`` |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 316 | # git_clone remote dest-dir branch |
| 317 | function git_clone { |
| 318 | [[ "$OFFLINE" = "True" ]] && return |
| 319 | |
| 320 | GIT_REMOTE=$1 |
| 321 | GIT_DEST=$2 |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 322 | GIT_REF=$3 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 323 | |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 324 | if echo $GIT_REF | egrep -q "^refs"; then |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 325 | # If our branch name is a gerrit style refs/changes/... |
| 326 | if [[ ! -d $GIT_DEST ]]; then |
James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 327 | [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 328 | git clone $GIT_REMOTE $GIT_DEST |
| 329 | fi |
| 330 | cd $GIT_DEST |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 331 | git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 332 | else |
| 333 | # do a full clone only if the directory doesn't exist |
| 334 | if [[ ! -d $GIT_DEST ]]; then |
James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 335 | [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 336 | git clone $GIT_REMOTE $GIT_DEST |
| 337 | cd $GIT_DEST |
| 338 | # This checkout syntax works for both branches and tags |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 339 | git checkout $GIT_REF |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 340 | elif [[ "$RECLONE" == "yes" ]]; then |
| 341 | # if it does exist then simulate what clone does if asked to RECLONE |
| 342 | cd $GIT_DEST |
| 343 | # set the url to pull from and fetch |
| 344 | git remote set-url origin $GIT_REMOTE |
| 345 | git fetch origin |
| 346 | # remove the existing ignored files (like pyc) as they cause breakage |
| 347 | # (due to the py files having older timestamps than our pyc, so python |
| 348 | # thinks the pyc files are correct using them) |
| 349 | find $GIT_DEST -name '*.pyc' -delete |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 350 | |
| 351 | # handle GIT_REF accordingly to type (tag, branch) |
| 352 | if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then |
| 353 | git_update_tag $GIT_REF |
| 354 | elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then |
| 355 | git_update_branch $GIT_REF |
Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 356 | elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then |
| 357 | git_update_remote_branch $GIT_REF |
Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 358 | else |
| 359 | echo $GIT_REF is neither branch nor tag |
| 360 | exit 1 |
| 361 | fi |
| 362 | |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 363 | fi |
| 364 | fi |
| 365 | } |
| 366 | |
| 367 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 368 | # Comment an option in an INI file |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 369 | # inicomment config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 370 | function inicomment() { |
| 371 | local file=$1 |
| 372 | local section=$2 |
| 373 | local option=$3 |
| 374 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file |
| 375 | } |
| 376 | |
Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 377 | # Uncomment an option in an INI file |
| 378 | # iniuncomment config-file section option |
| 379 | function iniuncomment() { |
| 380 | local file=$1 |
| 381 | local section=$2 |
| 382 | local option=$3 |
| 383 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file |
| 384 | } |
| 385 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 386 | |
| 387 | # Get an option from an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 388 | # iniget config-file section option |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 389 | function iniget() { |
| 390 | local file=$1 |
| 391 | local section=$2 |
| 392 | local option=$3 |
| 393 | local line |
| 394 | line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file) |
| 395 | echo ${line#*=} |
| 396 | } |
| 397 | |
| 398 | |
| 399 | # Set an option in an INI file |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 400 | # iniset config-file section option value |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 401 | function iniset() { |
| 402 | local file=$1 |
| 403 | local section=$2 |
| 404 | local option=$3 |
| 405 | local value=$4 |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 406 | if ! grep -q "^\[$section\]" $file; then |
| 407 | # Add section at the end |
| 408 | echo -e "\n[$section]" >>$file |
| 409 | fi |
| 410 | if [[ -z "$(iniget $file $section $option)" ]]; then |
| 411 | # Add it |
| 412 | sed -i -e "/^\[$section\]/ a\\ |
| 413 | $option = $value |
| 414 | " $file |
| 415 | else |
| 416 | # Replace it |
| 417 | sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file |
| 418 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 422 | # is_service_enabled() checks if the service(s) specified as arguments are |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 423 | # enabled by the user in ``ENABLED_SERVICES``. |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 424 | # |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 425 | # Multiple services specified as arguments are ``OR``'ed together; the test |
| 426 | # is a short-circuit boolean, i.e it returns on the first match. |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 427 | # |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 428 | # There are special cases for some 'catch-all' services:: |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 429 | # **nova** returns true if any service enabled start with **n-** |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 430 | # **cinder** returns true if any service enabled start with **c-** |
| 431 | # **ceilometer** returns true if any service enabled start with **ceilometer** |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 432 | # **glance** returns true if any service enabled start with **g-** |
| 433 | # **quantum** returns true if any service enabled start with **q-** |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 434 | # |
| 435 | # Uses global ``ENABLED_SERVICES`` |
| 436 | # is_service_enabled service [service ...] |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 437 | function is_service_enabled() { |
| 438 | services=$@ |
| 439 | for service in ${services}; do |
| 440 | [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 441 | [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 |
Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 442 | [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0 |
John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame] | 443 | [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0 |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 444 | [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 |
| 445 | [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 |
| 446 | done |
| 447 | return 1 |
| 448 | } |
| 449 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 450 | |
| 451 | # remove extra commas from the input string (i.e. ``ENABLED_SERVICES``) |
| 452 | # _cleanup_service_list service-list |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 453 | function _cleanup_service_list () { |
Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 454 | echo "$1" | sed -e ' |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 455 | s/,,/,/g; |
| 456 | s/^,//; |
| 457 | s/,$// |
| 458 | ' |
| 459 | } |
| 460 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 461 | |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 462 | # enable_service() adds the services passed as argument to the |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 463 | # ``ENABLED_SERVICES`` list, if they are not already present. |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 464 | # |
| 465 | # For example: |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 466 | # enable_service n-vol |
| 467 | # |
| 468 | # This function does not know about the special cases |
| 469 | # for nova, glance, and quantum built into is_service_enabled(). |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 470 | # Uses global ``ENABLED_SERVICES`` |
| 471 | # enable_service service [service ...] |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 472 | function enable_service() { |
| 473 | local tmpsvcs="${ENABLED_SERVICES}" |
| 474 | for service in $@; do |
| 475 | if ! is_service_enabled $service; then |
| 476 | tmpsvcs+=",$service" |
| 477 | fi |
| 478 | done |
| 479 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 480 | disable_negated_services |
| 481 | } |
| 482 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 483 | |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 484 | # disable_service() removes the services passed as argument to the |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 485 | # ``ENABLED_SERVICES`` list, if they are present. |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 486 | # |
| 487 | # For example: |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 488 | # disable_service n-vol |
| 489 | # |
| 490 | # This function does not know about the special cases |
| 491 | # for nova, glance, and quantum built into is_service_enabled(). |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 492 | # Uses global ``ENABLED_SERVICES`` |
| 493 | # disable_service service [service ...] |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 494 | function disable_service() { |
| 495 | local tmpsvcs=",${ENABLED_SERVICES}," |
| 496 | local service |
| 497 | for service in $@; do |
| 498 | if is_service_enabled $service; then |
| 499 | tmpsvcs=${tmpsvcs//,$service,/,} |
| 500 | fi |
| 501 | done |
| 502 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 503 | } |
| 504 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 505 | |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 506 | # disable_all_services() removes all current services |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 507 | # from ``ENABLED_SERVICES`` to reset the configuration |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 508 | # before a minimal installation |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 509 | # Uses global ``ENABLED_SERVICES`` |
| 510 | # disable_all_services |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 511 | function disable_all_services() { |
| 512 | ENABLED_SERVICES="" |
| 513 | } |
| 514 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 515 | |
| 516 | # Remove all services starting with '-'. For example, to install all default |
| 517 | # services except nova-volume (n-vol) set in ``localrc``: |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 518 | # ENABLED_SERVICES+=",-n-vol" |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 519 | # Uses global ``ENABLED_SERVICES`` |
| 520 | # disable_negated_services |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 521 | function disable_negated_services() { |
| 522 | local tmpsvcs="${ENABLED_SERVICES}" |
| 523 | local service |
| 524 | for service in ${tmpsvcs//,/ }; do |
| 525 | if [[ ${service} == -* ]]; then |
| 526 | tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g") |
| 527 | fi |
| 528 | done |
| 529 | ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") |
| 530 | } |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 531 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 532 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 533 | # Distro-agnostic package installer |
| 534 | # install_package package [package ...] |
| 535 | function install_package() { |
| 536 | if [[ -z "$os_PACKAGE" ]]; then |
| 537 | GetOSVersion |
| 538 | fi |
Vincent Untz | c0482e6 | 2012-06-12 11:30:43 +0200 | [diff] [blame] | 539 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 540 | if [[ "$os_PACKAGE" = "deb" ]]; then |
Vincent Untz | c0482e6 | 2012-06-12 11:30:43 +0200 | [diff] [blame] | 541 | [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update |
| 542 | NO_UPDATE_REPOS=True |
| 543 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 544 | apt_get install "$@" |
| 545 | else |
| 546 | yum_install "$@" |
| 547 | fi |
| 548 | } |
| 549 | |
| 550 | |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame^] | 551 | # Distro-agnostic function to tell if a package is installed |
| 552 | # is_package_installed package [package ...] |
| 553 | function is_package_installed() { |
| 554 | if [[ -z "$@" ]]; then |
| 555 | return 1 |
| 556 | fi |
| 557 | |
| 558 | if [[ -z "$os_PACKAGE" ]]; then |
| 559 | GetOSVersion |
| 560 | fi |
| 561 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 562 | dpkg -l "$@" > /dev/null |
| 563 | return $? |
| 564 | else |
| 565 | rpm --quiet -q "$@" |
| 566 | return $? |
| 567 | fi |
| 568 | } |
| 569 | |
| 570 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 571 | # Test if the named environment variable is set and not zero length |
| 572 | # is_set env-var |
| 573 | function is_set() { |
| 574 | local var=\$"$1" |
| 575 | if eval "[ -z $var ]"; then |
| 576 | return 1 |
| 577 | fi |
| 578 | return 0 |
| 579 | } |
| 580 | |
| 581 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 582 | # Wrapper for ``pip install`` to set cache and proxy environment variables |
| 583 | # Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``TRACK_DEPENDES``, ``*_proxy` |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 584 | # pip_install package [package ...] |
| 585 | function pip_install { |
Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 586 | [[ "$OFFLINE" = "True" || -z "$@" ]] && return |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 587 | if [[ -z "$os_PACKAGE" ]]; then |
| 588 | GetOSVersion |
| 589 | fi |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 590 | if [[ $TRACK_DEPENDS = True ]] ; then |
| 591 | source $DEST/.venv/bin/activate |
| 592 | CMD_PIP=$DEST/.venv/bin/pip |
| 593 | SUDO_PIP="env" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 594 | else |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 595 | SUDO_PIP="sudo" |
| 596 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 597 | CMD_PIP=/usr/bin/pip |
| 598 | else |
| 599 | CMD_PIP=/usr/bin/pip-python |
| 600 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 601 | fi |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 602 | $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \ |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 603 | HTTP_PROXY=$http_proxy \ |
| 604 | HTTPS_PROXY=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 605 | NO_PROXY=$no_proxy \ |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 606 | $CMD_PIP install --use-mirrors $@ |
| 607 | } |
| 608 | |
| 609 | |
| 610 | # Service wrapper to restart services |
| 611 | # restart_service service-name |
| 612 | function restart_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 613 | if [[ -z "$os_PACKAGE" ]]; then |
| 614 | GetOSVersion |
| 615 | fi |
| 616 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 617 | sudo /usr/sbin/service $1 restart |
| 618 | else |
| 619 | sudo /sbin/service $1 restart |
| 620 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | |
Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 624 | # Helper to launch a service in a named screen |
| 625 | # screen_it service "command-line" |
| 626 | function screen_it { |
| 627 | NL=`echo -ne '\015'` |
| 628 | SCREEN_NAME=${SCREEN_NAME:-stack} |
| 629 | if is_service_enabled $1; then |
| 630 | # Append the service to the screen rc file |
| 631 | screen_rc "$1" "$2" |
| 632 | |
| 633 | screen -S $SCREEN_NAME -X screen -t $1 |
| 634 | # sleep to allow bash to be ready to be send the command - we are |
| 635 | # creating a new window in screen and then sends characters, so if |
| 636 | # bash isn't running by the time we send the command, nothing happens |
| 637 | sleep 1.5 |
| 638 | |
| 639 | if [[ -n ${SCREEN_LOGDIR} ]]; then |
| 640 | screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log |
| 641 | screen -S $SCREEN_NAME -p $1 -X log on |
| 642 | ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log |
| 643 | fi |
| 644 | screen -S $SCREEN_NAME -p $1 -X stuff "$2$NL" |
| 645 | fi |
| 646 | } |
| 647 | |
| 648 | |
| 649 | # Screen rc file builder |
| 650 | # screen_rc service "command-line" |
| 651 | function screen_rc { |
| 652 | SCREEN_NAME=${SCREEN_NAME:-stack} |
| 653 | SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc |
| 654 | if [[ ! -e $SCREENRC ]]; then |
| 655 | # Name the screen session |
| 656 | echo "sessionname $SCREEN_NAME" > $SCREENRC |
| 657 | # Set a reasonable statusbar |
| 658 | echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC |
| 659 | echo "screen -t shell bash" >> $SCREENRC |
| 660 | fi |
| 661 | # If this service doesn't already exist in the screenrc file |
| 662 | if ! grep $1 $SCREENRC 2>&1 > /dev/null; then |
| 663 | NL=`echo -ne '\015'` |
| 664 | echo "screen -t $1 bash" >> $SCREENRC |
| 665 | echo "stuff \"$2$NL\"" >> $SCREENRC |
| 666 | fi |
| 667 | } |
| 668 | |
| 669 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 670 | # ``pip install`` the dependencies of the package before ``setup.py develop`` |
| 671 | # so pip and not distutils processes the dependency chain |
| 672 | # Uses globals ``TRACK_DEPENDES``, ``*_proxy` |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 673 | # setup_develop directory |
| 674 | function setup_develop() { |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 675 | if [[ $TRACK_DEPENDS = True ]] ; then |
| 676 | SUDO_CMD="env" |
| 677 | else |
| 678 | SUDO_CMD="sudo" |
| 679 | fi |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 680 | (cd $1; \ |
| 681 | python setup.py egg_info; \ |
| 682 | raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \ |
| 683 | depend_links=$(echo $raw_links | xargs); \ |
| 684 | pip_install -r *-info/requires.txt $depend_links; \ |
Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 685 | $SUDO_CMD \ |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 686 | HTTP_PROXY=$http_proxy \ |
| 687 | HTTPS_PROXY=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 688 | NO_PROXY=$no_proxy \ |
Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 689 | python setup.py develop \ |
| 690 | ) |
| 691 | } |
| 692 | |
| 693 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 694 | # Service wrapper to start services |
| 695 | # start_service service-name |
| 696 | function start_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 697 | if [[ -z "$os_PACKAGE" ]]; then |
| 698 | GetOSVersion |
| 699 | fi |
| 700 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 701 | sudo /usr/sbin/service $1 start |
| 702 | else |
| 703 | sudo /sbin/service $1 start |
| 704 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | |
| 708 | # Service wrapper to stop services |
| 709 | # stop_service service-name |
| 710 | function stop_service() { |
Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 711 | if [[ -z "$os_PACKAGE" ]]; then |
| 712 | GetOSVersion |
| 713 | fi |
| 714 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 715 | sudo /usr/sbin/service $1 stop |
| 716 | else |
| 717 | sudo /sbin/service $1 stop |
| 718 | fi |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | |
| 722 | # Normalize config values to True or False |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 723 | # Accepts as False: 0 no false False FALSE |
| 724 | # Accepts as True: 1 yes true True TRUE |
| 725 | # VAR=$(trueorfalse default-value test-value) |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 726 | function trueorfalse() { |
| 727 | local default=$1 |
| 728 | local testval=$2 |
| 729 | |
| 730 | [[ -z "$testval" ]] && { echo "$default"; return; } |
| 731 | [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } |
| 732 | [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } |
| 733 | echo "$default" |
| 734 | } |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 735 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 736 | |
Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 737 | # Retrieve an image from a URL and upload into Glance |
| 738 | # Uses the following variables: |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 739 | # ``FILES`` must be set to the cache dir |
| 740 | # ``GLANCE_HOSTPORT`` |
Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 741 | # upload_image image-url glance-token |
| 742 | function upload_image() { |
| 743 | local image_url=$1 |
| 744 | local token=$2 |
| 745 | |
| 746 | # Create a directory for the downloaded image tarballs. |
| 747 | mkdir -p $FILES/images |
| 748 | |
| 749 | # Downloads the image (uec ami+aki style), then extracts it. |
| 750 | IMAGE_FNAME=`basename "$image_url"` |
| 751 | if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then |
| 752 | wget -c $image_url -O $FILES/$IMAGE_FNAME |
| 753 | if [[ $? -ne 0 ]]; then |
| 754 | echo "Not found: $image_url" |
| 755 | return |
| 756 | fi |
| 757 | fi |
| 758 | |
| 759 | # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading |
| 760 | if [[ "$image_url" =~ 'openvz' ]]; then |
| 761 | IMAGE="$FILES/${IMAGE_FNAME}" |
| 762 | IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" |
| 763 | 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}" |
| 764 | return |
| 765 | fi |
| 766 | |
| 767 | KERNEL="" |
| 768 | RAMDISK="" |
| 769 | DISK_FORMAT="" |
| 770 | CONTAINER_FORMAT="" |
| 771 | UNPACK="" |
| 772 | case "$IMAGE_FNAME" in |
| 773 | *.tar.gz|*.tgz) |
| 774 | # Extract ami and aki files |
| 775 | [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] && |
| 776 | IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" || |
| 777 | IMAGE_NAME="${IMAGE_FNAME%.tgz}" |
| 778 | xdir="$FILES/images/$IMAGE_NAME" |
| 779 | rm -Rf "$xdir"; |
| 780 | mkdir "$xdir" |
| 781 | tar -zxf $FILES/$IMAGE_FNAME -C "$xdir" |
| 782 | KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do |
| 783 | [ -f "$f" ] && echo "$f" && break; done; true) |
| 784 | RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do |
| 785 | [ -f "$f" ] && echo "$f" && break; done; true) |
| 786 | IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do |
| 787 | [ -f "$f" ] && echo "$f" && break; done; true) |
| 788 | if [[ -z "$IMAGE_NAME" ]]; then |
| 789 | IMAGE_NAME=$(basename "$IMAGE" ".img") |
| 790 | fi |
| 791 | ;; |
| 792 | *.img) |
| 793 | IMAGE="$FILES/$IMAGE_FNAME"; |
| 794 | IMAGE_NAME=$(basename "$IMAGE" ".img") |
| 795 | DISK_FORMAT=raw |
| 796 | CONTAINER_FORMAT=bare |
| 797 | ;; |
| 798 | *.img.gz) |
| 799 | IMAGE="$FILES/${IMAGE_FNAME}" |
| 800 | IMAGE_NAME=$(basename "$IMAGE" ".img.gz") |
| 801 | DISK_FORMAT=raw |
| 802 | CONTAINER_FORMAT=bare |
| 803 | UNPACK=zcat |
| 804 | ;; |
| 805 | *.qcow2) |
| 806 | IMAGE="$FILES/${IMAGE_FNAME}" |
| 807 | IMAGE_NAME=$(basename "$IMAGE" ".qcow2") |
| 808 | DISK_FORMAT=qcow2 |
| 809 | CONTAINER_FORMAT=bare |
| 810 | ;; |
| 811 | *) echo "Do not know what to do with $IMAGE_FNAME"; false;; |
| 812 | esac |
| 813 | |
| 814 | if [ "$CONTAINER_FORMAT" = "bare" ]; then |
| 815 | if [ "$UNPACK" = "zcat" ]; then |
| 816 | 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}") |
| 817 | else |
| 818 | 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}" |
| 819 | fi |
| 820 | else |
| 821 | # Use glance client to add the kernel the root filesystem. |
| 822 | # We parse the results of the first upload to get the glance ID of the |
| 823 | # kernel for use when uploading the root filesystem. |
| 824 | KERNEL_ID=""; RAMDISK_ID=""; |
| 825 | if [ -n "$KERNEL" ]; then |
| 826 | 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) |
| 827 | fi |
| 828 | if [ -n "$RAMDISK" ]; then |
| 829 | 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) |
| 830 | fi |
| 831 | 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}" |
| 832 | fi |
| 833 | } |
| 834 | |
| 835 | |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 836 | # Wrapper for ``yum`` to set proxy environment variables |
| 837 | # Uses globals ``OFFLINE``, ``*_proxy` |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 838 | # yum_install package [package ...] |
| 839 | function yum_install() { |
| 840 | [[ "$OFFLINE" = "True" ]] && return |
| 841 | local sudo="sudo" |
| 842 | [[ "$(id -u)" = "0" ]] && sudo="env" |
| 843 | $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ |
Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 844 | no_proxy=$no_proxy \ |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 845 | yum install -y "$@" |
| 846 | } |
| 847 | |
| 848 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 849 | # Restore xtrace |
Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 850 | $XTRACE |
Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 851 | |
| 852 | |
| 853 | # Local variables: |
| 854 | # -*- mode: Shell-script -*- |
Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 855 | # End: |