2023-08-04 02:21:27 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -o functrace
|
|
|
|
set -o nounset
|
|
|
|
set -o errtrace
|
|
|
|
set -o errexit
|
2023-08-04 03:21:51 +00:00
|
|
|
shopt -s nullglob
|
2023-08-04 02:21:27 +00:00
|
|
|
|
2023-08-04 08:35:33 +00:00
|
|
|
function msg_base {
|
|
|
|
local prefix="$1" ; shift
|
2023-08-04 02:21:27 +00:00
|
|
|
local mesg="$1"; shift
|
2023-08-04 08:35:33 +00:00
|
|
|
printf "${prefix} ${mesg}\n" "$@"
|
2023-08-04 02:21:27 +00:00
|
|
|
}
|
2023-08-04 08:35:33 +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
|
|
|
|
|
2023-08-04 03:21:51 +00:00
|
|
|
local has_deb=0
|
2023-08-04 08:35:33 +00:00
|
|
|
local deb_name=""
|
|
|
|
|
|
|
|
msg1 "start build repository"
|
2023-08-04 03:21:51 +00:00
|
|
|
|
2023-08-04 02:49:38 +00:00
|
|
|
local return_code=0
|
2023-08-04 03:21:51 +00:00
|
|
|
for deb in "${INCOME_DIR}/"*.deb
|
2023-08-04 02:21:27 +00:00
|
|
|
do
|
2023-08-04 03:21:51 +00:00
|
|
|
has_deb=1
|
2023-08-04 08:35:33 +00:00
|
|
|
deb_name="$(basename "$deb")"
|
2023-08-04 02:21:27 +00:00
|
|
|
|
2023-08-04 08:35:33 +00:00
|
|
|
msg2 "start adding $deb_name"
|
2023-08-04 02:21:27 +00:00
|
|
|
for codename in "${CODENAME[@]}"
|
|
|
|
do
|
2023-08-04 08:35:33 +00:00
|
|
|
msg3 "adding $deb_name into $codename"
|
2023-08-04 02:49:38 +00:00
|
|
|
reprepro includedeb "$codename" "$deb" && return_code=$? || return_code=$?
|
|
|
|
|
|
|
|
if [[ "$return_code" -ne 0 ]]
|
|
|
|
then
|
2023-08-04 08:35:33 +00:00
|
|
|
msg3 "failed to add $deb_name into $codename"
|
|
|
|
msg3 "copy $deb_name to FAILED_DIR/$codename"
|
2023-08-04 02:49:38 +00:00
|
|
|
mkdir -p "$FAILED_DIR/$codename"
|
|
|
|
cp "$deb" "$FAILED_DIR/$codename/"
|
2023-08-04 08:35:33 +00:00
|
|
|
else
|
|
|
|
msg3 "added $deb_name into $codename"
|
2023-08-04 02:49:38 +00:00
|
|
|
fi
|
2023-08-04 02:21:27 +00:00
|
|
|
done
|
2023-08-04 08:35:33 +00:00
|
|
|
msg2 "finished adding $deb_name"
|
2023-08-04 02:21:27 +00:00
|
|
|
|
2023-08-04 08:35:33 +00:00
|
|
|
msg2 "removing $deb_name from INCOME_DIR"
|
2023-08-04 02:21:27 +00:00
|
|
|
rm $deb
|
|
|
|
done
|
|
|
|
|
2023-08-04 03:21:51 +00:00
|
|
|
if [[ "$has_deb" == 0 ]]
|
|
|
|
then
|
2023-08-04 08:35:33 +00:00
|
|
|
msg1 "no .deb file in INCOME_DIR, skip this build"
|
2023-08-04 03:21:51 +00:00
|
|
|
fi
|
|
|
|
|
2023-08-04 08:35:33 +00:00
|
|
|
msg1 "finish build repository"
|
|
|
|
|
2023-08-04 02:21:27 +00:00
|
|
|
trap 'term_handle' TERM SIGINT INT
|
|
|
|
}
|
|
|
|
|
2023-08-04 08:35:33 +00:00
|
|
|
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"
|
2023-08-04 08:35:33 +00:00
|
|
|
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="$!"
|
|
|
|
|
2023-08-04 02:49:38 +00:00
|
|
|
# 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
|
2023-08-04 08:35:33 +00:00
|
|
|
|
|
|
|
msg "found directory modify in INCOME_DIR"
|
2023-08-04 02:21:27 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
msg "$PROGRAM_NAME shutdown"
|