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