apt-repo-updater/apt-repo-updater

145 lines
3.3 KiB
Bash
Executable file

#!/usr/bin/env bash
set -o functrace
set -o nounset
set -o errtrace
set -o errexit
shopt -s nullglob
function msg_base {
local prefix="$1" ; shift
local mesg="$1"; shift
printf "${prefix} ${mesg}\n" "$@"
}
function msg { msg_base "====>" "$@" ; }
function msg1 { msg_base "======>" "$@" ; }
function msg2 { msg_base "=========>" "$@" ; }
function msg3 { msg_base "===========>" "$@" ; }
function term_handle {
if [[ -n "$inotify_pid" ]]
then
GO_ON=0
msg "sending TERM to inotifywait..."
kill -s TERM "$inotify_pid" 2>/dev/null || true
fi
}
function term_delay {
GO_ON=0
msg "building repo, will terminate later"
}
trap 'term_handle' TERM SIGINT INT
function build_repo {
trap 'term_delay' TERM SIGINT INT
local has_deb=0
local deb_name=""
msg1 "start build repository"
local return_code=0
for deb in "${INCOME_DIR}/"*.deb
do
has_deb=1
deb_name="$(basename "$deb")"
msg2 "start adding $deb_name"
for codename in "${CODENAME[@]}"
do
msg3 "adding $deb_name into $codename"
reprepro includedeb "$codename" "$deb" && return_code=$? || return_code=$?
if [[ "$return_code" -ne 0 ]]
then
msg3 "failed to add $deb_name into $codename"
msg3 "copy $deb_name to FAILED_DIR/$codename"
mkdir -p "$FAILED_DIR/$codename"
cp "$deb" "$FAILED_DIR/$codename/"
else
msg3 "added $deb_name into $codename"
fi
done
msg2 "finished adding $deb_name"
msg2 "removing $deb_name from INCOME_DIR"
rm $deb
done
if [[ "$has_deb" == 0 ]]
then
msg1 "no .deb file in INCOME_DIR, skip this build"
fi
msg1 "finish build repository"
trap 'term_handle' TERM SIGINT INT
}
if [[ "$#" -eq 0 ]]
then
set -- "$@" --help
fi
CODENAME=()
while [[ "$#" -gt 0 ]]
do
case $1 in
--work-dir)
WORK_DIR="$2" ; shift ;;
--codename)
CODENAME+=("$2") ; shift ;;
--help)
echo "usage: $0 --work-dir <work_dir>"
echo " [--codename bookworm]..."
exit 0
;;
*)
shift ;;
esac
shift
done
WORK_DIR=${WORK_DIR:?"WORK_DIR is not set!!"}
INCOME_DIR=${INCOME_DIR:="income"}
FAILED_DIR=${FAILED_DIR:="${INCOME_DIR}/failed"}
if [[ -z "${CODENAME-}" ]]
then
CODENAME+=(bookworm)
fi
# log environements before actullay run
msg "WORK_DIR: $WORK_DIR"
msg "INCOME_DIR: $INCOME_DIR"
msg "FAILED_DIR: $FAILED_DIR"
msg "CODENAME: ${CODENAME[*]}"
msg "============================"
cd "$WORK_DIR"
GO_ON=1 # flag of if continue the loop
PROGRAM_NAME="$0"
while [[ "$GO_ON" = "1" ]]
do
build_repo
mkdir -p "$INCOME_DIR"
msg "start watching INCOME_DIR"
inotifywait --quiet -e moved_to -e close_write "$INCOME_DIR" &
inotify_pid="$!"
# When inotifywait was killed, its return code is no-zero,
# so when wait return inotifywait's return code, script will exit due to errexit.
# Use '||' to fix the above problem
#
# Break the loop when failed to watching (or killed)
wait $inotify_pid || GO_ON=0
msg "found directory modify in INCOME_DIR"
done
msg "$PROGRAM_NAME shutdown"