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