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