| 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 | ac93efb | 2013-03-13 14:30:54 -0500 | [diff] [blame] | 56 | # Prints line number and "message" then exits | 
 | 57 | # die $LINENO "message" | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 58 | function die() { | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 59 |     local exitcode=$? | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 60 |     set +o xtrace | 
 | 61 |     local line=$1; shift | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 62 |     if [ $exitcode == 0 ]; then | 
 | 63 |         exitcode=1 | 
 | 64 |     fi | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 65 |     err $line "$*" | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 66 |     exit $exitcode | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 67 | } | 
 | 68 |  | 
 | 69 |  | 
 | 70 | # Checks an environment variable is not set or has length 0 OR if the | 
 | 71 | # exit code is non-zero and prints "message" and exits | 
 | 72 | # NOTE: env-var is the variable name without a '$' | 
| Dean Troyer | ac93efb | 2013-03-13 14:30:54 -0500 | [diff] [blame] | 73 | # die_if_not_set $LINENO env-var "message" | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 74 | function die_if_not_set() { | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 75 |     local exitcode=$? | 
 | 76 |     FXTRACE=$(set +o | grep xtrace) | 
 | 77 |     set +o xtrace | 
 | 78 |     local line=$1; shift | 
 | 79 |     local evar=$1; shift | 
 | 80 |     if ! is_set $evar || [ $exitcode != 0 ]; then | 
 | 81 |         die $line "$*" | 
 | 82 |     fi | 
 | 83 |     $FXTRACE | 
 | 84 | } | 
 | 85 |  | 
 | 86 |  | 
 | 87 | # Prints line number and "message" in error format | 
 | 88 | # err $LINENO "message" | 
 | 89 | function err() { | 
 | 90 |     local exitcode=$? | 
 | 91 |     errXTRACE=$(set +o | grep xtrace) | 
 | 92 |     set +o xtrace | 
 | 93 |     local msg="[ERROR] $0:$1 $2" | 
 | 94 |     echo $msg 1>&2; | 
 | 95 |     if [[ -n ${SCREEN_LOGDIR} ]]; then | 
 | 96 |         echo $msg >> "${SCREEN_LOGDIR}/error.log" | 
 | 97 |     fi | 
 | 98 |     $errXTRACE | 
 | 99 |     return $exitcode | 
 | 100 | } | 
 | 101 |  | 
 | 102 |  | 
 | 103 | # Checks an environment variable is not set or has length 0 OR if the | 
 | 104 | # exit code is non-zero and prints "message" | 
 | 105 | # NOTE: env-var is the variable name without a '$' | 
 | 106 | # err_if_not_set $LINENO env-var "message" | 
 | 107 | function err_if_not_set() { | 
 | 108 |     local exitcode=$? | 
 | 109 |     errinsXTRACE=$(set +o | grep xtrace) | 
 | 110 |     set +o xtrace | 
 | 111 |     local line=$1; shift | 
 | 112 |     local evar=$1; shift | 
 | 113 |     if ! is_set $evar || [ $exitcode != 0 ]; then | 
 | 114 |         err $line "$*" | 
 | 115 |     fi | 
 | 116 |     $errinsXTRACE | 
 | 117 |     return $exitcode | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 118 | } | 
 | 119 |  | 
 | 120 |  | 
| Dean Troyer | 48352ee | 2012-12-12 12:50:38 -0600 | [diff] [blame] | 121 | # HTTP and HTTPS proxy servers are supported via the usual environment variables [1] | 
 | 122 | # ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in | 
 | 123 | # ``localrc`` or on the command line if necessary:: | 
 | 124 | # | 
 | 125 | # [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html | 
 | 126 | # | 
 | 127 | #     http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh | 
 | 128 |  | 
 | 129 | function export_proxy_variables() { | 
 | 130 |     if [[ -n "$http_proxy" ]]; then | 
 | 131 |         export http_proxy=$http_proxy | 
 | 132 |     fi | 
 | 133 |     if [[ -n "$https_proxy" ]]; then | 
 | 134 |         export https_proxy=$https_proxy | 
 | 135 |     fi | 
 | 136 |     if [[ -n "$no_proxy" ]]; then | 
 | 137 |         export no_proxy=$no_proxy | 
 | 138 |     fi | 
 | 139 | } | 
 | 140 |  | 
 | 141 |  | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 142 | # Grab a numbered field from python prettytable output | 
 | 143 | # Fields are numbered starting with 1 | 
 | 144 | # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc. | 
 | 145 | # get_field field-number | 
 | 146 | function get_field() { | 
 | 147 |     while read data; do | 
 | 148 |         if [ "$1" -lt 0 ]; then | 
 | 149 |             field="(\$(NF$1))" | 
 | 150 |         else | 
 | 151 |             field="\$$(($1 + 1))" | 
 | 152 |         fi | 
 | 153 |         echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}" | 
 | 154 |     done | 
 | 155 | } | 
 | 156 |  | 
 | 157 |  | 
| Dean Troyer | c892bde | 2013-03-13 14:06:13 -0500 | [diff] [blame] | 158 | # Get the default value for HOST_IP | 
 | 159 | # get_default_host_ip fixed_range floating_range host_ip_iface host_ip | 
 | 160 | function get_default_host_ip() { | 
 | 161 |     local fixed_range=$1 | 
 | 162 |     local floating_range=$2 | 
 | 163 |     local host_ip_iface=$3 | 
 | 164 |     local host_ip=$4 | 
 | 165 |  | 
 | 166 |     # Find the interface used for the default route | 
 | 167 |     host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)} | 
 | 168 |     # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable | 
 | 169 |     if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then | 
 | 170 |         host_ip="" | 
 | 171 |         host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}'` | 
 | 172 |         for IP in $host_ips; do | 
 | 173 |             # Attempt to filter out IP addresses that are part of the fixed and | 
 | 174 |             # floating range. Note that this method only works if the ``netaddr`` | 
 | 175 |             # python library is installed. If it is not installed, an error | 
 | 176 |             # will be printed and the first IP from the interface will be used. | 
 | 177 |             # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct | 
 | 178 |             # address. | 
 | 179 |             if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then | 
 | 180 |                 host_ip=$IP | 
 | 181 |                 break; | 
 | 182 |             fi | 
 | 183 |         done | 
 | 184 |     fi | 
 | 185 |     echo $host_ip | 
 | 186 | } | 
 | 187 |  | 
 | 188 |  | 
| Isaku Yamahata | 8c43809 | 2013-02-12 22:30:56 +0900 | [diff] [blame] | 189 | function _get_package_dir() { | 
 | 190 |     local pkg_dir | 
 | 191 |     if is_ubuntu; then | 
 | 192 |         pkg_dir=$FILES/apts | 
 | 193 |     elif is_fedora; then | 
 | 194 |         pkg_dir=$FILES/rpms | 
 | 195 |     elif is_suse; then | 
 | 196 |         pkg_dir=$FILES/rpms-suse | 
 | 197 |     else | 
 | 198 |         exit_distro_not_supported "list of packages" | 
 | 199 |     fi | 
 | 200 |     echo "$pkg_dir" | 
 | 201 | } | 
 | 202 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 203 |  | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 204 | # 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] | 205 | # prerequisite files in ``files/{apts|rpms}``.  The list is intended | 
 | 206 | # to be passed to a package installer such as apt or yum. | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 207 | # | 
| Isaku Yamahata | 8c43809 | 2013-02-12 22:30:56 +0900 | [diff] [blame] | 208 | # Only packages required for the services in 1st argument will be | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 209 | # included.  Two bits of metadata are recognized in the prerequisite files: | 
 | 210 | # - ``# NOPRIME`` defers installation to be performed later in stack.sh | 
 | 211 | # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection | 
 | 212 | #   of the package to the distros listed.  The distro names are case insensitive. | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 213 | function get_packages() { | 
| Isaku Yamahata | 8c43809 | 2013-02-12 22:30:56 +0900 | [diff] [blame] | 214 |     local services=$1 | 
 | 215 |     local package_dir=$(_get_package_dir) | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 216 |     local file_to_parse | 
 | 217 |     local service | 
 | 218 |  | 
 | 219 |     if [[ -z "$package_dir" ]]; then | 
 | 220 |         echo "No package directory supplied" | 
 | 221 |         return 1 | 
 | 222 |     fi | 
 | 223 |     if [[ -z "$DISTRO" ]]; then | 
| Vincent Untz | 855c587 | 2012-10-04 13:36:46 +0200 | [diff] [blame] | 224 |         GetDistro | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 225 |     fi | 
| Isaku Yamahata | 8c43809 | 2013-02-12 22:30:56 +0900 | [diff] [blame] | 226 |     for service in general ${services//,/ }; do | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 227 |         # Allow individual services to specify dependencies | 
 | 228 |         if [[ -e ${package_dir}/${service} ]]; then | 
 | 229 |             file_to_parse="${file_to_parse} $service" | 
 | 230 |         fi | 
 | 231 |         # NOTE(sdague) n-api needs glance for now because that's where | 
 | 232 |         # glance client is | 
 | 233 |         if [[ $service == n-api ]]; then | 
 | 234 |             if [[ ! $file_to_parse =~ nova ]]; then | 
 | 235 |                 file_to_parse="${file_to_parse} nova" | 
 | 236 |             fi | 
 | 237 |             if [[ ! $file_to_parse =~ glance ]]; then | 
 | 238 |                 file_to_parse="${file_to_parse} glance" | 
 | 239 |             fi | 
 | 240 |         elif [[ $service == c-* ]]; then | 
 | 241 |             if [[ ! $file_to_parse =~ cinder ]]; then | 
 | 242 |                 file_to_parse="${file_to_parse} cinder" | 
 | 243 |             fi | 
| John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame] | 244 |         elif [[ $service == ceilometer-* ]]; then | 
 | 245 |             if [[ ! $file_to_parse =~ ceilometer ]]; then | 
 | 246 |                 file_to_parse="${file_to_parse} ceilometer" | 
 | 247 |             fi | 
| Chmouel Boudjnah | 0c3a558 | 2013-03-06 10:58:33 +0100 | [diff] [blame] | 248 |         elif [[ $service == s-* ]]; then | 
 | 249 |             if [[ ! $file_to_parse =~ swift ]]; then | 
 | 250 |                 file_to_parse="${file_to_parse} swift" | 
 | 251 |             fi | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 252 |         elif [[ $service == n-* ]]; then | 
 | 253 |             if [[ ! $file_to_parse =~ nova ]]; then | 
 | 254 |                 file_to_parse="${file_to_parse} nova" | 
 | 255 |             fi | 
 | 256 |         elif [[ $service == g-* ]]; then | 
 | 257 |             if [[ ! $file_to_parse =~ glance ]]; then | 
 | 258 |                 file_to_parse="${file_to_parse} glance" | 
 | 259 |             fi | 
 | 260 |         elif [[ $service == key* ]]; then | 
 | 261 |             if [[ ! $file_to_parse =~ keystone ]]; then | 
 | 262 |                 file_to_parse="${file_to_parse} keystone" | 
 | 263 |             fi | 
| Robert Collins | 0a9954f | 2012-11-20 11:34:25 +1300 | [diff] [blame] | 264 |         elif [[ $service == q-* ]]; then | 
 | 265 |             if [[ ! $file_to_parse =~ quantum ]]; then | 
 | 266 |                 file_to_parse="${file_to_parse} quantum" | 
 | 267 |             fi | 
| Dean Troyer | 7e27051 | 2012-06-14 15:23:24 -0500 | [diff] [blame] | 268 |         fi | 
 | 269 |     done | 
 | 270 |  | 
 | 271 |     for file in ${file_to_parse}; do | 
 | 272 |         local fname=${package_dir}/${file} | 
 | 273 |         local OIFS line package distros distro | 
 | 274 |         [[ -e $fname ]] || continue | 
 | 275 |  | 
 | 276 |         OIFS=$IFS | 
 | 277 |         IFS=$'\n' | 
 | 278 |         for line in $(<${fname}); do | 
 | 279 |             if [[ $line =~ "NOPRIME" ]]; then | 
 | 280 |                 continue | 
 | 281 |             fi | 
 | 282 |  | 
 | 283 |             if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then | 
 | 284 |                 # We are using BASH regexp matching feature. | 
 | 285 |                 package=${BASH_REMATCH[1]} | 
 | 286 |                 distros=${BASH_REMATCH[2]} | 
 | 287 |                 # In bash ${VAR,,} will lowecase VAR | 
 | 288 |                 [[ ${distros,,} =~ ${DISTRO,,} ]] && echo $package | 
 | 289 |                 continue | 
 | 290 |             fi | 
 | 291 |  | 
 | 292 |             echo ${line%#*} | 
 | 293 |         done | 
 | 294 |         IFS=$OIFS | 
 | 295 |     done | 
 | 296 | } | 
 | 297 |  | 
 | 298 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 299 | # Determine OS Vendor, Release and Update | 
 | 300 | # Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora | 
 | 301 | # Returns results in global variables: | 
 | 302 | # os_VENDOR - vendor name | 
 | 303 | # os_RELEASE - release | 
 | 304 | # os_UPDATE - update | 
 | 305 | # os_PACKAGE - package type | 
 | 306 | # os_CODENAME - vendor's codename for release | 
 | 307 | # GetOSVersion | 
 | 308 | GetOSVersion() { | 
 | 309 |     # Figure out which vendor we are | 
 | 310 |     if [[ -n "`which sw_vers 2>/dev/null`" ]]; then | 
 | 311 |         # OS/X | 
 | 312 |         os_VENDOR=`sw_vers -productName` | 
 | 313 |         os_RELEASE=`sw_vers -productVersion` | 
 | 314 |         os_UPDATE=${os_RELEASE##*.} | 
 | 315 |         os_RELEASE=${os_RELEASE%.*} | 
 | 316 |         os_PACKAGE="" | 
 | 317 |         if [[ "$os_RELEASE" =~ "10.7" ]]; then | 
 | 318 |             os_CODENAME="lion" | 
 | 319 |         elif [[ "$os_RELEASE" =~ "10.6" ]]; then | 
 | 320 |             os_CODENAME="snow leopard" | 
 | 321 |         elif [[ "$os_RELEASE" =~ "10.5" ]]; then | 
 | 322 |             os_CODENAME="leopard" | 
 | 323 |         elif [[ "$os_RELEASE" =~ "10.4" ]]; then | 
 | 324 |             os_CODENAME="tiger" | 
 | 325 |         elif [[ "$os_RELEASE" =~ "10.3" ]]; then | 
 | 326 |             os_CODENAME="panther" | 
 | 327 |         else | 
 | 328 |             os_CODENAME="" | 
 | 329 |         fi | 
 | 330 |     elif [[ -x $(which lsb_release 2>/dev/null) ]]; then | 
 | 331 |         os_VENDOR=$(lsb_release -i -s) | 
 | 332 |         os_RELEASE=$(lsb_release -r -s) | 
 | 333 |         os_UPDATE="" | 
| Attila Fazekas | af988fd | 2013-01-13 14:20:47 +0100 | [diff] [blame] | 334 |         os_PACKAGE="rpm" | 
| Derek Morton | 4a8496e | 2013-04-08 23:46:08 -0500 | [diff] [blame] | 335 |         if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 336 |             os_PACKAGE="deb" | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 337 |         elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then | 
 | 338 |             lsb_release -d -s | grep -q openSUSE | 
 | 339 |             if [[ $? -eq 0 ]]; then | 
 | 340 |                 os_VENDOR="openSUSE" | 
 | 341 |             fi | 
| Vincent Untz | cd1fe98 | 2013-03-12 18:04:29 +0100 | [diff] [blame] | 342 |         elif [[ $os_VENDOR == "openSUSE project" ]]; then | 
 | 343 |             os_VENDOR="openSUSE" | 
| Attila Fazekas | af988fd | 2013-01-13 14:20:47 +0100 | [diff] [blame] | 344 |         elif [[ $os_VENDOR =~ Red.*Hat ]]; then | 
 | 345 |             os_VENDOR="Red Hat" | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 346 |         fi | 
 | 347 |         os_CODENAME=$(lsb_release -c -s) | 
 | 348 |     elif [[ -r /etc/redhat-release ]]; then | 
 | 349 |         # Red Hat Enterprise Linux Server release 5.5 (Tikanga) | 
 | 350 |         # CentOS release 5.5 (Final) | 
 | 351 |         # CentOS Linux release 6.0 (Final) | 
 | 352 |         # Fedora release 16 (Verne) | 
 | 353 |         os_CODENAME="" | 
 | 354 |         for r in "Red Hat" CentOS Fedora; do | 
 | 355 |             os_VENDOR=$r | 
 | 356 |             if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then | 
 | 357 |                 ver=`sed -e 's/^.* \(.*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release` | 
 | 358 |                 os_CODENAME=${ver#*|} | 
 | 359 |                 os_RELEASE=${ver%|*} | 
 | 360 |                 os_UPDATE=${os_RELEASE##*.} | 
 | 361 |                 os_RELEASE=${os_RELEASE%.*} | 
 | 362 |                 break | 
 | 363 |             fi | 
 | 364 |             os_VENDOR="" | 
 | 365 |         done | 
 | 366 |         os_PACKAGE="rpm" | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 367 |     elif [[ -r /etc/SuSE-release ]]; then | 
 | 368 |         for r in openSUSE "SUSE Linux"; do | 
 | 369 |             if [[ "$r" = "SUSE Linux" ]]; then | 
 | 370 |                 os_VENDOR="SUSE LINUX" | 
 | 371 |             else | 
 | 372 |                 os_VENDOR=$r | 
 | 373 |             fi | 
 | 374 |  | 
 | 375 |             if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then | 
 | 376 |                 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'` | 
 | 377 |                 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'` | 
 | 378 |                 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'` | 
 | 379 |                 break | 
 | 380 |             fi | 
 | 381 |             os_VENDOR="" | 
 | 382 |         done | 
 | 383 |         os_PACKAGE="rpm" | 
| Émilien Macchi | b2ef890 | 2013-05-04 00:48:20 +0200 | [diff] [blame] | 384 |     # If lsb_release is not installed, we should be able to detect Debian OS | 
 | 385 |     elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then | 
 | 386 |         os_VENDOR="Debian" | 
 | 387 |         os_PACKAGE="deb" | 
 | 388 |         os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}') | 
 | 389 |         os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g') | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 390 |     fi | 
 | 391 |     export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME | 
 | 392 | } | 
 | 393 |  | 
| Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 394 |  | 
| Dean Troyer | a9e0a48 | 2012-07-09 14:07:23 -0500 | [diff] [blame] | 395 | # Translate the OS version values into common nomenclature | 
 | 396 | # Sets ``DISTRO`` from the ``os_*`` values | 
 | 397 | function GetDistro() { | 
 | 398 |     GetOSVersion | 
| Émilien Macchi | b2ef890 | 2013-05-04 00:48:20 +0200 | [diff] [blame] | 399 |     if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then | 
 | 400 |         # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective | 
| Dean Troyer | a9e0a48 | 2012-07-09 14:07:23 -0500 | [diff] [blame] | 401 |         DISTRO=$os_CODENAME | 
 | 402 |     elif [[ "$os_VENDOR" =~ (Fedora) ]]; then | 
 | 403 |         # For Fedora, just use 'f' and the release | 
 | 404 |         DISTRO="f$os_RELEASE" | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 405 |     elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then | 
 | 406 |         DISTRO="opensuse-$os_RELEASE" | 
 | 407 |     elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then | 
 | 408 |         # For SLE, also use the service pack | 
 | 409 |         if [[ -z "$os_UPDATE" ]]; then | 
 | 410 |             DISTRO="sle${os_RELEASE}" | 
 | 411 |         else | 
 | 412 |             DISTRO="sle${os_RELEASE}sp${os_UPDATE}" | 
 | 413 |         fi | 
| Ian Wienand | d857f4b | 2013-03-20 14:51:06 +1100 | [diff] [blame] | 414 |     elif [[ "$os_VENDOR" =~ (Red Hat) || "$os_VENDOR" =~ (CentOS) ]]; then | 
 | 415 |         # Drop the . release as we assume it's compatible | 
 | 416 |         DISTRO="rhel${os_RELEASE::1}" | 
| Dean Troyer | a9e0a48 | 2012-07-09 14:07:23 -0500 | [diff] [blame] | 417 |     else | 
 | 418 |         # Catch-all for now is Vendor + Release + Update | 
 | 419 |         DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE" | 
 | 420 |     fi | 
 | 421 |     export DISTRO | 
 | 422 | } | 
 | 423 |  | 
 | 424 |  | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 425 | # Determine if current distribution is a Fedora-based distribution | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 426 | # (Fedora, RHEL, CentOS, etc). | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 427 | # is_fedora | 
 | 428 | function is_fedora { | 
 | 429 |     if [[ -z "$os_VENDOR" ]]; then | 
 | 430 |         GetOSVersion | 
 | 431 |     fi | 
 | 432 |  | 
 | 433 |     [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || [ "$os_VENDOR" = "CentOS" ] | 
 | 434 | } | 
 | 435 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 436 |  | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 437 | # Determine if current distribution is a SUSE-based distribution | 
 | 438 | # (openSUSE, SLE). | 
 | 439 | # is_suse | 
 | 440 | function is_suse { | 
 | 441 |     if [[ -z "$os_VENDOR" ]]; then | 
 | 442 |         GetOSVersion | 
 | 443 |     fi | 
 | 444 |  | 
| Steve Baker | 1a7bbd2 | 2012-12-03 17:04:02 +1300 | [diff] [blame] | 445 |     [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ] | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 446 | } | 
 | 447 |  | 
 | 448 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 449 | # Determine if current distribution is an Ubuntu-based distribution | 
 | 450 | # It will also detect non-Ubuntu but Debian-based distros | 
 | 451 | # is_ubuntu | 
 | 452 | function is_ubuntu { | 
 | 453 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 454 |         GetOSVersion | 
 | 455 |     fi | 
 | 456 |     [ "$os_PACKAGE" = "deb" ] | 
 | 457 | } | 
 | 458 |  | 
 | 459 |  | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 460 | # Exit after outputting a message about the distribution not being supported. | 
 | 461 | # exit_distro_not_supported [optional-string-telling-what-is-missing] | 
 | 462 | function exit_distro_not_supported { | 
 | 463 |     if [[ -z "$DISTRO" ]]; then | 
 | 464 |         GetDistro | 
 | 465 |     fi | 
 | 466 |  | 
 | 467 |     if [ $# -gt 0 ]; then | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 468 |         die $LINENO "Support for $DISTRO is incomplete: no support for $@" | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 469 |     else | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 470 |         die $LINENO "Support for $DISTRO is incomplete." | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 471 |     fi | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 472 | } | 
 | 473 |  | 
 | 474 |  | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 475 | # git clone only if directory doesn't exist already.  Since ``DEST`` might not | 
 | 476 | # be owned by the installation user, we create the directory and change the | 
 | 477 | # ownership to the proper user. | 
 | 478 | # 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] | 479 | # Set global ERROR_ON_CLONE=True to abort execution with an error if the git repo | 
 | 480 | # does not exist (default is False, meaning the repo will be cloned). | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 481 | # Uses global ``OFFLINE`` | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 482 | # git_clone remote dest-dir branch | 
 | 483 | function git_clone { | 
 | 484 |     [[ "$OFFLINE" = "True" ]] && return | 
 | 485 |  | 
 | 486 |     GIT_REMOTE=$1 | 
 | 487 |     GIT_DEST=$2 | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 488 |     GIT_REF=$3 | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 489 |  | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 490 |     if echo $GIT_REF | egrep -q "^refs"; then | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 491 |         # If our branch name is a gerrit style refs/changes/... | 
 | 492 |         if [[ ! -d $GIT_DEST ]]; then | 
| James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 493 |             [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 494 |             git clone $GIT_REMOTE $GIT_DEST | 
 | 495 |         fi | 
 | 496 |         cd $GIT_DEST | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 497 |         git fetch $GIT_REMOTE $GIT_REF && git checkout FETCH_HEAD | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 498 |     else | 
 | 499 |         # do a full clone only if the directory doesn't exist | 
 | 500 |         if [[ ! -d $GIT_DEST ]]; then | 
| James E. Blair | 94cb960 | 2012-06-22 15:28:29 -0700 | [diff] [blame] | 501 |             [[ "$ERROR_ON_CLONE" = "True" ]] && exit 1 | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 502 |             git clone $GIT_REMOTE $GIT_DEST | 
 | 503 |             cd $GIT_DEST | 
 | 504 |             # This checkout syntax works for both branches and tags | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 505 |             git checkout $GIT_REF | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 506 |         elif [[ "$RECLONE" == "yes" ]]; then | 
 | 507 |             # if it does exist then simulate what clone does if asked to RECLONE | 
 | 508 |             cd $GIT_DEST | 
 | 509 |             # set the url to pull from and fetch | 
 | 510 |             git remote set-url origin $GIT_REMOTE | 
 | 511 |             git fetch origin | 
 | 512 |             # remove the existing ignored files (like pyc) as they cause breakage | 
 | 513 |             # (due to the py files having older timestamps than our pyc, so python | 
 | 514 |             # thinks the pyc files are correct using them) | 
 | 515 |             find $GIT_DEST -name '*.pyc' -delete | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 516 |  | 
 | 517 |             # handle GIT_REF accordingly to type (tag, branch) | 
 | 518 |             if [[ -n "`git show-ref refs/tags/$GIT_REF`" ]]; then | 
 | 519 |                 git_update_tag $GIT_REF | 
 | 520 |             elif [[ -n "`git show-ref refs/heads/$GIT_REF`" ]]; then | 
 | 521 |                 git_update_branch $GIT_REF | 
| Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 522 |             elif [[ -n "`git show-ref refs/remotes/origin/$GIT_REF`" ]]; then | 
 | 523 |                 git_update_remote_branch $GIT_REF | 
| Evgeniy Afonichev | 6a3912d | 2012-07-10 14:02:43 +0300 | [diff] [blame] | 524 |             else | 
 | 525 |                 echo $GIT_REF is neither branch nor tag | 
 | 526 |                 exit 1 | 
 | 527 |             fi | 
 | 528 |  | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 529 |         fi | 
 | 530 |     fi | 
 | 531 | } | 
 | 532 |  | 
 | 533 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 534 | # git update using reference as a branch. | 
 | 535 | # git_update_branch ref | 
 | 536 | function git_update_branch() { | 
 | 537 |  | 
 | 538 |     GIT_BRANCH=$1 | 
 | 539 |  | 
 | 540 |     git checkout -f origin/$GIT_BRANCH | 
 | 541 |     # a local branch might not exist | 
 | 542 |     git branch -D $GIT_BRANCH || true | 
 | 543 |     git checkout -b $GIT_BRANCH | 
 | 544 | } | 
 | 545 |  | 
 | 546 |  | 
 | 547 | # git update using reference as a branch. | 
 | 548 | # git_update_remote_branch ref | 
 | 549 | function git_update_remote_branch() { | 
 | 550 |  | 
 | 551 |     GIT_BRANCH=$1 | 
 | 552 |  | 
 | 553 |     git checkout -b $GIT_BRANCH -t origin/$GIT_BRANCH | 
 | 554 | } | 
 | 555 |  | 
 | 556 |  | 
 | 557 | # git update using reference as a tag. Be careful editing source at that repo | 
 | 558 | # as working copy will be in a detached mode | 
 | 559 | # git_update_tag ref | 
 | 560 | function git_update_tag() { | 
 | 561 |  | 
 | 562 |     GIT_TAG=$1 | 
 | 563 |  | 
 | 564 |     git tag -d $GIT_TAG | 
 | 565 |     # fetching given tag only | 
 | 566 |     git fetch origin tag $GIT_TAG | 
 | 567 |     git checkout -f $GIT_TAG | 
 | 568 | } | 
 | 569 |  | 
 | 570 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 571 | # Comment an option in an INI file | 
| Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 572 | # inicomment config-file section option | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 573 | function inicomment() { | 
 | 574 |     local file=$1 | 
 | 575 |     local section=$2 | 
 | 576 |     local option=$3 | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 577 |     sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file" | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 578 | } | 
 | 579 |  | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 580 |  | 
| Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 581 | # Uncomment an option in an INI file | 
 | 582 | # iniuncomment config-file section option | 
 | 583 | function iniuncomment() { | 
 | 584 |     local file=$1 | 
 | 585 |     local section=$2 | 
 | 586 |     local option=$3 | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 587 |     sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file" | 
| Chmouel Boudjnah | c7214e8 | 2012-06-06 13:56:39 +0200 | [diff] [blame] | 588 | } | 
 | 589 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 590 |  | 
 | 591 | # Get an option from an INI file | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 592 | # iniget config-file section option | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 593 | function iniget() { | 
 | 594 |     local file=$1 | 
 | 595 |     local section=$2 | 
 | 596 |     local option=$3 | 
 | 597 |     local line | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 598 |     line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 599 |     echo ${line#*=} | 
 | 600 | } | 
 | 601 |  | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 602 |  | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 603 | # Determinate is the given option present in the INI file | 
 | 604 | # ini_has_option config-file section option | 
 | 605 | function ini_has_option() { | 
 | 606 |     local file=$1 | 
 | 607 |     local section=$2 | 
 | 608 |     local option=$3 | 
 | 609 |     local line | 
 | 610 |     line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") | 
 | 611 |     [ -n "$line" ] | 
 | 612 | } | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 613 |  | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 614 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 615 | # Set an option in an INI file | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 616 | # iniset config-file section option value | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 617 | function iniset() { | 
 | 618 |     local file=$1 | 
 | 619 |     local section=$2 | 
 | 620 |     local option=$3 | 
 | 621 |     local value=$4 | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 622 |     if ! grep -q "^\[$section\]" "$file"; then | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 623 |         # Add section at the end | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 624 |         echo -e "\n[$section]" >>"$file" | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 625 |     fi | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 626 |     if ! ini_has_option "$file" "$section" "$option"; then | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 627 |         # Add it | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 628 |         sed -i -e "/^\[$section\]/ a\\ | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 629 | $option = $value | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 630 | " "$file" | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 631 |     else | 
 | 632 |         # Replace it | 
| Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 633 |         sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file" | 
| Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 634 |     fi | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 635 | } | 
 | 636 |  | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 637 |  | 
| Lianhao Lu | 239f324 | 2013-03-01 15:54:02 +0800 | [diff] [blame] | 638 | # Get a multiple line option from an INI file | 
 | 639 | # iniget_multiline config-file section option | 
 | 640 | function iniget_multiline() { | 
 | 641 |     local file=$1 | 
 | 642 |     local section=$2 | 
 | 643 |     local option=$3 | 
 | 644 |     local values | 
 | 645 |     values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file") | 
 | 646 |     echo ${values} | 
 | 647 | } | 
 | 648 |  | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 649 |  | 
| Lianhao Lu | 239f324 | 2013-03-01 15:54:02 +0800 | [diff] [blame] | 650 | # Set a multiple line option in an INI file | 
 | 651 | # iniset_multiline config-file section option value1 value2 valu3 ... | 
 | 652 | function iniset_multiline() { | 
 | 653 |     local file=$1 | 
 | 654 |     local section=$2 | 
 | 655 |     local option=$3 | 
 | 656 |     shift 3 | 
 | 657 |     local values | 
 | 658 |     for v in $@; do | 
 | 659 |         # The later sed command inserts each new value in the line next to | 
 | 660 |         # the section identifier, which causes the values to be inserted in | 
 | 661 |         # the reverse order. Do a reverse here to keep the original order. | 
 | 662 |         values="$v ${values}" | 
 | 663 |     done | 
 | 664 |     if ! grep -q "^\[$section\]" "$file"; then | 
 | 665 |         # Add section at the end | 
 | 666 |         echo -e "\n[$section]" >>"$file" | 
 | 667 |     else | 
 | 668 |         # Remove old values | 
 | 669 |         sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" | 
 | 670 |     fi | 
 | 671 |     # Add new ones | 
 | 672 |     for v in $values; do | 
 | 673 |         sed -i -e "/^\[$section\]/ a\\ | 
 | 674 | $option = $v | 
 | 675 | " "$file" | 
 | 676 |     done | 
 | 677 | } | 
 | 678 |  | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 679 |  | 
| Lianhao Lu | 239f324 | 2013-03-01 15:54:02 +0800 | [diff] [blame] | 680 | # Append a new option in an ini file without replacing the old value | 
 | 681 | # iniadd config-file section option value1 value2 value3 ... | 
 | 682 | function iniadd() { | 
 | 683 |     local file=$1 | 
 | 684 |     local section=$2 | 
 | 685 |     local option=$3 | 
 | 686 |     shift 3 | 
 | 687 |     local values="$(iniget_multiline $file $section $option) $@" | 
 | 688 |     iniset_multiline $file $section $option $values | 
 | 689 | } | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 690 |  | 
| Dean Troyer | 896eb66 | 2013-04-05 15:02:01 -0500 | [diff] [blame] | 691 | # Find out if a process exists by partial name. | 
 | 692 | # is_running name | 
 | 693 | function is_running() { | 
 | 694 |     local name=$1 | 
 | 695 |     ps auxw | grep -v grep | grep ${name} > /dev/null | 
 | 696 |     RC=$? | 
 | 697 |     # some times I really hate bash reverse binary logic | 
 | 698 |     return $RC | 
 | 699 | } | 
 | 700 |  | 
 | 701 |  | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 702 | # is_service_enabled() checks if the service(s) specified as arguments are | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 703 | # enabled by the user in ``ENABLED_SERVICES``. | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 704 | # | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 705 | # Multiple services specified as arguments are ``OR``'ed together; the test | 
 | 706 | # 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] | 707 | # | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 708 | # There are special cases for some 'catch-all' services:: | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 709 | #   **nova** returns true if any service enabled start with **n-** | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 710 | #   **cinder** returns true if any service enabled start with **c-** | 
 | 711 | #   **ceilometer** returns true if any service enabled start with **ceilometer** | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 712 | #   **glance** returns true if any service enabled start with **g-** | 
 | 713 | #   **quantum** returns true if any service enabled start with **q-** | 
| Chmouel Boudjnah | 0c3a558 | 2013-03-06 10:58:33 +0100 | [diff] [blame] | 714 | #   **swift** returns true if any service enabled start with **s-** | 
 | 715 | #   For backward compatibility if we have **swift** in ENABLED_SERVICES all the | 
 | 716 | #   **s-** services will be enabled. This will be deprecated in the future. | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 717 | # | 
 | 718 | # Uses global ``ENABLED_SERVICES`` | 
 | 719 | # is_service_enabled service [service ...] | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 720 | function is_service_enabled() { | 
 | 721 |     services=$@ | 
 | 722 |     for service in ${services}; do | 
 | 723 |         [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 | 
 | 724 |         [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && return 0 | 
| Dean Troyer | 67787e6 | 2012-05-02 11:48:15 -0500 | [diff] [blame] | 725 |         [[ ${service} == "cinder" && ${ENABLED_SERVICES} =~ "c-" ]] && return 0 | 
| John H. Tran | 9336164 | 2012-07-26 11:22:05 -0700 | [diff] [blame] | 726 |         [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && return 0 | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 727 |         [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && return 0 | 
 | 728 |         [[ ${service} == "quantum" && ${ENABLED_SERVICES} =~ "q-" ]] && return 0 | 
| Chmouel Boudjnah | 0c3a558 | 2013-03-06 10:58:33 +0100 | [diff] [blame] | 729 |         [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && return 0 | 
 | 730 |         [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && return 0 | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 731 |     done | 
 | 732 |     return 1 | 
 | 733 | } | 
 | 734 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 735 |  | 
 | 736 | # remove extra commas from the input string (i.e. ``ENABLED_SERVICES``) | 
 | 737 | # _cleanup_service_list service-list | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 738 | function _cleanup_service_list () { | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 739 |     echo "$1" | sed -e ' | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 740 |         s/,,/,/g; | 
 | 741 |         s/^,//; | 
 | 742 |         s/,$// | 
 | 743 |     ' | 
 | 744 | } | 
 | 745 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 746 |  | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 747 | # enable_service() adds the services passed as argument to the | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 748 | # ``ENABLED_SERVICES`` list, if they are not already present. | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 749 | # | 
 | 750 | # For example: | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 751 | #   enable_service qpid | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 752 | # | 
 | 753 | # This function does not know about the special cases | 
 | 754 | # for nova, glance, and quantum built into is_service_enabled(). | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 755 | # Uses global ``ENABLED_SERVICES`` | 
 | 756 | # enable_service service [service ...] | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 757 | function enable_service() { | 
 | 758 |     local tmpsvcs="${ENABLED_SERVICES}" | 
 | 759 |     for service in $@; do | 
 | 760 |         if ! is_service_enabled $service; then | 
 | 761 |             tmpsvcs+=",$service" | 
 | 762 |         fi | 
 | 763 |     done | 
 | 764 |     ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
 | 765 |     disable_negated_services | 
 | 766 | } | 
 | 767 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 768 |  | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 769 | # disable_service() removes the services passed as argument to the | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 770 | # ``ENABLED_SERVICES`` list, if they are present. | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 771 | # | 
 | 772 | # For example: | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 773 | #   disable_service rabbit | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 774 | # | 
 | 775 | # This function does not know about the special cases | 
 | 776 | # for nova, glance, and quantum built into is_service_enabled(). | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 777 | # Uses global ``ENABLED_SERVICES`` | 
 | 778 | # disable_service service [service ...] | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 779 | function disable_service() { | 
 | 780 |     local tmpsvcs=",${ENABLED_SERVICES}," | 
 | 781 |     local service | 
 | 782 |     for service in $@; do | 
 | 783 |         if is_service_enabled $service; then | 
 | 784 |             tmpsvcs=${tmpsvcs//,$service,/,} | 
 | 785 |         fi | 
 | 786 |     done | 
 | 787 |     ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
 | 788 | } | 
 | 789 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 790 |  | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 791 | # disable_all_services() removes all current services | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 792 | # from ``ENABLED_SERVICES`` to reset the configuration | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 793 | # before a minimal installation | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 794 | # Uses global ``ENABLED_SERVICES`` | 
 | 795 | # disable_all_services | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 796 | function disable_all_services() { | 
 | 797 |     ENABLED_SERVICES="" | 
 | 798 | } | 
 | 799 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 800 |  | 
 | 801 | # Remove all services starting with '-'.  For example, to install all default | 
| Joe Gordon | 6fd2811 | 2012-11-13 16:55:41 -0800 | [diff] [blame] | 802 | # services except rabbit (rabbit) set in ``localrc``: | 
 | 803 | # ENABLED_SERVICES+=",-rabbit" | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 804 | # Uses global ``ENABLED_SERVICES`` | 
 | 805 | # disable_negated_services | 
| Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 806 | function disable_negated_services() { | 
 | 807 |     local tmpsvcs="${ENABLED_SERVICES}" | 
 | 808 |     local service | 
 | 809 |     for service in ${tmpsvcs//,/ }; do | 
 | 810 |         if [[ ${service} == -* ]]; then | 
 | 811 |             tmpsvcs=$(echo ${tmpsvcs}|sed -r "s/(,)?(-)?${service#-}(,)?/,/g") | 
 | 812 |         fi | 
 | 813 |     done | 
 | 814 |     ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs") | 
 | 815 | } | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 816 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 817 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 818 | # Distro-agnostic package installer | 
 | 819 | # install_package package [package ...] | 
 | 820 | function install_package() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 821 |     if is_ubuntu; then | 
| Vincent Untz | c0482e6 | 2012-06-12 11:30:43 +0200 | [diff] [blame] | 822 |         [[ "$NO_UPDATE_REPOS" = "True" ]] || apt_get update | 
 | 823 |         NO_UPDATE_REPOS=True | 
 | 824 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 825 |         apt_get install "$@" | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 826 |     elif is_fedora; then | 
 | 827 |         yum_install "$@" | 
 | 828 |     elif is_suse; then | 
 | 829 |         zypper_install "$@" | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 830 |     else | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 831 |         exit_distro_not_supported "installing packages" | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 832 |     fi | 
 | 833 | } | 
 | 834 |  | 
 | 835 |  | 
| Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 836 | # Distro-agnostic package uninstaller | 
 | 837 | # uninstall_package package [package ...] | 
 | 838 | function uninstall_package() { | 
 | 839 |     if is_ubuntu; then | 
 | 840 |         apt_get purge "$@" | 
 | 841 |     elif is_fedora; then | 
| Ian Wienand | 2c678cc | 2013-03-20 13:00:44 +1100 | [diff] [blame] | 842 |         sudo yum remove -y "$@" | 
| Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 843 |     elif is_suse; then | 
| Ian Wienand | 2c678cc | 2013-03-20 13:00:44 +1100 | [diff] [blame] | 844 |         sudo rpm -e "$@" | 
| Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 845 |     else | 
 | 846 |         exit_distro_not_supported "uninstalling packages" | 
 | 847 |     fi | 
 | 848 | } | 
 | 849 |  | 
 | 850 |  | 
| Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 851 | # Distro-agnostic function to tell if a package is installed | 
 | 852 | # is_package_installed package [package ...] | 
 | 853 | function is_package_installed() { | 
 | 854 |     if [[ -z "$@" ]]; then | 
 | 855 |         return 1 | 
 | 856 |     fi | 
 | 857 |  | 
 | 858 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 859 |         GetOSVersion | 
 | 860 |     fi | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 861 |  | 
| Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 862 |     if [[ "$os_PACKAGE" = "deb" ]]; then | 
 | 863 |         dpkg -l "$@" > /dev/null | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 864 |     elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
| Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 865 |         rpm --quiet -q "$@" | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 866 |     else | 
 | 867 |         exit_distro_not_supported "finding if a package is installed" | 
| Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 868 |     fi | 
 | 869 | } | 
 | 870 |  | 
 | 871 |  | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 872 | # Test if the named environment variable is set and not zero length | 
 | 873 | # is_set env-var | 
 | 874 | function is_set() { | 
 | 875 |     local var=\$"$1" | 
| Attila Fazekas | 251d3b5 | 2012-12-16 15:05:44 +0100 | [diff] [blame] | 876 |     eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this | 
| Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 877 | } | 
 | 878 |  | 
 | 879 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 880 | # Wrapper for ``pip install`` to set cache and proxy environment variables | 
| Maru Newby | 3a87edd | 2012-10-25 23:01:06 +0000 | [diff] [blame] | 881 | # Uses globals ``OFFLINE``, ``PIP_DOWNLOAD_CACHE``, ``PIP_USE_MIRRORS``, | 
 | 882 | #   ``TRACK_DEPENDS``, ``*_proxy` | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 883 | # pip_install package [package ...] | 
 | 884 | function pip_install { | 
| Dean Troyer | d0b21e2 | 2012-03-07 14:52:25 -0600 | [diff] [blame] | 885 |     [[ "$OFFLINE" = "True" || -z "$@" ]] && return | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 886 |     if [[ -z "$os_PACKAGE" ]]; then | 
 | 887 |         GetOSVersion | 
 | 888 |     fi | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 889 |     if [[ $TRACK_DEPENDS = True ]]; then | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 890 |         source $DEST/.venv/bin/activate | 
 | 891 |         CMD_PIP=$DEST/.venv/bin/pip | 
 | 892 |         SUDO_PIP="env" | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 893 |     else | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 894 |         SUDO_PIP="sudo" | 
| Vincent Untz | 8ec2722 | 2012-11-29 09:25:31 +0100 | [diff] [blame] | 895 |         CMD_PIP=$(get_pip_command) | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 896 |     fi | 
| Ian Wienand | d67dd87 | 2013-04-11 11:14:36 +1000 | [diff] [blame] | 897 |  | 
 | 898 |     if [[ is_fedora && $DISTRO =~ (rhel6) ]]; then | 
 | 899 |         # RHEL6 pip by default doesn't have this (was introduced | 
 | 900 |         # around 0.8.1 or so) | 
 | 901 |         PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-False} | 
 | 902 |     else | 
 | 903 |         PIP_USE_MIRRORS=${PIP_USE_MIRRORS:-True} | 
 | 904 |     fi | 
| Maru Newby | 3a87edd | 2012-10-25 23:01:06 +0000 | [diff] [blame] | 905 |     if [[ "$PIP_USE_MIRRORS" != "False" ]]; then | 
 | 906 |         PIP_MIRROR_OPT="--use-mirrors" | 
 | 907 |     fi | 
| Ian Wienand | d67dd87 | 2013-04-11 11:14:36 +1000 | [diff] [blame] | 908 |  | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 909 |     $SUDO_PIP PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-/var/cache/pip} \ | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 910 |         HTTP_PROXY=$http_proxy \ | 
 | 911 |         HTTPS_PROXY=$https_proxy \ | 
| Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 912 |         NO_PROXY=$no_proxy \ | 
| Maru Newby | 3a87edd | 2012-10-25 23:01:06 +0000 | [diff] [blame] | 913 |         $CMD_PIP install $PIP_MIRROR_OPT $@ | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 914 | } | 
 | 915 |  | 
 | 916 |  | 
 | 917 | # Service wrapper to restart services | 
 | 918 | # restart_service service-name | 
 | 919 | function restart_service() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 920 |     if is_ubuntu; then | 
| Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 921 |         sudo /usr/sbin/service $1 restart | 
 | 922 |     else | 
 | 923 |         sudo /sbin/service $1 restart | 
 | 924 |     fi | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 925 | } | 
 | 926 |  | 
 | 927 |  | 
| Dean Troyer | 681f3fd | 2013-02-27 19:00:39 -0600 | [diff] [blame] | 928 | # _run_process() is designed to be backgrounded by run_process() to simulate a | 
 | 929 | # fork.  It includes the dirty work of closing extra filehandles and preparing log | 
 | 930 | # files to produce the same logs as screen_it().  The log filename is derived | 
 | 931 | # from the service name and global-and-now-misnamed SCREEN_LOGDIR | 
 | 932 | # _run_process service "command-line" | 
 | 933 | function _run_process() { | 
 | 934 |     local service=$1 | 
 | 935 |     local command="$2" | 
 | 936 |  | 
 | 937 |     # Undo logging redirections and close the extra descriptors | 
 | 938 |     exec 1>&3 | 
 | 939 |     exec 2>&3 | 
 | 940 |     exec 3>&- | 
 | 941 |     exec 6>&- | 
 | 942 |  | 
 | 943 |     if [[ -n ${SCREEN_LOGDIR} ]]; then | 
 | 944 |         exec 1>&${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log 2>&1 | 
 | 945 |         ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log | 
 | 946 |  | 
 | 947 |         # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs. | 
 | 948 |         export PYTHONUNBUFFERED=1 | 
 | 949 |     fi | 
 | 950 |  | 
 | 951 |     exec /bin/bash -c "$command" | 
 | 952 |     die "$service exec failure: $command" | 
 | 953 | } | 
 | 954 |  | 
 | 955 |  | 
 | 956 | # run_process() launches a child process that closes all file descriptors and | 
 | 957 | # then exec's the passed in command.  This is meant to duplicate the semantics | 
 | 958 | # of screen_it() without screen.  PIDs are written to | 
 | 959 | # $SERVICE_DIR/$SCREEN_NAME/$service.pid | 
 | 960 | # run_process service "command-line" | 
 | 961 | function run_process() { | 
 | 962 |     local service=$1 | 
 | 963 |     local command="$2" | 
 | 964 |  | 
 | 965 |     # Spawn the child process | 
 | 966 |     _run_process "$service" "$command" & | 
 | 967 |     echo $! | 
 | 968 | } | 
 | 969 |  | 
 | 970 |  | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 971 | # Helper to launch a service in a named screen | 
 | 972 | # screen_it service "command-line" | 
 | 973 | function screen_it { | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 974 |     SCREEN_NAME=${SCREEN_NAME:-stack} | 
| jiajun xu | a941424 | 2012-12-06 16:30:57 +0800 | [diff] [blame] | 975 |     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
| Dean Troyer | 681f3fd | 2013-02-27 19:00:39 -0600 | [diff] [blame] | 976 |     USE_SCREEN=$(trueorfalse True $USE_SCREEN) | 
| jiajun xu | a941424 | 2012-12-06 16:30:57 +0800 | [diff] [blame] | 977 |  | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 978 |     if is_service_enabled $1; then | 
 | 979 |         # Append the service to the screen rc file | 
 | 980 |         screen_rc "$1" "$2" | 
 | 981 |  | 
| Dean Troyer | 681f3fd | 2013-02-27 19:00:39 -0600 | [diff] [blame] | 982 |         if [[ "$USE_SCREEN" = "True" ]]; then | 
 | 983 |             screen -S $SCREEN_NAME -X screen -t $1 | 
| Jeremy Stanley | 25ebbcd | 2013-02-17 15:45:55 +0000 | [diff] [blame] | 984 |  | 
| Dean Troyer | 681f3fd | 2013-02-27 19:00:39 -0600 | [diff] [blame] | 985 |             if [[ -n ${SCREEN_LOGDIR} ]]; then | 
 | 986 |                 screen -S $SCREEN_NAME -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log | 
 | 987 |                 screen -S $SCREEN_NAME -p $1 -X log on | 
 | 988 |                 ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log | 
 | 989 |             fi | 
| Jeremy Stanley | 25ebbcd | 2013-02-17 15:45:55 +0000 | [diff] [blame] | 990 |  | 
| Vishvananda Ishaya | 58e2134 | 2013-02-11 16:48:12 -0800 | [diff] [blame] | 991 |             # sleep to allow bash to be ready to be send the command - we are | 
 | 992 |             # creating a new window in screen and then sends characters, so if | 
 | 993 |             # bash isn't running by the time we send the command, nothing happens | 
 | 994 |             sleep 1.5 | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 995 |  | 
| Vishvananda Ishaya | 58e2134 | 2013-02-11 16:48:12 -0800 | [diff] [blame] | 996 |             NL=`echo -ne '\015'` | 
 | 997 |             screen -S $SCREEN_NAME -p $1 -X stuff "$2 || touch \"$SERVICE_DIR/$SCREEN_NAME/$1.failure\"$NL" | 
 | 998 |         else | 
| Dean Troyer | 681f3fd | 2013-02-27 19:00:39 -0600 | [diff] [blame] | 999 |             # Spawn directly without screen | 
 | 1000 |             run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$service.pid | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 1001 |         fi | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 1002 |     fi | 
 | 1003 | } | 
 | 1004 |  | 
 | 1005 |  | 
 | 1006 | # Screen rc file builder | 
 | 1007 | # screen_rc service "command-line" | 
 | 1008 | function screen_rc { | 
 | 1009 |     SCREEN_NAME=${SCREEN_NAME:-stack} | 
 | 1010 |     SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc | 
 | 1011 |     if [[ ! -e $SCREENRC ]]; then | 
 | 1012 |         # Name the screen session | 
 | 1013 |         echo "sessionname $SCREEN_NAME" > $SCREENRC | 
 | 1014 |         # Set a reasonable statusbar | 
 | 1015 |         echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC | 
 | 1016 |         echo "screen -t shell bash" >> $SCREENRC | 
 | 1017 |     fi | 
 | 1018 |     # If this service doesn't already exist in the screenrc file | 
 | 1019 |     if ! grep $1 $SCREENRC 2>&1 > /dev/null; then | 
 | 1020 |         NL=`echo -ne '\015'` | 
 | 1021 |         echo "screen -t $1 bash" >> $SCREENRC | 
 | 1022 |         echo "stuff \"$2$NL\"" >> $SCREENRC | 
 | 1023 |     fi | 
 | 1024 | } | 
 | 1025 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1026 |  | 
| jiajun xu | a941424 | 2012-12-06 16:30:57 +0800 | [diff] [blame] | 1027 | # Helper to remove the *.failure files under $SERVICE_DIR/$SCREEN_NAME | 
 | 1028 | # This is used for service_check when all the screen_it are called finished | 
 | 1029 | # init_service_check | 
 | 1030 | function init_service_check() { | 
 | 1031 |     SCREEN_NAME=${SCREEN_NAME:-stack} | 
 | 1032 |     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
 | 1033 |  | 
 | 1034 |     if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then | 
 | 1035 |         mkdir -p "$SERVICE_DIR/$SCREEN_NAME" | 
 | 1036 |     fi | 
 | 1037 |  | 
 | 1038 |     rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure | 
 | 1039 | } | 
 | 1040 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1041 |  | 
| jiajun xu | a941424 | 2012-12-06 16:30:57 +0800 | [diff] [blame] | 1042 | # Helper to get the status of each running service | 
 | 1043 | # service_check | 
 | 1044 | function service_check() { | 
 | 1045 |     local service | 
 | 1046 |     local failures | 
 | 1047 |     SCREEN_NAME=${SCREEN_NAME:-stack} | 
 | 1048 |     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} | 
 | 1049 |  | 
 | 1050 |  | 
 | 1051 |     if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then | 
 | 1052 |         echo "No service status directory found" | 
 | 1053 |         return | 
 | 1054 |     fi | 
 | 1055 |  | 
 | 1056 |     # Check if there is any falure flag file under $SERVICE_DIR/$SCREEN_NAME | 
 | 1057 |     failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null` | 
 | 1058 |  | 
 | 1059 |     for service in $failures; do | 
 | 1060 |         service=`basename $service` | 
 | 1061 |         service=${service::-8} | 
 | 1062 |         echo "Error: Service $service is not running" | 
 | 1063 |     done | 
 | 1064 |  | 
 | 1065 |     if [ -n "$failures" ]; then | 
 | 1066 |         echo "More details about the above errors can be found with screen, with ./rejoin-stack.sh" | 
 | 1067 |     fi | 
 | 1068 | } | 
| Dean Troyer | 1573335 | 2012-09-06 11:51:30 -0500 | [diff] [blame] | 1069 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1070 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 1071 | # ``pip install`` the dependencies of the package before ``setup.py develop`` | 
 | 1072 | # so pip and not distutils processes the dependency chain | 
 | 1073 | # Uses globals ``TRACK_DEPENDES``, ``*_proxy` | 
| Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 1074 | # setup_develop directory | 
 | 1075 | function setup_develop() { | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 1076 |     if [[ $TRACK_DEPENDS = True ]]; then | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 1077 |         SUDO_CMD="env" | 
 | 1078 |     else | 
 | 1079 |         SUDO_CMD="sudo" | 
 | 1080 |     fi | 
| Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 1081 |     (cd $1; \ | 
 | 1082 |         python setup.py egg_info; \ | 
 | 1083 |         raw_links=$(awk '/^.+/ {print "-f " $1}' *.egg-info/dependency_links.txt); \ | 
 | 1084 |         depend_links=$(echo $raw_links | xargs); \ | 
| Dean Troyer | 1a3c9fe | 2012-09-29 17:25:02 -0500 | [diff] [blame] | 1085 |         require_file=$([ ! -r *-info/requires.txt ] || echo "-r *-info/requires.txt"); \ | 
 | 1086 |         pip_install $require_file $depend_links; \ | 
| Monty Taylor | 47f0206 | 2012-07-26 11:09:24 -0500 | [diff] [blame] | 1087 |         $SUDO_CMD \ | 
| Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 1088 |             HTTP_PROXY=$http_proxy \ | 
 | 1089 |             HTTPS_PROXY=$https_proxy \ | 
| Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 1090 |             NO_PROXY=$no_proxy \ | 
| Dean Troyer | bbafb1b | 2012-06-11 16:51:39 -0500 | [diff] [blame] | 1091 |             python setup.py develop \ | 
 | 1092 |     ) | 
 | 1093 | } | 
 | 1094 |  | 
 | 1095 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 1096 | # Service wrapper to start services | 
 | 1097 | # start_service service-name | 
 | 1098 | function start_service() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 1099 |     if is_ubuntu; then | 
| Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 1100 |         sudo /usr/sbin/service $1 start | 
 | 1101 |     else | 
 | 1102 |         sudo /sbin/service $1 start | 
 | 1103 |     fi | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 1104 | } | 
 | 1105 |  | 
 | 1106 |  | 
 | 1107 | # Service wrapper to stop services | 
 | 1108 | # stop_service service-name | 
 | 1109 | function stop_service() { | 
| Vincent Untz | c18b965 | 2012-12-04 12:36:34 +0100 | [diff] [blame] | 1110 |     if is_ubuntu; then | 
| Dean Troyer | 5218d45 | 2012-02-04 02:13:23 -0600 | [diff] [blame] | 1111 |         sudo /usr/sbin/service $1 stop | 
 | 1112 |     else | 
 | 1113 |         sudo /sbin/service $1 stop | 
 | 1114 |     fi | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 1115 | } | 
 | 1116 |  | 
 | 1117 |  | 
 | 1118 | # Normalize config values to True or False | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 1119 | # Accepts as False: 0 no false False FALSE | 
 | 1120 | # Accepts as True: 1 yes true True TRUE | 
 | 1121 | # VAR=$(trueorfalse default-value test-value) | 
| Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 1122 | function trueorfalse() { | 
 | 1123 |     local default=$1 | 
 | 1124 |     local testval=$2 | 
 | 1125 |  | 
 | 1126 |     [[ -z "$testval" ]] && { echo "$default"; return; } | 
 | 1127 |     [[ "0 no false False FALSE" =~ "$testval" ]] && { echo "False"; return; } | 
 | 1128 |     [[ "1 yes true True TRUE" =~ "$testval" ]] && { echo "True"; return; } | 
 | 1129 |     echo "$default" | 
 | 1130 | } | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 1131 |  | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 1132 |  | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 1133 | # Retrieve an image from a URL and upload into Glance | 
 | 1134 | # Uses the following variables: | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 1135 | #   ``FILES`` must be set to the cache dir | 
 | 1136 | #   ``GLANCE_HOSTPORT`` | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 1137 | # upload_image image-url glance-token | 
 | 1138 | function upload_image() { | 
 | 1139 |     local image_url=$1 | 
 | 1140 |     local token=$2 | 
 | 1141 |  | 
 | 1142 |     # Create a directory for the downloaded image tarballs. | 
 | 1143 |     mkdir -p $FILES/images | 
 | 1144 |  | 
 | 1145 |     # Downloads the image (uec ami+aki style), then extracts it. | 
 | 1146 |     IMAGE_FNAME=`basename "$image_url"` | 
 | 1147 |     if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then | 
 | 1148 |         wget -c $image_url -O $FILES/$IMAGE_FNAME | 
 | 1149 |         if [[ $? -ne 0 ]]; then | 
 | 1150 |             echo "Not found: $image_url" | 
 | 1151 |             return | 
 | 1152 |         fi | 
 | 1153 |     fi | 
 | 1154 |  | 
 | 1155 |     # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading | 
 | 1156 |     if [[ "$image_url" =~ 'openvz' ]]; then | 
 | 1157 |         IMAGE="$FILES/${IMAGE_FNAME}" | 
 | 1158 |         IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" | 
 | 1159 |         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}" | 
 | 1160 |         return | 
 | 1161 |     fi | 
 | 1162 |  | 
| Davanum Srinivas | 316ed6c | 2013-02-06 15:29:49 -0500 | [diff] [blame] | 1163 |     # XenServer-ovf-format images are provided as .vhd.tgz as well | 
 | 1164 |     # and should not be decompressed prior to loading | 
 | 1165 |     if [[ "$image_url" =~ '.vhd.tgz' ]]; then | 
 | 1166 |         IMAGE="$FILES/${IMAGE_FNAME}" | 
 | 1167 |         IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}" | 
 | 1168 |         glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format=ovf --disk-format=vhd < "${IMAGE}" | 
 | 1169 |         return | 
 | 1170 |     fi | 
 | 1171 |  | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 1172 |     KERNEL="" | 
 | 1173 |     RAMDISK="" | 
 | 1174 |     DISK_FORMAT="" | 
 | 1175 |     CONTAINER_FORMAT="" | 
 | 1176 |     UNPACK="" | 
 | 1177 |     case "$IMAGE_FNAME" in | 
 | 1178 |         *.tar.gz|*.tgz) | 
 | 1179 |             # Extract ami and aki files | 
 | 1180 |             [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] && | 
 | 1181 |                 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" || | 
 | 1182 |                 IMAGE_NAME="${IMAGE_FNAME%.tgz}" | 
 | 1183 |             xdir="$FILES/images/$IMAGE_NAME" | 
 | 1184 |             rm -Rf "$xdir"; | 
 | 1185 |             mkdir "$xdir" | 
 | 1186 |             tar -zxf $FILES/$IMAGE_FNAME -C "$xdir" | 
 | 1187 |             KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do | 
 | 1188 |                      [ -f "$f" ] && echo "$f" && break; done; true) | 
 | 1189 |             RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do | 
 | 1190 |                      [ -f "$f" ] && echo "$f" && break; done; true) | 
 | 1191 |             IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do | 
 | 1192 |                      [ -f "$f" ] && echo "$f" && break; done; true) | 
 | 1193 |             if [[ -z "$IMAGE_NAME" ]]; then | 
 | 1194 |                 IMAGE_NAME=$(basename "$IMAGE" ".img") | 
 | 1195 |             fi | 
 | 1196 |             ;; | 
 | 1197 |         *.img) | 
 | 1198 |             IMAGE="$FILES/$IMAGE_FNAME"; | 
 | 1199 |             IMAGE_NAME=$(basename "$IMAGE" ".img") | 
| Dean Troyer | 636a3ff | 2012-09-14 11:36:07 -0500 | [diff] [blame] | 1200 |             format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }') | 
 | 1201 |             if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then | 
 | 1202 |                 DISK_FORMAT=$format | 
 | 1203 |             else | 
 | 1204 |                 DISK_FORMAT=raw | 
 | 1205 |             fi | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 1206 |             CONTAINER_FORMAT=bare | 
 | 1207 |             ;; | 
 | 1208 |         *.img.gz) | 
 | 1209 |             IMAGE="$FILES/${IMAGE_FNAME}" | 
 | 1210 |             IMAGE_NAME=$(basename "$IMAGE" ".img.gz") | 
 | 1211 |             DISK_FORMAT=raw | 
 | 1212 |             CONTAINER_FORMAT=bare | 
 | 1213 |             UNPACK=zcat | 
 | 1214 |             ;; | 
 | 1215 |         *.qcow2) | 
 | 1216 |             IMAGE="$FILES/${IMAGE_FNAME}" | 
 | 1217 |             IMAGE_NAME=$(basename "$IMAGE" ".qcow2") | 
 | 1218 |             DISK_FORMAT=qcow2 | 
 | 1219 |             CONTAINER_FORMAT=bare | 
 | 1220 |             ;; | 
| Jonathan Michalon | 0680204 | 2013-03-21 14:29:58 +0100 | [diff] [blame] | 1221 |         *.iso) | 
 | 1222 |             IMAGE="$FILES/${IMAGE_FNAME}" | 
 | 1223 |             IMAGE_NAME=$(basename "$IMAGE" ".iso") | 
 | 1224 |             DISK_FORMAT=iso | 
 | 1225 |             CONTAINER_FORMAT=bare | 
 | 1226 |             ;; | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 1227 |         *) echo "Do not know what to do with $IMAGE_FNAME"; false;; | 
 | 1228 |     esac | 
 | 1229 |  | 
 | 1230 |     if [ "$CONTAINER_FORMAT" = "bare" ]; then | 
 | 1231 |         if [ "$UNPACK" = "zcat" ]; then | 
 | 1232 |             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}") | 
 | 1233 |         else | 
 | 1234 |             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}" | 
 | 1235 |         fi | 
 | 1236 |     else | 
 | 1237 |         # Use glance client to add the kernel the root filesystem. | 
 | 1238 |         # We parse the results of the first upload to get the glance ID of the | 
 | 1239 |         # kernel for use when uploading the root filesystem. | 
 | 1240 |         KERNEL_ID=""; RAMDISK_ID=""; | 
 | 1241 |         if [ -n "$KERNEL" ]; then | 
 | 1242 |             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) | 
 | 1243 |         fi | 
 | 1244 |         if [ -n "$RAMDISK" ]; then | 
 | 1245 |             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) | 
 | 1246 |         fi | 
 | 1247 |         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}" | 
 | 1248 |     fi | 
 | 1249 | } | 
 | 1250 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1251 |  | 
| Dean Troyer | c1b486a | 2012-11-05 14:26:09 -0600 | [diff] [blame] | 1252 | # Set the database backend to use | 
 | 1253 | # When called from stackrc/localrc DATABASE_BACKENDS has not been | 
 | 1254 | # initialized yet, just save the configuration selection and call back later | 
 | 1255 | # to validate it. | 
 | 1256 | #  $1 The name of the database backend to use (mysql, postgresql, ...) | 
 | 1257 | function use_database { | 
 | 1258 |     if [[ -z "$DATABASE_BACKENDS" ]]; then | 
| Dean Troyer | afc29fe | 2013-02-07 15:56:24 -0600 | [diff] [blame] | 1259 |         # No backends registered means this is likely called from ``localrc`` | 
 | 1260 |         # This is now deprecated usage | 
| Dean Troyer | c1b486a | 2012-11-05 14:26:09 -0600 | [diff] [blame] | 1261 |         DATABASE_TYPE=$1 | 
| Bob Ball | 3aa8887 | 2013-02-28 17:39:41 +0000 | [diff] [blame] | 1262 |         DEPRECATED_TEXT="$DEPRECATED_TEXT\nThe database backend needs to be properly set in ENABLED_SERVICES; use_database is deprecated localrc\n" | 
| Attila Fazekas | 251d3b5 | 2012-12-16 15:05:44 +0100 | [diff] [blame] | 1263 |     else | 
| Dean Troyer | afc29fe | 2013-02-07 15:56:24 -0600 | [diff] [blame] | 1264 |         # This should no longer get called...here for posterity | 
| Attila Fazekas | 251d3b5 | 2012-12-16 15:05:44 +0100 | [diff] [blame] | 1265 |         use_exclusive_service DATABASE_BACKENDS DATABASE_TYPE $1 | 
| Dean Troyer | c1b486a | 2012-11-05 14:26:09 -0600 | [diff] [blame] | 1266 |     fi | 
| Dean Troyer | c1b486a | 2012-11-05 14:26:09 -0600 | [diff] [blame] | 1267 | } | 
 | 1268 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1269 |  | 
| Terry Wilson | 428af5a | 2012-11-01 16:12:39 -0400 | [diff] [blame] | 1270 | # Toggle enable/disable_service for services that must run exclusive of each other | 
 | 1271 | #  $1 The name of a variable containing a space-separated list of services | 
 | 1272 | #  $2 The name of a variable in which to store the enabled service's name | 
 | 1273 | #  $3 The name of the service to enable | 
 | 1274 | function use_exclusive_service { | 
 | 1275 |     local options=${!1} | 
 | 1276 |     local selection=$3 | 
 | 1277 |     out=$2 | 
 | 1278 |     [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1 | 
 | 1279 |     for opt in $options;do | 
 | 1280 |         [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt | 
 | 1281 |     done | 
 | 1282 |     eval "$out=$selection" | 
 | 1283 |     return 0 | 
 | 1284 | } | 
| Dean Troyer | ca0e3d0 | 2012-04-13 15:58:37 -0500 | [diff] [blame] | 1285 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1286 |  | 
| Dean Troyer | 3a3a2ba | 2012-12-11 15:26:24 -0600 | [diff] [blame] | 1287 | # Wait for an HTTP server to start answering requests | 
 | 1288 | # wait_for_service timeout url | 
 | 1289 | function wait_for_service() { | 
 | 1290 |     local timeout=$1 | 
 | 1291 |     local url=$2 | 
 | 1292 |     timeout $timeout sh -c "while ! http_proxy= https_proxy= curl -s $url >/dev/null; do sleep 1; done" | 
 | 1293 | } | 
 | 1294 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1295 |  | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 1296 | # Wrapper for ``yum`` to set proxy environment variables | 
 | 1297 | # Uses globals ``OFFLINE``, ``*_proxy` | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 1298 | # yum_install package [package ...] | 
 | 1299 | function yum_install() { | 
 | 1300 |     [[ "$OFFLINE" = "True" ]] && return | 
 | 1301 |     local sudo="sudo" | 
 | 1302 |     [[ "$(id -u)" = "0" ]] && sudo="env" | 
 | 1303 |     $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ | 
| Osamu Habuka | 7abe4f2 | 2012-07-25 12:39:32 +0900 | [diff] [blame] | 1304 |         no_proxy=$no_proxy \ | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 1305 |         yum install -y "$@" | 
 | 1306 | } | 
 | 1307 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1308 |  | 
 | 1309 | # zypper wrapper to set arguments correctly | 
 | 1310 | # zypper_install package [package ...] | 
 | 1311 | function zypper_install() { | 
 | 1312 |     [[ "$OFFLINE" = "True" ]] && return | 
 | 1313 |     local sudo="sudo" | 
 | 1314 |     [[ "$(id -u)" = "0" ]] && sudo="env" | 
 | 1315 |     $sudo http_proxy=$http_proxy https_proxy=$https_proxy \ | 
 | 1316 |         zypper --non-interactive install --auto-agree-with-licenses "$@" | 
 | 1317 | } | 
 | 1318 |  | 
 | 1319 |  | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1320 | # ping check | 
 | 1321 | # Uses globals ``ENABLED_SERVICES`` | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1322 | # ping_check from-net ip boot-timeout expected | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1323 | function ping_check() { | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1324 |     if is_service_enabled quantum; then | 
 | 1325 |         _ping_check_quantum  "$1" $2 $3 $4 | 
 | 1326 |         return | 
 | 1327 |     fi | 
 | 1328 |     _ping_check_novanet "$1" $2 $3 $4 | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1329 | } | 
 | 1330 |  | 
 | 1331 | # ping check for nova | 
 | 1332 | # Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK`` | 
 | 1333 | function _ping_check_novanet() { | 
 | 1334 |     local from_net=$1 | 
 | 1335 |     local ip=$2 | 
 | 1336 |     local boot_timeout=$3 | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1337 |     local expected=${4:-"True"} | 
 | 1338 |     local check_command="" | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1339 |     MULTI_HOST=`trueorfalse False $MULTI_HOST` | 
 | 1340 |     if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then | 
 | 1341 |         sleep $boot_timeout | 
 | 1342 |         return | 
 | 1343 |     fi | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1344 |     if [[ "$expected" = "True" ]]; then | 
 | 1345 |         check_command="while ! ping -c1 -w1 $ip; do sleep 1; done" | 
 | 1346 |     else | 
 | 1347 |         check_command="while ping -c1 -w1 $ip; do sleep 1; done" | 
 | 1348 |     fi | 
 | 1349 |     if ! timeout $boot_timeout sh -c "$check_command"; then | 
 | 1350 |         if [[ "$expected" = "True" ]]; then | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 1351 |             die $LINENO "[Fail] Couldn't ping server" | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1352 |         else | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 1353 |             die $LINENO "[Fail] Could ping server" | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1354 |         fi | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1355 |         exit 1 | 
 | 1356 |     fi | 
 | 1357 | } | 
 | 1358 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1359 |  | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1360 | # ssh check | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1361 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1362 | # ssh_check net-name key-file floating-ip default-user active-timeout | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1363 | function ssh_check() { | 
| Nachi Ueno | 5db5bfa | 2012-10-29 11:25:29 -0700 | [diff] [blame] | 1364 |     if is_service_enabled quantum; then | 
 | 1365 |         _ssh_check_quantum  "$1" $2 $3 $4 $5 | 
 | 1366 |         return | 
 | 1367 |     fi | 
 | 1368 |     _ssh_check_novanet "$1" $2 $3 $4 $5 | 
 | 1369 | } | 
 | 1370 |  | 
 | 1371 | function _ssh_check_novanet() { | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1372 |     local NET_NAME=$1 | 
 | 1373 |     local KEY_FILE=$2 | 
 | 1374 |     local FLOATING_IP=$3 | 
 | 1375 |     local DEFAULT_INSTANCE_USER=$4 | 
 | 1376 |     local ACTIVE_TIMEOUT=$5 | 
| Dean Troyer | 6931c13 | 2012-11-07 16:51:21 -0600 | [diff] [blame] | 1377 |     local probe_cmd="" | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 1378 |     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 | 
| Nachi Ueno | 07115eb | 2013-02-26 12:38:18 -0800 | [diff] [blame] | 1379 |         die $LINENO "server didn't become ssh-able!" | 
| Nachi Ueno | fda946e | 2012-10-24 17:26:02 -0700 | [diff] [blame] | 1380 |     fi | 
 | 1381 | } | 
| Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 1382 |  | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1383 |  | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1384 | # Add a user to a group. | 
 | 1385 | # add_user_to_group user group | 
 | 1386 | function add_user_to_group() { | 
 | 1387 |     local user=$1 | 
 | 1388 |     local group=$2 | 
 | 1389 |  | 
 | 1390 |     if [[ -z "$os_VENDOR" ]]; then | 
 | 1391 |         GetOSVersion | 
 | 1392 |     fi | 
 | 1393 |  | 
 | 1394 |     # SLE11 and openSUSE 12.2 don't have the usual usermod | 
 | 1395 |     if ! is_suse || [[ "$os_VENDOR" = "openSUSE" && "$os_RELEASE" != "12.2" ]]; then | 
 | 1396 |         sudo usermod -a -G "$group" "$user" | 
 | 1397 |     else | 
 | 1398 |         sudo usermod -A "$group" "$user" | 
 | 1399 |     fi | 
 | 1400 | } | 
 | 1401 |  | 
 | 1402 |  | 
| Jakub Ruzicka | 4196d55 | 2013-01-30 15:35:54 +0100 | [diff] [blame] | 1403 | # Get the path to the direcotry where python executables are installed. | 
 | 1404 | # get_python_exec_prefix | 
 | 1405 | function get_python_exec_prefix() { | 
 | 1406 |     if is_fedora; then | 
 | 1407 |         echo "/usr/bin" | 
 | 1408 |     else | 
 | 1409 |         echo "/usr/local/bin" | 
 | 1410 |     fi | 
 | 1411 | } | 
 | 1412 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1413 |  | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1414 | # Get the location of the $module-rootwrap executables, where module is cinder | 
 | 1415 | # or nova. | 
 | 1416 | # get_rootwrap_location module | 
 | 1417 | function get_rootwrap_location() { | 
 | 1418 |     local module=$1 | 
 | 1419 |  | 
| Jakub Ruzicka | 4196d55 | 2013-01-30 15:35:54 +0100 | [diff] [blame] | 1420 |     echo "$(get_python_exec_prefix)/$module-rootwrap" | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1421 | } | 
 | 1422 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1423 |  | 
| Vincent Untz | 8ec2722 | 2012-11-29 09:25:31 +0100 | [diff] [blame] | 1424 | # Get the path to the pip command. | 
 | 1425 | # get_pip_command | 
 | 1426 | function get_pip_command() { | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 1427 |     if is_fedora; then | 
| Nikhil Manchanda | 35138ed | 2013-01-03 17:49:58 -0800 | [diff] [blame] | 1428 |         which pip-python | 
| Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 1429 |     else | 
| Nikhil Manchanda | 35138ed | 2013-01-03 17:49:58 -0800 | [diff] [blame] | 1430 |         which pip | 
| Vincent Untz | 8ec2722 | 2012-11-29 09:25:31 +0100 | [diff] [blame] | 1431 |     fi | 
| Ian Wienand | 535a814 | 2013-05-15 09:25:27 +1000 | [diff] [blame] | 1432 |  | 
 | 1433 |     if [ $? -ne 0 ]; then | 
 | 1434 |         die $LINENO "Unable to find pip; cannot continue" | 
 | 1435 |     fi | 
| Vincent Untz | 8ec2722 | 2012-11-29 09:25:31 +0100 | [diff] [blame] | 1436 | } | 
| Vincent Untz | 856a11e | 2012-11-21 16:04:12 +0100 | [diff] [blame] | 1437 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1438 |  | 
| Ian Wienand | 0488edd | 2013-04-11 12:04:36 +1000 | [diff] [blame] | 1439 | # Path permissions sanity check | 
 | 1440 | # check_path_perm_sanity path | 
 | 1441 | function check_path_perm_sanity() { | 
 | 1442 |     # Ensure no element of the path has 0700 permissions, which is very | 
 | 1443 |     # likely to cause issues for daemons.  Inspired by default 0700 | 
 | 1444 |     # homedir permissions on RHEL and common practice of making DEST in | 
 | 1445 |     # the stack user's homedir. | 
 | 1446 |  | 
 | 1447 |     local real_path=$(readlink -f $1) | 
 | 1448 |     local rebuilt_path="" | 
 | 1449 |     for i in $(echo ${real_path} | tr "/" " "); do | 
 | 1450 |         rebuilt_path=$rebuilt_path"/"$i | 
 | 1451 |  | 
 | 1452 |         if [[ $(stat -c '%a' ${rebuilt_path}) = 700 ]]; then | 
 | 1453 |             echo "*** DEST path element" | 
 | 1454 |             echo "***    ${rebuilt_path}" | 
 | 1455 |             echo "*** appears to have 0700 permissions." | 
 | 1456 |             echo "*** This is very likely to cause fatal issues for devstack daemons." | 
 | 1457 |  | 
 | 1458 |             if [[ -n "$SKIP_PATH_SANITY" ]]; then | 
 | 1459 |                 return | 
 | 1460 |             else | 
 | 1461 |                 echo "*** Set SKIP_PATH_SANITY to skip this check" | 
 | 1462 |                 die $LINENO "Invalid path permissions" | 
 | 1463 |             fi | 
 | 1464 |         fi | 
 | 1465 |     done | 
 | 1466 | } | 
 | 1467 |  | 
| Dean Troyer | 1a6d449 | 2013-06-03 16:47:36 -0500 | [diff] [blame] | 1468 |  | 
| Kyle Mestery | 51a3f1f | 2013-06-13 11:47:56 +0000 | [diff] [blame] | 1469 | # This function recursively compares versions, and is not meant to be | 
 | 1470 | # called by anything other than vercmp_numbers below. This function does | 
 | 1471 | # not work with alphabetic versions. | 
 | 1472 | # | 
 | 1473 | # _vercmp_r sep ver1 ver2 | 
 | 1474 | function _vercmp_r { | 
 | 1475 |   typeset sep | 
 | 1476 |   typeset -a ver1=() ver2=() | 
 | 1477 |   sep=$1; shift | 
 | 1478 |   ver1=("${@:1:sep}") | 
 | 1479 |   ver2=("${@:sep+1}") | 
 | 1480 |  | 
 | 1481 |   if ((ver1 > ver2)); then | 
 | 1482 |     echo 1; return 0 | 
 | 1483 |   elif ((ver2 > ver1)); then | 
 | 1484 |     echo -1; return 0 | 
 | 1485 |   fi | 
 | 1486 |  | 
 | 1487 |   if ((sep <= 1)); then | 
 | 1488 |     echo 0; return 0 | 
 | 1489 |   fi | 
 | 1490 |  | 
 | 1491 |   _vercmp_r $((sep-1)) "${ver1[@]:1}" "${ver2[@]:1}" | 
 | 1492 | } | 
 | 1493 |  | 
 | 1494 |  | 
 | 1495 | # This function compares two versions and is meant to be called by | 
 | 1496 | # external callers. Please note the function assumes non-alphabetic | 
 | 1497 | # versions. For example, this will work: | 
 | 1498 | # | 
 | 1499 | #   vercmp_numbers 1.10 1.4 | 
 | 1500 | # | 
 | 1501 | # The above will return "1", as 1.10 is greater than 1.4. | 
 | 1502 | # | 
 | 1503 | #   vercmp_numbers 5.2 6.4 | 
 | 1504 | # | 
 | 1505 | # The above will return "-1", as 5.2 is less than 6.4. | 
 | 1506 | # | 
 | 1507 | #   vercmp_numbers 4.0 4.0 | 
 | 1508 | # | 
 | 1509 | # The above will return "0", as the versions are equal. | 
 | 1510 | # | 
 | 1511 | # vercmp_numbers ver1 ver2 | 
 | 1512 | vercmp_numbers() { | 
 | 1513 |   typeset v1=$1 v2=$2 sep | 
 | 1514 |   typeset -a ver1 ver2 | 
 | 1515 |  | 
 | 1516 |   IFS=. read -ra ver1 <<< "$v1" | 
 | 1517 |   IFS=. read -ra ver2 <<< "$v2" | 
 | 1518 |  | 
 | 1519 |   _vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}" | 
 | 1520 | } | 
 | 1521 |  | 
 | 1522 |  | 
| Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 1523 | # Restore xtrace | 
| Chmouel Boudjnah | 408b009 | 2012-03-15 23:21:55 +0000 | [diff] [blame] | 1524 | $XTRACE | 
| Dean Troyer | 4a43b7b | 2012-08-28 17:43:40 -0500 | [diff] [blame] | 1525 |  | 
 | 1526 |  | 
 | 1527 | # Local variables: | 
| Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 1528 | # mode: shell-script | 
| Andrew Laski | f900bd7 | 2012-09-05 17:23:14 -0400 | [diff] [blame] | 1529 | # End: |