functions-common
e263c82e
 #!/bin/bash
 #
dff49a24
 # functions-common - Common functions used by DevStack components
 #
 # The canonical copy of this file is maintained in the DevStack repo.
 # All modifications should be made there and then sync'ed to other repos
 # as required.
 #
 # This file is sorted alphabetically within the function groups.
 #
 # - Config Functions
 # - Control Functions
 # - Distro Functions
 # - Git Functions
 # - OpenStack Functions
 # - Package Functions
 # - Process Functions
 # - Service Functions
f6368d3e
 # - System Functions
dff49a24
 #
 # The following variables are assumed to be defined by certain functions:
 #
 # - ``ENABLED_SERVICES``
 # - ``ERROR_ON_CLONE``
 # - ``FILES``
 # - ``OFFLINE``
 # - ``RECLONE``
d20f632a
 # - ``REQUIREMENTS_DIR``
 # - ``STACK_USER``
dff49a24
 # - ``http_proxy``, ``https_proxy``, ``no_proxy``
3324f19f
 #
dff49a24
 
 # Save trace setting
523f4880
 _XTRACE_FUNCTIONS_COMMON=$(set +o | grep xtrace)
dff49a24
 set +o xtrace
 
4ffb4541
 # ensure we don't re-source this in the same environment
 [[ -z "$_DEVSTACK_FUNCTIONS_COMMON" ]] || return 0
afef8bf0
 declare -r -g _DEVSTACK_FUNCTIONS_COMMON=1
4ffb4541
 
cc52406a
 # Global Config Variables
afef8bf0
 declare -A -g GITREPO
 declare -A -g GITBRANCH
 declare -A -g GITDIR
cc52406a
 
ef60f2b7
 KILL_PATH="$(which kill)"
53753293
 
6816234d
 # Save these variables to .stackenv
 STACK_ENV_VARS="BASE_SQL_CONN DATA_DIR DEST ENABLED_SERVICES HOST_IP \
32c00890
     KEYSTONE_SERVICE_URI \
0effa1a6
     LOGFILE OS_CACERT SERVICE_HOST STACK_USER TLS_IP \
5d04db20
     HOST_IPV6 SERVICE_IP_VERSION"
6816234d
 
 
 # Saves significant environment variables to .stackenv for later use
 # Refers to a lot of globals, only TOP_DIR and STACK_ENV_VARS are required to
 # function, the rest are simply saved and do not cause problems if they are undefined.
 # save_stackenv [tag]
 function save_stackenv {
     local tag=${1:-""}
     # Save some values we generated for later use
     time_stamp=$(date "+$TIMESTAMP_FORMAT")
     echo "# $time_stamp $tag" >$TOP_DIR/.stackenv
     for i in $STACK_ENV_VARS; do
         echo $i=${!i} >>$TOP_DIR/.stackenv
     done
 }
dff49a24
 
7224eecb
 # Update/create user clouds.yaml file.
 # clouds.yaml will have
 # - A `devstack` entry for the `demo` user for the `demo` project.
 # - A `devstack-admin` entry for the `admin` user for the `admin` project.
 # write_clouds_yaml
 function write_clouds_yaml {
ee9bb766
     # The location is a variable to allow for easier refactoring later to make it
     # overridable. There is currently no usecase where doing so makes sense, so
     # it's not currently configurable.
313ddaee
 
ee9bb766
     CLOUDS_YAML=/etc/openstack/clouds.yaml
7224eecb
 
ee9bb766
     sudo mkdir -p $(dirname $CLOUDS_YAML)
     sudo chown -R $STACK_USER /etc/openstack
7224eecb
 
ee9bb766
     CA_CERT_ARG=''
     if [ -f "$SSL_BUNDLE_FILE" ]; then
         CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
     fi
c67d22e2
     # demo -> devstack
51ecf0a8
     $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
ee9bb766
         --file $CLOUDS_YAML \
         --os-cloud devstack \
         --os-region-name $REGION_NAME \
         $CA_CERT_ARG \
e43f60ba
         --os-auth-url $KEYSTONE_SERVICE_URI \
ee9bb766
         --os-username demo \
         --os-password $ADMIN_PASSWORD \
         --os-project-name demo
c67d22e2
 
     # alt_demo -> devstack-alt
51ecf0a8
     $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
c67d22e2
         --file $CLOUDS_YAML \
e20cb43f
         --os-cloud devstack-alt \
c67d22e2
         --os-region-name $REGION_NAME \
         $CA_CERT_ARG \
e43f60ba
         --os-auth-url $KEYSTONE_SERVICE_URI \
c67d22e2
         --os-username alt_demo \
         --os-password $ADMIN_PASSWORD \
         --os-project-name alt_demo
 
     # admin -> devstack-admin
51ecf0a8
     $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
ee9bb766
         --file $CLOUDS_YAML \
         --os-cloud devstack-admin \
         --os-region-name $REGION_NAME \
         $CA_CERT_ARG \
e43f60ba
         --os-auth-url $KEYSTONE_SERVICE_URI \
ee9bb766
         --os-username admin \
         --os-password $ADMIN_PASSWORD \
         --os-project-name admin
74379df1
 
56905820
     # admin with a system-scoped token -> devstack-system
     $PYTHON $TOP_DIR/tools/update_clouds_yaml.py \
         --file $CLOUDS_YAML \
         --os-cloud devstack-system-admin \
         --os-region-name $REGION_NAME \
         $CA_CERT_ARG \
         --os-auth-url $KEYSTONE_SERVICE_URI \
         --os-username admin \
         --os-password $ADMIN_PASSWORD \
         --os-system-scope all
 
7a0fa4fd
     cat >> $CLOUDS_YAML <<EOF
 functional:
   image_name: $DEFAULT_IMAGE_NAME
 EOF
 
74379df1
     # CLean up any old clouds.yaml files we had laying around
0b4a009e
     rm -f $(eval echo ~"$STACK_USER")/.config/openstack/clouds.yaml
7224eecb
 }
 
e82bac04
 # trueorfalse <True|False> <VAR>
 #
 # Normalize config-value provided in variable VAR to either "True" or
 # "False".  If VAR is unset (i.e. $VAR evaluates as empty), the value
 # of the second argument will be used as the default value.
 #
 #  Accepts as False: 0 no  No  NO  false False FALSE
 #  Accepts as True:  1 yes Yes YES true  True  TRUE
 #
 # usage:
 #  VAL=$(trueorfalse False VAL)
aee18c74
 function trueorfalse {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
45917cc4
     set +o xtrace
98f59aaf
 
dff49a24
     local default=$1
e82bac04
 
     if [ -z $2 ]; then
         die $LINENO "variable to normalize required"
     fi
98f59aaf
     local testval=${!2:-}
 
     case "$testval" in
         "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;;
         "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;;
         * )                                       echo "$default" ;;
     esac
dff49a24
 
45917cc4
     $xtrace
dff49a24
 }
 
1bd79596
 function isset {
     [[ -v "$1" ]]
 }
dff49a24
 
6816234d
 
dff49a24
 # Control Functions
 # =================
 
 # Prints backtrace info
 # filename:lineno:function
 # backtrace level
 function backtrace {
     local level=$1
ada886dd
     local deep
     deep=$((${#BASH_SOURCE[@]} - 1))
dff49a24
     echo "[Call Trace]"
     while [ $level -le $deep ]; do
         echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
         deep=$((deep - 1))
     done
 }
 
 # Prints line number and "message" then exits
 # die $LINENO "message"
aee18c74
 function die {
dff49a24
     local exitcode=$?
     set +o xtrace
     local line=$1; shift
     if [ $exitcode == 0 ]; then
         exitcode=1
     fi
     backtrace 2
     err $line "$*"
a25a6f6d
     # Give buffers a second to flush
     sleep 1
dff49a24
     exit $exitcode
 }
 
 # Checks an environment variable is not set or has length 0 OR if the
 # exit code is non-zero and prints "message" and exits
 # NOTE: env-var is the variable name without a '$'
 # die_if_not_set $LINENO env-var "message"
aee18c74
 function die_if_not_set {
dff49a24
     local exitcode=$?
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
dff49a24
     set +o xtrace
     local line=$1; shift
     local evar=$1; shift
     if ! is_set $evar || [ $exitcode != 0 ]; then
         die $line "$*"
     fi
d5dfa4c5
     $xtrace
dff49a24
 }
 
1de9e330
 function deprecated {
     local text=$1
     DEPRECATED_TEXT+="\n$text"
8b1bbd69
     echo "WARNING: $text" >&2
1de9e330
 }
 
dff49a24
 # Prints line number and "message" in error format
 # err $LINENO "message"
aee18c74
 function err {
dff49a24
     local exitcode=$?
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
dff49a24
     set +o xtrace
     local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
e8a6a026
     echo "$msg" 1>&2;
dde41d07
     if [[ -n ${LOGDIR} ]]; then
e8a6a026
         echo "$msg" >> "${LOGDIR}/error.log"
dff49a24
     fi
d5dfa4c5
     $xtrace
dff49a24
     return $exitcode
 }
 
 # Checks an environment variable is not set or has length 0 OR if the
 # exit code is non-zero and prints "message"
 # NOTE: env-var is the variable name without a '$'
 # err_if_not_set $LINENO env-var "message"
aee18c74
 function err_if_not_set {
dff49a24
     local exitcode=$?
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
dff49a24
     set +o xtrace
     local line=$1; shift
     local evar=$1; shift
     if ! is_set $evar || [ $exitcode != 0 ]; then
         err $line "$*"
     fi
d5dfa4c5
     $xtrace
dff49a24
     return $exitcode
 }
 
 # Exit after outputting a message about the distribution not being supported.
 # exit_distro_not_supported [optional-string-telling-what-is-missing]
 function exit_distro_not_supported {
     if [[ -z "$DISTRO" ]]; then
         GetDistro
     fi
 
     if [ $# -gt 0 ]; then
         die $LINENO "Support for $DISTRO is incomplete: no support for $@"
     else
         die $LINENO "Support for $DISTRO is incomplete."
     fi
 }
 
 # Test if the named environment variable is set and not zero length
 # is_set env-var
aee18c74
 function is_set {
dff49a24
     local var=\$"$1"
     eval "[ -n \"$var\" ]" # For ex.: sh -c "[ -n \"$var\" ]" would be better, but several exercises depends on this
 }
 
 # Prints line number and "message" in warning format
 # warn $LINENO "message"
aee18c74
 function warn {
dff49a24
     local exitcode=$?
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
dff49a24
     set +o xtrace
     local msg="[WARNING] ${BASH_SOURCE[2]}:$1 $2"
e8a6a026
     echo "$msg"
d5dfa4c5
     $xtrace
dff49a24
     return $exitcode
 }
 
 
 # Distro Functions
 # ================
 
 # Determine OS Vendor, Release and Update
7710e7fc
 
 #
 # NOTE : For portability, you almost certainly do not want to use
 # these variables directly!  The "is_*" functions defined below this
 # bundle up compatible platforms under larger umbrellas that we have
 # determinted are compatible enough (e.g. is_ubuntu covers Ubuntu &
 # Debian, is_fedora covers RPM-based distros).  Higher-level functions
 # such as "install_package" further abstract things in better ways.
 #
d5dfa4c5
 # ``os_VENDOR`` - vendor name: ``Ubuntu``, ``Fedora``, etc
ff10ac31
 # ``os_RELEASE`` - major release: ``16.04`` (Ubuntu), ``23`` (Fedora)
d5dfa4c5
 # ``os_PACKAGE`` - package type: ``deb`` or ``rpm``
ff10ac31
 # ``os_CODENAME`` - vendor's codename for release: ``xenial``
7710e7fc
 
afef8bf0
 declare -g os_VENDOR os_RELEASE os_PACKAGE os_CODENAME
7710e7fc
 
 # Make a *best effort* attempt to install lsb_release packages for the
 # user if not available.  Note can't use generic install_package*
 # because they depend on this!
 function _ensure_lsb_release {
dab3901d
     if [[ -x $(command -v lsb_release 2>/dev/null) ]]; then
7710e7fc
         return
     fi
 
dab3901d
     if [[ -x $(command -v apt-get 2>/dev/null) ]]; then
7710e7fc
         sudo apt-get install -y lsb-release
dab3901d
     elif [[ -x $(command -v zypper 2>/dev/null) ]]; then
5ef8a125
         sudo zypper -n install lsb-release
dab3901d
     elif [[ -x $(command -v dnf 2>/dev/null) ]]; then
7710e7fc
         sudo dnf install -y redhat-lsb-core
     else
         die $LINENO "Unable to find or auto-install lsb_release"
     fi
 }
d5dfa4c5
 
dff49a24
 # GetOSVersion
7710e7fc
 #  Set the following variables:
 #  - os_RELEASE
 #  - os_CODENAME
 #  - os_VENDOR
 #  - os_PACKAGE
aee18c74
 function GetOSVersion {
7710e7fc
     # We only support distros that provide a sane lsb_release
     _ensure_lsb_release
d5dfa4c5
 
7710e7fc
     os_RELEASE=$(lsb_release -r -s)
     os_CODENAME=$(lsb_release -c -s)
     os_VENDOR=$(lsb_release -i -s)
 
     if [[ $os_VENDOR =~ (Debian|Ubuntu|LinuxMint) ]]; then
dff49a24
         os_PACKAGE="deb"
7710e7fc
     else
         os_PACKAGE="rpm"
dff49a24
     fi
7710e7fc
 
     typeset -xr os_VENDOR
     typeset -xr os_RELEASE
     typeset -xr os_PACKAGE
     typeset -xr os_CODENAME
dff49a24
 }
 
 # Translate the OS version values into common nomenclature
 # Sets global ``DISTRO`` from the ``os_*`` values
afef8bf0
 declare -g DISTRO
d5dfa4c5
 
aee18c74
 function GetDistro {
dff49a24
     GetOSVersion
96006658
     if [[ "$os_VENDOR" =~ (Ubuntu) || "$os_VENDOR" =~ (Debian) || \
             "$os_VENDOR" =~ (LinuxMint) ]]; then
         # 'Everyone' refers to Ubuntu / Debian / Mint releases by
7710e7fc
         # the code name adjective
dff49a24
         DISTRO=$os_CODENAME
     elif [[ "$os_VENDOR" =~ (Fedora) ]]; then
         # For Fedora, just use 'f' and the release
         DISTRO="f$os_RELEASE"
b6f04ca5
     elif is_opensuse; then
dff49a24
         DISTRO="opensuse-$os_RELEASE"
4404f680
         # Tumbleweed uses "n/a" as a codename, and the release is a datestring
297a50ac
         # like 20180218, so not very useful. Leap however uses a release
         # with a "dot", so for example 15.0
         [ "$os_CODENAME" = "n/a" -a "$os_RELEASE" = "${os_RELEASE/\./}" ] && \
             DISTRO="opensuse-tumbleweed"
b6f04ca5
     elif is_suse_linux_enterprise; then
7710e7fc
         # just use major release
         DISTRO="sle${os_RELEASE%.*}"
     elif [[ "$os_VENDOR" =~ (Red.*Hat) || \
6c639c9d
         "$os_VENDOR" =~ (CentOS) || \
1da4e790
         "$os_VENDOR" =~ (Scientific) || \
4e6f8ca6
         "$os_VENDOR" =~ (OracleServer) || \
718512c4
         "$os_VENDOR" =~ (Virtuozzo) ]]; then
dff49a24
         # Drop the . release as we assume it's compatible
7710e7fc
         # XXX re-evaluate when we get RHEL10
dff49a24
         DISTRO="rhel${os_RELEASE::1}"
     elif [[ "$os_VENDOR" =~ (XenServer) ]]; then
7710e7fc
         DISTRO="xs${os_RELEASE%.*}"
dff49a24
     else
ed92e431
         # We can't make a good choice here.  Setting a sensible DISTRO
         # is part of the problem, but not the major issue -- we really
         # only use DISTRO in the code as a fine-filter.
         #
         # The bigger problem is categorising the system into one of
         # our two big categories as Ubuntu/Debian-ish or
         # Fedora/CentOS-ish.
         #
         # The setting of os_PACKAGE above is only set to "deb" based
         # on a hard-coded list of vendor names ... thus we will
         # default to thinking unknown distros are RPM based
         # (ie. is_ubuntu does not match).  But the platform will then
         # also not match in is_fedora, because that also has a list of
         # names.
         #
         # So, if you are reading this, getting your distro supported
         # is really about making sure it matches correctly in these
         # functions.  Then you can choose a sensible way to construct
         # DISTRO based on your distros release approach.
         die $LINENO "Unable to determine DISTRO, can not continue."
dff49a24
     fi
7710e7fc
     typeset -xr DISTRO
dff49a24
 }
 
 # Utility function for checking machine architecture
 # is_arch arch-type
 function is_arch {
d5dfa4c5
     [[ "$(uname -m)" == "$1" ]]
dff49a24
 }
 
ec47bc1d
 # Determine if current distribution is an Oracle distribution
 # is_oraclelinux
 function is_oraclelinux {
     if [[ -z "$os_VENDOR" ]]; then
         GetOSVersion
     fi
 
4e6f8ca6
     [ "$os_VENDOR" = "OracleServer" ]
ec47bc1d
 }
 
 
dff49a24
 # Determine if current distribution is a Fedora-based distribution
 # (Fedora, RHEL, CentOS, etc).
 # is_fedora
 function is_fedora {
     if [[ -z "$os_VENDOR" ]]; then
         GetOSVersion
     fi
 
6c639c9d
     [ "$os_VENDOR" = "Fedora" ] || [ "$os_VENDOR" = "Red Hat" ] || \
78d3739d
         [ "$os_VENDOR" = "RedHatEnterpriseServer" ] || \
587e0a35
         [ "$os_VENDOR" = "RedHatEnterprise" ] || \
4e6f8ca6
         [ "$os_VENDOR" = "CentOS" ] || [ "$os_VENDOR" = "OracleServer" ] || \
b8335eeb
         [ "$os_VENDOR" = "Virtuozzo" ]
dff49a24
 }
 
 
 # Determine if current distribution is a SUSE-based distribution
 # (openSUSE, SLE).
 # is_suse
 function is_suse {
b6f04ca5
     is_opensuse || is_suse_linux_enterprise
 }
 
 
 # Determine if current distribution is an openSUSE distribution
 # is_opensuse
 function is_opensuse {
     if [[ -z "$os_VENDOR" ]]; then
         GetOSVersion
     fi
 
     [[ "$os_VENDOR" =~ (openSUSE) ]]
 }
 
 
 # Determine if current distribution is a SUSE Linux Enterprise (SLE)
 # distribution
 # is_suse_linux_enterprise
 function is_suse_linux_enterprise {
dff49a24
     if [[ -z "$os_VENDOR" ]]; then
         GetOSVersion
     fi
 
b6f04ca5
     [[ "$os_VENDOR" =~ (^SUSE) ]]
dff49a24
 }
 
 
 # Determine if current distribution is an Ubuntu-based distribution
 # It will also detect non-Ubuntu but Debian-based distros
 # is_ubuntu
 function is_ubuntu {
     if [[ -z "$os_PACKAGE" ]]; then
         GetOSVersion
     fi
     [ "$os_PACKAGE" = "deb" ]
 }
 
 
 # Git Functions
 # =============
 
abc7b1d7
 # Returns openstack release name for a given branch name
 # ``get_release_name_from_branch branch-name``
aee18c74
 function get_release_name_from_branch {
abc7b1d7
     local branch=$1
8f385725
     if [[ $branch =~ "stable/" || $branch =~ "proposed/" ]]; then
abc7b1d7
         echo ${branch#*/}
     else
         echo "master"
     fi
 }
 
dff49a24
 # git clone only if directory doesn't exist already.  Since ``DEST`` might not
 # be owned by the installation user, we create the directory and change the
 # ownership to the proper user.
50cda69f
 # Set global ``RECLONE=yes`` to simulate a clone when dest-dir exists
 # Set global ``ERROR_ON_CLONE=True`` to abort execution with an error if the git repo
dff49a24
 # does not exist (default is False, meaning the repo will be cloned).
53753293
 # Uses globals ``ERROR_ON_CLONE``, ``OFFLINE``, ``RECLONE``
dff49a24
 # git_clone remote dest-dir branch
 function git_clone {
50cda69f
     local git_remote=$1
     local git_dest=$2
     local git_ref=$3
ada886dd
     local orig_dir
     orig_dir=$(pwd)
51f0de5c
     local git_clone_flags=""
50cda69f
 
53753293
     RECLONE=$(trueorfalse False RECLONE)
59d52f30
     if [[ "${GIT_DEPTH}" -gt 0 ]]; then
51f0de5c
         git_clone_flags="$git_clone_flags --depth $GIT_DEPTH"
     fi
 
dff49a24
     if [[ "$OFFLINE" = "True" ]]; then
         echo "Running in offline mode, clones already exist"
         # print out the results so we know what change was used in the logs
50cda69f
         cd $git_dest
dff49a24
         git show --oneline | head -1
64bd0165
         cd $orig_dir
dff49a24
         return
     fi
 
50cda69f
     if echo $git_ref | egrep -q "^refs"; then
dff49a24
         # If our branch name is a gerrit style refs/changes/...
50cda69f
         if [[ ! -d $git_dest ]]; then
ebe63d82
             if [[ "$ERROR_ON_CLONE" = "True" ]]; then
                 echo "The $git_dest project was not found; if this is a gate job, add"
35a0c573
                 echo "the project to 'required-projects' in the job definition."
dff49a24
                 die $LINENO "Cloning not allowed in this configuration"
ebe63d82
             fi
51f0de5c
             git_timed clone $git_clone_flags $git_remote $git_dest
dff49a24
         fi
50cda69f
         cd $git_dest
         git_timed fetch $git_remote $git_ref && git checkout FETCH_HEAD
dff49a24
     else
         # do a full clone only if the directory doesn't exist
50cda69f
         if [[ ! -d $git_dest ]]; then
ebe63d82
             if [[ "$ERROR_ON_CLONE" = "True" ]]; then
                 echo "The $git_dest project was not found; if this is a gate job, add"
                 echo "the project to the \$PROJECTS variable in the job definition."
dff49a24
                 die $LINENO "Cloning not allowed in this configuration"
ebe63d82
             fi
90742fc1
             # '--branch' can also take tags
             git_timed clone $git_clone_flags $git_remote $git_dest --branch $git_ref
dff49a24
         elif [[ "$RECLONE" = "True" ]]; then
             # if it does exist then simulate what clone does if asked to RECLONE
50cda69f
             cd $git_dest
dff49a24
             # set the url to pull from and fetch
50cda69f
             git remote set-url origin $git_remote
d53ad0b0
             git_timed fetch origin
dff49a24
             # remove the existing ignored files (like pyc) as they cause breakage
             # (due to the py files having older timestamps than our pyc, so python
             # thinks the pyc files are correct using them)
50cda69f
             find $git_dest -name '*.pyc' -delete
 
             # handle git_ref accordingly to type (tag, branch)
             if [[ -n "`git show-ref refs/tags/$git_ref`" ]]; then
                 git_update_tag $git_ref
             elif [[ -n "`git show-ref refs/heads/$git_ref`" ]]; then
                 git_update_branch $git_ref
             elif [[ -n "`git show-ref refs/remotes/origin/$git_ref`" ]]; then
                 git_update_remote_branch $git_ref
dff49a24
             else
50cda69f
                 die $LINENO "$git_ref is neither branch nor tag"
dff49a24
             fi
 
         fi
     fi
 
     # print out the results so we know what change was used in the logs
50cda69f
     cd $git_dest
dff49a24
     git show --oneline | head -1
64bd0165
     cd $orig_dir
dff49a24
 }
 
cc52406a
 # A variation on git clone that lets us specify a project by it's
 # actual name, like oslo.config. This is exceptionally useful in the
 # library installation case
 function git_clone_by_name {
     local name=$1
     local repo=${GITREPO[$name]}
     local dir=${GITDIR[$name]}
     local branch=${GITBRANCH[$name]}
     git_clone $repo $dir $branch
 }
 
 
d53ad0b0
 # git can sometimes get itself infinitely stuck with transient network
 # errors or other issues with the remote end.  This wraps git in a
 # timeout/retry loop and is intended to watch over non-local git
 # processes that might hang.  GIT_TIMEOUT, if set, is passed directly
 # to timeout(1); otherwise the default value of 0 maintains the status
 # quo of waiting forever.
 # usage: git_timed <git-command>
aee18c74
 function git_timed {
d53ad0b0
     local count=0
     local timeout=0
 
     if [[ -n "${GIT_TIMEOUT}" ]]; then
         timeout=${GIT_TIMEOUT}
     fi
 
2ca8af45
     time_start "git_timed"
d53ad0b0
     until timeout -s SIGINT ${timeout} git "$@"; do
         # 124 is timeout(1)'s special return code when it reached the
         # timeout; otherwise assume fatal failure
         if [[ $? -ne 124 ]]; then
             die $LINENO "git call failed: [git $@]"
         fi
 
         count=$(($count + 1))
e4af9298
         warn $LINENO "timeout ${count} for git call: [git $@]"
d53ad0b0
         if [ $count -eq 3 ]; then
             die $LINENO "Maximum of 3 git retries reached"
         fi
         sleep 5
     done
2ca8af45
     time_stop "git_timed"
d53ad0b0
 }
 
dff49a24
 # git update using reference as a branch.
 # git_update_branch ref
aee18c74
 function git_update_branch {
50cda69f
     local git_branch=$1
dff49a24
 
50cda69f
     git checkout -f origin/$git_branch
dff49a24
     # a local branch might not exist
50cda69f
     git branch -D $git_branch || true
     git checkout -b $git_branch
dff49a24
 }
 
 # git update using reference as a branch.
 # git_update_remote_branch ref
aee18c74
 function git_update_remote_branch {
50cda69f
     local git_branch=$1
dff49a24
 
50cda69f
     git checkout -b $git_branch -t origin/$git_branch
dff49a24
 }
 
 # git update using reference as a tag. Be careful editing source at that repo
 # as working copy will be in a detached mode
 # git_update_tag ref
aee18c74
 function git_update_tag {
50cda69f
     local git_tag=$1
dff49a24
 
50cda69f
     git tag -d $git_tag
dff49a24
     # fetching given tag only
50cda69f
     git_timed fetch origin tag $git_tag
     git checkout -f $git_tag
dff49a24
 }
 
 
 # OpenStack Functions
 # ===================
 
 # Get the default value for HOST_IP
 # get_default_host_ip fixed_range floating_range host_ip_iface host_ip
aee18c74
 function get_default_host_ip {
dff49a24
     local fixed_range=$1
     local floating_range=$2
     local host_ip_iface=$3
     local host_ip=$4
180f5eb6
     local af=$5
dff49a24
 
     # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
     if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
         host_ip=""
a3430270
         # Find the interface used for the default route
180f5eb6
         host_ip_iface=${host_ip_iface:-$(ip -f $af route | awk '/default/ {print $5}' | head -1)}
ada886dd
         local host_ips
         host_ips=$(LC_ALL=C ip -f $af addr show ${host_ip_iface} | sed /temporary/d |awk /$af'/ {split($2,parts,"/");  print parts[1]}')
d5dfa4c5
         local ip
         for ip in $host_ips; do
dff49a24
             # Attempt to filter out IP addresses that are part of the fixed and
             # floating range. Note that this method only works if the ``netaddr``
             # python library is installed. If it is not installed, an error
             # will be printed and the first IP from the interface will be used.
             # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
             # address.
180f5eb6
             if [[ "$af" == "inet6" ]]; then
                 host_ip=$ip
                 break;
             fi
d5dfa4c5
             if ! (address_in_net $ip $fixed_range || address_in_net $ip $floating_range); then
                 host_ip=$ip
dff49a24
                 break;
             fi
         done
     fi
     echo $host_ip
 }
 
f71b500b
 # Generates hex string from ``size`` byte of pseudo random data
 # generate_hex_string size
 function generate_hex_string {
     local size=$1
     hexdump -n "$size" -v -e '/1 "%02x"' /dev/urandom
 }
 
dff49a24
 # Grab a numbered field from python prettytable output
 # Fields are numbered starting with 1
 # Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
 # get_field field-number
aee18c74
 function get_field {
d5dfa4c5
     local data field
dff49a24
     while read data; do
         if [ "$1" -lt 0 ]; then
             field="(\$(NF$1))"
         else
             field="\$$(($1 + 1))"
         fi
         echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
     done
 }
 
f26deea6
 # install default policy
 # copy over a default policy.json and policy.d for projects
 function install_default_policy {
     local project=$1
ada886dd
     local project_uc
     project_uc=$(echo $1|tr a-z A-Z)
f26deea6
     local conf_dir="${project_uc}_CONF_DIR"
     # eval conf dir to get the variable
     conf_dir="${!conf_dir}"
     local project_dir="${project_uc}_DIR"
     # eval project dir to get the variable
     project_dir="${!project_dir}"
     local sample_conf_dir="${project_dir}/etc/${project}"
     local sample_policy_dir="${project_dir}/etc/${project}/policy.d"
 
     # first copy any policy.json
     cp -p $sample_conf_dir/policy.json $conf_dir
     # then optionally copy over policy.d
     if [[ -d $sample_policy_dir ]]; then
         cp -r $sample_policy_dir $conf_dir/policy.d
     fi
 }
 
dff49a24
 # Add a policy to a policy.json file
 # Do nothing if the policy already exists
 # ``policy_add policy_file policy_name policy_permissions``
aee18c74
 function policy_add {
dff49a24
     local policy_file=$1
     local policy_name=$2
     local policy_perm=$3
 
     if grep -q ${policy_name} ${policy_file}; then
         echo "Policy ${policy_name} already exists in ${policy_file}"
         return
     fi
 
     # Add a terminating comma to policy lines without one
     # Remove the closing '}' and all lines following to the end-of-file
ada886dd
     local tmpfile
     tmpfile=$(mktemp)
dff49a24
     uniq ${policy_file} | sed -e '
         s/]$/],/
         /^[}]/,$d
     ' > ${tmpfile}
 
     # Append policy and closing brace
     echo "    \"${policy_name}\": ${policy_perm}" >>${tmpfile}
     echo "}" >>${tmpfile}
 
     mv ${tmpfile} ${policy_file}
 }
 
24779f65
 # Gets or creates a domain
 # Usage: get_or_create_domain <name> <description>
 function get_or_create_domain {
11d276c7
     local domain_id
24779f65
     # Gets domain id
11d276c7
     domain_id=$(
24779f65
         # Gets domain id
050a0d5b
         openstack domain show $1 \
24779f65
             -f value -c id 2>/dev/null ||
         # Creates new domain
050a0d5b
         openstack domain create $1 \
24779f65
             --description "$2" \
             -f value -c id
     )
     echo $domain_id
 }
 
b74e01c3
 # Gets or creates group
9d7e776b
 # Usage: get_or_create_group <groupname> <domain> [<description>]
b74e01c3
 function get_or_create_group {
     local desc="${3:-}"
11d276c7
     local group_id
b74e01c3
     # Gets group id
11d276c7
     group_id=$(
b74e01c3
         # Creates new group with --or-show
050a0d5b
         openstack group create $1 \
9d7e776b
             --domain $2 --description "$desc" --or-show \
b74e01c3
             -f value -c id
     )
     echo $group_id
 }
 
0abde393
 # Gets or creates user
9d7e776b
 # Usage: get_or_create_user <username> <password> <domain> [<email>]
0abde393
 function get_or_create_user {
11d276c7
     local user_id
9d7e776b
     if [[ ! -z "$4" ]]; then
         local email="--email=$4"
6dd8a8be
     else
d5dfa4c5
         local email=""
6dd8a8be
     fi
0abde393
     # Gets user id
11d276c7
     user_id=$(
245daa27
         # Creates new user with --or-show
9d7e776b
         openstack user create \
0abde393
             $1 \
             --password "$2" \
9d7e776b
             --domain=$3 \
d5dfa4c5
             $email \
245daa27
             --or-show \
0abde393
             -f value -c id
     )
d5dfa4c5
     echo $user_id
0abde393
 }
 
 # Gets or creates project
b632c9ef
 # Usage: get_or_create_project <name> <domain>
0abde393
 function get_or_create_project {
11d276c7
     local project_id
     project_id=$(
245daa27
         # Creates new project with --or-show
050a0d5b
         openstack project create $1 \
b632c9ef
             --domain=$2 \
             --or-show -f value -c id
0abde393
     )
d5dfa4c5
     echo $project_id
0abde393
 }
 
 # Gets or creates role
 # Usage: get_or_create_role <name>
 function get_or_create_role {
11d276c7
     local role_id
     role_id=$(
245daa27
         # Creates role with --or-show
72ce6acd
         openstack role create $1 \
             --or-show -f value -c id
0abde393
     )
d5dfa4c5
     echo $role_id
0abde393
 }
 
cbcbd8f3
 # Returns the domain parts of a function call if present
 # Usage: _get_domain_args [<user_domain> <project_domain>]
 function _get_domain_args {
     local domain
     domain=""
 
     if [[ -n "$1" ]]; then
         domain="$domain --user-domain $1"
     fi
     if [[ -n "$2" ]]; then
         domain="$domain --project-domain $2"
     fi
 
     echo $domain
 }
 
9b215db5
 # Gets or adds user role to project
cbcbd8f3
 # Usage: get_or_add_user_project_role <role> <user> <project> [<user_domain> <project_domain>]
9b215db5
 function get_or_add_user_project_role {
11d276c7
     local user_role_id
cbcbd8f3
 
     domain_args=$(_get_domain_args $4 $5)
 
0abde393
     # Gets user role id
c271b3ea
     user_role_id=$(openstack role assignment list \
3e9b562d
         --role $1 \
5541a618
         --user $2 \
72ce6acd
         --project $3 \
cbcbd8f3
         $domain_args \
3e9b562d
         | grep '^|\s[a-f0-9]\+' | get_field 1)
d5dfa4c5
     if [[ -z "$user_role_id" ]]; then
50821bed
         # Adds role to user and get it
         openstack role add $1 \
0abde393
             --user $2 \
cbcbd8f3
             --project $3 \
             $domain_args
c271b3ea
         user_role_id=$(openstack role assignment list \
3e9b562d
             --role $1 \
50821bed
             --user $2 \
             --project $3 \
cbcbd8f3
             $domain_args \
3e9b562d
             | grep '^|\s[a-f0-9]\+' | get_field 1)
0abde393
     fi
d5dfa4c5
     echo $user_role_id
0abde393
 }
 
4ce859ab
 # Gets or adds user role to domain
 # Usage: get_or_add_user_domain_role <role> <user> <domain>
 function get_or_add_user_domain_role {
     local user_role_id
     # Gets user role id
c271b3ea
     user_role_id=$(openstack role assignment list \
3e9b562d
         --role $1 \
4ce859ab
         --user $2 \
         --domain $3 \
3e9b562d
         | grep '^|\s[a-f0-9]\+' | get_field 1)
4ce859ab
     if [[ -z "$user_role_id" ]]; then
         # Adds role to user and get it
         openstack role add $1 \
             --user $2 \
             --domain $3
c271b3ea
         user_role_id=$(openstack role assignment list \
3e9b562d
             --role $1 \
4ce859ab
             --user $2 \
             --domain $3 \
3e9b562d
             | grep '^|\s[a-f0-9]\+' | get_field 1)
4ce859ab
     fi
     echo $user_role_id
 }
 
4599fd17
 # Gets or adds group role to project
 # Usage: get_or_add_group_project_role <role> <group> <project>
 function get_or_add_group_project_role {
11d276c7
     local group_role_id
4599fd17
     # Gets group role id
c271b3ea
     group_role_id=$(openstack role assignment list \
3e9b562d
         --role $1 \
4599fd17
         --group $2 \
         --project $3 \
c271b3ea
         -f value)
4599fd17
     if [[ -z "$group_role_id" ]]; then
72ce6acd
         # Adds role to group and get it
         openstack role add $1 \
             --group $2 \
             --project $3
c271b3ea
         group_role_id=$(openstack role assignment list \
3e9b562d
             --role $1 \
4599fd17
             --group $2 \
             --project $3 \
c271b3ea
             -f value)
4599fd17
     fi
     echo $group_role_id
 }
 
0abde393
 # Gets or creates service
 # Usage: get_or_create_service <name> <type> <description>
 function get_or_create_service {
11d276c7
     local service_id
0abde393
     # Gets service id
11d276c7
     service_id=$(
0abde393
         # Gets service id
aedb8b97
         openstack service show $2 -f value -c id 2>/dev/null ||
0abde393
         # Creates new service if not exists
         openstack service create \
789af5cc
             $2 \
             --name $1 \
0abde393
             --description="$3" \
             -f value -c id
     )
d5dfa4c5
     echo $service_id
0abde393
 }
 
b17ad756
 # Create an endpoint with a specific interface
ae4578be
 # Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
 function _get_or_create_endpoint_with_interface {
11d276c7
     local endpoint_id
ae4578be
     endpoint_id=$(openstack endpoint list \
         --service $1 \
         --interface $2 \
         --region $4 \
         -c ID -f value)
     if [[ -z "$endpoint_id" ]]; then
         # Creates new endpoint
         endpoint_id=$(openstack endpoint create \
             $1 $2 $3 --region $4 -f value -c id)
     fi
dff49a24
 
ae4578be
     echo $endpoint_id
7d1ec430
 }
 
 # Gets or creates endpoint
11eb2017
 # Usage: get_or_create_endpoint <service> <region> <publicurl> [adminurl] [internalurl]
ae4578be
 function get_or_create_endpoint {
b17ad756
     # NOTE(jamielennnox): when converting to v3 endpoint creation we go from
     # creating one endpoint with multiple urls to multiple endpoints each with
     # a different interface.  To maintain the existing function interface we
ae4578be
     # create 3 endpoints and return the id of the public one. In reality
b17ad756
     # returning the public id will not make a lot of difference as there are no
     # scenarios currently that use the returned id. Ideally this behaviour
     # should be pushed out to the service setups and let them create the
     # endpoints they need.
ada886dd
     local public_id
ae4578be
     public_id=$(_get_or_create_endpoint_with_interface $1 public $3 $2)
11eb2017
     # only create admin/internal urls if provided content for them
     if [[ -n "$4" ]]; then
         _get_or_create_endpoint_with_interface $1 admin $4 $2
     fi
     if [[ -n "$5" ]]; then
         _get_or_create_endpoint_with_interface $1 internal $5 $2
     fi
b17ad756
     # return the public id to indicate success, and this is the endpoint most likely wanted
     echo $public_id
 }
 
 # Get a URL from the identity service
 # Usage: get_endpoint_url <service> <interface>
 function get_endpoint_url {
     echo $(openstack endpoint list \
             --service $1 --interface $2 \
             -c URL -f value)
 }
 
47367071
 # check if we are using ironic with hardware
 # TODO(jroll) this is a kludge left behind when ripping ironic code
 # out of tree, as it is used by nova and neutron.
 # figure out a way to refactor nova/neutron code to eliminate this
 function is_ironic_hardware {
8a4dea24
     is_service_enabled ironic && [[ "$IRONIC_IS_HARDWARE" == "True" ]] && return 0
47367071
     return 1
 }
 
d5dfa4c5
 
dff49a24
 # Package Functions
 # =================
 
 # _get_package_dir
aee18c74
 function _get_package_dir {
7ca90cde
     local base_dir=$1
dff49a24
     local pkg_dir
7ca90cde
 
     if [[ -z "$base_dir" ]]; then
         base_dir=$FILES
     fi
dff49a24
     if is_ubuntu; then
7ca90cde
         pkg_dir=$base_dir/debs
dff49a24
     elif is_fedora; then
7ca90cde
         pkg_dir=$base_dir/rpms
dff49a24
     elif is_suse; then
7ca90cde
         pkg_dir=$base_dir/rpms-suse
dff49a24
     else
         exit_distro_not_supported "list of packages"
     fi
     echo "$pkg_dir"
 }
 
88ee8ce4
 # Wrapper for ``apt-get update`` to try multiple times on the update
 # to address bad package mirrors (which happen all the time).
 function apt_get_update {
     # only do this once per run
     if [[ "$REPOS_UPDATED" == "True" && "$RETRY_UPDATE" != "True" ]]; then
         return
     fi
 
     # bail if we are offline
     [[ "$OFFLINE" = "True" ]] && return
 
     local sudo="sudo"
     [[ "$(id -u)" = "0" ]] && sudo="env"
 
     # time all the apt operations
     time_start "apt-get-update"
 
     local proxies="http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} no_proxy=${no_proxy:-} "
     local update_cmd="$sudo $proxies apt-get update"
     if ! timeout 300 sh -c "while ! $update_cmd; do sleep 30; done"; then
         die $LINENO "Failed to update apt repos, we're dead now"
     fi
 
     REPOS_UPDATED=True
     # stop the clock
     time_stop "apt-get-update"
 }
 
dff49a24
 # Wrapper for ``apt-get`` to set cache and proxy environment variables
 # Uses globals ``OFFLINE``, ``*_proxy``
 # apt_get operation package [package ...]
aee18c74
 function apt_get {
e208d060
     local xtrace result
433a9b10
     xtrace=$(set +o | grep xtrace)
45917cc4
     set +o xtrace
 
dff49a24
     [[ "$OFFLINE" = "True" || -z "$@" ]] && return
     local sudo="sudo"
     [[ "$(id -u)" = "0" ]] && sudo="env"
45917cc4
 
95c33d53
     # time all the apt operations
     time_start "apt-get"
 
45917cc4
     $xtrace
53753293
 
dff49a24
     $sudo DEBIAN_FRONTEND=noninteractive \
53753293
         http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
         no_proxy=${no_proxy:-} \
f568c3a1
         apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@" < /dev/null
e208d060
     result=$?
95c33d53
 
     # stop the clock
     time_stop "apt-get"
e208d060
     return $result
dff49a24
 }
 
7ca90cde
 function _parse_package_files {
     local files_to_parse=$@
 
     if [[ -z "$DISTRO" ]]; then
         GetDistro
     fi
 
     for fname in ${files_to_parse}; do
         local OIFS line package distros distro
         [[ -e $fname ]] || continue
 
         OIFS=$IFS
         IFS=$'\n'
         for line in $(<${fname}); do
             if [[ $line =~ "NOPRIME" ]]; then
                 continue
             fi
 
1ff75ff8
             # Assume we want this package; free-form
             # comments allowed after a #
             package=${line%%#*}
7ca90cde
             inst_pkg=1
 
             # Look for # dist:xxx in comment
             if [[ $line =~ (.*)#.*dist:([^ ]*) ]]; then
                 # We are using BASH regexp matching feature.
                 package=${BASH_REMATCH[1]}
                 distros=${BASH_REMATCH[2]}
33c9a67e
                 # In bash ${VAR,,} will lowercase VAR
7ca90cde
                 # Look for a match in the distro list
                 if [[ ! ${distros,,} =~ ${DISTRO,,} ]]; then
                     # If no match then skip this package
                     inst_pkg=0
                 fi
             fi
 
682e0abe
             # Look for # not:xxx in comment
             if [[ $line =~ (.*)#.*not:([^ ]*) ]]; then
                 # We are using BASH regexp matching feature.
                 package=${BASH_REMATCH[1]}
                 distros=${BASH_REMATCH[2]}
                 # In bash ${VAR,,} will lowercase VAR
                 # Look for a match in the distro list
                 if [[ ${distros,,} =~ ${DISTRO,,} ]]; then
                     # If match then skip this package
                     inst_pkg=0
                 fi
             fi
 
7ca90cde
             if [[ $inst_pkg = 1 ]]; then
                 echo $package
             fi
         done
         IFS=$OIFS
     done
 }
 
dff49a24
 # get_packages() collects a list of package names of any type from the
81a016db
 # prerequisite files in ``files/{debs|rpms}``.  The list is intended
dff49a24
 # to be passed to a package installer such as apt or yum.
 #
 # Only packages required for the services in 1st argument will be
 # included.  Two bits of metadata are recognized in the prerequisite files:
 #
 # - ``# NOPRIME`` defers installation to be performed later in `stack.sh`
 # - ``# dist:DISTRO`` or ``dist:DISTRO1,DISTRO2`` limits the selection
 #   of the package to the distros listed.  The distro names are case insensitive.
682e0abe
 # - ``# not:DISTRO`` or ``not:DISTRO1,DISTRO2`` limits the selection
 #   of the package to the distros not listed. The distro names are case insensitive.
aee18c74
 function get_packages {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
45917cc4
     set +o xtrace
dff49a24
     local services=$@
ada886dd
     local package_dir
     package_dir=$(_get_package_dir)
53753293
     local file_to_parse=""
     local service=""
dff49a24
 
7d515b5d
     if [ $# -ne 1 ]; then
         die $LINENO "get_packages takes a single, comma-separated argument"
     fi
 
dff49a24
     if [[ -z "$package_dir" ]]; then
         echo "No package directory supplied"
         return 1
     fi
     for service in ${services//,/ }; do
         # Allow individual services to specify dependencies
         if [[ -e ${package_dir}/${service} ]]; then
7ca90cde
             file_to_parse="${file_to_parse} ${package_dir}/${service}"
dff49a24
         fi
         # NOTE(sdague) n-api needs glance for now because that's where
         # glance client is
         if [[ $service == n-api ]]; then
6f3f3108
             if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
7ca90cde
                 file_to_parse="${file_to_parse} ${package_dir}/nova"
dff49a24
             fi
6f3f3108
             if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
7ca90cde
                 file_to_parse="${file_to_parse} ${package_dir}/glance"
dff49a24
             fi
52e52d89
             if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
                 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
             fi
dff49a24
         elif [[ $service == c-* ]]; then
6f3f3108
             if [[ ! $file_to_parse =~ $package_dir/cinder ]]; then
7ca90cde
                 file_to_parse="${file_to_parse} ${package_dir}/cinder"
dff49a24
             fi
52e52d89
             if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
                 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
             fi
dff49a24
         elif [[ $service == s-* ]]; then
6f3f3108
             if [[ ! $file_to_parse =~ $package_dir/swift ]]; then
7ca90cde
                 file_to_parse="${file_to_parse} ${package_dir}/swift"
dff49a24
             fi
         elif [[ $service == n-* ]]; then
6f3f3108
             if [[ ! $file_to_parse =~ $package_dir/nova ]]; then
7ca90cde
                 file_to_parse="${file_to_parse} ${package_dir}/nova"
dff49a24
             fi
52e52d89
             if [[ ! $file_to_parse =~ $package_dir/os-brick ]]; then
                 file_to_parse="${file_to_parse} ${package_dir}/os-brick"
             fi
dff49a24
         elif [[ $service == g-* ]]; then
6f3f3108
             if [[ ! $file_to_parse =~ $package_dir/glance ]]; then
7ca90cde
                 file_to_parse="${file_to_parse} ${package_dir}/glance"
dff49a24
             fi
         elif [[ $service == key* ]]; then
6f3f3108
             if [[ ! $file_to_parse =~ $package_dir/keystone ]]; then
7ca90cde
                 file_to_parse="${file_to_parse} ${package_dir}/keystone"
dff49a24
             fi
387aadd1
         elif [[ $service == q-* || $service == neutron-* ]]; then
             if [[ ! $file_to_parse =~ $package_dir/neutron-common ]]; then
                 file_to_parse="${file_to_parse} ${package_dir}/neutron-common"
dff49a24
             fi
539ec438
         elif [[ $service == ir-* ]]; then
6f3f3108
             if [[ ! $file_to_parse =~ $package_dir/ironic ]]; then
7ca90cde
                 file_to_parse="${file_to_parse} ${package_dir}/ironic"
539ec438
             fi
dff49a24
         fi
     done
7ca90cde
     echo "$(_parse_package_files $file_to_parse)"
     $xtrace
 }
dff49a24
 
7ca90cde
 # get_plugin_packages() collects a list of package names of any type from a
 # plugin's prerequisite files in ``$PLUGIN/devstack/files/{debs|rpms}``.  The
 # list is intended to be passed to a package installer such as apt or yum.
 #
 # Only packages required for enabled and collected plugins will included.
 #
dc97cb71
 # The same metadata used in the main DevStack prerequisite files may be used
7ca90cde
 # in these prerequisite files, see get_packages() for more info.
 function get_plugin_packages {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
7ca90cde
     set +o xtrace
     local files_to_parse=""
     local package_dir=""
     for plugin in ${DEVSTACK_PLUGINS//,/ }; do
7ae97298
         package_dir="$(_get_package_dir ${GITDIR[$plugin]}/devstack/files)"
5979f47a
         files_to_parse+=" $package_dir/$plugin"
dff49a24
     done
7ca90cde
     echo "$(_parse_package_files $files_to_parse)"
45917cc4
     $xtrace
dff49a24
 }
 
fa9aadfd
 # Search plugins for a bindep.txt file
 #
 # Uses globals ``BINDEP_CMD``, ``GITDIR``, ``DEVSTACK_PLUGINS``
 #
 # Note this is only valid after BINDEP_CMD is setup in stack.sh, and
 # is thus not really intended to be called externally.
 function _get_plugin_bindep_packages {
     local xtrace
     xtrace=$(set +o | grep xtrace)
     set +o xtrace
 
     local bindep_file
     local packages
 
     for plugin in ${DEVSTACK_PLUGINS//,/ }; do
         bindep_file=${GITDIR[$plugin]}/devstack/files/bindep.txt
         if [[ -f ${bindep_file} ]]; then
             packages+=$($BINDEP_CMD -b --file ${bindep_file} || true)
         fi
     done
     echo "${packages}"
     $xtrace
 }
 
dff49a24
 # Distro-agnostic package installer
d5dfa4c5
 # Uses globals ``NO_UPDATE_REPOS``, ``REPOS_UPDATED``, ``RETRY_UPDATE``
dff49a24
 # install_package package [package ...]
5cc6d2cd
 function update_package_repo {
53753293
     NO_UPDATE_REPOS=${NO_UPDATE_REPOS:-False}
     REPOS_UPDATED=${REPOS_UPDATED:-False}
     RETRY_UPDATE=${RETRY_UPDATE:-False}
 
9e17974a
     if [[ "$NO_UPDATE_REPOS" = "True" ]]; then
5cc6d2cd
         return 0
     fi
dff49a24
 
5cc6d2cd
     if is_ubuntu; then
88ee8ce4
         apt_get_update
5cc6d2cd
     fi
 }
 
 function real_install_package {
     if is_ubuntu; then
dff49a24
         apt_get install "$@"
     elif is_fedora; then
         yum_install "$@"
     elif is_suse; then
         zypper_install "$@"
     else
         exit_distro_not_supported "installing packages"
     fi
 }
 
5cc6d2cd
 # Distro-agnostic package installer
 # install_package package [package ...]
 function install_package {
     update_package_repo
ecc1f41d
     if ! real_install_package "$@"; then
         RETRY_UPDATE=True update_package_repo && real_install_package "$@"
     fi
5cc6d2cd
 }
 
dff49a24
 # Distro-agnostic function to tell if a package is installed
 # is_package_installed package [package ...]
aee18c74
 function is_package_installed {
dff49a24
     if [[ -z "$@" ]]; then
         return 1
     fi
 
     if [[ -z "$os_PACKAGE" ]]; then
         GetOSVersion
     fi
 
     if [[ "$os_PACKAGE" = "deb" ]]; then
         dpkg -s "$@" > /dev/null 2> /dev/null
     elif [[ "$os_PACKAGE" = "rpm" ]]; then
         rpm --quiet -q "$@"
     else
         exit_distro_not_supported "finding if a package is installed"
     fi
 }
 
 # Distro-agnostic package uninstaller
 # uninstall_package package [package ...]
aee18c74
 function uninstall_package {
dff49a24
     if is_ubuntu; then
         apt_get purge "$@"
     elif is_fedora; then
67fd81a4
         sudo dnf remove -y "$@" ||:
dff49a24
     elif is_suse; then
56b39126
         sudo zypper remove -y "$@" ||:
dff49a24
     else
         exit_distro_not_supported "uninstalling packages"
     fi
 }
 
67fd81a4
 # Wrapper for ``dnf`` to set proxy environment variables
 # Uses globals ``OFFLINE``, ``*_proxy``
 # The name is kept for backwards compatability with external
 # callers, despite none of our supported platforms using yum
 # any more.
dff49a24
 # yum_install package [package ...]
aee18c74
 function yum_install {
5dcbf7ae
     local result parse_yum_result
 
dff49a24
     [[ "$OFFLINE" = "True" ]] && return
5dcbf7ae
 
     time_start "yum_install"
67fd81a4
     sudo_with_proxies dnf install -y "$@"
5dcbf7ae
     time_stop "yum_install"
dff49a24
 }
 
 # zypper wrapper to set arguments correctly
d5dfa4c5
 # Uses globals ``OFFLINE``, ``*_proxy``
dff49a24
 # zypper_install package [package ...]
aee18c74
 function zypper_install {
dff49a24
     [[ "$OFFLINE" = "True" ]] && return
     local sudo="sudo"
     [[ "$(id -u)" = "0" ]] && sudo="env"
fdf00f27
     $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}" \
         no_proxy="${no_proxy:-}" \
297a50ac
         zypper --non-interactive install --auto-agree-with-licenses --no-recommends "$@"
dff49a24
 }
 
58243f62
 # Run bindep and install packages it outputs
 #
 # Usage:
 #  install_bindep <path-to-bindep.txt> [profile,profile]
 #
 # Note unlike the bindep command itself, profile(s) specified should
 # be a single, comma-separated string, no spaces.
 function install_bindep {
     local file=$1
     local profiles=${2:-""}
     local pkgs
 
     if [[ ! -f $file ]]; then
dd3731c8
         warn $LINENO "Can not find bindep file: $file"
         return
58243f62
     fi
 
     # converting here makes it much easier to work with passing
     # arguments
     profiles=${profiles/,/ /}
 
     # Note bindep returns 1 when packages need to be installed, so we
     # have to ignore it's return for "-e"
     pkgs=$($DEST/bindep-venv/bin/bindep -b --file $file $profiles || true)
 
     if [[ -n "${pkgs}" ]]; then
         install_package ${pkgs}
     fi
 }
 
5edae548
 function write_user_unit_file {
     local service=$1
     local command="$2"
     local group=$3
     local user=$4
     local extra=""
     if [[ -n "$group" ]]; then
         extra="Group=$group"
     fi
     local unitfile="$SYSTEMD_DIR/$service"
     mkdir -p $SYSTEMD_DIR
 
     iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
     iniset -sudo $unitfile "Service" "User" "$user"
     iniset -sudo $unitfile "Service" "ExecStart" "$command"
477a9628
     iniset -sudo $unitfile "Service" "KillMode" "process"
3b815a3c
     iniset -sudo $unitfile "Service" "TimeoutStopSec" "300"
ef60f2b7
     iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
5edae548
     if [[ -n "$group" ]]; then
         iniset -sudo $unitfile "Service" "Group" "$group"
     fi
     iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
 
     # changes to existing units sometimes need a refresh
     $SYSTEMCTL daemon-reload
 }
 
9c5ffd8d
 function write_uwsgi_user_unit_file {
     local service=$1
     local command="$2"
     local group=$3
     local user=$4
     local unitfile="$SYSTEMD_DIR/$service"
     mkdir -p $SYSTEMD_DIR
 
     iniset -sudo $unitfile "Unit" "Description" "Devstack $service"
f41bf2a9
     iniset -sudo $unitfile "Service" "SyslogIdentifier" "$service"
9c5ffd8d
     iniset -sudo $unitfile "Service" "User" "$user"
     iniset -sudo $unitfile "Service" "ExecStart" "$command"
ef60f2b7
     iniset -sudo $unitfile "Service" "ExecReload" "$KILL_PATH -HUP \$MAINPID"
9c5ffd8d
     iniset -sudo $unitfile "Service" "Type" "notify"
477a9628
     iniset -sudo $unitfile "Service" "KillMode" "process"
07b3bc24
     iniset -sudo $unitfile "Service" "Restart" "always"
9c5ffd8d
     iniset -sudo $unitfile "Service" "NotifyAccess" "all"
     iniset -sudo $unitfile "Service" "RestartForceExitStatus" "100"
 
     if [[ -n "$group" ]]; then
         iniset -sudo $unitfile "Service" "Group" "$group"
     fi
     iniset -sudo $unitfile "Install" "WantedBy" "multi-user.target"
 
     # changes to existing units sometimes need a refresh
     $SYSTEMCTL daemon-reload
 }
 
148d58c3
 function _common_systemd_pitfalls {
     local cmd=$1
     # do some sanity checks on $cmd to see things we don't expect to work
 
     if [[ "$cmd" =~ "sudo" ]]; then
4e16c3dd
         read -r -d '' msg << EOF || true  # read returns 1 for EOF, but it is ok here
148d58c3
 You are trying to use run_process with sudo, this is not going to work under systemd.
 
e8a6a026
 If you need to run a service as a user other than \$STACK_USER call it with:
148d58c3
 
    run_process \$name \$cmd \$group \$user
 EOF
e8a6a026
         die $LINENO "$msg"
148d58c3
     fi
 
     if [[ ! "$cmd" =~ ^/ ]]; then
4e16c3dd
         read -r -d '' msg << EOF || true  # read returns 1 for EOF, but it is ok here
148d58c3
 The cmd="$cmd" does not start with an absolute path. It will fail to
 start under systemd.
 
 Please update your run_process stanza to have an absolute path.
 EOF
e8a6a026
         die $LINENO "$msg"
148d58c3
     fi
 
 }
 
c087c718
 # Helper function to build a basic unit file and run it under systemd.
 function _run_under_systemd {
5edae548
     local service=$1
     local command="$2"
     local cmd=$command
148d58c3
     # sanity check the command
     _common_systemd_pitfalls "$cmd"
 
5edae548
     local systemd_service="devstack@$service.service"
     local group=$3
     local user=${4:-$STACK_USER}
9c5ffd8d
     if [[ "$command" =~ "uwsgi" ]] ; then
         write_uwsgi_user_unit_file $systemd_service "$cmd" "$group" "$user"
     else
         write_user_unit_file $systemd_service "$cmd" "$group" "$user"
     fi
c087c718
 
     $SYSTEMCTL enable $systemd_service
     $SYSTEMCTL start $systemd_service
5edae548
 }
 
dff49a24
 # Find out if a process exists by partial name.
 # is_running name
aee18c74
 function is_running {
dff49a24
     local name=$1
     ps auxw | grep -v grep | grep ${name} > /dev/null
d5dfa4c5
     local exitcode=$?
dff49a24
     # some times I really hate bash reverse binary logic
d5dfa4c5
     return $exitcode
dff49a24
 }
 
3159a821
 # Run a single service under screen or directly
 # If the command includes shell metachatacters (;<>*) it must be run using a shell
2f27a0ed
 # If an optional group is provided sg will be used to run the
 # command as that group.
148d58c3
 # run_process service "command-line" [group] [user]
aee18c74
 function run_process {
dff49a24
     local service=$1
     local command="$2"
2f27a0ed
     local group=$3
5edae548
     local user=$4
edc11c2f
 
5edae548
     local name=$service
dff49a24
 
2ca8af45
     time_start "run_process"
3159a821
     if is_service_enabled $service; then
cdba1b37
         _run_under_systemd "$name" "$command" "$group" "$user"
3159a821
     fi
2ca8af45
     time_stop "run_process"
dff49a24
 }
 
3159a821
 # Stop a service process
 # If a PID is available use it, kill the whole process group via TERM
 # If screen is being used kill the screen window; this will catch processes
 # that did not leave a PID behind
2b97a816
 # Uses globals ``SERVICE_DIR``
3159a821
 # stop_process service
 function stop_process {
     local service=$1
 
     SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
 
     if is_service_enabled $service; then
803acffc
         # Only do this for units which appear enabled, this also
         # catches units that don't really exist for cases like
         # keystone without a failure.
         if $SYSTEMCTL is-enabled devstack@$service.service; then
             $SYSTEMCTL stop devstack@$service.service
             $SYSTEMCTL disable devstack@$service.service
5edae548
         fi
dff49a24
     fi
 }
 
cdba1b37
 # use systemctl to check service status
aee18c74
 function service_check {
dff49a24
     local service
cdba1b37
     for service in ${ENABLED_SERVICES//,/ }; do
         # because some things got renamed like key => keystone
         if $SYSTEMCTL is-enabled devstack@$service.service; then
             # no-pager is needed because otherwise status dumps to a
             # pager when in interactive mode, which will stop a manual
             # devstack run.
             $SYSTEMCTL status devstack@$service.service --no-pager
         fi
dff49a24
     done
 }
 
2f27a0ed
 
cdba1b37
 function tail_log {
     deprecated "With the removal of screen support, tail_log is deprecated and will be removed after Queens"
3159a821
 }
 
2c65e71a
 # Plugin Functions
 # =================
 
 DEVSTACK_PLUGINS=${DEVSTACK_PLUGINS:-""}
 
 # enable_plugin <name> <url> [branch]
 #
 # ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
 # ``url`` is a git url
 # ``branch`` is a gitref. If it's not set, defaults to master
 function enable_plugin {
     local name=$1
     local url=$2
     local branch=${3:-master}
51584861
     if is_plugin_enabled $name; then
21d84c29
         die $LINENO "Plugin attempted to be enabled twice: ${name} ${url} ${branch}"
     fi
2c65e71a
     DEVSTACK_PLUGINS+=",$name"
     GITREPO[$name]=$url
     GITDIR[$name]=$DEST/$name
     GITBRANCH[$name]=$branch
 }
 
51584861
 # is_plugin_enabled <name>
 #
 # Check if the plugin was enabled, e.g. using enable_plugin
 #
 # ``name`` The name with which the plugin was enabled
 function is_plugin_enabled {
     local name=$1
     if [[ ",${DEVSTACK_PLUGINS}," =~ ",${name}," ]]; then
         return 0
     fi
     return 1
 }
 
2c65e71a
 # fetch_plugins
 #
 # clones all plugins
 function fetch_plugins {
     local plugins="${DEVSTACK_PLUGINS}"
     local plugin
 
     # short circuit if nothing to do
     if [[ -z $plugins ]]; then
         return
     fi
 
dc97cb71
     echo "Fetching DevStack plugins"
2c65e71a
     for plugin in ${plugins//,/ }; do
         git_clone_by_name $plugin
     done
 }
 
 # load_plugin_settings
 #
 # Load settings from plugins in the order that they were registered
 function load_plugin_settings {
     local plugins="${DEVSTACK_PLUGINS}"
     local plugin
 
     # short circuit if nothing to do
     if [[ -z $plugins ]]; then
         return
     fi
 
     echo "Loading plugin settings"
     for plugin in ${plugins//,/ }; do
         local dir=${GITDIR[$plugin]}
         # source any known settings
         if [[ -f $dir/devstack/settings ]]; then
             source $dir/devstack/settings
         fi
     done
 }
 
6e275e17
 # plugin_override_defaults
 #
 # Run an extremely early setting phase for plugins that allows default
 # overriding of services.
 function plugin_override_defaults {
     local plugins="${DEVSTACK_PLUGINS}"
     local plugin
 
     # short circuit if nothing to do
     if [[ -z $plugins ]]; then
         return
     fi
 
     echo "Overriding Configuration Defaults"
     for plugin in ${plugins//,/ }; do
         local dir=${GITDIR[$plugin]}
         # source any overrides
         if [[ -f $dir/devstack/override-defaults ]]; then
             # be really verbose that an override is happening, as it
             # may not be obvious if things fail later.
33c9a67e
             echo "$plugin has overridden the following defaults"
6e275e17
             cat $dir/devstack/override-defaults
             source $dir/devstack/override-defaults
         fi
     done
 }
 
2c65e71a
 # run_plugins
 #
 # Run the devstack/plugin.sh in all the plugin directories. These are
 # run in registration order.
 function run_plugins {
     local mode=$1
     local phase=$2
441ff07b
 
     local plugins="${DEVSTACK_PLUGINS}"
     local plugin
2c65e71a
     for plugin in ${plugins//,/ }; do
         local dir=${GITDIR[$plugin]}
         if [[ -f $dir/devstack/plugin.sh ]]; then
             source $dir/devstack/plugin.sh $mode $phase
         fi
     done
 }
 
 function run_phase {
     local mode=$1
     local phase=$2
     if [[ -d $TOP_DIR/extras.d ]]; then
64be3210
         local extra_plugin_file_name
         for extra_plugin_file_name in $TOP_DIR/extras.d/*.sh; do
41d01104
             # NOTE(sdague): only process extras.d for the 3 explicitly
             # white listed elements in tree. We want these to move out
             # over time as well, but they are in tree, so we need to
             # manage that.
c9f63278
             local exceptions="80-tempest.sh"
5cdee8dd
             local extra
             extra=$(basename $extra_plugin_file_name)
1de9e330
             if [[ ! ( $exceptions =~ "$extra" ) ]]; then
41d01104
                 warn "use of extras.d is no longer supported"
                 warn "processing of project $extra is skipped"
             else
                 [[ -r $extra_plugin_file_name ]] && source $extra_plugin_file_name $mode $phase
1de9e330
             fi
2c65e71a
         done
     fi
     # the source phase corresponds to settings loading in plugins
     if [[ "$mode" == "source" ]]; then
         load_plugin_settings
c6d47014
         verify_disabled_services
6e275e17
     elif [[ "$mode" == "override_defaults" ]]; then
         plugin_override_defaults
2c65e71a
     else
         run_plugins $mode $phase
     fi
 }
 
c5853ac1
 # define_plugin <name>
 #
 # This function is a no-op.  It allows a plugin to define its name So
 # that other plugins may reference it by name.  It should generally be
 # the last component of the canonical git repo name.  E.g.,
 # openstack/devstack-foo should use "devstack-foo" as the name here.
 #
 # This function is currently a noop, but the value may still be used
 # by external tools (as in plugin_requires) and may be used by
 # devstack in the future.
 #
 # ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
 function define_plugin {
     :
 }
 
 # plugin_requires <name> <other>
 #
 # This function is a no-op.  It is currently used by external tools
 # (such as the devstack module for Ansible) to automatically generate
 # local.conf files.  It is not currently used by devstack itself to
 # resolve dependencies.
 #
 # ``name`` is an arbitrary name - (aka: glusterfs, nova-docker, zaqar)
 # ``other`` is the name of another plugin
 function plugin_requires {
     :
 }
 
dff49a24
 
 # Service Functions
 # =================
 
 # remove extra commas from the input string (i.e. ``ENABLED_SERVICES``)
 # _cleanup_service_list service-list
aee18c74
 function _cleanup_service_list {
8043bfaf
     local xtrace
     xtrace=$(set +o | grep xtrace)
     set +o xtrace
 
dff49a24
     echo "$1" | sed -e '
         s/,,/,/g;
         s/^,//;
         s/,$//
     '
8043bfaf
 
     $xtrace
dff49a24
 }
 
 # disable_all_services() removes all current services
 # from ``ENABLED_SERVICES`` to reset the configuration
 # before a minimal installation
 # Uses global ``ENABLED_SERVICES``
 # disable_all_services
aee18c74
 function disable_all_services {
dff49a24
     ENABLED_SERVICES=""
 }
 
 # Remove all services starting with '-'.  For example, to install all default
 # services except rabbit (rabbit) set in ``localrc``:
 # ENABLED_SERVICES+=",-rabbit"
 # Uses global ``ENABLED_SERVICES``
 # disable_negated_services
aee18c74
 function disable_negated_services {
8043bfaf
     local xtrace
     xtrace=$(set +o | grep xtrace)
     set +o xtrace
 
2796a82a
     local to_remove=""
     local remaining=""
dff49a24
     local service
2796a82a
 
     # build up list of services that should be removed; i.e. they
     # begin with "-"
     for service in ${ENABLED_SERVICES//,/ }; do
dff49a24
         if [[ ${service} == -* ]]; then
2796a82a
             to_remove+=",${service#-}"
         else
             remaining+=",${service}"
dff49a24
         fi
     done
2796a82a
 
     # go through the service list.  if this service appears in the "to
     # be removed" list, drop it
8606c98c
     ENABLED_SERVICES=$(remove_disabled_services "$remaining" "$to_remove")
8043bfaf
 
     $xtrace
dff49a24
 }
 
c6d47014
 # disable_service() prepares the services passed as argument to be
 # removed from the ``ENABLED_SERVICES`` list, if they are present.
dff49a24
 #
 # For example:
 #   disable_service rabbit
 #
c6d47014
 # Uses global ``DISABLED_SERVICES``
dff49a24
 # disable_service service [service ...]
aee18c74
 function disable_service {
8043bfaf
     local xtrace
     xtrace=$(set +o | grep xtrace)
     set +o xtrace
 
c6d47014
     local disabled_svcs="${DISABLED_SERVICES}"
     local enabled_svcs=",${ENABLED_SERVICES},"
dff49a24
     local service
     for service in $@; do
c6d47014
         disabled_svcs+=",$service"
dff49a24
         if is_service_enabled $service; then
c6d47014
             enabled_svcs=${enabled_svcs//,$service,/,}
dff49a24
         fi
     done
c6d47014
     DISABLED_SERVICES=$(_cleanup_service_list "$disabled_svcs")
     ENABLED_SERVICES=$(_cleanup_service_list "$enabled_svcs")
8043bfaf
 
     $xtrace
dff49a24
 }
 
 # enable_service() adds the services passed as argument to the
 # ``ENABLED_SERVICES`` list, if they are not already present.
 #
 # For example:
37eca489
 #   enable_service q-svc
dff49a24
 #
 # This function does not know about the special cases
 # for nova, glance, and neutron built into is_service_enabled().
 # Uses global ``ENABLED_SERVICES``
 # enable_service service [service ...]
aee18c74
 function enable_service {
8043bfaf
     local xtrace
     xtrace=$(set +o | grep xtrace)
     set +o xtrace
 
dff49a24
     local tmpsvcs="${ENABLED_SERVICES}"
d5dfa4c5
     local service
dff49a24
     for service in $@; do
c6d47014
         if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
             warn $LINENO "Attempt to enable_service ${service} when it has been disabled"
             continue
         fi
dff49a24
         if ! is_service_enabled $service; then
             tmpsvcs+=",$service"
         fi
     done
     ENABLED_SERVICES=$(_cleanup_service_list "$tmpsvcs")
     disable_negated_services
8043bfaf
 
     $xtrace
dff49a24
 }
 
 # is_service_enabled() checks if the service(s) specified as arguments are
 # enabled by the user in ``ENABLED_SERVICES``.
 #
 # Multiple services specified as arguments are ``OR``'ed together; the test
 # is a short-circuit boolean, i.e it returns on the first match.
 #
 # There are special cases for some 'catch-all' services::
 #   **nova** returns true if any service enabled start with **n-**
 #   **cinder** returns true if any service enabled start with **c-**
 #   **glance** returns true if any service enabled start with **g-**
 #   **neutron** returns true if any service enabled start with **q-**
 #   **swift** returns true if any service enabled start with **s-**
 #   **trove** returns true if any service enabled start with **tr-**
 #   For backward compatibility if we have **swift** in ENABLED_SERVICES all the
 #   **s-** services will be enabled. This will be deprecated in the future.
 #
 # Uses global ``ENABLED_SERVICES``
 # is_service_enabled service [service ...]
aee18c74
 function is_service_enabled {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
45917cc4
     set +o xtrace
8043bfaf
 
45917cc4
     local enabled=1
d5dfa4c5
     local services=$@
     local service
dff49a24
     for service in ${services}; do
45917cc4
         [[ ,${ENABLED_SERVICES}, =~ ,${service}, ]] && enabled=0
dff49a24
 
         # Look for top-level 'enabled' function for this service
         if type is_${service}_enabled >/dev/null 2>&1; then
             # A function exists for this service, use it
3db0aad6
             is_${service}_enabled && enabled=0
dff49a24
         fi
 
         # TODO(dtroyer): Remove these legacy special-cases after the is_XXX_enabled()
         #                are implemented
 
7b3ae533
         [[ ${service} == n-cpu-* && ,${ENABLED_SERVICES} =~ ,"n-cpu" ]] && enabled=0
         [[ ${service} == "nova" && ,${ENABLED_SERVICES} =~ ,"n-" ]] && enabled=0
         [[ ${service} == "glance" && ,${ENABLED_SERVICES} =~ ,"g-" ]] && enabled=0
         [[ ${service} == "neutron" && ,${ENABLED_SERVICES} =~ ,"q-" ]] && enabled=0
         [[ ${service} == "trove" && ,${ENABLED_SERVICES} =~ ,"tr-" ]] && enabled=0
         [[ ${service} == "swift" && ,${ENABLED_SERVICES} =~ ,"s-" ]] && enabled=0
         [[ ${service} == s-* && ,${ENABLED_SERVICES} =~ ,"swift" ]] && enabled=0
dff49a24
     done
8043bfaf
 
45917cc4
     $xtrace
     return $enabled
dff49a24
 }
 
8606c98c
 # remove specified list from the input string
 # remove_disabled_services service-list remove-list
 function remove_disabled_services {
8043bfaf
     local xtrace
     xtrace=$(set +o | grep xtrace)
     set +o xtrace
 
8606c98c
     local service_list=$1
     local remove_list=$2
     local service
     local enabled=""
 
     for service in ${service_list//,/ }; do
         local remove
         local add=1
         for remove in ${remove_list//,/ }; do
             if [[ ${remove} == ${service} ]]; then
                 add=0
                 break
             fi
         done
         if [[ $add == 1 ]]; then
             enabled="${enabled},$service"
         fi
     done
8043bfaf
 
     $xtrace
 
8606c98c
     _cleanup_service_list "$enabled"
 }
 
dff49a24
 # Toggle enable/disable_service for services that must run exclusive of each other
 #  $1 The name of a variable containing a space-separated list of services
 #  $2 The name of a variable in which to store the enabled service's name
 #  $3 The name of the service to enable
 function use_exclusive_service {
     local options=${!1}
     local selection=$3
d5dfa4c5
     local out=$2
dff49a24
     [ -z $selection ] || [[ ! "$options" =~ "$selection" ]] && return 1
d5dfa4c5
     local opt
dff49a24
     for opt in $options;do
         [[ "$opt" = "$selection" ]] && enable_service $opt || disable_service $opt
     done
     eval "$out=$selection"
     return 0
 }
 
c6d47014
 # Make sure that nothing has manipulated ENABLED_SERVICES in a way
 # that conflicts with prior calls to disable_service.
 # Uses global ``ENABLED_SERVICES``
 function verify_disabled_services {
     local service
     for service in ${ENABLED_SERVICES//,/ }; do
         if [[ ,${DISABLED_SERVICES}, =~ ,${service}, ]]; then
             die $LINENO "ENABLED_SERVICES directly modified to overcome 'disable_service ${service}'"
         fi
     done
 }
 
dff49a24
 
f6368d3e
 # System Functions
 # ================
dff49a24
 
 # Only run the command if the target file (the last arg) is not on an
 # NFS filesystem.
aee18c74
 function _safe_permission_operation {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
45917cc4
     set +o xtrace
dff49a24
     local args=( $@ )
     local last
     local sudo_cmd
     local dir_to_check
 
     let last="${#args[*]} - 1"
 
d5dfa4c5
     local dir_to_check=${args[$last]}
dff49a24
     if [ ! -d "$dir_to_check" ]; then
         dir_to_check=`dirname "$dir_to_check"`
     fi
 
     if is_nfs_directory "$dir_to_check" ; then
45917cc4
         $xtrace
dff49a24
         return 0
     fi
 
bcb2c30c
     sudo_cmd="sudo"
dff49a24
 
45917cc4
     $xtrace
dff49a24
     $sudo_cmd $@
 }
 
 # Exit 0 if address is in network or 1 if address is not in network
 # ip-range is in CIDR notation: 1.2.3.4/20
 # address_in_net ip-address ip-range
aee18c74
 function address_in_net {
dff49a24
     local ip=$1
     local range=$2
     local masklen=${range#*/}
ada886dd
     local network
     network=$(maskip ${range%/*} $(cidr2netmask $masklen))
     local subnet
     subnet=$(maskip $ip $(cidr2netmask $masklen))
dff49a24
     [[ $network == $subnet ]]
 }
 
 # Add a user to a group.
 # add_user_to_group user group
aee18c74
 function add_user_to_group {
dff49a24
     local user=$1
     local group=$2
 
a858085a
     sudo usermod -a -G "$group" "$user"
dff49a24
 }
 
 # Convert CIDR notation to a IPv4 netmask
 # cidr2netmask cidr-bits
aee18c74
 function cidr2netmask {
dff49a24
     local maskpat="255 255 255 255"
     local maskdgt="254 252 248 240 224 192 128"
     set -- ${maskpat:0:$(( ($1 / 8) * 4 ))}${maskdgt:$(( (7 - ($1 % 8)) * 4 )):3}
     echo ${1-0}.${2-0}.${3-0}.${4-0}
 }
 
69e3c0aa
 # Check if this is a valid ipv4 address string
 function is_ipv4_address {
     local address=$1
7617ac20
     local regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
69e3c0aa
     # TODO(clarkb) make this more robust
     if [[ "$address" =~ $regex ]] ; then
         return 0
     else
         return 1
     fi
 }
 
dc7b4294
 # Remove "[]" around urlquoted IPv6 addresses
 function ipv6_unquote {
     echo $1 | tr -d []
 }
 
dff49a24
 # Gracefully cp only if source file/dir exists
 # cp_it source destination
 function cp_it {
     if [ -e $1 ] || [ -d $1 ]; then
         cp -pRL $1 $2
     fi
 }
 
 # HTTP and HTTPS proxy servers are supported via the usual environment variables [1]
 # ``http_proxy``, ``https_proxy`` and ``no_proxy``. They can be set in
 # ``localrc`` or on the command line if necessary::
 #
 # [1] http://www.w3.org/Daemon/User/Proxies/ProxyClients.html
 #
 #     http_proxy=http://proxy.example.com:3128/ no_proxy=repo.example.net ./stack.sh
 
aee18c74
 function export_proxy_variables {
53753293
     if isset http_proxy ; then
dff49a24
         export http_proxy=$http_proxy
     fi
53753293
     if isset https_proxy ; then
dff49a24
         export https_proxy=$https_proxy
     fi
53753293
     if isset no_proxy ; then
dff49a24
         export no_proxy=$no_proxy
     fi
 }
 
 # Returns true if the directory is on a filesystem mounted via NFS.
aee18c74
 function is_nfs_directory {
ada886dd
     local mount_type
     mount_type=`stat -f -L -c %T $1`
dff49a24
     test "$mount_type" == "nfs"
 }
 
 # Return the network portion of the given IP address using netmask
 # netmask is in the traditional dotted-quad format
 # maskip ip-address netmask
aee18c74
 function maskip {
dff49a24
     local ip=$1
     local mask=$2
     local l="${ip%.*}"; local r="${ip#*.}"; local n="${mask%.*}"; local m="${mask#*.}"
ada886dd
     local subnet
     subnet=$((${ip%%.*}&${mask%%.*})).$((${r%%.*}&${m%%.*})).$((${l##*.}&${n##*.})).$((${ip##*.}&${mask##*.}))
dff49a24
     echo $subnet
 }
 
7938d83d
 function is_provider_network {
     if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
         return 0
     fi
     return 1
 }
 
 
6d213dfd
 # Return just the <major>.<minor> for the given python interpreter
 function _get_python_version {
     local interp=$1
     local version
4324f4ec
     # disable erroring out here, otherwise if python 3 doesn't exist we fail hard.
6bd4924c
     if [[ -x $(which $interp 2> /dev/null) ]]; then
4324f4ec
         version=$($interp -c 'import sys; print("%s.%s" % sys.version_info[0:2])')
     fi
6d213dfd
     echo ${version}
 }
 
3a2c86aa
 # Return the current python as "python<major>.<minor>"
 function python_version {
ada886dd
     local python_version
6d213dfd
     python_version=$(_get_python_version python2)
     echo "python${python_version}"
 }
 
 function python3_version {
     local python3_version
     python3_version=$(_get_python_version python3)
0417858a
     echo "python${python3_version}"
3a2c86aa
 }
 
6d213dfd
 
dff49a24
 # Service wrapper to restart services
 # restart_service service-name
aee18c74
 function restart_service {
247e4462
     if [ -x /bin/systemctl ]; then
         sudo /bin/systemctl restart $1
dff49a24
     else
247e4462
         sudo service $1 restart
dff49a24
     fi
247e4462
 
dff49a24
 }
 
 # Only change permissions of a file or directory if it is not on an
 # NFS filesystem.
aee18c74
 function safe_chmod {
dff49a24
     _safe_permission_operation chmod $@
 }
 
 # Only change ownership of a file or directory if it is not on an NFS
 # filesystem.
aee18c74
 function safe_chown {
dff49a24
     _safe_permission_operation chown $@
 }
 
 # Service wrapper to start services
 # start_service service-name
aee18c74
 function start_service {
247e4462
     if [ -x /bin/systemctl ]; then
         sudo /bin/systemctl start $1
dff49a24
     else
247e4462
         sudo service $1 start
dff49a24
     fi
 }
 
 # Service wrapper to stop services
 # stop_service service-name
aee18c74
 function stop_service {
247e4462
     if [ -x /bin/systemctl ]; then
         sudo /bin/systemctl stop $1
dff49a24
     else
247e4462
         sudo service $1 stop
dff49a24
     fi
 }
 
bcaadd63
 # Service wrapper to reload services
 # If the service was not in running state it will start it
4b49e409
 # reload_service service-name
 function reload_service {
     if [ -x /bin/systemctl ]; then
bcaadd63
         sudo /bin/systemctl reload-or-restart $1
4b49e409
     else
         sudo service $1 reload
     fi
 }
 
442e4e96
 # Test with a finite retry loop.
 #
 function test_with_retry {
     local testcmd=$1
     local failmsg=$2
     local until=${3:-10}
     local sleep=${4:-0.5}
 
2ca8af45
     time_start "test_with_retry"
442e4e96
     if ! timeout $until sh -c "while ! $testcmd; do sleep $sleep; done"; then
         die $LINENO "$failmsg"
     fi
2ca8af45
     time_stop "test_with_retry"
442e4e96
 }
 
5dcbf7ae
 # Like sudo but forwarding http_proxy https_proxy no_proxy environment vars.
 # If it is run as superuser then sudo is replaced by env.
 #
 function sudo_with_proxies {
     local sudo
 
     [[ "$(id -u)" = "0" ]] && sudo="env" || sudo="sudo"
 
     $sudo http_proxy="${http_proxy:-}" https_proxy="${https_proxy:-}"\
         no_proxy="${no_proxy:-}" "$@"
 }
 
95c33d53
 # Timing infrastructure - figure out where large blocks of time are
 # used in DevStack
 #
 # The timing infrastructure for DevStack is about collecting buckets
 # of time that are spend in some subtask. For instance, that might be
 # 'apt', 'pip', 'osc', even database migrations. We do this by a pair
 # of functions: time_start / time_stop.
 #
 # These take a single parameter: $name - which specifies the name of
 # the bucket to be accounted against. time_totals function spits out
 # the results.
 #
 # Resolution is only in whole seconds, so should be used for long
 # running activities.
 
afef8bf0
 declare -A -g _TIME_TOTAL
 declare -A -g _TIME_START
 declare -r -g _TIME_BEGIN=$(date +%s)
95c33d53
 
 # time_start $name
 #
 # starts the clock for a timer by name. Errors if that clock is
 # already started.
 function time_start {
     local name=$1
908a3a95
     local start_time=${_TIME_START[$name]}
95c33d53
     if [[ -n "$start_time" ]]; then
         die $LINENO "Trying to start the clock on $name, but it's already been started"
     fi
85cf2933
     _TIME_START[$name]=$(date +%s%3N)
95c33d53
 }
 
 # time_stop $name
 #
 # stops the clock for a timer by name, and accumulate that time in the
 # global counter for that name. Errors if that clock had not
 # previously been started.
 function time_stop {
5cdee8dd
     local name
     local end_time
38245da0
     local elapsed_time
5cdee8dd
     local total
     local start_time
 
     name=$1
908a3a95
     start_time=${_TIME_START[$name]}
5cdee8dd
 
95c33d53
     if [[ -z "$start_time" ]]; then
         die $LINENO "Trying to stop the clock on $name, but it was never started"
     fi
85cf2933
     end_time=$(date +%s%3N)
5cdee8dd
     elapsed_time=$(($end_time - $start_time))
908a3a95
     total=${_TIME_TOTAL[$name]:-0}
95c33d53
     # reset the clock so we can start it in the future
908a3a95
     _TIME_START[$name]=""
     _TIME_TOTAL[$name]=$(($total + $elapsed_time))
95c33d53
 }
 
85cf2933
 function oscwrap {
474f535a
     local xtrace
     xtrace=$(set +o | grep xtrace)
     set +o xtrace
 
85cf2933
     local out
     local rc
     local start
     local end
     # Cannot use timer_start and timer_stop as we run in subshells
     # and those rely on modifying vars in the same process (which cannot
     # happen from a subshell.
     start=$(date +%s%3N)
     out=$(command openstack "$@")
     rc=$?
     end=$(date +%s%3N)
     echo $((end - start)) >> $OSCWRAP_TIMER_FILE
 
     echo "$out"
474f535a
     $xtrace
85cf2933
     return $rc
 }
 
 function install_oscwrap {
     # File to accumulate our timing data
     OSCWRAP_TIMER_FILE=$(mktemp)
     # Bash by default doesn't expand aliases, allow it for the aliases
     # we want to whitelist.
     shopt -s expand_aliases
     # Remove all aliases that might be expanded to preserve old unexpanded
     # behavior
     unalias -a
     # Add only the alias we want for openstack
     alias openstack=oscwrap
 }
 
 function cleanup_oscwrap {
     local total=0
ffd0047a
     total=$(cat $OSCWRAP_TIMER_FILE | $PYTHON -c "import sys; print(sum(int(l) for l in sys.stdin))")
85cf2933
     _TIME_TOTAL["osc"]=$total
     rm $OSCWRAP_TIMER_FILE
 }
 
95c33d53
 # time_totals
908a3a95
 #  Print out total time summary
95c33d53
 function time_totals {
908a3a95
     local elapsed_time
     local end_time
5ad4e588
     local len=20
908a3a95
     local xtrace
5ad4e588
     local unaccounted_time
908a3a95
 
     end_time=$(date +%s)
     elapsed_time=$(($end_time - $_TIME_BEGIN))
5ad4e588
     unaccounted_time=$elapsed_time
908a3a95
 
     # pad 1st column this far
     for t in ${!_TIME_TOTAL[*]}; do
         if [[ ${#t} -gt $len ]]; then
             len=${#t}
         fi
     done
 
85cf2933
     cleanup_oscwrap
 
908a3a95
     xtrace=$(set +o | grep xtrace)
     set +o xtrace
 
95c33d53
     echo
908a3a95
     echo "========================="
     echo "DevStack Component Timing"
5ad4e588
     echo " (times are in seconds)  "
908a3a95
     echo "========================="
     for t in ${!_TIME_TOTAL[*]}; do
         local v=${_TIME_TOTAL[$t]}
85cf2933
         # because we're recording in milliseconds
         v=$(($v / 1000))
908a3a95
         printf "%-${len}s %3d\n" "$t" "$v"
5ad4e588
         unaccounted_time=$(($unaccounted_time - $v))
95c33d53
     done
5ad4e588
     echo "-------------------------"
     printf "%-${len}s %3d\n" "Unaccounted time" "$unaccounted_time"
908a3a95
     echo "========================="
5ad4e588
     printf "%-${len}s %3d\n" "Total runtime" "$elapsed_time"
908a3a95
 
     $xtrace
95c33d53
 }
dff49a24
 
 # Restore xtrace
523f4880
 $_XTRACE_FUNCTIONS_COMMON
dff49a24
 
 # Local variables:
 # mode: shell-script
 # End: