#!/bin/sh
# SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
# SPDX-FileCopyrightText: (C) 2025 Serena Willis
#
# SPDX-License-Identifier: AGPL-3.0
#
# BuildMan 0.0.1
# Distributed CI/CD system with portability to older UNIX systems
#
# bma - buildman agent
#
# Copyright (C) 2025 Coherent Logic Development LLC
#
# Author: Serena Willis <snw@coherent-logic.com>
#
PROGN=`basename $0`
if [ -f "/etc/default/bm" ]
then
. /etc/default/bm
else
echo "${PROGN} error: no /etc/default/bm"
exit 1
fi
if [ -z ${BUILDMAN_BASE} ]
then
echo "${PROGN} error: BUILDMAN_BASE undefined"
exit 1
fi
if [ -z ${BUILDMAN_UID} ]
then
echo "${PROGN} error: BUILDMAN_UID undefined"
exit 1
fi
if [ -z ${BUILDMAN_GID} ]
then
echo "${PROGN} error: BUILDMAN_GID undefined"
exit 1
fi
#if [ $EUID -ne ${BUILDMAN_UID} ]
#then
# echo "${PROGN} error: must be run as user \"${BUILDMAN_USER}\""
# exit 1
#fi
OS=`uname`
DT=`date +"%Y%m%d"`
if [ "$OS" = "SCO_SV" ] ; then
THOST=`hostname -s`
PATH=/usr/gnu/bin:$PATH
export PATH
else
if [ "$OS" = "SunOS" ] ; then
THOST=`hostname`
else
THOST=`hostname -s`
fi
fi
tmphost=`echo ${THOST} | cut -d. -f1`
THOST="${tmphost}"
cd "${BUILDMAN_BASE}/projects"
PROJECTS=`ls -1 -d *`
for PROJECT in $PROJECTS
do
HOSTDIR="${BUILDMAN_BASE}/projects/${PROJECT}/hosts/${THOST}"
BUILD_WANTED="${HOSTDIR}/build-wanted"
if [ -d "${HOSTDIR}" ]
then
echo "$PROGN: project ${PROJECT} references build host ${THOST}"
if [ -f "${BUILD_WANTED}" ]
then
echo "$PROGN: project ${PROJECT} has a build request pending"
rm -f "${BUILD_WANTED}"
DT=`date +"%Y%m%d%H%M%S"`
JOBID="$$-${DT}"
echo "$PROGN: creating job ${JOBID}"
JOBDIR="${HOSTDIR}/jobs/${JOBID}"
JLOG="${JOBDIR}/job.log"
JSCR="${BUILDMAN_BASE}/projects/${PROJECT}/scripts/build.sh"
PRESCR="${HOSTDIR}/prebuild.sh"
POSTSCR="${HOSTDIR}/postbuild.sh"
mkdir -p "${JOBDIR}"
cd "${JOBDIR}"
echo "Coherent Logic Development BuildMan" >> "${JLOG}"
echo "Build agent is running job ID ${JOBID} for project ${PROJECT} on node ${THOST}" >> "${JLOG}"
echo "${JOBID}" > "${HOSTDIR}/build-running"
echo "${JOBID}" > "${JOBDIR}/running"
if [ -f "${PRESCR}" ]
then
echo "$PROGN: running host-specific prebuild script ${PRESCR} for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
echo "$PROGN: running host-specific prebuild script ${PRESCR} for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
sh "${PRESCR}" >> ${JLOG}
RETCODE=$?
if [ $RETCODE -gt 0 ]
then
echo "$PROGN: host-specific prebuild script failed for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
echo "$PROGN: host-specific prebuild script failed for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
rm -f "${HOSTDIR}/build-running"
rm -f "${JOBDIR}/running"
rm -f "${HOSTDIR}/build-failing"
rm -f "${HOSTDIR}/build-passing"
echo "${JOBID}" > "${HOSTDIR}/build-failing"
echo "${JOBID}" > "${JOBDIR}/failing"
exit 1
fi
else
echo "$PROGN: no host-specific prebuild script exists for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
echo "$PROGN: no host-specific prebuild script exists for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
fi
if [ -f "${JSCR}" ]
then
echo "$PROGN: running build script ${JSCR} for project ${PROJECT} [job id ${JOBID}]"
echo "$PROGN: running build script ${JSCR} for project ${PROJECT} [job id ${JOBID}]" >> ${JLOG}
sh "${JSCR}" >> ${JLOG}
RETCODE=$?
rm -f "${HOSTDIR}/build-running"
rm -f "${JOBDIR}/running"
rm -f "${HOSTDIR}/build-failing"
rm -f "${HOSTDIR}/build-passing"
case $RETCODE in
0)
echo "${JOBID}" > "${HOSTDIR}/build-passing"
echo "${JOBID}" > "${JOBDIR}/passing"
;;
*)
echo "${JOBID}" > "${HOSTDIR}/build-failing"
echo "${JOBID}" > "${JOBDIR}/failing"
exit 1
;;
esac
else
echo "$PROGN: build script ${JSCR} missing for project ${PROJECT} [job id ${JOBID}]"
echo "$PROGN: build script ${JSCR} missing for project ${PROJECT} [job id ${JOBID}]" > "${HOSTDIR}/build-failing"
echo "$PROGN: build script ${JSCR} missing for project ${PROJECT} [job id ${JOBID}]" > "${JOBDIR}/failing"
echo "$PROGN: build script ${JSCR} missing for project ${PROJECT} [job id ${JOBID}]" >> "${JLOG}"
rm -f "${HOSTDIR}/build-running"
rm -f "${JOBDIR}/running"
fi
if [ -f "${POSTSCR}" ]
then
echo "$PROGN: running host-specific postbuild script ${POSTSCR} for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
echo "$PROGN: running host-specific postbuild script ${POSTSCR} for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
sh "${POSTSCR}" >> ${JLOG}
RETCODE=$?
if [ $RETCODE -gt 0 ]
then
echo "$PROGN: host-specific postbuild script failed for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
echo "$PROGN: host-specific postbuild script failed for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
rm -f "${HOSTDIR}/build-running"
rm -f "${JOBDIR}/running"
rm -f "${HOSTDIR}/build-failing"
rm -f "${HOSTDIR}/build-passing"
echo "${JOBID}" > "${HOSTDIR}/build-failing"
echo "${JOBID}" > "${JOBDIR}/failing"
exit 1
fi
else
echo "$PROGN: no host-specific postbuild script exists for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
echo "$PROGN: no host-specific postbuild script exists for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
fi
fi
fi
done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>