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