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