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