catalyst-auto: move all code into functions

Mixing inline funcs and executable code makes it hard to follow and
shuffle ordering of operations.  Put everything other than variables
into functions.

The delta here is large, but it's almost entirely indentation changes.

This also makes updating the script (like `git pull`) more resilient.
This commit is contained in:
Mike Frysinger 2018-01-15 20:11:44 -05:00
parent b5e4453a0e
commit b291ea4754

View file

@ -19,6 +19,17 @@ unset UNSHARE
CATALYST_CONFIG=/etc/catalyst/catalyst.conf CATALYST_CONFIG=/etc/catalyst/catalyst.conf
# Probe the default source dir from this script name.
REPO_DIR=$(dirname "$(dirname "$(realpath "$0")")")
# Set up defaults that config files can override if they want.
SUBARCH=$(uname -m)
EMAIL_TO="releng@gentoo.org,gentoo-releng-autobuilds@lists.gentoo.org"
# Use full hostname by default as Gentoo servers will reject short names.
EMAIL_FROM="catalyst@$(hostname -f)"
EMAIL_SUBJECT_PREPEND="[${SUBARCH}-auto]"
# Variables updated by command line arguments.
declare -a config_files declare -a config_files
config_files=() config_files=()
verbose=0 verbose=0
@ -28,9 +39,6 @@ preclean=0
lastrun=0 lastrun=0
lock_file= lock_file=
# Set pipefail so that run_cmd returns the right value in $?
set -o pipefail
usage() { usage() {
local msg=$1 local msg=$1
@ -125,10 +133,10 @@ trigger_post_build() {
fi fi
} }
# Parse args parse_args() {
while [ ${#} -gt 0 ] local a
do while [[ $# -gt 0 ]] ; do
a=${1} a=$1
shift shift
case "${a}" in case "${a}" in
-h|--help) -h|--help)
@ -168,85 +176,68 @@ do
exit 1 exit 1
;; ;;
esac esac
done done
}
( run_catalyst_commands() {
doneconfig=0
if [[ -n ${lock_file} ]]; then for config_file in "${config_files[@]}"; do
if ! flock -n 9; then # Make sure all required values were specified.
echo "catalyst-auto already running" if [[ -z "${config_file}" || ! -e "${config_file}" ]]; then
exit 1
fi
fi
# Probe the default source dir from this script name.
REPO_DIR=$(dirname "$(dirname "$(realpath "$0")")")
# Set up defaults that config files can override if they want.
SUBARCH=$(uname -m)
EMAIL_TO="releng@gentoo.org,gentoo-releng-autobuilds@lists.gentoo.org"
# Use full hostname by default as Gentoo servers will reject short names.
EMAIL_FROM="catalyst@$(hostname -f)"
EMAIL_SUBJECT_PREPEND="[${SUBARCH}-auto]"
doneconfig=0
for config_file in "${config_files[@]}"; do
# Make sure all required values were specified
if [ -z "${config_file}" -o ! -e "${config_file}" ]; then
usage "ERROR: You must specify a valid config file to use: '$config_file' is not valid" usage "ERROR: You must specify a valid config file to use: '$config_file' is not valid"
exit 1 exit 1
fi fi
source "${config_file}" source "${config_file}"
doneconfig=1 doneconfig=1
done done
if [[ $doneconfig -eq 0 ]]; then if [[ ${doneconfig} == 0 ]]; then
usage "ERROR: You must specify at least one valid config file to use" usage "ERROR: You must specify at least one valid config file to use"
exit 1 exit 1
fi fi
# Some configs will set this explicitly, so don't clobber it. # Some configs will set this explicitly, so don't clobber it.
: ${BUILD_SRCDIR_BASE:=$(catalyst_var storedir)} : ${BUILD_SRCDIR_BASE:=$(catalyst_var storedir)}
# See if we had a recent success. # See if we had a recent success.
if [[ ${lastrun} -ne 0 ]]; then if [[ ${lastrun} != 0 ]]; then
last_success_file="${BUILD_SRCDIR_BASE}/.last_success" last_success_file="${BUILD_SRCDIR_BASE}/.last_success"
delay=$(( lastrun * 24 * 60 * 60 )) delay=$(( lastrun * 24 * 60 * 60 ))
last_success=$(head -1 "${last_success_file}" 2>/dev/null || echo 0) last_success=$(head -1 "${last_success_file}" 2>/dev/null || echo 0)
if [[ $(date +%s) -lt $(( last_success + delay )) ]]; then if [[ $(date +%s) -lt $(( last_success + delay )) ]]; then
exit 0 exit 0
fi fi
fi fi
DATESTAMP=$(date -u +%Y%m%d) DATESTAMP=$(date -u +%Y%m%d)
TIMESTAMP=$(date -u +%Y%m%dT%H%M%SZ) TIMESTAMP=$(date -u +%Y%m%dT%H%M%SZ)
TMPDIR=$(mktemp -d --tmpdir="${TMP_PATH:-/tmp}" "catalyst-auto.${TIMESTAMP}.XXXXXX") TMPDIR=$(mktemp -d --tmpdir="${TMP_PATH:-/tmp}" "catalyst-auto.${TIMESTAMP}.XXXXXX")
# Nuke any previous tmpdirs to keep them from accumulating. # Nuke any previous tmpdirs to keep them from accumulating.
if [[ ${preclean} -eq 1 ]]; then if [[ ${preclean} == 1 ]]; then
rm -rf "${TMPDIR%.??????}".* rm -rf "${TMPDIR%.??????}".*
mkdir "${TMPDIR}" mkdir "${TMPDIR}"
fi fi
if [ ${verbose} -ge 1 ]; then if [[ ${verbose} -ge 1 ]]; then
echo "TMPDIR = ${TMPDIR}" echo "TMPDIR = ${TMPDIR}"
echo "DATESTAMP = ${DATESTAMP}" echo "DATESTAMP = ${DATESTAMP}"
echo "TIMESTAMP = ${TIMESTAMP}" echo "TIMESTAMP = ${TIMESTAMP}"
fi fi
if ! mkdir -p "${TMPDIR}"/{specs,kconfig,log}; then if ! mkdir -p "${TMPDIR}"/{specs,kconfig,log}; then
echo "Couldn't create tempdirs!" echo "Couldn't create tempdirs!"
exit 1 exit 1
fi fi
if ! run_cmd "${TMPDIR}/log/pre_build.log" pre_build; then if ! run_cmd "${TMPDIR}/log/pre_build.log" pre_build; then
send_email "Catalyst build error - pre_build" "The pre_build function failed" "${TMPDIR}/log/pre_build.log" send_email "Catalyst build error - pre_build" "The pre_build function failed" "${TMPDIR}/log/pre_build.log"
exit 1 exit 1
fi fi
cd "${SPECS_DIR}" || exit 1 cd "${SPECS_DIR}" || exit 1
for a in "" ${SETS}; do for a in "" ${SETS}; do
if [ -z "${a}" ]; then if [[ -z "${a}" ]]; then
specs_var="SPECS" specs_var="SPECS"
optional_specs_var="OPTIONAL_SPECS" optional_specs_var="OPTIONAL_SPECS"
else else
@ -257,14 +248,14 @@ for a in "" ${SETS}; do
for i in ${!specs_var} ${!optional_specs_var}; do for i in ${!specs_var} ${!optional_specs_var}; do
cp --parents "${i}" "${TMPDIR}"/specs/ cp --parents "${i}" "${TMPDIR}"/specs/
done done
done done
find "${KCONFIG_DIR}" -type f -exec cp {} "${TMPDIR}"/kconfig \; find "${KCONFIG_DIR}" -type f -exec cp {} "${TMPDIR}"/kconfig \;
cd "${TMPDIR}/specs" || exit 1 cd "${TMPDIR}/specs" || exit 1
# Fix up specs with datestamp # Fix up specs with datestamp
for i in $(find -name '*.spec'); do for i in $(find -name '*.spec'); do
# Grab current version_stamp and source_subpath # Grab current version_stamp and source_subpath
old_version_stamp=$(grep version_stamp "${i}" | sed -e 's|^version_stamp: *||') old_version_stamp=$(grep version_stamp "${i}" | sed -e 's|^version_stamp: *||')
old_source_subpath=$(grep source_subpath "${i}" | sed -e 's|^source_subpath: *||') old_source_subpath=$(grep source_subpath "${i}" | sed -e 's|^source_subpath: *||')
@ -284,7 +275,7 @@ for i in $(find -name '*.spec'); do
sed -i "/^livecd\/volid/s|${old_version_stamp}|${new_version_stamp}|" "${i}" sed -i "/^livecd\/volid/s|${old_version_stamp}|${new_version_stamp}|" "${i}"
kconfig_lines=$(grep '^boot/kernel/[^/]\+/config:' "${i}") kconfig_lines=$(grep '^boot/kernel/[^/]\+/config:' "${i}")
if [ -n "${kconfig_lines}" ]; then if [[ -n ${kconfig_lines} ]]; then
echo "${kconfig_lines}" | while read line; do echo "${kconfig_lines}" | while read line; do
key=$(echo "${line}" | cut -d: -f1) key=$(echo "${line}" | cut -d: -f1)
filename=$(basename $(echo "${line}" | cut -d: -f2)) filename=$(basename $(echo "${line}" | cut -d: -f2))
@ -298,14 +289,14 @@ for i in $(find -name '*.spec'); do
-e "s:@TIMESTAMP@:${TIMESTAMP}:g" \ -e "s:@TIMESTAMP@:${TIMESTAMP}:g" \
-e "s:@REPO_DIR@:${REPO_DIR}:g" \ -e "s:@REPO_DIR@:${REPO_DIR}:g" \
"${i}" "${i}"
done done
if [ "${testing}" -eq 1 ]; then if [[ ${testing} == 1 ]]; then
echo "Exiting due to --test" echo "Exiting due to --test"
exit exit
fi fi
if [[ ${preclean} -eq 1 ]]; then if [[ ${preclean} == 1 ]]; then
snapshot_cache=$(catalyst_var snapshot_cache) snapshot_cache=$(catalyst_var snapshot_cache)
if [[ -z ${snapshot_cache} ]]; then if [[ -z ${snapshot_cache} ]]; then
echo "error: snapshot_cache not set in config file" echo "error: snapshot_cache not set in config file"
@ -315,21 +306,21 @@ if [[ ${preclean} -eq 1 ]]; then
rm -rf --one-file-system \ rm -rf --one-file-system \
kerncache packages snapshots tmp "${snapshot_cache}"/* kerncache packages snapshots tmp "${snapshot_cache}"/*
popd >/dev/null popd >/dev/null
fi fi
# Create snapshot # Create snapshot
if ! run_cmd "${TMPDIR}/log/snapshot.log" catalyst -c "${CATALYST_CONFIG}" -s "${TIMESTAMP}"; then if ! run_cmd "${TMPDIR}/log/snapshot.log" catalyst -c "${CATALYST_CONFIG}" -s "${TIMESTAMP}"; then
send_email "Catalyst build error - snapshot" "" "${TMPDIR}/log/snapshot.log" send_email "Catalyst build error - snapshot" "" "${TMPDIR}/log/snapshot.log"
exit 1 exit 1
fi fi
build_failure=0 build_failure=0
timeprefix=() timeprefix=()
which time >/dev/null && timeprefix=( "time" ) which time >/dev/null && timeprefix=( "time" )
for a in "" ${SETS}; do for a in "" ${SETS}; do
if [ -z "${a}" ]; then if [[ -z ${a} ]]; then
specs_var="SPECS" specs_var="SPECS"
optional_specs_var="OPTIONAL_SPECS" optional_specs_var="OPTIONAL_SPECS"
else else
@ -340,7 +331,7 @@ for a in "" ${SETS}; do
for i in ${!specs_var}; do for i in ${!specs_var}; do
LOGFILE="${TMPDIR}/log/$(echo "${i}" | sed -e 's:/:_:' -e 's:\.spec$::').log" LOGFILE="${TMPDIR}/log/$(echo "${i}" | sed -e 's:/:_:' -e 's:\.spec$::').log"
run_cmd "${LOGFILE}" "${timeprefix[@]}" catalyst -a -c "${CATALYST_CONFIG}" -f "${i}" run_cmd "${LOGFILE}" "${timeprefix[@]}" catalyst -a -c "${CATALYST_CONFIG}" -f "${i}"
if [ $? != 0 ]; then if [[ $? != 0 ]]; then
build_failure=1 build_failure=1
send_email "Catalyst fatal build error - ${i}" "" "${LOGFILE}" send_email "Catalyst fatal build error - ${i}" "" "${LOGFILE}"
continue 2 continue 2
@ -352,7 +343,7 @@ for a in "" ${SETS}; do
for i in ${!optional_specs_var}; do for i in ${!optional_specs_var}; do
LOGFILE="${TMPDIR}/log/$(echo "${i}" | sed -e 's:/:_:' -e 's:\.spec$::').log" LOGFILE="${TMPDIR}/log/$(echo "${i}" | sed -e 's:/:_:' -e 's:\.spec$::').log"
run_cmd "${LOGFILE}" "${timeprefix[@]}" catalyst -a -c "${CATALYST_CONFIG}" -f "${i}" run_cmd "${LOGFILE}" "${timeprefix[@]}" catalyst -a -c "${CATALYST_CONFIG}" -f "${i}"
if [ $? != 0 ]; then if [[ $? != 0 ]]; then
build_failure=1 build_failure=1
send_email "Catalyst non-fatal build error - ${i}" "" "${LOGFILE}" send_email "Catalyst non-fatal build error - ${i}" "" "${LOGFILE}"
break break
@ -367,27 +358,46 @@ for a in "" ${SETS}; do
done done
update_symlinks update_symlinks
done done
trigger_post_build trigger_post_build
if [ ${build_failure} = 0 ]; then if [[ ${build_failure} == 0 ]]; then
if [[ ${lastrun} -ne 0 ]]; then if [[ ${lastrun} != 0 ]]; then
stamp=$(date) stamp=$(date)
(date -d"${stamp}" +%s; echo "${stamp}") >"${last_success_file}" (date -d"${stamp}" +%s; echo "${stamp}") >"${last_success_file}"
fi fi
send_email "Catalyst build success" "Build process complete." send_email "Catalyst build success" "Build process complete."
if [ "${keep_tmpdir}" = 0 ]; then if [[ ${keep_tmpdir} == 0 ]]; then
if ! rm -rf "${TMPDIR}"; then if ! rm -rf "${TMPDIR}"; then
echo "Could not remove tmpdir ${TMPDIR}!" echo "Could not remove tmpdir ${TMPDIR}!"
exit 1 exit 1
fi fi
fi fi
else
else
send_email "Catalyst build complete, but with errors" "Build process has completed, but there were errors. Please consult previous emails to determine the problem." send_email "Catalyst build complete, but with errors" "Build process has completed, but there were errors. Please consult previous emails to determine the problem."
fi fi
}
) 9>"${lock_file:-/dev/null}" main() {
# Set pipefail so that run_cmd returns the right value in $?.
set -o pipefail
# Parse user arguments before we try doing container logic.
parse_args "$@"
(
if [[ -n ${lock_file} ]]; then
if ! flock -n 9; then
echo "catalyst-auto already running"
exit 1
fi
fi
run_catalyst_commands
) 9>"${lock_file:-/dev/null}"
}
main "$@"