2015-10-05 16:39:26 -04:00
#!/bin/bash
#
# functions - puppet-openstack-integration specific functions
#
2021-02-02 14:03:36 +05:30
# To retry a command until it succeed for a given
# number of retries(default to 3).
retry_cmd() {
local cmd=$1
local total_tries=${2:-3}
local delay=${3:-5}
local retry_count=1
local ret_code=1
until [[ ${ret_code} -eq 0 || ${retry_count} -gt ${total_tries} ]]; do
echo Retry count:-${retry_count}, Command:-$cmd
2021-02-05 14:01:00 +05:30
set +e
2021-02-02 14:03:36 +05:30
$cmd
ret_code=$?
2021-02-05 14:01:00 +05:30
set -e
2021-02-02 14:03:36 +05:30
((retry_count++))
sleep ${delay}
done
2021-02-05 14:01:00 +05:30
if [ ${ret_code} -ne 0 ]; then
echo Failed even after ${total_tries} retries, Exiting
exit ${ret_code}
fi
2021-02-02 14:03:36 +05:30
}
2015-10-05 16:39:26 -04:00
# Install external Puppet modules with r10k
# Uses the following variables:
#
# - ``SCRIPT_DIR`` must be set to script path
# - ``GEM_BIN_DIR`` must be set to Gem bin directory
install_external() {
2021-02-02 14:03:36 +05:30
install_cmd="r10k -v DEBUG puppetfile install \
2020-05-10 14:12:18 +09:00
--puppetfile ${SCRIPT_DIR}/Puppetfile1 \
2021-02-02 14:03:36 +05:30
--moduledir ${PUPPETFILE_DIR}"
retry_cmd "${install_cmd}"
2015-10-05 16:39:26 -04:00
}
2019-06-26 12:52:35 +02:00
# Install Puppet OpenStack modules from zuul checkouts
2015-10-05 16:39:26 -04:00
# Uses the following variables:
#
2018-02-06 03:34:34 +00:00
# - ``PUPPETFILE_DIR`` must be set to Puppet modules directory
2015-10-05 16:39:26 -04:00
# - ``SCRIPT_DIR`` must be set to script path
2016-04-20 09:38:56 -04:00
# - ``ZUUL_BRANCH`` must be set to Zuul branch. Fallback to 'master'.
2018-07-02 09:31:53 +02:00
# - ``CEPH_VERSION`` can be set to override Ceph version.
2015-10-05 16:39:26 -04:00
install_openstack() {
2018-06-19 17:32:27 +02:00
# Periodic jobs run without ref on master
ZUUL_BRANCH=${ZUUL_BRANCH:-master}
2018-07-02 09:31:53 +02:00
if [ "$ZUUL_PROJECT" != "openstack/puppet-ceph" ] && [ -n "$CEPH_VERSION" ]; then
2022-08-21 13:43:58 +09:00
if [ "$CEPH_VERSION" == "jewel" ] || [ "$CEPH_VERSION" == "luminous" ] || [ "$CEPH_VERSION" == "mimic" ]; then
2018-06-19 17:32:27 +02:00
ZUUL_BRANCH="stable/$CEPH_VERSION"
2022-08-21 13:43:58 +09:00
else
ZUUL_BRANCH="master"
2018-06-19 17:32:27 +02:00
fi
fi
2015-10-05 16:39:26 -04:00
local project_names=$(awk '{ if ($1 == ":git") print $3 }' \
${SCRIPT_DIR}/Puppetfile0 | tr -d "'," | cut -d '/' -f 4- | xargs
)
2019-06-26 12:52:35 +02:00
for project in $project_names openstack/puppet-openstack-integration
do
local module_name=$(echo $project | cut -d "-" -f2-)
if [ -d /home/zuul/src/opendev.org/$project ]; then
cp -R /home/zuul/src/opendev.org/$project $PUPPETFILE_DIR/$module_name
else
git clone -b $ZUUL_BRANCH https://opendev.org/$project $PUPPETFILE_DIR/$module_name
fi
done
2015-11-06 14:43:25 -05:00
# Because openstack-integration can't be a class name.
# https://projects.puppetlabs.com/issues/5268
2018-02-06 03:34:34 +00:00
mv $PUPPETFILE_DIR/openstack-integration $PUPPETFILE_DIR/openstack_integration
2015-10-05 16:39:26 -04:00
}
# Install all Puppet modules with r10k
# Uses the following variables:
#
2020-05-10 14:12:18 +09:00
# - ``PUPPETFILE_DIR`` must be set to Puppet modules directory
2015-10-05 16:39:26 -04:00
# - ``SCRIPT_DIR`` must be set to script path
install_all() {
2016-09-16 11:48:00 -04:00
# When installing from local source, we want to install the current source
# we're working from.
2021-02-02 14:03:36 +05:30
install_cmd="r10k -v DEBUG puppetfile install \
2020-05-14 18:08:51 +02:00
--puppetfile ${SCRIPT_DIR}/Puppetfile \
2021-02-02 14:03:36 +05:30
--moduledir ${PUPPETFILE_DIR}"
retry_cmd "${install_cmd}"
2018-02-06 03:34:34 +00:00
cp -a ${SCRIPT_DIR} ${PUPPETFILE_DIR}/openstack_integration
2015-10-05 16:39:26 -04:00
}
# Install Puppet OpenStack modules and dependencies by using
2019-06-26 12:52:35 +02:00
# zuul checkouts or r10k.
2015-10-05 16:39:26 -04:00
# Uses the following variables:
#
2018-02-06 03:34:34 +00:00
# - ``PUPPETFILE_DIR`` must be set to Puppet modules directory
2015-10-05 16:39:26 -04:00
# - ``SCRIPT_DIR`` must be set to script path
# - ``ZUUL_BRANCH`` must be set to Zuul branch
install_modules() {
2019-06-26 12:52:35 +02:00
if [ -d /home/zuul/src/opendev.org ] ; then
2020-02-26 09:53:23 +01:00
csplit ${SCRIPT_DIR}/Puppetfile /'External modules'/ \
2015-10-05 16:39:26 -04:00
--prefix ${SCRIPT_DIR}/Puppetfile \
--suffix '%d'
install_external
install_openstack
else
install_all
fi
}
2015-11-05 12:02:15 -05:00
2018-11-20 01:23:37 +01:00
# This is only executed from install_modules_unit.sh because we have
# some modules that is only required for puppet6 unit testing.
# Uses the following variables:
#
# - ``PUPPETFILE_DIR`` must be set to Puppet modules directory
# - ``SCRIPT_DIR`` must be set to script path
# - ``ZUUL_BRANCH`` must be set to Zuul branch
install_modules_unit() {
2019-06-26 12:52:35 +02:00
if [ -d /home/zuul/src/opendev.org ] ; then
2020-02-26 09:53:23 +01:00
csplit ${SCRIPT_DIR}/Puppetfile /'External modules'/ \
2018-11-20 01:23:37 +01:00
--prefix ${SCRIPT_DIR}/Puppetfile \
--suffix '%d'
cat ${SCRIPT_DIR}/Puppetfile_unit >> ${SCRIPT_DIR}/Puppetfile1
install_external
install_openstack
else
cat ${SCRIPT_DIR}/Puppetfile_unit >> ${SCRIPT_DIR}/Puppetfile
install_all
fi
}
2016-07-13 13:50:17 -06:00
# Write out basic hiera configuration
#
# Uses the following variables:
# - ``SCRIPT_DIR`` must be set to the dir that contains a /hiera folder to use
# - ``HIERA_CONFIG`` must be set to the hiera config file location
#
configure_hiera() {
cat <<EOF >$HIERA_CONFIG
---
2019-01-15 14:49:57 +01:00
version: 5
defaults:
datadir: ${SCRIPT_DIR}/hiera
data_hash: yaml_data
hierarchy:
- name: "OS specific"
2023-04-28 16:20:05 +09:00
path: "%{facts.os.name}.yaml"
2019-01-15 14:49:57 +01:00
- name: "OS family specific"
2023-04-28 16:20:05 +09:00
path: "%{facts.os.family}.yaml"
2019-01-15 14:49:57 +01:00
- name: "Common"
path: "common.yaml"
2016-07-13 13:50:17 -06:00
EOF
}
2015-11-05 12:02:15 -05:00
is_fedora() {
if [ -f /etc/os-release ]; then
source /etc/os-release
test "$ID" = "fedora" -o "$ID" = "centos"
else
return 1
fi
}
uses_debs() {
# check if apt-get is installed, valid for debian based
type "apt-get" 2>/dev/null
}
2018-06-13 19:07:02 +05:30
if type "dnf" 2>/dev/null;then
export YUM=dnf
else
export YUM=yum
fi
2016-04-14 14:04:57 -06:00
print_header() {
if [ -n "$(set | grep xtrace)" ]; then
set +x
local enable_xtrace='yes'
fi
local msg=$1
printf '%.0s-' {1..80}; echo
printf '| %-76s |\n' "${msg}"
printf '%.0s-' {1..80}; echo
if [ -n "${enable_xtrace}" ]; then
set -x
fi
}
2016-06-08 11:31:00 -04:00
2023-11-09 22:47:10 +09:00
install_puppetlabs_repo() {
print_header 'Install Puppetlabs repo'
2016-06-08 11:31:00 -04:00
if uses_debs; then
2023-11-09 22:47:10 +09:00
PUPPET_CODENAME=$(lsb_release -s -c)
$SUDO mkdir -p /etc/apt/sources.list.d
echo "deb ${NODEPOOL_PUPPETLABS_MIRROR} ${PUPPET_CODENAME} puppet${PUPPET_MAJ_VERSION}" | $SUDO tee /etc/apt/sources.list.d/puppetlabs.list
$SUDO apt-key add files/GPG-KEY-puppetlabs
$SUDO apt-key add files/GPG-KEY-ceph
$SUDO apt-get update
2016-06-08 11:31:00 -04:00
elif is_fedora; then
2021-05-27 17:34:06 +02:00
source /etc/os-release
2023-11-09 22:47:10 +09:00
$SUDO rpm --import ${NODEPOOL_PUPPETLABS_MIRROR}/RPM-GPG-KEY-puppetlabs
$SUDO rpm --import ${NODEPOOL_PUPPETLABS_MIRROR}/RPM-GPG-KEY-puppet
$SUDO rpm --import ${NODEPOOL_PUPPETLABS_MIRROR}/RPM-GPG-KEY-puppet-20250406
$SUDO bash -c "cat << EOF > /etc/yum.repos.d/puppetlabs.repo
2018-01-24 23:01:06 -08:00
[puppetlabs-products]
2020-12-11 15:55:04 +05:30
name=Puppet Labs Products El ${VERSION_ID} - x86_64
baseurl=${NODEPOOL_PUPPETLABS_MIRROR}/puppet${PUPPET_MAJ_VERSION}/el/${VERSION_ID}/x86_64/
2021-01-23 21:48:49 +09:00
gpgkey=${NODEPOOL_PUPPETLABS_MIRROR}/RPM-GPG-KEY-puppetlabs
${NODEPOOL_PUPPETLABS_MIRROR}/RPM-GPG-KEY-puppet
${NODEPOOL_PUPPETLABS_MIRROR}/RPM-GPG-KEY-puppet-20250406
2018-01-24 23:01:06 -08:00
enabled=1
gpgcheck=1
EOF"
2023-11-09 22:47:10 +09:00
fi
}
install_puppet() {
print_header 'Install Puppet'
if uses_debs; then
$SUDO apt-get install -y ${PUPPET_PKG}
DISTRIBUTION_VENDOR=$(lsb_release -s -i)
if [ ${DISTRIBUTION_VENDOR} = 'Debian' ]; then
if [ "${USE_PUPPETLABS}" != 'true' ] && [ "${PUPPET_PKG}" = 'puppet' ]; then
# NOTE(tkajinam): puppet pacakge in Debian is separated to
# sub packages.
$SUDO apt-get install -y \
puppet-module-puppetlabs-augeas-core \
puppet-module-puppetlabs-cron-core \
puppet-module-puppetlabs-mount-core \
puppet-module-puppetlabs-sshkeys-core
fi
2017-05-30 11:48:50 +02:00
fi
2023-11-09 22:47:10 +09:00
elif is_fedora; then
2018-06-13 19:07:02 +05:30
$SUDO $YUM install -y ${PUPPET_PKG}
2016-06-08 11:31:00 -04:00
fi
}
2016-06-08 11:44:10 -04:00
function run_puppet() {
local manifest=$1
$SUDO $PUPPET_FULL_PATH apply $PUPPET_ARGS fixtures/${manifest}.pp
local res=$?
return $res
}
function catch_selinux_alerts() {
if is_fedora; then
2021-02-04 20:58:26 +05:30
sealert_cmd="$SUDO sealert -a /var/log/audit/audit.log"
retry_cmd "$sealert_cmd"
2016-06-08 11:44:10 -04:00
if $SUDO grep -iq 'type=AVC' /var/log/audit/audit.log; then
echo "AVC detected in /var/log/audit/audit.log"
2019-12-10 11:22:06 +05:30
source /etc/os-release
2016-06-08 11:44:10 -04:00
# TODO: figure why latest rabbitmq deployed with SSL tries to write in SSL pem file.
# https://bugzilla.redhat.com/show_bug.cgi?id=1341738
if $SUDO grep -iqE 'denied.*system_r:rabbitmq_t' /var/log/audit/audit.log; then
echo "non-critical RabbitMQ AVC, ignoring it now."
else
echo "Please file a bug on https://bugzilla.redhat.com/enter_bug.cgi?product=Red%20Hat%20OpenStack&component=openstack-selinux showing sealert output."
exit 1
fi
else
echo 'No AVC detected in /var/log/audit/audit.log'
fi
fi
}
2016-10-31 15:15:34 -06:00
function timestamp_puppet_log() {
2016-12-02 10:45:17 -07:00
$SUDO mv ${WORKSPACE}/puppet.log ${WORKSPACE}/puppet-$(date +%Y%m%d_%H%M%S).log
2016-10-31 15:15:34 -06:00
}
function catch_puppet_failures() {
2018-03-19 17:44:12 +08:00
$SUDO grep -wiE '(Error|\(err\))' ${WORKSPACE}/puppet.log
2016-10-31 15:15:34 -06:00
}