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