blob: cf140072fd783e937764dfed74d13d68aecac4a6 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyerdff49a22014-01-30 15:37:40 -06003# functions-common - Common functions used by DevStack components
4#
5# The canonical copy of this file is maintained in the DevStack repo.
6# All modifications should be made there and then sync'ed to other repos
7# as required.
8#
9# This file is sorted alphabetically within the function groups.
10#
11# - Config Functions
12# - Control Functions
13# - Distro Functions
14# - Git Functions
15# - OpenStack Functions
16# - Package Functions
17# - Process Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060018# - Service Functions
Masayuki Igawaf6368d32014-02-20 13:31:26 +090019# - System Functions
Dean Troyerdff49a22014-01-30 15:37:40 -060020#
21# The following variables are assumed to be defined by certain functions:
22#
23# - ``ENABLED_SERVICES``
24# - ``ERROR_ON_CLONE``
25# - ``FILES``
26# - ``OFFLINE``
Dean Troyerdff49a22014-01-30 15:37:40 -060027# - ``RECLONE``
Masayuki Igawad20f6322014-02-28 09:22:37 +090028# - ``REQUIREMENTS_DIR``
29# - ``STACK_USER``
Dean Troyerdff49a22014-01-30 15:37:40 -060030# - ``TRACK_DEPENDS``
31# - ``http_proxy``, ``https_proxy``, ``no_proxy``
Dean Troyer3324f192014-09-18 09:26:39 -050032#
Dean Troyerdff49a22014-01-30 15:37:40 -060033
34# Save trace setting
35XTRACE=$(set +o | grep xtrace)
36set +o xtrace
37
Ian Wienand4ffb4542015-06-30 11:00:32 +100038# ensure we don't re-source this in the same environment
39[[ -z "$_DEVSTACK_FUNCTIONS_COMMON" ]] || return 0
40declare -r _DEVSTACK_FUNCTIONS_COMMON=1
41
Sean Daguecc524062014-10-01 09:06:43 -040042# Global Config Variables
43declare -A GITREPO
44declare -A GITBRANCH
45declare -A GITDIR
46
Sean Dague53753292014-12-04 19:38:15 -050047TRACK_DEPENDS=${TRACK_DEPENDS:-False}
48
Dean Troyer68162342015-05-13 15:41:03 -050049# Save these variables to .stackenv
50STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
51 KEYSTONE_AUTH_PROTOCOL KEYSTONE_AUTH_URI KEYSTONE_SERVICE_URI \
Brian Haley180f5eb2015-06-16 13:14:31 -040052 LOGFILE OS_CACERT SERVICE_HOST SERVICE_PROTOCOL STACK_USER TLS_IP \
Brian Haley5d04db22015-06-16 13:14:31 -040053 HOST_IPV6 SERVICE_IP_VERSION"
Dean Troyer68162342015-05-13 15:41:03 -050054
55
56# Saves significant environment variables to .stackenv for later use
57# Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
58# function, the rest are simply saved and do not cause problems if they are undefined.
59# save_stackenv [tag]
60function save_stackenv {
61 local tag=${1:-""}
62 # Save some values we generated for later use
63 time_stamp=$(date "+$TIMESTAMP_FORMAT")
64 echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
65 for i in $STACK_ENV_VARS; do
66 echo $i=${!i} >>$TOP_DIR/.stackenv
67 done
68}
Dean Troyerdff49a22014-01-30 15:37:40 -060069
Monty Taylor7224eec2015-09-19 11:26:18 -040070# Update/create user clouds.yaml file.
71# clouds.yaml will have
72# - A `devstack` entry for the `demo` user for the `demo` project.
73# - A `devstack-admin` entry for the `admin` user for the `admin` project.
74# write_clouds_yaml
75function write_clouds_yaml {
76 # The location is a variable to allow for easier refactoring later to make it
77 # overridable. There is currently no usecase where doing so makes sense, so
78 # it's not currently configurable.
79 CLOUDS_YAML=~/.config/openstack/clouds.yaml
80
81 mkdir -p $(dirname $CLOUDS_YAML)
82
83 CA_CERT_ARG=''
84 if [ -f "$SSL_BUNDLE_FILE" ]; then
85 CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
86 fi
87 $TOP_DIR/tools/update_clouds_yaml.py \
88 --file $CLOUDS_YAML \
89 --os-cloud devstack \
90 --os-region-name $REGION_NAME \
91 --os-identity-api-version $IDENTITY_API_VERSION \
92 $CA_CERT_ARG \
93 --os-auth-url $KEYSTONE_AUTH_URI/v$IDENTITY_API_VERSION \
94 --os-username demo \
95 --os-password $ADMIN_PASSWORD \
96 --os-project-name demo
97 $TOP_DIR/tools/update_clouds_yaml.py \
98 --file $CLOUDS_YAML \
99 --os-cloud devstack-admin \
100 --os-region-name $REGION_NAME \
101 --os-identity-api-version $IDENTITY_API_VERSION \
102 $CA_CERT_ARG \
103 --os-auth-url $KEYSTONE_AUTH_URI/v$IDENTITY_API_VERSION \
104 --os-username admin \
105 --os-password $ADMIN_PASSWORD \
106 --os-project-name admin
107}
108
Dean Troyerdff49a22014-01-30 15:37:40 -0600109# Normalize config values to True or False
110# Accepts as False: 0 no No NO false False FALSE
111# Accepts as True: 1 yes Yes YES true True TRUE
112# VAR=$(trueorfalse default-value test-value)
Ian Wienandaee18c72014-02-21 15:35:08 +1100113function trueorfalse {
Sean Dague45917cc2014-02-24 16:09:14 -0500114 local xtrace=$(set +o | grep xtrace)
115 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600116
Mahito OGURA98f59aa2015-05-11 18:02:34 +0900117 local default=$1
118 local testval=${!2:-}
119
120 case "$testval" in
121 "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
122 "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
123 * ) echo "$default" ;;
124 esac
125
Sean Dague45917cc2014-02-24 16:09:14 -0500126 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600127}
128
Attila Fazekas1bd79592015-02-24 14:06:56 +0100129function isset {
130 [[ -v "$1" ]]
131}
Dean Troyerdff49a22014-01-30 15:37:40 -0600132
Dean Troyer68162342015-05-13 15:41:03 -0500133
Dean Troyerdff49a22014-01-30 15:37:40 -0600134# Control Functions
135# =================
136
137# Prints backtrace info
138# filename:lineno:function
139# backtrace level
140function backtrace {
141 local level=$1
142 local deep=$((${#BASH_SOURCE[@]} - 1))
143 echo "[Call Trace]"
144 while [ $level -le $deep ]; do
145 echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
146 deep=$((deep - 1))
147 done
148}
149
150# Prints line number and "message" then exits
151# die $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100152function die {
Dean Troyerdff49a22014-01-30 15:37:40 -0600153 local exitcode=$?
154 set +o xtrace
155 local line=$1; shift
156 if [ $exitcode == 0 ]; then
157 exitcode=1
158 fi
159 backtrace 2
160 err $line "$*"
Dean Troyera25a6f62014-02-24 16:03:41 -0600161 # Give buffers a second to flush
162 sleep 1
Dean Troyerdff49a22014-01-30 15:37:40 -0600163 exit $exitcode
164}
165
166# Checks an environment variable is not set or has length 0 OR if the
167# exit code is non-zero and prints "message" and exits
168# NOTE: env-var is the variable name without a '$'
169# die_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100170function die_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600171 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500172 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600173 set +o xtrace
174 local line=$1; shift
175 local evar=$1; shift
176 if ! is_set $evar || [ $exitcode != 0 ]; then
177 die $line "$*"
178 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500179 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600180}
181
182# Prints line number and "message" in error format
183# err $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100184function err {
Dean Troyerdff49a22014-01-30 15:37:40 -0600185 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500186 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600187 set +o xtrace
188 local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
189 echo $msg 1>&2;
Dean Troyerdde41d02014-12-09 17:47:57 -0600190 if [[ -n ${LOGDIR} ]]; then
191 echo $msg >> "${LOGDIR}/error.log"
Dean Troyerdff49a22014-01-30 15:37:40 -0600192 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500193 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600194 return $exitcode
195}
196
197# Checks an environment variable is not set or has length 0 OR if the
198# exit code is non-zero and prints "message"
199# NOTE: env-var is the variable name without a '$'
200# err_if_not_set $LINENO env-var "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100201function err_if_not_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600202 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500203 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600204 set +o xtrace
205 local line=$1; shift
206 local evar=$1; shift
207 if ! is_set $evar || [ $exitcode != 0 ]; then
208 err $line "$*"
209 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500210 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600211 return $exitcode
212}
213
214# Exit after outputting a message about the distribution not being supported.
215# exit_distro_not_supported [optional-string-telling-what-is-missing]
216function exit_distro_not_supported {
217 if [[ -z "$DISTRO" ]]; then
218 GetDistro
219 fi
220
221 if [ $# -gt 0 ]; then
222 die $LINENO "Support for $DISTRO is incomplete: no support for $@"
223 else
224 die $LINENO "Support for $DISTRO is incomplete."
225 fi
226}
227
228# Test if the named environment variable is set and not zero length
229# is_set env-var
Ian Wienandaee18c72014-02-21 15:35:08 +1100230function is_set {
Dean Troyerdff49a22014-01-30 15:37:40 -0600231 local var=\$"$1"
232 eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
233}
234
235# Prints line number and "message" in warning format
236# warn $LINENO "message"
Ian Wienandaee18c72014-02-21 15:35:08 +1100237function warn {
Dean Troyerdff49a22014-01-30 15:37:40 -0600238 local exitcode=$?
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500239 local xtrace=$(set +o | grep xtrace)
Dean Troyerdff49a22014-01-30 15:37:40 -0600240 set +o xtrace
241 local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
Sean Daguee4af9292015-04-28 08:57:57 -0400242 echo $msg
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500243 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -0600244 return $exitcode
245}
246
247
248# Distro Functions
249# ================
250
251# Determine OS Vendor, Release and Update
252# Tested with OS/X, Ubuntu, RedHat, CentOS, Fedora
253# Returns results in global variables:
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500254# ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
255# ``os_RELEASE`` - major release: ``14.04`` (Ubuntu), ``20`` (Fedora)
256# ``os_UPDATE`` - update: ex. the ``5`` in ``RHEL6.5``
257# ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
258# ``os_CODENAME`` - vendor's codename for release: ``snow leopard``, ``trusty``
Sean Dague53753292014-12-04 19:38:15 -0500259os_VENDOR=""
260os_RELEASE=""
261os_UPDATE=""
262os_PACKAGE=""
263os_CODENAME=""
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500264
Dean Troyerdff49a22014-01-30 15:37:40 -0600265# GetOSVersion
Ian Wienandaee18c72014-02-21 15:35:08 +1100266function GetOSVersion {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500267
Dean Troyerdff49a22014-01-30 15:37:40 -0600268 # Figure out which vendor we are
269 if [[ -x "`which sw_vers 2>/dev/null`" ]]; then
270 # OS/X
271 os_VENDOR=`sw_vers -productName`
272 os_RELEASE=`sw_vers -productVersion`
273 os_UPDATE=${os_RELEASE##*.}
274 os_RELEASE=${os_RELEASE%.*}
275 os_PACKAGE=""
276 if [[ "$os_RELEASE" =~ "10.7" ]]; then
277 os_CODENAME="lion"
278 elif [[ "$os_RELEASE" =~ "10.6" ]]; then
279 os_CODENAME="snow leopard"
280 elif [[ "$os_RELEASE" =~ "10.5" ]]; then
281 os_CODENAME="leopard"
282 elif [[ "$os_RELEASE" =~ "10.4" ]]; then
283 os_CODENAME="tiger"
284 elif [[ "$os_RELEASE" =~ "10.3" ]]; then
285 os_CODENAME="panther"
286 else
287 os_CODENAME=""
288 fi
289 elif [[ -x $(which lsb_release 2>/dev/null) ]]; then
290 os_VENDOR=$(lsb_release -i -s)
291 os_RELEASE=$(lsb_release -r -s)
292 os_UPDATE=""
293 os_PACKAGE="rpm"
294 if [[ "Debian,Ubuntu,LinuxMint" =~ $os_VENDOR ]]; then
295 os_PACKAGE="deb"
296 elif [[ "SUSE LINUX" =~ $os_VENDOR ]]; then
297 lsb_release -d -s | grep -q openSUSE
298 if [[ $? -eq 0 ]]; then
299 os_VENDOR="openSUSE"
300 fi
301 elif [[ $os_VENDOR == "openSUSE project" ]]; then
302 os_VENDOR="openSUSE"
303 elif [[ $os_VENDOR =~ Red.*Hat ]]; then
304 os_VENDOR="Red Hat"
305 fi
306 os_CODENAME=$(lsb_release -c -s)
307 elif [[ -r /etc/redhat-release ]]; then
308 # Red Hat Enterprise Linux Server release 5.5 (Tikanga)
309 # Red Hat Enterprise Linux Server release 7.0 Beta (Maipo)
310 # CentOS release 5.5 (Final)
311 # CentOS Linux release 6.0 (Final)
312 # Fedora release 16 (Verne)
313 # XenServer release 6.2.0-70446c (xenenterprise)
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700314 # Oracle Linux release 7
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300315 # CloudLinux release 7.1
Dean Troyerdff49a22014-01-30 15:37:40 -0600316 os_CODENAME=""
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300317 for r in "Red Hat" CentOS Fedora XenServer CloudLinux; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600318 os_VENDOR=$r
319 if [[ -n "`grep \"$r\" /etc/redhat-release`" ]]; then
320 ver=`sed -e 's/^.* \([0-9].*\) (\(.*\)).*$/\1\|\2/' /etc/redhat-release`
321 os_CODENAME=${ver#*|}
322 os_RELEASE=${ver%|*}
323 os_UPDATE=${os_RELEASE##*.}
324 os_RELEASE=${os_RELEASE%.*}
325 break
326 fi
327 os_VENDOR=""
328 done
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700329 if [ "$os_VENDOR" = "Red Hat" ] && [[ -r /etc/oracle-release ]]; then
330 os_VENDOR=OracleLinux
331 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600332 os_PACKAGE="rpm"
333 elif [[ -r /etc/SuSE-release ]]; then
334 for r in openSUSE "SUSE Linux"; do
335 if [[ "$r" = "SUSE Linux" ]]; then
336 os_VENDOR="SUSE LINUX"
337 else
338 os_VENDOR=$r
339 fi
340
341 if [[ -n "`grep \"$r\" /etc/SuSE-release`" ]]; then
342 os_CODENAME=`grep "CODENAME = " /etc/SuSE-release | sed 's:.* = ::g'`
343 os_RELEASE=`grep "VERSION = " /etc/SuSE-release | sed 's:.* = ::g'`
344 os_UPDATE=`grep "PATCHLEVEL = " /etc/SuSE-release | sed 's:.* = ::g'`
345 break
346 fi
347 os_VENDOR=""
348 done
349 os_PACKAGE="rpm"
350 # If lsb_release is not installed, we should be able to detect Debian OS
351 elif [[ -f /etc/debian_version ]] && [[ $(cat /proc/version) =~ "Debian" ]]; then
352 os_VENDOR="Debian"
353 os_PACKAGE="deb"
354 os_CODENAME=$(awk '/VERSION=/' /etc/os-release | sed 's/VERSION=//' | sed -r 's/\"|\(|\)//g' | awk '{print $2}')
355 os_RELEASE=$(awk '/VERSION_ID=/' /etc/os-release | sed 's/VERSION_ID=//' | sed 's/\"//g')
356 fi
357 export os_VENDOR os_RELEASE os_UPDATE os_PACKAGE os_CODENAME
358}
359
360# Translate the OS version values into common nomenclature
361# Sets global ``DISTRO`` from the ``os_*`` values
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500362declare DISTRO
363
Ian Wienandaee18c72014-02-21 15:35:08 +1100364function GetDistro {
Dean Troyerdff49a22014-01-30 15:37:40 -0600365 GetOSVersion
366 if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) ]]; then
367 # 'Everyone' refers to Ubuntu / Debian releases by the code name adjective
368 DISTRO=$os_CODENAME
369 elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
370 # For Fedora, just use 'f' and the release
371 DISTRO="f$os_RELEASE"
372 elif [[ "$os_VENDOR" =~ (openSUSE) ]]; then
373 DISTRO="opensuse-$os_RELEASE"
374 elif [[ "$os_VENDOR" =~ (SUSE LINUX) ]]; then
375 # For SLE, also use the service pack
376 if [[ -z "$os_UPDATE" ]]; then
377 DISTRO="sle${os_RELEASE}"
378 else
379 DISTRO="sle${os_RELEASE}sp${os_UPDATE}"
380 fi
anju Tiwari6c639c92014-07-15 18:11:54 +0530381 elif [[ "$os_VENDOR" =~ (Red Hat) || \
382 "$os_VENDOR" =~ (CentOS) || \
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700383 "$os_VENDOR" =~ (OracleLinux) ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600384 # Drop the . release as we assume it's compatible
385 DISTRO="rhel${os_RELEASE::1}"
386 elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
387 DISTRO="xs$os_RELEASE"
388 else
389 # Catch-all for now is Vendor + Release + Update
390 DISTRO="$os_VENDOR-$os_RELEASE.$os_UPDATE"
391 fi
392 export DISTRO
393}
394
395# Utility function for checking machine architecture
396# is_arch arch-type
397function is_arch {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500398 [[ "$(uname -m)" == "$1" ]]
Dean Troyerdff49a22014-01-30 15:37:40 -0600399}
400
Wiekus Beukesec47bc12015-03-19 08:20:38 -0700401# Determine if current distribution is an Oracle distribution
402# is_oraclelinux
403function is_oraclelinux {
404 if [[ -z "$os_VENDOR" ]]; then
405 GetOSVersion
406 fi
407
408 [ "$os_VENDOR" = "OracleLinux" ]
409}
410
411
Dean Troyerdff49a22014-01-30 15:37:40 -0600412# Determine if current distribution is a Fedora-based distribution
413# (Fedora, RHEL, CentOS, etc).
414# is_fedora
415function is_fedora {
416 if [[ -z "$os_VENDOR" ]]; then
417 GetOSVersion
418 fi
419
anju Tiwari6c639c92014-07-15 18:11:54 +0530420 [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
Maxim Nestratove6f37b92015-06-30 14:54:12 +0300421 [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleLinux" ] || \
422 [ "$os_VENDOR" = "CloudLinux" ]
Dean Troyerdff49a22014-01-30 15:37:40 -0600423}
424
425
426# Determine if current distribution is a SUSE-based distribution
427# (openSUSE, SLE).
428# is_suse
429function is_suse {
430 if [[ -z "$os_VENDOR" ]]; then
431 GetOSVersion
432 fi
433
434 [ "$os_VENDOR" = "openSUSE" ] || [ "$os_VENDOR" = "SUSE LINUX" ]
435}
436
437
438# Determine if current distribution is an Ubuntu-based distribution
439# It will also detect non-Ubuntu but Debian-based distros
440# is_ubuntu
441function is_ubuntu {
442 if [[ -z "$os_PACKAGE" ]]; then
443 GetOSVersion
444 fi
445 [ "$os_PACKAGE" = "deb" ]
446}
447
448
449# Git Functions
450# =============
451
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600452# Returns openstack release name for a given branch name
453# ``get_release_name_from_branch branch-name``
Ian Wienandaee18c72014-02-21 15:35:08 +1100454function get_release_name_from_branch {
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600455 local branch=$1
Adam Gandelman8f385722014-10-14 15:50:18 -0700456 if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
Dean Troyerabc7b1d2014-02-12 12:09:22 -0600457 echo ${branch#*/}
458 else
459 echo "master"
460 fi
461}
462
Dean Troyerdff49a22014-01-30 15:37:40 -0600463# git clone only if directory doesn't exist already. Since ``DEST`` might not
464# be owned by the installation user, we create the directory and change the
465# ownership to the proper user.
Dean Troyer50cda692014-07-25 11:57:20 -0500466# Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
467# Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
Dean Troyerdff49a22014-01-30 15:37:40 -0600468# does not exist (default is False, meaning the repo will be cloned).
Sean Dague53753292014-12-04 19:38:15 -0500469# Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
Dean Troyerdff49a22014-01-30 15:37:40 -0600470# git_clone remote dest-dir branch
471function git_clone {
Dean Troyer50cda692014-07-25 11:57:20 -0500472 local git_remote=$1
473 local git_dest=$2
474 local git_ref=$3
475 local orig_dir=$(pwd)
Jamie Lennox51f0de52014-10-20 16:32:34 +0200476 local git_clone_flags=""
Dean Troyer50cda692014-07-25 11:57:20 -0500477
Sean Dague53753292014-12-04 19:38:15 -0500478 RECLONE=$(trueorfalse False RECLONE)
Kevin Benton59d52f32015-01-17 11:29:12 -0800479 if [[ "${GIT_DEPTH}" -gt 0 ]]; then
Jamie Lennox51f0de52014-10-20 16:32:34 +0200480 git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
481 fi
482
Dean Troyerdff49a22014-01-30 15:37:40 -0600483 if [[ "$OFFLINE" = "True" ]]; then
484 echo "Running in offline mode, clones already exist"
485 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500486 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600487 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400488 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600489 return
490 fi
491
Dean Troyer50cda692014-07-25 11:57:20 -0500492 if echo $git_ref | egrep -q "^refs"; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600493 # If our branch name is a gerrit style refs/changes/...
Dean Troyer50cda692014-07-25 11:57:20 -0500494 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600495 [[ "$ERROR_ON_CLONE" = "True" ]] && \
496 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200497 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600498 fi
Dean Troyer50cda692014-07-25 11:57:20 -0500499 cd $git_dest
500 git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
Dean Troyerdff49a22014-01-30 15:37:40 -0600501 else
502 # do a full clone only if the directory doesn't exist
Dean Troyer50cda692014-07-25 11:57:20 -0500503 if [[ ! -d $git_dest ]]; then
Dean Troyerdff49a22014-01-30 15:37:40 -0600504 [[ "$ERROR_ON_CLONE" = "True" ]] && \
505 die $LINENO "Cloning not allowed in this configuration"
Jamie Lennox51f0de52014-10-20 16:32:34 +0200506 git_timed clone $git_clone_flags $git_remote $git_dest
Dean Troyer50cda692014-07-25 11:57:20 -0500507 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600508 # This checkout syntax works for both branches and tags
Dean Troyer50cda692014-07-25 11:57:20 -0500509 git checkout $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600510 elif [[ "$RECLONE" = "True" ]]; then
511 # if it does exist then simulate what clone does if asked to RECLONE
Dean Troyer50cda692014-07-25 11:57:20 -0500512 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600513 # set the url to pull from and fetch
Dean Troyer50cda692014-07-25 11:57:20 -0500514 git remote set-url origin $git_remote
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100515 git_timed fetch origin
Dean Troyerdff49a22014-01-30 15:37:40 -0600516 # remove the existing ignored files (like pyc) as they cause breakage
517 # (due to the py files having older timestamps than our pyc, so python
518 # thinks the pyc files are correct using them)
Dean Troyer50cda692014-07-25 11:57:20 -0500519 find $git_dest -name '*.pyc' -delete
Dean Troyerdff49a22014-01-30 15:37:40 -0600520
Dean Troyer50cda692014-07-25 11:57:20 -0500521 # handle git_ref accordingly to type (tag, branch)
522 if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
523 git_update_tag $git_ref
524 elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
525 git_update_branch $git_ref
526 elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
527 git_update_remote_branch $git_ref
Dean Troyerdff49a22014-01-30 15:37:40 -0600528 else
Dean Troyer50cda692014-07-25 11:57:20 -0500529 die $LINENO "$git_ref is neither branch nor tag"
Dean Troyerdff49a22014-01-30 15:37:40 -0600530 fi
531
532 fi
533 fi
534
535 # print out the results so we know what change was used in the logs
Dean Troyer50cda692014-07-25 11:57:20 -0500536 cd $git_dest
Dean Troyerdff49a22014-01-30 15:37:40 -0600537 git show --oneline | head -1
Sean Dague64bd0162014-03-12 13:04:22 -0400538 cd $orig_dir
Dean Troyerdff49a22014-01-30 15:37:40 -0600539}
540
Sean Daguecc524062014-10-01 09:06:43 -0400541# A variation on git clone that lets us specify a project by it's
542# actual name, like oslo.config. This is exceptionally useful in the
543# library installation case
544function git_clone_by_name {
545 local name=$1
546 local repo=${GITREPO[$name]}
547 local dir=${GITDIR[$name]}
548 local branch=${GITBRANCH[$name]}
549 git_clone $repo $dir $branch
550}
551
552
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100553# git can sometimes get itself infinitely stuck with transient network
554# errors or other issues with the remote end. This wraps git in a
555# timeout/retry loop and is intended to watch over non-local git
556# processes that might hang. GIT_TIMEOUT, if set, is passed directly
557# to timeout(1); otherwise the default value of 0 maintains the status
558# quo of waiting forever.
559# usage: git_timed <git-command>
Ian Wienandaee18c72014-02-21 15:35:08 +1100560function git_timed {
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100561 local count=0
562 local timeout=0
563
564 if [[ -n "${GIT_TIMEOUT}" ]]; then
565 timeout=${GIT_TIMEOUT}
566 fi
567
568 until timeout -s SIGINT ${timeout} git "$@"; do
569 # 124 is timeout(1)'s special return code when it reached the
570 # timeout; otherwise assume fatal failure
571 if [[ $? -ne 124 ]]; then
572 die $LINENO "git call failed: [git $@]"
573 fi
574
575 count=$(($count + 1))
Sean Daguee4af9292015-04-28 08:57:57 -0400576 warn $LINENO "timeout ${count} for git call: [git $@]"
Ian Wienandd53ad0b2014-02-20 13:55:13 +1100577 if [ $count -eq 3 ]; then
578 die $LINENO "Maximum of 3 git retries reached"
579 fi
580 sleep 5
581 done
582}
583
Dean Troyerdff49a22014-01-30 15:37:40 -0600584# git update using reference as a branch.
585# git_update_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100586function git_update_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500587 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600588
Dean Troyer50cda692014-07-25 11:57:20 -0500589 git checkout -f origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600590 # a local branch might not exist
Dean Troyer50cda692014-07-25 11:57:20 -0500591 git branch -D $git_branch || true
592 git checkout -b $git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600593}
594
595# git update using reference as a branch.
596# git_update_remote_branch ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100597function git_update_remote_branch {
Dean Troyer50cda692014-07-25 11:57:20 -0500598 local git_branch=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600599
Dean Troyer50cda692014-07-25 11:57:20 -0500600 git checkout -b $git_branch -t origin/$git_branch
Dean Troyerdff49a22014-01-30 15:37:40 -0600601}
602
603# git update using reference as a tag. Be careful editing source at that repo
604# as working copy will be in a detached mode
605# git_update_tag ref
Ian Wienandaee18c72014-02-21 15:35:08 +1100606function git_update_tag {
Dean Troyer50cda692014-07-25 11:57:20 -0500607 local git_tag=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600608
Dean Troyer50cda692014-07-25 11:57:20 -0500609 git tag -d $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600610 # fetching given tag only
Dean Troyer50cda692014-07-25 11:57:20 -0500611 git_timed fetch origin tag $git_tag
612 git checkout -f $git_tag
Dean Troyerdff49a22014-01-30 15:37:40 -0600613}
614
615
616# OpenStack Functions
617# ===================
618
619# Get the default value for HOST_IP
620# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
Ian Wienandaee18c72014-02-21 15:35:08 +1100621function get_default_host_ip {
Dean Troyerdff49a22014-01-30 15:37:40 -0600622 local fixed_range=$1
623 local floating_range=$2
624 local host_ip_iface=$3
625 local host_ip=$4
Brian Haley180f5eb2015-06-16 13:14:31 -0400626 local af=$5
Dean Troyerdff49a22014-01-30 15:37:40 -0600627
Dean Troyerdff49a22014-01-30 15:37:40 -0600628 # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
629 if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
630 host_ip=""
Andreas Scheuringa3430272015-03-09 16:55:32 +0100631 # Find the interface used for the default route
Brian Haley180f5eb2015-06-16 13:14:31 -0400632 host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
Sean M. Collinsd20435b2015-08-25 19:24:51 -0400633 local host_ips=$(LC_ALL=C ip -f $af addr show ${host_ip_iface} | sed /temporary/d |awk /$af'/ {split($2,parts,"/"); print parts[1]}')
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500634 local ip
635 for ip in $host_ips; do
Dean Troyerdff49a22014-01-30 15:37:40 -0600636 # Attempt to filter out IP addresses that are part of the fixed and
637 # floating range. Note that this method only works if the ``netaddr``
638 # python library is installed. If it is not installed, an error
639 # will be printed and the first IP from the interface will be used.
640 # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
641 # address.
Brian Haley180f5eb2015-06-16 13:14:31 -0400642 if [[ "$af" == "inet6" ]]; then
643 host_ip=$ip
644 break;
645 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500646 if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
647 host_ip=$ip
Dean Troyerdff49a22014-01-30 15:37:40 -0600648 break;
649 fi
650 done
651 fi
652 echo $host_ip
653}
654
Attila Fazekasf71b5002014-05-28 09:52:22 +0200655# Generates hex string from ``size`` byte of pseudo random data
656# generate_hex_string size
657function generate_hex_string {
658 local size=$1
659 hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
660}
661
Dean Troyerdff49a22014-01-30 15:37:40 -0600662# Grab a numbered field from python prettytable output
663# Fields are numbered starting with 1
664# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
665# get_field field-number
Ian Wienandaee18c72014-02-21 15:35:08 +1100666function get_field {
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500667 local data field
Dean Troyerdff49a22014-01-30 15:37:40 -0600668 while read data; do
669 if [ "$1" -lt 0 ]; then
670 field="(\$(NF$1))"
671 else
672 field="\$$(($1 + 1))"
673 fi
674 echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
675 done
676}
677
yuntongjinf26deea2015-02-28 10:50:34 +0800678# install default policy
679# copy over a default policy.json and policy.d for projects
680function install_default_policy {
681 local project=$1
682 local project_uc=$(echo $1|tr a-z A-Z)
683 local conf_dir="${project_uc}_CONF_DIR"
684 # eval conf dir to get the variable
685 conf_dir="${!conf_dir}"
686 local project_dir="${project_uc}_DIR"
687 # eval project dir to get the variable
688 project_dir="${!project_dir}"
689 local sample_conf_dir="${project_dir}/etc/${project}"
690 local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
691
692 # first copy any policy.json
693 cp -p $sample_conf_dir/policy.json $conf_dir
694 # then optionally copy over policy.d
695 if [[ -d $sample_policy_dir ]]; then
696 cp -r $sample_policy_dir $conf_dir/policy.d
697 fi
698}
699
Dean Troyerdff49a22014-01-30 15:37:40 -0600700# Add a policy to a policy.json file
701# Do nothing if the policy already exists
702# ``policy_add policy_file policy_name policy_permissions``
Ian Wienandaee18c72014-02-21 15:35:08 +1100703function policy_add {
Dean Troyerdff49a22014-01-30 15:37:40 -0600704 local policy_file=$1
705 local policy_name=$2
706 local policy_perm=$3
707
708 if grep -q ${policy_name} ${policy_file}; then
709 echo "Policy ${policy_name} already exists in ${policy_file}"
710 return
711 fi
712
713 # Add a terminating comma to policy lines without one
714 # Remove the closing '}' and all lines following to the end-of-file
715 local tmpfile=$(mktemp)
716 uniq ${policy_file} | sed -e '
717 s/]$/],/
718 /^[}]/,$d
719 ' > ${tmpfile}
720
721 # Append policy and closing brace
722 echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile}
723 echo "}" >>${tmpfile}
724
725 mv ${tmpfile} ${policy_file}
726}
727
Alistair Coles24779f62014-10-15 18:57:59 +0100728# Gets or creates a domain
729# Usage: get_or_create_domain <name> <description>
730function get_or_create_domain {
Ian Wienand11d276c2015-07-02 09:34:34 +1000731 local domain_id
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100732 local os_url="$KEYSTONE_SERVICE_URI_V3"
Alistair Coles24779f62014-10-15 18:57:59 +0100733 # Gets domain id
Ian Wienand11d276c2015-07-02 09:34:34 +1000734 domain_id=$(
Alistair Coles24779f62014-10-15 18:57:59 +0100735 # Gets domain id
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100736 openstack --os-token=$OS_TOKEN --os-url=$os_url \
737 --os-identity-api-version=3 domain show $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100738 -f value -c id 2>/dev/null ||
739 # Creates new domain
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100740 openstack --os-token=$OS_TOKEN --os-url=$os_url \
741 --os-identity-api-version=3 domain create $1 \
Alistair Coles24779f62014-10-15 18:57:59 +0100742 --description "$2" \
743 -f value -c id
744 )
745 echo $domain_id
746}
747
Steve Martinellib74e01c2014-12-18 01:35:35 -0500748# Gets or creates group
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000749# Usage: get_or_create_group <groupname> <domain> [<description>]
Steve Martinellib74e01c2014-12-18 01:35:35 -0500750function get_or_create_group {
Steve Martinellib74e01c2014-12-18 01:35:35 -0500751 local desc="${3:-}"
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100752 local os_url="$KEYSTONE_SERVICE_URI_V3"
Ian Wienand11d276c2015-07-02 09:34:34 +1000753 local group_id
Steve Martinellib74e01c2014-12-18 01:35:35 -0500754 # Gets group id
Ian Wienand11d276c2015-07-02 09:34:34 +1000755 group_id=$(
Steve Martinellib74e01c2014-12-18 01:35:35 -0500756 # Creates new group with --or-show
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100757 openstack --os-token=$OS_TOKEN --os-url=$os_url \
758 --os-identity-api-version=3 group create $1 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000759 --domain $2 --description "$desc" --or-show \
Steve Martinellib74e01c2014-12-18 01:35:35 -0500760 -f value -c id
761 )
762 echo $group_id
763}
764
Bartosz Górski0abde392014-02-28 14:15:19 +0100765# Gets or creates user
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000766# Usage: get_or_create_user <username> <password> <domain> [<email>]
Bartosz Górski0abde392014-02-28 14:15:19 +0100767function get_or_create_user {
Ian Wienand11d276c2015-07-02 09:34:34 +1000768 local user_id
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000769 if [[ ! -z "$4" ]]; then
770 local email="--email=$4"
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200771 else
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500772 local email=""
Gael Chamoulaud6dd8a8b2014-07-22 01:12:12 +0200773 fi
Bartosz Górski0abde392014-02-28 14:15:19 +0100774 # Gets user id
Ian Wienand11d276c2015-07-02 09:34:34 +1000775 user_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500776 # Creates new user with --or-show
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000777 openstack user create \
Bartosz Górski0abde392014-02-28 14:15:19 +0100778 $1 \
779 --password "$2" \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100780 --os-url=$KEYSTONE_SERVICE_URI_V3 \
781 --os-identity-api-version=3 \
Jamie Lennox9d7e7762015-05-29 01:08:53 +0000782 --domain=$3 \
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500783 $email \
Steve Martinelli245daa22014-11-14 02:17:22 -0500784 --or-show \
Bartosz Górski0abde392014-02-28 14:15:19 +0100785 -f value -c id
786 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500787 echo $user_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100788}
789
790# Gets or creates project
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000791# Usage: get_or_create_project <name> <domain>
Bartosz Górski0abde392014-02-28 14:15:19 +0100792function get_or_create_project {
Ian Wienand11d276c2015-07-02 09:34:34 +1000793 local project_id
794 project_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500795 # Creates new project with --or-show
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100796 openstack --os-url=$KEYSTONE_SERVICE_URI_V3 \
797 --os-identity-api-version=3 \
798 project create $1 \
Jamie Lennoxb632c9e2015-05-28 23:36:15 +0000799 --domain=$2 \
800 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100801 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500802 echo $project_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100803}
804
805# Gets or creates role
806# Usage: get_or_create_role <name>
807function get_or_create_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000808 local role_id
809 role_id=$(
Steve Martinelli245daa22014-11-14 02:17:22 -0500810 # Creates role with --or-show
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000811 openstack role create $1 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100812 --os-url=$KEYSTONE_SERVICE_URI_V3 \
813 --os-identity-api-version=3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000814 --or-show -f value -c id
Bartosz Górski0abde392014-02-28 14:15:19 +0100815 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500816 echo $role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100817}
818
Jamie Lennox9b215db2015-02-10 18:19:57 +1100819# Gets or adds user role to project
820# Usage: get_or_add_user_project_role <role> <user> <project>
821function get_or_add_user_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000822 local user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100823 # Gets user role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000824 user_role_id=$(openstack role list \
Steve Martinelli5541a612015-01-19 15:58:49 -0500825 --user $2 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100826 --os-url=$KEYSTONE_SERVICE_URI_V3 \
827 --os-identity-api-version=3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100828 --column "ID" \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000829 --project $3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100830 --column "Name" \
831 | grep " $1 " | get_field 1)
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500832 if [[ -z "$user_role_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100833 # Adds role to user
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500834 user_role_id=$(openstack role add \
Bartosz Górski0abde392014-02-28 14:15:19 +0100835 $1 \
836 --user $2 \
837 --project $3 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100838 --os-url=$KEYSTONE_SERVICE_URI_V3 \
839 --os-identity-api-version=3 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100840 | grep " id " | get_field 2)
841 fi
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500842 echo $user_role_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100843}
844
Steve Martinelli4599fd12015-03-12 21:30:58 -0400845# Gets or adds group role to project
846# Usage: get_or_add_group_project_role <role> <group> <project>
847function get_or_add_group_project_role {
Ian Wienand11d276c2015-07-02 09:34:34 +1000848 local group_role_id
Steve Martinelli4599fd12015-03-12 21:30:58 -0400849 # Gets group role id
Ian Wienand11d276c2015-07-02 09:34:34 +1000850 group_role_id=$(openstack role list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100851 --os-url=$KEYSTONE_SERVICE_URI_V3 \
852 --os-identity-api-version=3 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400853 --group $2 \
854 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000855 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400856 if [[ -z "$group_role_id" ]]; then
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000857 # Adds role to group and get it
858 openstack role add $1 \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100859 --os-url=$KEYSTONE_SERVICE_URI_V3 \
860 --os-identity-api-version=3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000861 --group $2 \
862 --project $3
863 group_role_id=$(openstack role list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100864 --os-url=$KEYSTONE_SERVICE_URI_V3 \
865 --os-identity-api-version=3 \
Steve Martinelli4599fd12015-03-12 21:30:58 -0400866 --group $2 \
867 --project $3 \
Jamie Lennox72ce6ac2015-07-02 09:19:01 +1000868 -c "ID" -f value)
Steve Martinelli4599fd12015-03-12 21:30:58 -0400869 fi
870 echo $group_role_id
871}
872
Bartosz Górski0abde392014-02-28 14:15:19 +0100873# Gets or creates service
874# Usage: get_or_create_service <name> <type> <description>
875function get_or_create_service {
Ian Wienand11d276c2015-07-02 09:34:34 +1000876 local service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100877 # Gets service id
Ian Wienand11d276c2015-07-02 09:34:34 +1000878 service_id=$(
Bartosz Górski0abde392014-02-28 14:15:19 +0100879 # Gets service id
Jamie Lennoxaedb8b92015-07-02 17:39:07 +1000880 openstack service show $2 -f value -c id 2>/dev/null ||
Bartosz Górski0abde392014-02-28 14:15:19 +0100881 # Creates new service if not exists
882 openstack service create \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100883 --os-url $KEYSTONE_SERVICE_URI_V3 \
884 --os-identity-api-version=3 \
Steve Martinelli789af5c2015-01-19 16:11:44 -0500885 $2 \
886 --name $1 \
Bartosz Górski0abde392014-02-28 14:15:19 +0100887 --description="$3" \
888 -f value -c id
889 )
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500890 echo $service_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100891}
892
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000893# Create an endpoint with a specific interface
894# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
895function _get_or_create_endpoint_with_interface {
Ian Wienand11d276c2015-07-02 09:34:34 +1000896 local endpoint_id
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200897 # TODO(dgonzalez): The check of the region name, as done in the grep
898 # statement below, exists only because keystone does currently
899 # not allow filtering the region name when listing endpoints. If keystone
900 # gets support for this, the check for the region name can be removed.
901 # Related bug in keystone: https://bugs.launchpad.net/keystone/+bug/1482772
Ian Wienand11d276c2015-07-02 09:34:34 +1000902 endpoint_id=$(openstack endpoint list \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100903 --os-url $KEYSTONE_SERVICE_URI_V3 \
904 --os-identity-api-version=3 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000905 --service $1 \
906 --interface $2 \
907 --region $4 \
Daniel Gonzalez1991e752015-08-11 19:34:22 +0200908 -c ID -c Region -f value | grep $4 | cut -f 1 -d " ")
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500909 if [[ -z "$endpoint_id" ]]; then
Bartosz Górski0abde392014-02-28 14:15:19 +0100910 # Creates new endpoint
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500911 endpoint_id=$(openstack endpoint create \
Lucas Alvares Gomesf7687872015-09-04 15:34:06 +0100912 --os-url $KEYSTONE_SERVICE_URI_V3 \
913 --os-identity-api-version=3 \
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000914 $1 $2 $3 --region $4 -f value -c id)
Bartosz Górski0abde392014-02-28 14:15:19 +0100915 fi
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000916
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500917 echo $endpoint_id
Bartosz Górski0abde392014-02-28 14:15:19 +0100918}
Dean Troyerdff49a22014-01-30 15:37:40 -0600919
Jamie Lennoxb17ad752015-05-29 06:04:47 +0000920# Gets or creates endpoint
921# Usage: get_or_create_endpoint <service> <region> <publicurl> <adminurl> <internalurl>
922function get_or_create_endpoint {
923 # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
924 # creating one endpoint with multiple urls to multiple endpoints each with
925 # a different interface. To maintain the existing function interface we
926 # create 3 endpoints and return the id of the public one. In reality
927 # returning the public id will not make a lot of difference as there are no
928 # scenarios currently that use the returned id. Ideally this behaviour
929 # should be pushed out to the service setups and let them create the
930 # endpoints they need.
931 local public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
932 _get_or_create_endpoint_with_interface $1 admin $4 $2
933 _get_or_create_endpoint_with_interface $1 internal $5 $2
934
935 # return the public id to indicate success, and this is the endpoint most likely wanted
936 echo $public_id
937}
938
939# Get a URL from the identity service
940# Usage: get_endpoint_url <service> <interface>
941function get_endpoint_url {
942 echo $(openstack endpoint list \
943 --service $1 --interface $2 \
944 --os-url $KEYSTONE_SERVICE_URI_V3 \
945 --os-identity-api-version=3 \
946 -c URL -f value)
947}
948
Dean Troyerd5dfa4c2014-07-25 11:13:11 -0500949
Dean Troyerdff49a22014-01-30 15:37:40 -0600950# Package Functions
951# =================
952
953# _get_package_dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100954function _get_package_dir {
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800955 local base_dir=$1
Dean Troyerdff49a22014-01-30 15:37:40 -0600956 local pkg_dir
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800957
958 if [[ -z "$base_dir" ]]; then
959 base_dir=$FILES
960 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600961 if is_ubuntu; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800962 pkg_dir=$base_dir/debs
Dean Troyerdff49a22014-01-30 15:37:40 -0600963 elif is_fedora; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800964 pkg_dir=$base_dir/rpms
Dean Troyerdff49a22014-01-30 15:37:40 -0600965 elif is_suse; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800966 pkg_dir=$base_dir/rpms-suse
Dean Troyerdff49a22014-01-30 15:37:40 -0600967 else
968 exit_distro_not_supported "list of packages"
969 fi
970 echo "$pkg_dir"
971}
972
973# Wrapper for ``apt-get`` to set cache and proxy environment variables
974# Uses globals ``OFFLINE``, ``*_proxy``
975# apt_get operation package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100976function apt_get {
Sean Dague45917cc2014-02-24 16:09:14 -0500977 local xtrace=$(set +o | grep xtrace)
978 set +o xtrace
979
Dean Troyerdff49a22014-01-30 15:37:40 -0600980 [[ "$OFFLINE" = "True" || -z "$@" ]] && return
981 local sudo="sudo"
982 [[ "$(id -u)" = "0" ]] && sudo="env"
Sean Dague45917cc2014-02-24 16:09:14 -0500983
984 $xtrace
Sean Dague53753292014-12-04 19:38:15 -0500985
Dean Troyerdff49a22014-01-30 15:37:40 -0600986 $sudo DEBIAN_FRONTEND=noninteractive \
Sean Dague53753292014-12-04 19:38:15 -0500987 http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
988 no_proxy=${no_proxy:-} \
Dean Troyerdff49a22014-01-30 15:37:40 -0600989 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
990}
991
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800992function _parse_package_files {
993 local files_to_parse=$@
Dean Troyerdff49a22014-01-30 15:37:40 -0600994
Dean Troyerdff49a22014-01-30 15:37:40 -0600995 if [[ -z "$DISTRO" ]]; then
996 GetDistro
997 fi
Dean Troyerdff49a22014-01-30 15:37:40 -0600998
Adam Gandelman7ca90cd2015-03-04 17:25:07 -0800999 for fname in ${files_to_parse}; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001000 local OIFS line package distros distro
1001 [[ -e $fname ]] || continue
1002
1003 OIFS=$IFS
1004 IFS=$'\n'
1005 for line in $(<${fname}); do
1006 if [[ $line =~ "NOPRIME" ]]; then
1007 continue
1008 fi
1009
1010 # Assume we want this package
1011 package=${line%#*}
1012 inst_pkg=1
1013
1014 # Look for # dist:xxx in comment
1015 if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
1016 # We are using BASH regexp matching feature.
1017 package=${BASH_REMATCH[1]}
1018 distros=${BASH_REMATCH[2]}
1019 # In bash ${VAR,,} will lowecase VAR
1020 # Look for a match in the distro list
1021 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
1022 # If no match then skip this package
1023 inst_pkg=0
1024 fi
1025 fi
1026
Dean Troyerdff49a22014-01-30 15:37:40 -06001027 if [[ $inst_pkg = 1 ]]; then
1028 echo $package
1029 fi
1030 done
1031 IFS=$OIFS
1032 done
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001033}
1034
1035# get_packages() collects a list of package names of any type from the
1036# prerequisite files in ``files/{debs|rpms}``. The list is intended
1037# to be passed to a package installer such as apt or yum.
1038#
1039# Only packages required for the services in 1st argument will be
1040# included. Two bits of metadata are recognized in the prerequisite files:
1041#
1042# - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
1043# - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
1044# of the package to the distros listed. The distro names are case insensitive.
1045function get_packages {
1046 local xtrace=$(set +o | grep xtrace)
1047 set +o xtrace
1048 local services=$@
1049 local package_dir=$(_get_package_dir)
1050 local file_to_parse=""
1051 local service=""
1052
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001053 if [[ -z "$package_dir" ]]; then
1054 echo "No package directory supplied"
1055 return 1
1056 fi
1057 for service in ${services//,/ }; do
1058 # Allow individual services to specify dependencies
1059 if [[ -e ${package_dir}/${service} ]]; then
1060 file_to_parse="${file_to_parse} ${package_dir}/${service}"
1061 fi
1062 # NOTE(sdague) n-api needs glance for now because that's where
1063 # glance client is
1064 if [[ $service == n-api ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001065 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001066 file_to_parse="${file_to_parse} ${package_dir}/nova"
1067 fi
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001068 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001069 file_to_parse="${file_to_parse} ${package_dir}/glance"
1070 fi
1071 elif [[ $service == c-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001072 if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001073 file_to_parse="${file_to_parse} ${package_dir}/cinder"
1074 fi
1075 elif [[ $service == ceilometer-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001076 if [[ ! $file_to_parse =~ $package_dir/ceilometer ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001077 file_to_parse="${file_to_parse} ${package_dir}/ceilometer"
1078 fi
1079 elif [[ $service == s-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001080 if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001081 file_to_parse="${file_to_parse} ${package_dir}/swift"
1082 fi
1083 elif [[ $service == n-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001084 if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001085 file_to_parse="${file_to_parse} ${package_dir}/nova"
1086 fi
1087 elif [[ $service == g-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001088 if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001089 file_to_parse="${file_to_parse} ${package_dir}/glance"
1090 fi
1091 elif [[ $service == key* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001092 if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001093 file_to_parse="${file_to_parse} ${package_dir}/keystone"
1094 fi
1095 elif [[ $service == q-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001096 if [[ ! $file_to_parse =~ $package_dir/neutron ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001097 file_to_parse="${file_to_parse} ${package_dir}/neutron"
1098 fi
1099 elif [[ $service == ir-* ]]; then
Ryan Hsu6f3f3102015-03-19 16:26:45 -07001100 if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001101 file_to_parse="${file_to_parse} ${package_dir}/ironic"
1102 fi
1103 fi
1104 done
1105 echo "$(_parse_package_files $file_to_parse)"
1106 $xtrace
1107}
1108
1109# get_plugin_packages() collects a list of package names of any type from a
1110# plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``. The
1111# list is intended to be passed to a package installer such as apt or yum.
1112#
1113# Only packages required for enabled and collected plugins will included.
1114#
Dean Troyerdc97cb72015-03-28 08:20:50 -05001115# The same metadata used in the main DevStack prerequisite files may be used
Adam Gandelman7ca90cd2015-03-04 17:25:07 -08001116# in these prerequisite files, see get_packages() for more info.
1117function get_plugin_packages {
1118 local xtrace=$(set +o | grep xtrace)
1119 set +o xtrace
1120 local files_to_parse=""
1121 local package_dir=""
1122 for plugin in ${DEVSTACK_PLUGINS//,/ }; do
1123 local package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
1124 files_to_parse+="$package_dir/$plugin"
1125 done
1126 echo "$(_parse_package_files $files_to_parse)"
Sean Dague45917cc2014-02-24 16:09:14 -05001127 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001128}
1129
1130# Distro-agnostic package installer
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001131# Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
Dean Troyerdff49a22014-01-30 15:37:40 -06001132# install_package package [package ...]
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001133function update_package_repo {
Sean Dague53753292014-12-04 19:38:15 -05001134 NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
1135 REPOS_UPDATED=${REPOS_UPDATED:-False}
1136 RETRY_UPDATE=${RETRY_UPDATE:-False}
1137
Paul Linchpiner9e179742014-07-13 22:23:00 -07001138 if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001139 return 0
1140 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001141
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001142 if is_ubuntu; then
1143 local xtrace=$(set +o | grep xtrace)
1144 set +o xtrace
1145 if [[ "$REPOS_UPDATED" != "True" || "$RETRY_UPDATE" = "True" ]]; then
1146 # if there are transient errors pulling the updates, that's fine.
1147 # It may be secondary repositories that we don't really care about.
1148 apt_get update || /bin/true
1149 REPOS_UPDATED=True
1150 fi
Sean Dague45917cc2014-02-24 16:09:14 -05001151 $xtrace
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001152 fi
1153}
1154
1155function real_install_package {
1156 if is_ubuntu; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001157 apt_get install "$@"
1158 elif is_fedora; then
1159 yum_install "$@"
1160 elif is_suse; then
1161 zypper_install "$@"
1162 else
1163 exit_distro_not_supported "installing packages"
1164 fi
1165}
1166
Monty Taylor5cc6d2c2014-06-06 08:45:16 -04001167# Distro-agnostic package installer
1168# install_package package [package ...]
1169function install_package {
1170 update_package_repo
1171 real_install_package $@ || RETRY_UPDATE=True update_package_repo && real_install_package $@
1172}
1173
Dean Troyerdff49a22014-01-30 15:37:40 -06001174# Distro-agnostic function to tell if a package is installed
1175# is_package_installed package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001176function is_package_installed {
Dean Troyerdff49a22014-01-30 15:37:40 -06001177 if [[ -z "$@" ]]; then
1178 return 1
1179 fi
1180
1181 if [[ -z "$os_PACKAGE" ]]; then
1182 GetOSVersion
1183 fi
1184
1185 if [[ "$os_PACKAGE" = "deb" ]]; then
1186 dpkg -s "$@" > /dev/null 2> /dev/null
1187 elif [[ "$os_PACKAGE" = "rpm" ]]; then
1188 rpm --quiet -q "$@"
1189 else
1190 exit_distro_not_supported "finding if a package is installed"
1191 fi
1192}
1193
1194# Distro-agnostic package uninstaller
1195# uninstall_package package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001196function uninstall_package {
Dean Troyerdff49a22014-01-30 15:37:40 -06001197 if is_ubuntu; then
1198 apt_get purge "$@"
1199 elif is_fedora; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001200 sudo ${YUM:-yum} remove -y "$@" ||:
Dean Troyerdff49a22014-01-30 15:37:40 -06001201 elif is_suse; then
1202 sudo zypper rm "$@"
1203 else
1204 exit_distro_not_supported "uninstalling packages"
1205 fi
1206}
1207
1208# Wrapper for ``yum`` to set proxy environment variables
Daniel P. Berrange63d25d92014-12-09 15:21:22 +00001209# Uses globals ``OFFLINE``, ``*_proxy``, ``YUM``
Dean Troyerdff49a22014-01-30 15:37:40 -06001210# yum_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001211function yum_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001212 [[ "$OFFLINE" = "True" ]] && return
1213 local sudo="sudo"
1214 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001215
1216 # The manual check for missing packages is because yum -y assumes
1217 # missing packages are OK. See
1218 # https://bugzilla.redhat.com/show_bug.cgi?id=965567
Ian Wienandfdf00f22015-03-13 11:50:02 +11001219 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1220 no_proxy="${no_proxy:-}" \
Ian Wienand36298ee2015-02-04 10:29:31 +11001221 ${YUM:-yum} install -y "$@" 2>&1 | \
Ian Wienandb27f16d2014-02-28 14:29:02 +11001222 awk '
1223 BEGIN { fail=0 }
1224 /No package/ { fail=1 }
1225 { print }
1226 END { exit fail }' || \
1227 die $LINENO "Missing packages detected"
1228
1229 # also ensure we catch a yum failure
1230 if [[ ${PIPESTATUS[0]} != 0 ]]; then
Ian Wienand36298ee2015-02-04 10:29:31 +11001231 die $LINENO "${YUM:-yum} install failure"
Ian Wienandb27f16d2014-02-28 14:29:02 +11001232 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001233}
1234
1235# zypper wrapper to set arguments correctly
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001236# Uses globals ``OFFLINE``, ``*_proxy``
Dean Troyerdff49a22014-01-30 15:37:40 -06001237# zypper_install package [package ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001238function zypper_install {
Dean Troyerdff49a22014-01-30 15:37:40 -06001239 [[ "$OFFLINE" = "True" ]] && return
1240 local sudo="sudo"
1241 [[ "$(id -u)" = "0" ]] && sudo="env"
Ian Wienandfdf00f22015-03-13 11:50:02 +11001242 $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
1243 no_proxy="${no_proxy:-}" \
Dean Troyerdff49a22014-01-30 15:37:40 -06001244 zypper --non-interactive install --auto-agree-with-licenses "$@"
1245}
1246
1247
1248# Process Functions
1249# =================
1250
1251# _run_process() is designed to be backgrounded by run_process() to simulate a
1252# fork. It includes the dirty work of closing extra filehandles and preparing log
1253# files to produce the same logs as screen_it(). The log filename is derived
Dean Troyerdde41d02014-12-09 17:47:57 -06001254# from the service name.
1255# Uses globals ``CURRENT_LOG_TIME``, ``LOGDIR``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001256# If an optional group is provided sg will be used to set the group of
1257# the command.
1258# _run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001259function _run_process {
Sean Dague6e137ab2015-04-29 08:22:24 -04001260 # disable tracing through the exec redirects, it's just confusing in the logs.
1261 xtrace=$(set +o | grep xtrace)
1262 set +o xtrace
1263
Dean Troyerdff49a22014-01-30 15:37:40 -06001264 local service=$1
1265 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001266 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001267
1268 # Undo logging redirections and close the extra descriptors
1269 exec 1>&3
1270 exec 2>&3
1271 exec 3>&-
1272 exec 6>&-
1273
Dean Troyerdde41d02014-12-09 17:47:57 -06001274 local real_logfile="${LOGDIR}/${service}.log.${CURRENT_LOG_TIME}"
1275 if [[ -n ${LOGDIR} ]]; then
1276 exec 1>&"$real_logfile" 2>&1
1277 ln -sf "$real_logfile" ${LOGDIR}/${service}.log
1278 if [[ -n ${SCREEN_LOGDIR} ]]; then
1279 # Drop the backward-compat symlink
1280 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${service}.log
1281 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001282
1283 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1284 export PYTHONUNBUFFERED=1
1285 fi
1286
Sean Dague6e137ab2015-04-29 08:22:24 -04001287 # reenable xtrace before we do *real* work
1288 $xtrace
1289
Dean Troyer3159a822014-08-27 14:13:58 -05001290 # Run under ``setsid`` to force the process to become a session and group leader.
1291 # The pid saved can be used with pkill -g to get the entire process group.
Chris Dent2f27a0e2014-09-09 13:46:02 +01001292 if [[ -n "$group" ]]; then
1293 setsid sg $group "$command" & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1294 else
1295 setsid $command & echo $! >$SERVICE_DIR/$SCREEN_NAME/$service.pid
1296 fi
Dean Troyer3159a822014-08-27 14:13:58 -05001297
1298 # Just silently exit this process
1299 exit 0
Dean Troyerdff49a22014-01-30 15:37:40 -06001300}
1301
1302# Helper to remove the ``*.failure`` files under ``$SERVICE_DIR/$SCREEN_NAME``.
1303# This is used for ``service_check`` when all the ``screen_it`` are called finished
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001304# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001305# init_service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001306function init_service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001307 SCREEN_NAME=${SCREEN_NAME:-stack}
1308 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1309
1310 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1311 mkdir -p "$SERVICE_DIR/$SCREEN_NAME"
1312 fi
1313
1314 rm -f "$SERVICE_DIR/$SCREEN_NAME"/*.failure
1315}
1316
1317# Find out if a process exists by partial name.
1318# is_running name
Ian Wienandaee18c72014-02-21 15:35:08 +11001319function is_running {
Dean Troyerdff49a22014-01-30 15:37:40 -06001320 local name=$1
1321 ps auxw | grep -v grep | grep ${name} > /dev/null
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001322 local exitcode=$?
Dean Troyerdff49a22014-01-30 15:37:40 -06001323 # some times I really hate bash reverse binary logic
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001324 return $exitcode
Dean Troyerdff49a22014-01-30 15:37:40 -06001325}
1326
Dean Troyer3159a822014-08-27 14:13:58 -05001327# Run a single service under screen or directly
1328# If the command includes shell metachatacters (;<>*) it must be run using a shell
Chris Dent2f27a0e2014-09-09 13:46:02 +01001329# If an optional group is provided sg will be used to run the
1330# command as that group.
1331# run_process service "command-line" [group]
Ian Wienandaee18c72014-02-21 15:35:08 +11001332function run_process {
Dean Troyerdff49a22014-01-30 15:37:40 -06001333 local service=$1
1334 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001335 local group=$3
Dean Troyerdff49a22014-01-30 15:37:40 -06001336
Dean Troyer3159a822014-08-27 14:13:58 -05001337 if is_service_enabled $service; then
1338 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001339 screen_process "$service" "$command" "$group"
Dean Troyer3159a822014-08-27 14:13:58 -05001340 else
1341 # Spawn directly without screen
Chris Dent2f27a0e2014-09-09 13:46:02 +01001342 _run_process "$service" "$command" "$group" &
Dean Troyer3159a822014-08-27 14:13:58 -05001343 fi
1344 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001345}
1346
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001347# Helper to launch a process in a named screen
Dean Troyerdde41d02014-12-09 17:47:57 -06001348# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001349# ``SERVICE_DIR``, ``USE_SCREEN``
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001350# screen_process name "command-line" [group]
Chris Dent2f27a0e2014-09-09 13:46:02 +01001351# Run a command in a shell in a screen window, if an optional group
1352# is provided, use sg to set the group of the command.
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001353function screen_process {
1354 local name=$1
Dean Troyer3159a822014-08-27 14:13:58 -05001355 local command="$2"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001356 local group=$3
Dean Troyer3159a822014-08-27 14:13:58 -05001357
Sean Dagueea22a4f2014-06-27 15:21:41 -04001358 SCREEN_NAME=${SCREEN_NAME:-stack}
1359 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001360 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001361
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001362 screen -S $SCREEN_NAME -X screen -t $name
Dean Troyerdff49a22014-01-30 15:37:40 -06001363
Dean Troyerdde41d02014-12-09 17:47:57 -06001364 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
1365 echo "LOGDIR: $LOGDIR"
1366 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
1367 echo "log: $real_logfile"
1368 if [[ -n ${LOGDIR} ]]; then
1369 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001370 screen -S $SCREEN_NAME -p $name -X log on
Dean Troyerdde41d02014-12-09 17:47:57 -06001371 ln -sf "$real_logfile" ${LOGDIR}/${name}.log
1372 if [[ -n ${SCREEN_LOGDIR} ]]; then
1373 # Drop the backward-compat symlink
1374 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log
1375 fi
Dean Troyerdff49a22014-01-30 15:37:40 -06001376 fi
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001377
1378 # sleep to allow bash to be ready to be send the command - we are
1379 # creating a new window in screen and then sends characters, so if
Sean Dague4d7ee092015-04-07 10:40:49 -04001380 # bash isn't running by the time we send the command, nothing
1381 # happens. This sleep was added originally to handle gate runs
1382 # where we needed this to be at least 3 seconds to pass
1383 # consistently on slow clouds. Now this is configurable so that we
1384 # can determine a reasonable value for the local case which should
1385 # be much smaller.
1386 sleep ${SCREEN_SLEEP:-3}
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001387
1388 NL=`echo -ne '\015'`
1389 # This fun command does the following:
1390 # - the passed server command is backgrounded
1391 # - the pid of the background process is saved in the usual place
1392 # - the server process is brought back to the foreground
1393 # - if the server process exits prematurely the fg command errors
1394 # and a message is written to stdout and the process failure file
1395 #
1396 # The pid saved can be used in stop_process() as a process group
1397 # id to kill off all child processes
1398 if [[ -n "$group" ]]; then
1399 command="sg $group '$command'"
1400 fi
Ian Wienandb28b2702015-04-16 08:43:43 +10001401
1402 # Append the process to the screen rc file
1403 screen_rc "$name" "$command"
1404
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001405 screen -S $SCREEN_NAME -p $name -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${name}.pid; fg || echo \"$name failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${name}.failure\"$NL"
Dean Troyerdff49a22014-01-30 15:37:40 -06001406}
1407
1408# Screen rc file builder
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001409# Uses globals ``SCREEN_NAME``, ``SCREENRC``
Dean Troyerdff49a22014-01-30 15:37:40 -06001410# screen_rc service "command-line"
1411function screen_rc {
1412 SCREEN_NAME=${SCREEN_NAME:-stack}
1413 SCREENRC=$TOP_DIR/$SCREEN_NAME-screenrc
1414 if [[ ! -e $SCREENRC ]]; then
1415 # Name the screen session
1416 echo "sessionname $SCREEN_NAME" > $SCREENRC
1417 # Set a reasonable statusbar
1418 echo "hardstatus alwayslastline '$SCREEN_HARDSTATUS'" >> $SCREENRC
1419 # Some distributions override PROMPT_COMMAND for the screen terminal type - turn that off
1420 echo "setenv PROMPT_COMMAND /bin/true" >> $SCREENRC
1421 echo "screen -t shell bash" >> $SCREENRC
1422 fi
1423 # If this service doesn't already exist in the screenrc file
1424 if ! grep $1 $SCREENRC 2>&1 > /dev/null; then
1425 NL=`echo -ne '\015'`
1426 echo "screen -t $1 bash" >> $SCREENRC
1427 echo "stuff \"$2$NL\"" >> $SCREENRC
1428
Dean Troyerdde41d02014-12-09 17:47:57 -06001429 if [[ -n ${LOGDIR} ]]; then
1430 echo "logfile ${LOGDIR}/${1}.log.${CURRENT_LOG_TIME}" >>$SCREENRC
Dean Troyerdff49a22014-01-30 15:37:40 -06001431 echo "log on" >>$SCREENRC
1432 fi
1433 fi
1434}
1435
1436# Stop a service in screen
1437# If a PID is available use it, kill the whole process group via TERM
1438# If screen is being used kill the screen window; this will catch processes
1439# that did not leave a PID behind
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001440# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``, ``USE_SCREEN``
Chris Dent2f27a0e2014-09-09 13:46:02 +01001441# screen_stop_service service
Dean Troyer3159a822014-08-27 14:13:58 -05001442function screen_stop_service {
1443 local service=$1
1444
Dean Troyerdff49a22014-01-30 15:37:40 -06001445 SCREEN_NAME=${SCREEN_NAME:-stack}
1446 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001447 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyerdff49a22014-01-30 15:37:40 -06001448
Dean Troyer3159a822014-08-27 14:13:58 -05001449 if is_service_enabled $service; then
1450 # Clean up the screen window
Attila Fazekasf750a6f2015-07-01 12:17:35 +02001451 screen -S $SCREEN_NAME -p $service -X kill || true
Dean Troyer3159a822014-08-27 14:13:58 -05001452 fi
1453}
1454
1455# Stop a service process
1456# If a PID is available use it, kill the whole process group via TERM
1457# If screen is being used kill the screen window; this will catch processes
1458# that did not leave a PID behind
1459# Uses globals ``SERVICE_DIR``, ``USE_SCREEN``
1460# stop_process service
1461function stop_process {
1462 local service=$1
1463
1464 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
Sean Dague53753292014-12-04 19:38:15 -05001465 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Dean Troyer3159a822014-08-27 14:13:58 -05001466
1467 if is_service_enabled $service; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001468 # Kill via pid if we have one available
Dean Troyer3159a822014-08-27 14:13:58 -05001469 if [[ -r $SERVICE_DIR/$SCREEN_NAME/$service.pid ]]; then
1470 pkill -g $(cat $SERVICE_DIR/$SCREEN_NAME/$service.pid)
1471 rm $SERVICE_DIR/$SCREEN_NAME/$service.pid
Dean Troyerdff49a22014-01-30 15:37:40 -06001472 fi
1473 if [[ "$USE_SCREEN" = "True" ]]; then
1474 # Clean up the screen window
Dean Troyer3159a822014-08-27 14:13:58 -05001475 screen_stop_service $service
Dean Troyerdff49a22014-01-30 15:37:40 -06001476 fi
1477 fi
1478}
1479
1480# Helper to get the status of each running service
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001481# Uses globals ``SCREEN_NAME``, ``SERVICE_DIR``
Dean Troyerdff49a22014-01-30 15:37:40 -06001482# service_check
Ian Wienandaee18c72014-02-21 15:35:08 +11001483function service_check {
Dean Troyerdff49a22014-01-30 15:37:40 -06001484 local service
1485 local failures
1486 SCREEN_NAME=${SCREEN_NAME:-stack}
1487 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
1488
1489
1490 if [[ ! -d "$SERVICE_DIR/$SCREEN_NAME" ]]; then
1491 echo "No service status directory found"
1492 return
1493 fi
1494
Wei Jiangange3340f12015-09-21 17:52:14 +08001495 # Check if there is any failure flag file under $SERVICE_DIR/$SCREEN_NAME
Sean Dague09bd7c82014-02-03 08:35:26 +09001496 # make this -o errexit safe
1497 failures=`ls "$SERVICE_DIR/$SCREEN_NAME"/*.failure 2>/dev/null || /bin/true`
Dean Troyerdff49a22014-01-30 15:37:40 -06001498
1499 for service in $failures; do
1500 service=`basename $service`
1501 service=${service%.failure}
1502 echo "Error: Service $service is not running"
1503 done
1504
1505 if [ -n "$failures" ]; then
Sean Dague12379222014-02-27 17:16:46 -05001506 die $LINENO "More details about the above errors can be found with screen, with ./rejoin-stack.sh"
Dean Troyerdff49a22014-01-30 15:37:40 -06001507 fi
1508}
1509
Chris Dent2f27a0e2014-09-09 13:46:02 +01001510# Tail a log file in a screen if USE_SCREEN is true.
1511function tail_log {
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001512 local name=$1
Chris Dent2f27a0e2014-09-09 13:46:02 +01001513 local logfile=$2
1514
Sean Dague53753292014-12-04 19:38:15 -05001515 USE_SCREEN=$(trueorfalse True USE_SCREEN)
Chris Dent2f27a0e2014-09-09 13:46:02 +01001516 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001517 screen_process "$name" "sudo tail -f $logfile"
Chris Dent2f27a0e2014-09-09 13:46:02 +01001518 fi
1519}
1520
Dean Troyerdff49a22014-01-30 15:37:40 -06001521
Dean Troyer3159a822014-08-27 14:13:58 -05001522# Deprecated Functions
1523# --------------------
1524
1525# _old_run_process() is designed to be backgrounded by old_run_process() to simulate a
1526# fork. It includes the dirty work of closing extra filehandles and preparing log
1527# files to produce the same logs as screen_it(). The log filename is derived
1528# from the service name and global-and-now-misnamed ``SCREEN_LOGDIR``
1529# Uses globals ``CURRENT_LOG_TIME``, ``SCREEN_LOGDIR``, ``SCREEN_NAME``, ``SERVICE_DIR``
1530# _old_run_process service "command-line"
1531function _old_run_process {
1532 local service=$1
1533 local command="$2"
1534
1535 # Undo logging redirections and close the extra descriptors
1536 exec 1>&3
1537 exec 2>&3
1538 exec 3>&-
1539 exec 6>&-
1540
1541 if [[ -n ${SCREEN_LOGDIR} ]]; then
Dean Troyerad5cc982014-12-10 16:35:32 -06001542 exec 1>&${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} 2>&1
1543 ln -sf ${SCREEN_LOGDIR}/screen-${1}.log.${CURRENT_LOG_TIME} ${SCREEN_LOGDIR}/screen-${1}.log
Dean Troyer3159a822014-08-27 14:13:58 -05001544
1545 # TODO(dtroyer): Hack to get stdout from the Python interpreter for the logs.
1546 export PYTHONUNBUFFERED=1
1547 fi
1548
1549 exec /bin/bash -c "$command"
1550 die "$service exec failure: $command"
1551}
1552
1553# old_run_process() launches a child process that closes all file descriptors and
1554# then exec's the passed in command. This is meant to duplicate the semantics
1555# of screen_it() without screen. PIDs are written to
1556# ``$SERVICE_DIR/$SCREEN_NAME/$service.pid`` by the spawned child process.
1557# old_run_process service "command-line"
1558function old_run_process {
1559 local service=$1
1560 local command="$2"
1561
1562 # Spawn the child process
1563 _old_run_process "$service" "$command" &
1564 echo $!
1565}
1566
1567# Compatibility for existing start_XXXX() functions
1568# Uses global ``USE_SCREEN``
1569# screen_it service "command-line"
1570function screen_it {
1571 if is_service_enabled $1; then
1572 # Append the service to the screen rc file
1573 screen_rc "$1" "$2"
1574
1575 if [[ "$USE_SCREEN" = "True" ]]; then
Adam Gandelman8543a0f2014-10-16 17:42:33 -07001576 screen_process "$1" "$2"
Dean Troyer3159a822014-08-27 14:13:58 -05001577 else
1578 # Spawn directly without screen
1579 old_run_process "$1" "$2" >$SERVICE_DIR/$SCREEN_NAME/$1.pid
1580 fi
1581 fi
1582}
1583
1584# Compatibility for existing stop_XXXX() functions
1585# Stop a service in screen
1586# If a PID is available use it, kill the whole process group via TERM
1587# If screen is being used kill the screen window; this will catch processes
1588# that did not leave a PID behind
1589# screen_stop service
1590function screen_stop {
1591 # Clean up the screen window
1592 stop_process $1
1593}
1594
1595
Sean Dague2c65e712014-12-18 09:44:56 -05001596# Plugin Functions
1597# =================
1598
1599DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
1600
1601# enable_plugin <name> <url> [branch]
1602#
1603# ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
1604# ``url`` is a git url
1605# ``branch`` is a gitref. If it's not set, defaults to master
1606function enable_plugin {
1607 local name=$1
1608 local url=$2
1609 local branch=${3:-master}
1610 DEVSTACK_PLUGINS+=",$name"
1611 GITREPO[$name]=$url
1612 GITDIR[$name]=$DEST/$name
1613 GITBRANCH[$name]=$branch
1614}
1615
1616# fetch_plugins
1617#
1618# clones all plugins
1619function fetch_plugins {
1620 local plugins="${DEVSTACK_PLUGINS}"
1621 local plugin
1622
1623 # short circuit if nothing to do
1624 if [[ -z $plugins ]]; then
1625 return
1626 fi
1627
Dean Troyerdc97cb72015-03-28 08:20:50 -05001628 echo "Fetching DevStack plugins"
Sean Dague2c65e712014-12-18 09:44:56 -05001629 for plugin in ${plugins//,/ }; do
1630 git_clone_by_name $plugin
1631 done
1632}
1633
1634# load_plugin_settings
1635#
1636# Load settings from plugins in the order that they were registered
1637function load_plugin_settings {
1638 local plugins="${DEVSTACK_PLUGINS}"
1639 local plugin
1640
1641 # short circuit if nothing to do
1642 if [[ -z $plugins ]]; then
1643 return
1644 fi
1645
1646 echo "Loading plugin settings"
1647 for plugin in ${plugins//,/ }; do
1648 local dir=${GITDIR[$plugin]}
1649 # source any known settings
1650 if [[ -f $dir/devstack/settings ]]; then
1651 source $dir/devstack/settings
1652 fi
1653 done
1654}
1655
Sean Dague6e275e12015-03-26 05:54:28 -04001656# plugin_override_defaults
1657#
1658# Run an extremely early setting phase for plugins that allows default
1659# overriding of services.
1660function plugin_override_defaults {
1661 local plugins="${DEVSTACK_PLUGINS}"
1662 local plugin
1663
1664 # short circuit if nothing to do
1665 if [[ -z $plugins ]]; then
1666 return
1667 fi
1668
1669 echo "Overriding Configuration Defaults"
1670 for plugin in ${plugins//,/ }; do
1671 local dir=${GITDIR[$plugin]}
1672 # source any overrides
1673 if [[ -f $dir/devstack/override-defaults ]]; then
1674 # be really verbose that an override is happening, as it
1675 # may not be obvious if things fail later.
1676 echo "$plugin has overriden the following defaults"
1677 cat $dir/devstack/override-defaults
1678 source $dir/devstack/override-defaults
1679 fi
1680 done
1681}
1682
Sean Dague2c65e712014-12-18 09:44:56 -05001683# run_plugins
1684#
1685# Run the devstack/plugin.sh in all the plugin directories. These are
1686# run in registration order.
1687function run_plugins {
1688 local mode=$1
1689 local phase=$2
Bharat Kumar Kobagana441ff072015-01-08 12:26:26 +05301690
1691 local plugins="${DEVSTACK_PLUGINS}"
1692 local plugin
Sean Dague2c65e712014-12-18 09:44:56 -05001693 for plugin in ${plugins//,/ }; do
1694 local dir=${GITDIR[$plugin]}
1695 if [[ -f $dir/devstack/plugin.sh ]]; then
1696 source $dir/devstack/plugin.sh $mode $phase
1697 fi
1698 done
1699}
1700
1701function run_phase {
1702 local mode=$1
1703 local phase=$2
1704 if [[ -d $TOP_DIR/extras.d ]]; then
1705 for i in $TOP_DIR/extras.d/*.sh; do
1706 [[ -r $i ]] && source $i $mode $phase
1707 done
1708 fi
1709 # the source phase corresponds to settings loading in plugins
1710 if [[ "$mode" == "source" ]]; then
1711 load_plugin_settings
Sean Dague6e275e12015-03-26 05:54:28 -04001712 elif [[ "$mode" == "override_defaults" ]]; then
1713 plugin_override_defaults
Sean Dague2c65e712014-12-18 09:44:56 -05001714 else
1715 run_plugins $mode $phase
1716 fi
1717}
1718
Dean Troyerdff49a22014-01-30 15:37:40 -06001719
1720# Service Functions
1721# =================
1722
1723# remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
1724# _cleanup_service_list service-list
Ian Wienandaee18c72014-02-21 15:35:08 +11001725function _cleanup_service_list {
Dean Troyerdff49a22014-01-30 15:37:40 -06001726 echo "$1" | sed -e '
1727 s/,,/,/g;
1728 s/^,//;
1729 s/,$//
1730 '
1731}
1732
1733# disable_all_services() removes all current services
1734# from ``ENABLED_SERVICES`` to reset the configuration
1735# before a minimal installation
1736# Uses global ``ENABLED_SERVICES``
1737# disable_all_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001738function disable_all_services {
Dean Troyerdff49a22014-01-30 15:37:40 -06001739 ENABLED_SERVICES=""
1740}
1741
1742# Remove all services starting with '-'. For example, to install all default
1743# services except rabbit (rabbit) set in ``localrc``:
1744# ENABLED_SERVICES+=",-rabbit"
1745# Uses global ``ENABLED_SERVICES``
1746# disable_negated_services
Ian Wienandaee18c72014-02-21 15:35:08 +11001747function disable_negated_services {
Ian Wienand2796a822015-04-15 08:59:04 +10001748 local to_remove=""
1749 local remaining=""
Dean Troyerdff49a22014-01-30 15:37:40 -06001750 local service
Ian Wienand2796a822015-04-15 08:59:04 +10001751
1752 # build up list of services that should be removed; i.e. they
1753 # begin with "-"
1754 for service in ${ENABLED_SERVICES//,/ }; do
Dean Troyerdff49a22014-01-30 15:37:40 -06001755 if [[ ${service} == -* ]]; then
Ian Wienand2796a822015-04-15 08:59:04 +10001756 to_remove+=",${service#-}"
1757 else
1758 remaining+=",${service}"
Dean Troyerdff49a22014-01-30 15:37:40 -06001759 fi
1760 done
Ian Wienand2796a822015-04-15 08:59:04 +10001761
1762 # go through the service list. if this service appears in the "to
1763 # be removed" list, drop it
fumihiko kakuma8606c982015-04-13 09:55:06 +09001764 ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
Dean Troyerdff49a22014-01-30 15:37:40 -06001765}
1766
1767# disable_service() removes the services passed as argument to the
1768# ``ENABLED_SERVICES`` list, if they are present.
1769#
1770# For example:
1771# disable_service rabbit
1772#
1773# This function does not know about the special cases
1774# for nova, glance, and neutron built into is_service_enabled().
1775# Uses global ``ENABLED_SERVICES``
1776# disable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001777function disable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001778 local tmpsvcs=",${ENABLED_SERVICES},"
1779 local service
1780 for service in $@; do
1781 if is_service_enabled $service; then
1782 tmpsvcs=${tmpsvcs//,$service,/,}
1783 fi
1784 done
1785 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1786}
1787
1788# enable_service() adds the services passed as argument to the
1789# ``ENABLED_SERVICES`` list, if they are not already present.
1790#
1791# For example:
Sean Dague37eca482015-06-16 07:19:22 -04001792# enable_service q-svc
Dean Troyerdff49a22014-01-30 15:37:40 -06001793#
1794# This function does not know about the special cases
1795# for nova, glance, and neutron built into is_service_enabled().
1796# Uses global ``ENABLED_SERVICES``
1797# enable_service service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001798function enable_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06001799 local tmpsvcs="${ENABLED_SERVICES}"
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001800 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001801 for service in $@; do
1802 if ! is_service_enabled $service; then
1803 tmpsvcs+=",$service"
1804 fi
1805 done
1806 ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
1807 disable_negated_services
1808}
1809
1810# is_service_enabled() checks if the service(s) specified as arguments are
1811# enabled by the user in ``ENABLED_SERVICES``.
1812#
1813# Multiple services specified as arguments are ``OR``'ed together; the test
1814# is a short-circuit boolean, i.e it returns on the first match.
1815#
1816# There are special cases for some 'catch-all' services::
1817# **nova** returns true if any service enabled start with **n-**
1818# **cinder** returns true if any service enabled start with **c-**
1819# **ceilometer** returns true if any service enabled start with **ceilometer**
1820# **glance** returns true if any service enabled start with **g-**
1821# **neutron** returns true if any service enabled start with **q-**
1822# **swift** returns true if any service enabled start with **s-**
1823# **trove** returns true if any service enabled start with **tr-**
1824# For backward compatibility if we have **swift** in ENABLED_SERVICES all the
1825# **s-** services will be enabled. This will be deprecated in the future.
1826#
1827# Cells within nova is enabled if **n-cell** is in ``ENABLED_SERVICES``.
1828# We also need to make sure to treat **n-cell-region** and **n-cell-child**
1829# as enabled in this case.
1830#
1831# Uses global ``ENABLED_SERVICES``
1832# is_service_enabled service [service ...]
Ian Wienandaee18c72014-02-21 15:35:08 +11001833function is_service_enabled {
Sean Dague45917cc2014-02-24 16:09:14 -05001834 local xtrace=$(set +o | grep xtrace)
1835 set +o xtrace
1836 local enabled=1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001837 local services=$@
1838 local service
Dean Troyerdff49a22014-01-30 15:37:40 -06001839 for service in ${services}; do
Sean Dague45917cc2014-02-24 16:09:14 -05001840 [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001841
1842 # Look for top-level 'enabled' function for this service
1843 if type is_${service}_enabled >/dev/null 2>&1; then
1844 # A function exists for this service, use it
Christian Schwede3db0aad2015-09-10 11:15:39 +00001845 is_${service}_enabled && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001846 fi
1847
1848 # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
1849 # are implemented
1850
Sean Dague45917cc2014-02-24 16:09:14 -05001851 [[ ${service} == n-cell-* && ${ENABLED_SERVICES} =~ "n-cell" ]] && enabled=0
Chris Dent2f27a0e2014-09-09 13:46:02 +01001852 [[ ${service} == n-cpu-* && ${ENABLED_SERVICES} =~ "n-cpu" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001853 [[ ${service} == "nova" && ${ENABLED_SERVICES} =~ "n-" ]] && enabled=0
Sean Dague45917cc2014-02-24 16:09:14 -05001854 [[ ${service} == "ceilometer" && ${ENABLED_SERVICES} =~ "ceilometer-" ]] && enabled=0
1855 [[ ${service} == "glance" && ${ENABLED_SERVICES} =~ "g-" ]] && enabled=0
1856 [[ ${service} == "ironic" && ${ENABLED_SERVICES} =~ "ir-" ]] && enabled=0
1857 [[ ${service} == "neutron" && ${ENABLED_SERVICES} =~ "q-" ]] && enabled=0
1858 [[ ${service} == "trove" && ${ENABLED_SERVICES} =~ "tr-" ]] && enabled=0
1859 [[ ${service} == "swift" && ${ENABLED_SERVICES} =~ "s-" ]] && enabled=0
1860 [[ ${service} == s-* && ${ENABLED_SERVICES} =~ "swift" ]] && enabled=0
Dean Troyerdff49a22014-01-30 15:37:40 -06001861 done
Sean Dague45917cc2014-02-24 16:09:14 -05001862 $xtrace
1863 return $enabled
Dean Troyerdff49a22014-01-30 15:37:40 -06001864}
1865
fumihiko kakuma8606c982015-04-13 09:55:06 +09001866# remove specified list from the input string
1867# remove_disabled_services service-list remove-list
1868function remove_disabled_services {
1869 local service_list=$1
1870 local remove_list=$2
1871 local service
1872 local enabled=""
1873
1874 for service in ${service_list//,/ }; do
1875 local remove
1876 local add=1
1877 for remove in ${remove_list//,/ }; do
1878 if [[ ${remove} == ${service} ]]; then
1879 add=0
1880 break
1881 fi
1882 done
1883 if [[ $add == 1 ]]; then
1884 enabled="${enabled},$service"
1885 fi
1886 done
1887 _cleanup_service_list "$enabled"
1888}
1889
Dean Troyerdff49a22014-01-30 15:37:40 -06001890# Toggle enable/disable_service for services that must run exclusive of each other
1891# $1 The name of a variable containing a space-separated list of services
1892# $2 The name of a variable in which to store the enabled service's name
1893# $3 The name of the service to enable
1894function use_exclusive_service {
1895 local options=${!1}
1896 local selection=$3
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001897 local out=$2
Dean Troyerdff49a22014-01-30 15:37:40 -06001898 [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001899 local opt
Dean Troyerdff49a22014-01-30 15:37:40 -06001900 for opt in $options;do
1901 [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
1902 done
1903 eval "$out=$selection"
1904 return 0
1905}
1906
1907
Masayuki Igawaf6368d32014-02-20 13:31:26 +09001908# System Functions
1909# ================
Dean Troyerdff49a22014-01-30 15:37:40 -06001910
1911# Only run the command if the target file (the last arg) is not on an
1912# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11001913function _safe_permission_operation {
Sean Dague45917cc2014-02-24 16:09:14 -05001914 local xtrace=$(set +o | grep xtrace)
1915 set +o xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001916 local args=( $@ )
1917 local last
1918 local sudo_cmd
1919 local dir_to_check
1920
1921 let last="${#args[*]} - 1"
1922
Dean Troyerd5dfa4c2014-07-25 11:13:11 -05001923 local dir_to_check=${args[$last]}
Dean Troyerdff49a22014-01-30 15:37:40 -06001924 if [ ! -d "$dir_to_check" ]; then
1925 dir_to_check=`dirname "$dir_to_check"`
1926 fi
1927
1928 if is_nfs_directory "$dir_to_check" ; then
Sean Dague45917cc2014-02-24 16:09:14 -05001929 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001930 return 0
1931 fi
1932
1933 if [[ $TRACK_DEPENDS = True ]]; then
1934 sudo_cmd="env"
1935 else
1936 sudo_cmd="sudo"
1937 fi
1938
Sean Dague45917cc2014-02-24 16:09:14 -05001939 $xtrace
Dean Troyerdff49a22014-01-30 15:37:40 -06001940 $sudo_cmd $@
1941}
1942
1943# Exit 0 if address is in network or 1 if address is not in network
1944# ip-range is in CIDR notation: 1.2.3.4/20
1945# address_in_net ip-address ip-range
Ian Wienandaee18c72014-02-21 15:35:08 +11001946function address_in_net {
Dean Troyerdff49a22014-01-30 15:37:40 -06001947 local ip=$1
1948 local range=$2
1949 local masklen=${range#*/}
1950 local network=$(maskip ${range%/*} $(cidr2netmask $masklen))
1951 local subnet=$(maskip $ip $(cidr2netmask $masklen))
1952 [[ $network == $subnet ]]
1953}
1954
1955# Add a user to a group.
1956# add_user_to_group user group
Ian Wienandaee18c72014-02-21 15:35:08 +11001957function add_user_to_group {
Dean Troyerdff49a22014-01-30 15:37:40 -06001958 local user=$1
1959 local group=$2
1960
Thomas Bechtolda8580852015-05-31 00:04:33 +02001961 sudo usermod -a -G "$group" "$user"
Dean Troyerdff49a22014-01-30 15:37:40 -06001962}
1963
1964# Convert CIDR notation to a IPv4 netmask
1965# cidr2netmask cidr-bits
Ian Wienandaee18c72014-02-21 15:35:08 +11001966function cidr2netmask {
Dean Troyerdff49a22014-01-30 15:37:40 -06001967 local maskpat="255 255 255 255"
1968 local maskdgt="254 252 248 240 224 192 128"
1969 set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
1970 echo ${1-0}.${2-0}.${3-0}.${4-0}
1971}
1972
1973# Gracefully cp only if source file/dir exists
1974# cp_it source destination
1975function cp_it {
1976 if [ -e $1 ] || [ -d $1 ]; then
1977 cp -pRL $1 $2
1978 fi
1979}
1980
1981# HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
1982# ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
1983# ``localrc`` or on the command line if necessary::
1984#
1985# [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
1986#
1987# http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
1988
Ian Wienandaee18c72014-02-21 15:35:08 +11001989function export_proxy_variables {
Sean Dague53753292014-12-04 19:38:15 -05001990 if isset http_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001991 export http_proxy=$http_proxy
1992 fi
Sean Dague53753292014-12-04 19:38:15 -05001993 if isset https_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001994 export https_proxy=$https_proxy
1995 fi
Sean Dague53753292014-12-04 19:38:15 -05001996 if isset no_proxy ; then
Dean Troyerdff49a22014-01-30 15:37:40 -06001997 export no_proxy=$no_proxy
1998 fi
1999}
2000
2001# Returns true if the directory is on a filesystem mounted via NFS.
Ian Wienandaee18c72014-02-21 15:35:08 +11002002function is_nfs_directory {
Dean Troyerdff49a22014-01-30 15:37:40 -06002003 local mount_type=`stat -f -L -c %T $1`
2004 test "$mount_type" == "nfs"
2005}
2006
2007# Return the network portion of the given IP address using netmask
2008# netmask is in the traditional dotted-quad format
2009# maskip ip-address netmask
Ian Wienandaee18c72014-02-21 15:35:08 +11002010function maskip {
Dean Troyerdff49a22014-01-30 15:37:40 -06002011 local ip=$1
2012 local mask=$2
2013 local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
2014 local subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
2015 echo $subnet
2016}
2017
Chris Dent3a2c86a2015-05-12 13:41:25 +00002018# Return the current python as "python<major>.<minor>"
2019function python_version {
2020 local python_version=$(python -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
2021 echo "python${python_version}"
2022}
2023
Dean Troyerdff49a22014-01-30 15:37:40 -06002024# Service wrapper to restart services
2025# restart_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002026function restart_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002027 if is_ubuntu; then
2028 sudo /usr/sbin/service $1 restart
2029 else
2030 sudo /sbin/service $1 restart
2031 fi
2032}
2033
2034# Only change permissions of a file or directory if it is not on an
2035# NFS filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002036function safe_chmod {
Dean Troyerdff49a22014-01-30 15:37:40 -06002037 _safe_permission_operation chmod $@
2038}
2039
2040# Only change ownership of a file or directory if it is not on an NFS
2041# filesystem.
Ian Wienandaee18c72014-02-21 15:35:08 +11002042function safe_chown {
Dean Troyerdff49a22014-01-30 15:37:40 -06002043 _safe_permission_operation chown $@
2044}
2045
2046# Service wrapper to start services
2047# start_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002048function start_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002049 if is_ubuntu; then
2050 sudo /usr/sbin/service $1 start
2051 else
2052 sudo /sbin/service $1 start
2053 fi
2054}
2055
2056# Service wrapper to stop services
2057# stop_service service-name
Ian Wienandaee18c72014-02-21 15:35:08 +11002058function stop_service {
Dean Troyerdff49a22014-01-30 15:37:40 -06002059 if is_ubuntu; then
2060 sudo /usr/sbin/service $1 stop
2061 else
2062 sudo /sbin/service $1 stop
2063 fi
2064}
2065
Sean Dague442e4e92015-06-24 13:24:02 -04002066# Test with a finite retry loop.
2067#
2068function test_with_retry {
2069 local testcmd=$1
2070 local failmsg=$2
2071 local until=${3:-10}
2072 local sleep=${4:-0.5}
2073
2074 if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
2075 die $LINENO "$failmsg"
2076 fi
2077}
2078
Dean Troyerdff49a22014-01-30 15:37:40 -06002079
2080# Restore xtrace
2081$XTRACE
2082
2083# Local variables:
2084# mode: shell-script
2085# End: