2022-12-10 23:42:02 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Run poudriere at system boot. Useful for virtual machines so launching the VM also kicks off a build.
|
|
|
|
set -euo pipefail
|
|
|
|
IFS=$'\n\t'
|
|
|
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
2023-06-22 02:42:42 +00:00
|
|
|
: ${LOCKFILE:="/var/run/poudboot.lock"}
|
2023-06-22 03:13:30 +00:00
|
|
|
: ${INFO_DIR:="/opt/poudriere/run_info"}
|
|
|
|
: ${PORT_UPDATE_SECONDS:="86400"}
|
2024-02-11 19:59:55 +00:00
|
|
|
: ${CLEAN_SECONDS:="1209600"}
|
2023-06-22 03:13:30 +00:00
|
|
|
: ${BUILD_SECONDS:="7200"}
|
2023-09-01 15:25:36 +00:00
|
|
|
: ${LOG_RETENTION_DAYS:="30"}
|
2023-06-22 02:42:42 +00:00
|
|
|
|
|
|
|
############## Setup #########################
|
|
|
|
|
|
|
|
# function cleanup {
|
|
|
|
# for f in "${folders[@]}"; do
|
|
|
|
# log "Deleting $f"
|
|
|
|
# rm -rf "$f"
|
|
|
|
# done
|
|
|
|
# }
|
|
|
|
# folders=()
|
|
|
|
# for sig in EXIT INT QUIT HUP TERM; do
|
|
|
|
# trap "set +e; cleanup" "$sig"
|
|
|
|
# done
|
|
|
|
|
|
|
|
function die {
|
|
|
|
local status_code="$1"
|
|
|
|
shift
|
|
|
|
(>&2 echo "${@}")
|
|
|
|
exit "$status_code"
|
|
|
|
}
|
|
|
|
|
|
|
|
function log {
|
|
|
|
(>&2 echo "${@}")
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_locked {
|
|
|
|
if [ "${RUN_LOCKED:-}" != "RUN" ]; then
|
|
|
|
exec env RUN_LOCKED=RUN flock --nonblock $LOCKFILE $0 $@
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
############## Program #########################
|
|
|
|
|
2022-12-10 23:58:09 +00:00
|
|
|
function main {
|
2023-06-22 02:42:42 +00:00
|
|
|
local COMMAND="$1"
|
2022-12-10 23:58:09 +00:00
|
|
|
|
|
|
|
if [ "$COMMAND" = "start" ]; then
|
2023-06-22 02:42:42 +00:00
|
|
|
run_locked "${@}"
|
|
|
|
shift 1
|
2022-12-10 23:58:09 +00:00
|
|
|
cmd_start "${@}"
|
|
|
|
elif [ "$COMMAND" = "stop" ]; then
|
2023-06-22 02:42:42 +00:00
|
|
|
shift 1
|
2022-12-10 23:58:09 +00:00
|
|
|
cmd_stop "${@}"
|
|
|
|
else
|
|
|
|
die 1 "Unrecognized command: $COMMAND"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function abort_if_jobs_running {
|
|
|
|
if [[ $(sudo poudriere status) != *"No running builds"* ]]; then
|
|
|
|
echo "There is already a poudriere build in progress, exiting."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function build {
|
2024-02-11 19:59:55 +00:00
|
|
|
poudriere pkgclean -v -y "$@"
|
2022-12-10 23:58:09 +00:00
|
|
|
poudriere bulk -J "${POUDRIERE_JOBS:-1}" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
function cmd_start {
|
2022-12-11 00:01:14 +00:00
|
|
|
abort_if_jobs_running
|
|
|
|
|
2023-06-22 03:13:30 +00:00
|
|
|
while true; do
|
|
|
|
for conf in /opt/poudriere/build_configs/*; do
|
|
|
|
(
|
|
|
|
# Allow command failures without quitting the script because some
|
|
|
|
# package sets might fail whereas others may succeed based on which
|
|
|
|
# packages are in each set.
|
|
|
|
set +e
|
|
|
|
|
|
|
|
source "$conf"
|
|
|
|
local RUN_DIR="$INFO_DIR/$JAIL-$PORTS-$SET"
|
|
|
|
local TIMES_FILE="$RUN_DIR/times"
|
|
|
|
mkdir -p "$RUN_DIR"
|
|
|
|
local PORTUPDATE=0
|
|
|
|
local LASTBUILD=0
|
|
|
|
if [ -e "$TIMES_FILE" ]; then
|
|
|
|
source "$TIMES_FILE"
|
|
|
|
fi
|
|
|
|
local now=$(date +%s)
|
|
|
|
if [ $((now - PORTUPDATE)) -gt "$PORT_UPDATE_SECONDS" ]; then
|
|
|
|
log "Updating ports for $JAIL-$PORTS-$SET"
|
|
|
|
portshaker -U
|
|
|
|
portshaker -M
|
|
|
|
PORTUPDATE=$(date +%s)
|
|
|
|
fi
|
|
|
|
if [ $((now - LASTBUILD)) -gt "$BUILD_SECONDS" ]; then
|
|
|
|
log "Building ports for $JAIL-$PORTS-$SET"
|
|
|
|
build -j "$JAIL" -p "$PORTS" -z "$SET" -f /usr/local/etc/poudriere.d/$JAIL-$PORTS-$SET-pkglist
|
|
|
|
LASTBUILD=$(date +%s)
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat > "$TIMES_FILE" <<EOF
|
|
|
|
PORTUPDATE=$PORTUPDATE
|
|
|
|
LASTBUILD=$LASTBUILD
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
done
|
2024-04-12 00:20:49 +00:00
|
|
|
|
|
|
|
(
|
|
|
|
set +e
|
|
|
|
local GLOBAL_TIMES_FILE="$INFO_DIR/times"
|
|
|
|
local LASTCLEAN=0
|
|
|
|
if [ -e "$GLOBAL_TIMES_FILE" ]; then
|
|
|
|
source "$GLOBAL_TIMES_FILE"
|
|
|
|
fi
|
|
|
|
local now=$(date +%s)
|
|
|
|
if [ $((now - LASTCLEAN)) -gt "$CLEAN_SECONDS" ]; then
|
|
|
|
log "Global Cleaning."
|
|
|
|
# Cleanup old unused dist files
|
|
|
|
poudriere distclean -v -y -a
|
|
|
|
poudriere logclean -v -y "$LOG_RETENTION_DAYS"
|
|
|
|
LASTCLEAN=$(date +%s)
|
|
|
|
cat > "$GLOBAL_TIMES_FILE" <<EOF
|
|
|
|
LASTCLEAN=$LASTCLEAN
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
|
|
|
|
)
|
2023-06-22 03:13:30 +00:00
|
|
|
sleep 300
|
2022-12-10 23:58:09 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function cmd_stop {
|
2022-12-11 00:02:50 +00:00
|
|
|
echo "cmd_stop not implemented."
|
2022-12-10 23:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main "${@}"
|