root/build-tools/modify-build-cfg

206 lines
7.5 KiB
Bash
Executable File

#!/bin/sh
#
# Copyright (c) 2018-2020 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
# This script modifies a mock configuration file (typically $MY_BUILD_CFG)
# to add build time environment variables to the mock environment (things
# like what branch we're building on, etc).
#
# For reasons of security, the host environment variables cannot normally be
# passed through to the mock environment, so this scripts sets the variables
# to literal values.
#
# usage: modify-build-cfg [file.cfg] [<layer>]
#
MODIFY_BUILD_CFG_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )"
# Set PKG_MANAGER for our build environment.
source "${MODIFY_BUILD_CFG_DIR}/pkg-manager-utils.sh"
LAYER=${2:-$LAYER}
# For backward compatibility. Old repo location or new?
CENTOS_REPO=${MY_REPO}/centos-repo
if [ ! -d ${CENTOS_REPO} ]; then
CENTOS_REPO=${MY_REPO}/cgcs-centos-repo
if [ ! -d ${CENTOS_REPO} ]; then
echo "ERROR: directory ${MY_REPO}/centos-repo not found."
exit 1
fi
fi
# Preferred python
if rpm -q --whatprovides --quiet python3; then
PYTHON_PKG=python3
else
PYTHON_PKG=python2
fi
# Try to find a layer specific mock.cfg.proto
MOCK_CFG_PROTO="${CENTOS_REPO}/mock.cfg.${LAYER}.proto"
echo "==== Try MOCK_CFG_PROTO=$MOCK_CFG_PROTO ===="
if [ ! -f "$MOCK_CFG_PROTO" ]; then
# Not present, Use default mock.cfg.proto
MOCK_CFG_PROTO="${CENTOS_REPO}/mock.cfg.proto"
fi
echo "==== Use MOCK_CFG_PROTO=$MOCK_CFG_PROTO ===="
if [ ! -f "$MOCK_CFG_PROTO" ]; then
echo "ERROR: Couldn't find mock config prototype at '$MOCK_CFG_PROTO'"
exit 1
fi
if [ "${1}x" == "x" ]; then
FILE=$MY_BUILD_CFG
else
FILE=$1
fi
if [ -f $MOCK_CFG_PROTO ]; then
if [ -f $FILE ]; then
NEWER=$(find "$MOCK_CFG_PROTO" -newer "$FILE")
if [ "x$NEWER" != "x" ]; then
\rm -f -v "$FILE"
fi
fi
fi
if [ ! -f $FILE ]; then
if [ -z $MY_BUILD_ENVIRONMENT ] || [ -z $MY_BUILD_DIR ] || [ -z $MY_REPO ]; then
echo "Can't create $FILE without MY_BUILD_ENVIRONMENT, MY_BUILD_DIR and MY_REPO environment variables"
exit 1
fi
echo "Recreating $FILE"
\cp -f -v "$MOCK_CFG_PROTO" "$FILE"
if [ $? -ne 0 ]; then
echo "Couldn't find config file '$FILE', nor construct it from '$MOCK_CFG_PROTO'"
exit 1
fi
# eg: LOCAL_BASE/MY_BUILD_DIR => http://127.0.0.1:8088/MY_BUILD_DIR
sed -i "s%LOCAL_BASE%http://127.0.0.1:8088%g" "$FILE"
sed -i "s%MIRROR_BASE%http://127.0.0.1:8088%g" "$FILE"
sed -i "s%BUILD_ENV%$MY_BUILD_ENVIRONMENT%g" "$FILE"
# eg http://127.0.0.1:8088/MY_BUILD_DIR => http://12.0.0.1:8088/localdisk/loadbuild/...
sed -i "s%/MY_BUILD_DIR%$MY_BUILD_DIR_TOP%g" "$FILE"
sed -i "s%/MY_REPO_DIR%$MY_REPO%g" "$FILE"
# eg = MY_BUILD_DIR/xyz => /localdisk/loadbuild/.../xyz
sed -i "s%MY_BUILD_DIR%$MY_BUILD_DIR_TOP%g" "$FILE"
sed -i "s%MY_REPO_DIR%$MY_REPO%g" "$FILE"
# Disable all local-* repos for the build-types other than the current one
for bt in std rt; do
if [ "$bt" != "$BUILD_TYPE" ]; then
# Use the range of lines starting with pattern [local-$bt] until the next line starting with []
sed -i "/^\[local-$bt\]/,/^\[/ s/enabled=1/enabled=0/" $FILE
sed -i "/^\[StxCentos7Distro-$bt\]/,/^\[/ s/enabled=1/enabled=0/" $FILE
fi
done
fi
# Add environment variables to mock config if they don't exist
grep -q "config_opts\['environment'\]\['BUILD_BY'\]" $FILE || \
echo "config_opts['environment']['BUILD_BY']" >> $FILE
grep -q "config_opts\['environment'\]\['BUILD_DATE'\]" $FILE || \
echo "config_opts['environment']['BUILD_DATE']" >> $FILE
grep -q "config_opts\['environment'\]\['REPO'\]" $FILE || \
echo "config_opts['environment']['REPO']" >> $FILE
grep -q "config_opts\['environment'\]\['WRS_GIT_BRANCH'\]" $FILE || \
echo "config_opts['environment']['WRS_GIT_BRANCH']" >> $FILE
grep -q "config_opts\['environment'\]\['CGCS_GIT_BRANCH'\]" $FILE || \
echo "config_opts['environment']['CGCS_GIT_BRANCH']" >> $FILE
if [ -z $FORMAL_BUILD ]; then
grep -q "config_opts\['macros'\]\['%_no_cgcs_license_check'\] = '1'" $FILE || \
echo "config_opts['macros']['%_no_cgcs_license_check'] = '1'" >> $FILE
else
sed -i "/config_opts\['macros'\]\['%_no_cgcs_license_check'\] = '1'/d" $FILE
fi
grep -q "config_opts\['macros'\]\['%_tis_build_type'\] = '$BUILD_TYPE'" $FILE || \
echo "config_opts['macros']['%_tis_build_type'] = '$BUILD_TYPE'" >> $FILE
if [ -f /usr/lib64/nosync/nosync.so ]; then
grep -q "config_opts\['nosync'\] = True" $FILE || \
echo "config_opts['nosync'] = True" >> $FILE
fi
NETWORK_PKGS=""
if [ "containers" == "$BUILD_TYPE" ]; then
NETWORK_PKGS="bind-utils"
fi
BUILD_PKGS=''
if [ "${PKG_MANAGER}" == "yum" ]; then
BUILD_PKGS='@buildsys-build'
elif [ "${PKG_MANAGER}" == "dnf" ]; then
# buildsys-build group was dropped when Centos-8 switched to dnf.
# We must list all the members plus a few new ones (fedpkg-minimal, epel-rpm-macros).
BUILD_PKGS='bash bzip2 coreutils cpio diffutils epel-release epel-rpm-macros fedpkg-minimal findutils gawk gcc gcc-c++ grep gzip info make patch redhat-rpm-config redhat-release rpm-build sed shadow-utils tar unzip util-linux which xz'
fi
STX_PKGS='pigz lbzip2 bash'
PKGS="${BUILD_PKGS} ${STX_PKGS} ${PKG_MANAGER} ${PYTHON_PKG} ${NETWORK_PKGS}"
grep -q "config_opts\['chroot_setup_cmd'\] = 'install ${PKGS}'" $FILE || \
echo "config_opts['chroot_setup_cmd'] = 'install ${PKGS}'" >> $FILE
# Special case for containers.
# rpmbuild_networking is required for invoking helm commands within mock
# building containers requires the std repo to be enabled.
if [ "containers" == "$BUILD_TYPE" ]; then
grep -q "config_opts\['rpmbuild_networking'\] = True" $FILE || \
echo "config_opts['rpmbuild_networking'] = True" >> $FILE
grep -q "config_opts\['use_host_resolv'\] = True" $FILE || \
echo "config_opts['use_host_resolv'] = True" >> $FILE
sed -i "/^\[local-std\]/,/^\[/ s/enabled=0/enabled=1/" $FILE
fi
#
# Read macros from tis.macros to add to the build config file,
# for use in RPM spec files
#
RPM_MACROS=$MY_REPO/build-tools/tis.macros
sed 's/#.*//' $RPM_MACROS | grep '=' | while IFS='=' read name value; do
# Check if the entry already exists. If so, go to next line
grep -q "^config_opts\['macros'\]\['${name}'\] = '${value}'$" $FILE && continue
# Update or add the entry
grep -q "^config_opts\['macros'\]\['${name}'\]" $FILE
if [ $? -eq 0 ]; then
sed -i -r "s#^(config_opts\['macros'\]\['${name}'\]).*#\1 = '${value}'#" $FILE
else
echo "config_opts['macros']['${name}'] = '${value}'" >> $FILE
fi
done
# okay, now we have lines for each env var. Generate the correct values
BUILD_DATE=`date "+%F %T %z"`
CGCS_GIT_BRANCH=`cd $MY_REPO/stx/; git rev-parse --abbrev-ref HEAD`
WRS_GIT_BRANCH=`cd $MY_REPO; git rev-parse --abbrev-ref HEAD`
REPO=$MY_REPO
# Finally, our good friend sed will place the values in the mock config file
sed -i \
-e "s#config_opts\['environment'\]\['BUILD_BY'\].*#config_opts\['environment'\]\['BUILD_BY'\] = '$USER'#" \
-e "s#config_opts\['environment'\]\['BUILD_DATE'\].*#config_opts\['environment'\]\['BUILD_DATE'\] = '$BUILD_DATE'#" \
-e "s#config_opts\['environment'\]\['REPO'\].*#config_opts\['environment'\]\['REPO'\] = '$REPO'#" \
-e "s#config_opts\['environment'\]\['WRS_GIT_BRANCH'\].*#config_opts\['environment'\]\['WRS_GIT_BRANCH'\] = '$WRS_GIT_BRANCH'#" \
-e "s#config_opts\['environment'\]\['CGCS_GIT_BRANCH'\].*#config_opts\['environment'\]\['CGCS_GIT_BRANCH'\] = '$CGCS_GIT_BRANCH'#" \
$FILE