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