File:  [Coherent Logic Development] / buildman / bma
Revision 1.6: download - view: text, annotated - select for diffs
Wed Mar 12 02:31:46 2025 UTC (4 months, 3 weeks ago) by snw
Branches: MAIN
CVS tags: HEAD
Add support for host-specific pre and post-build scripts

    1: #!/bin/sh
    2: 
    3: # SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
    4: # SPDX-FileCopyrightText: (C) 2025 Serena Willis
    5: #
    6: # SPDX-License-Identifier: AGPL-3.0
    7: 
    8: #
    9: # BuildMan 0.0.1
   10: #  Distributed CI/CD system with portability to older UNIX systems
   11: #
   12: #  bma - buildman agent
   13: #
   14: # Copyright (C) 2025 Coherent Logic Development LLC
   15: #
   16: # Author: Serena Willis <snw@coherent-logic.com>
   17: #
   18: 
   19: PROGN=`basename $0`
   20: 
   21: if [ -f "/etc/default/bm" ]
   22: then
   23:     . /etc/default/bm
   24: else
   25:     echo "${PROGN} error:  no /etc/default/bm"
   26:     exit 1
   27: fi
   28: 
   29: if [ -z ${BUILDMAN_BASE} ]
   30: then
   31:     echo "${PROGN} error:  BUILDMAN_BASE undefined"
   32:     exit 1
   33: fi
   34: 
   35: if [ -z ${BUILDMAN_UID} ]
   36: then
   37:     echo "${PROGN} error:  BUILDMAN_UID undefined"
   38:     exit 1
   39: fi
   40: 
   41: if [ -z ${BUILDMAN_GID} ]
   42: then
   43:     echo "${PROGN} error:  BUILDMAN_GID undefined"
   44:     exit 1
   45: fi
   46: 
   47: #if [ $EUID -ne ${BUILDMAN_UID} ]
   48: #then
   49: #    echo "${PROGN} error:  must be run as user \"${BUILDMAN_USER}\""
   50: #    exit 1
   51: #fi
   52: 
   53: OS=`uname`
   54: DT=`date +"%Y%m%d"`
   55: 
   56: if [ "$OS" = "SCO_SV" ] ; then
   57:     THOST=`hostname -s`
   58:     PATH=/usr/gnu/bin:$PATH
   59:     export PATH
   60: else
   61:     if [ "$OS" = "SunOS" ] ; then
   62:         THOST=`hostname`
   63:     else    
   64:         THOST=`hostname -s`
   65:     fi
   66: fi
   67: 
   68: tmphost=`echo ${THOST} | cut -d. -f1`
   69: THOST="${tmphost}"
   70: 
   71: cd "${BUILDMAN_BASE}/projects"
   72: PROJECTS=`ls -1 -d *`
   73: 
   74: for PROJECT in $PROJECTS
   75: do
   76:     HOSTDIR="${BUILDMAN_BASE}/projects/${PROJECT}/hosts/${THOST}"
   77:     BUILD_WANTED="${HOSTDIR}/build-wanted"
   78:     
   79:     if [ -d "${HOSTDIR}" ]
   80:     then
   81:         echo "$PROGN:  project ${PROJECT} references build host ${THOST}"
   82: 
   83:         if [ -f "${BUILD_WANTED}" ]
   84:         then
   85:             echo "$PROGN:  project ${PROJECT} has a build request pending"
   86:             rm -f "${BUILD_WANTED}"
   87: 
   88:             DT=`date +"%Y%m%d%H%M%S"`
   89:             JOBID="$$-${DT}"
   90: 
   91:             echo "$PROGN:  creating job ${JOBID}"
   92:             JOBDIR="${HOSTDIR}/jobs/${JOBID}"
   93:             JLOG="${JOBDIR}/job.log"
   94:             JSCR="${BUILDMAN_BASE}/projects/${PROJECT}/scripts/build.sh"
   95:             PRESCR="${HOSTDIR}/prebuild.sh"
   96:             POSTSCR="${HOSTDIR}/postbuild.sh"
   97:             
   98:             mkdir -p "${JOBDIR}"            
   99:             cd "${JOBDIR}"
  100: 
  101:             echo "Coherent Logic Development BuildMan" >> "${JLOG}"
  102:             echo "Build agent is running job ID ${JOBID} for project ${PROJECT} on node ${THOST}" >> "${JLOG}"
  103:             
  104:             echo "${JOBID}" > "${HOSTDIR}/build-running"
  105:             echo "${JOBID}" > "${JOBDIR}/running"
  106: 
  107:             if [ -f "${PRESCR}" ]
  108:             then
  109:                 echo "$PROGN:  running host-specific prebuild script ${PRESCR} for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
  110:                 echo "$PROGN:  running host-specific prebuild script ${PRESCR} for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
  111:                 sh "${PRESCR}" >> ${JLOG}
  112:                 RETCODE=$?
  113: 
  114:                 if [ $RETCODE -gt 0 ]
  115:                 then
  116:                     echo "$PROGN:  host-specific prebuild script failed for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
  117:                     echo "$PROGN:  host-specific prebuild script failed for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
  118: 
  119:                     rm -f "${HOSTDIR}/build-running"
  120:                     rm -f "${JOBDIR}/running"                   
  121:                     rm -f "${HOSTDIR}/build-failing"
  122:                     rm -f "${HOSTDIR}/build-passing"
  123:                     
  124:                     echo "${JOBID}" > "${HOSTDIR}/build-failing"
  125:                     echo "${JOBID}" > "${JOBDIR}/failing"
  126: 
  127:                     exit 1                    
  128:                 fi
  129:             else
  130:                 echo "$PROGN:  no host-specific prebuild script exists for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
  131:                 echo "$PROGN:  no host-specific prebuild script exists for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
  132:             fi
  133:             
  134:             if [ -f "${JSCR}" ]
  135:             then
  136:                 echo "$PROGN:  running build script ${JSCR} for project ${PROJECT} [job id ${JOBID}]"
  137:                 echo "$PROGN:  running build script ${JSCR} for project ${PROJECT} [job id ${JOBID}]" >> ${JLOG}
  138:                 sh "${JSCR}" >> ${JLOG}
  139:                 RETCODE=$?
  140: 
  141:                 rm -f "${HOSTDIR}/build-running"
  142:                 rm -f "${JOBDIR}/running"
  143: 
  144:                 rm -f "${HOSTDIR}/build-failing"
  145:                 rm -f "${HOSTDIR}/build-passing"
  146:                 
  147:                 case $RETCODE in
  148: 
  149:                     0)
  150:                         echo "${JOBID}" > "${HOSTDIR}/build-passing"
  151:                         echo "${JOBID}" > "${JOBDIR}/passing"
  152:                         ;;
  153:                     *)
  154:                         echo "${JOBID}" > "${HOSTDIR}/build-failing"
  155:                         echo "${JOBID}" > "${JOBDIR}/failing"
  156:                         exit 1
  157:                         ;;
  158: 
  159:                 esac
  160:             else
  161:                 echo "$PROGN:  build script ${JSCR} missing for project ${PROJECT} [job id ${JOBID}]"
  162:                 echo "$PROGN:  build script ${JSCR} missing for project ${PROJECT} [job id ${JOBID}]" > "${HOSTDIR}/build-failing"
  163:                 echo "$PROGN:  build script ${JSCR} missing for project ${PROJECT} [job id ${JOBID}]" > "${JOBDIR}/failing"
  164:                 echo "$PROGN:  build script ${JSCR} missing for project ${PROJECT} [job id ${JOBID}]" >> "${JLOG}"
  165:                 rm -f "${HOSTDIR}/build-running"
  166:                 rm -f "${JOBDIR}/running"
  167:             fi
  168: 
  169:             if [ -f "${POSTSCR}" ]
  170:             then
  171:                 echo "$PROGN:  running host-specific postbuild script ${POSTSCR} for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
  172:                 echo "$PROGN:  running host-specific postbuild script ${POSTSCR} for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
  173:                 sh "${POSTSCR}" >> ${JLOG}
  174:                 RETCODE=$?
  175: 
  176:                 if [ $RETCODE -gt 0 ]
  177:                 then
  178:                     echo "$PROGN:  host-specific postbuild script failed for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
  179:                     echo "$PROGN:  host-specific postbuild script failed for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
  180: 
  181:                     rm -f "${HOSTDIR}/build-running"
  182:                     rm -f "${JOBDIR}/running"                   
  183:                     rm -f "${HOSTDIR}/build-failing"
  184:                     rm -f "${HOSTDIR}/build-passing"
  185:                     
  186:                     echo "${JOBID}" > "${HOSTDIR}/build-failing"
  187:                     echo "${JOBID}" > "${JOBDIR}/failing"
  188:                     exit 1                    
  189:                 fi
  190:             else
  191:                 echo "$PROGN:  no host-specific postbuild script exists for project ${PROJECT} on host ${THOST} [job id ${JOBID}]"
  192:                 echo "$PROGN:  no host-specific postbuild script exists for project ${PROJECT} on host ${THOST} [job id ${JOBID}]" >> ${JLOG}
  193:             fi
  194:             
  195:         fi
  196:     fi
  197:     
  198: done

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>