apt-repo-updater/apt-repo-updater

145 lines
3.3 KiB
Plaintext
Raw Normal View History

2023-08-04 02:21:27 +00:00
#!/usr/bin/env bash
set -o functrace
set -o nounset
set -o errtrace
set -o errexit
shopt -s nullglob
2023-08-04 02:21:27 +00:00
function msg_base {
local prefix="$1" ; shift
2023-08-04 02:21:27 +00:00
local mesg="$1"; shift
printf "${prefix} ${mesg}\n" "$@"
2023-08-04 02:21:27 +00:00
}
function msg { msg_base "====>" "$@" ; }
function msg1 { msg_base "======>" "$@" ; }
function msg2 { msg_base "=========>" "$@" ; }
function msg3 { msg_base "===========>" "$@" ; }
2023-08-04 02:21:27 +00:00
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
2023-08-04 02:21:27 +00:00
do
has_deb=1
deb_name="$(basename "$deb")"
2023-08-04 02:21:27 +00:00
msg2 "start adding $deb_name"
2023-08-04 02:21:27 +00:00
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
2023-08-04 02:21:27 +00:00
done
msg2 "finished adding $deb_name"
2023-08-04 02:21:27 +00:00
msg2 "removing $deb_name from INCOME_DIR"
2023-08-04 02:21:27 +00:00
rm $deb
done
if [[ "$has_deb" == 0 ]]
then
msg1 "no .deb file in INCOME_DIR, skip this build"
fi
msg1 "finish build repository"
2023-08-04 02:21:27 +00:00
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 "============================"
2023-08-04 02:21:27 +00:00
cd "$WORK_DIR"
GO_ON=1 # flag of if continue the loop
PROGRAM_NAME="$0"
while [[ "$GO_ON" = "1" ]]
do
build_repo
2023-08-04 08:53:46 +00:00
mkdir -p "$INCOME_DIR"
msg "start watching INCOME_DIR"
2023-08-04 02:21:27 +00:00
inotifywait --quiet -e move -e modify -e create "$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"
2023-08-04 02:21:27 +00:00
done
msg "$PROGRAM_NAME shutdown"