relative path, command arguments, log

- use command arguments instead of environemnt
- switch back to relative path
- reformat log
This commit is contained in:
leafee98 2023-08-04 16:35:33 +08:00
parent 696ebf3b22
commit 632fb43004

View file

@ -1,21 +1,20 @@
#!/usr/bin/env bash #!/usr/bin/env bash
WORK_DIR=${WORK_DIR:?"WORK_DIR is not set!!"}
WORK_DIR="$(realpath "$WORK_DIR")"
INCOME_DIR=${INCOME_DIR:="${WORK_DIR}/income"}
FAILED_DIR=${FAILED_DIR:="${INCOME_DIR}/failed"}
CODENAME=(bookworm)
set -o functrace set -o functrace
set -o nounset set -o nounset
set -o errtrace set -o errtrace
set -o errexit set -o errexit
shopt -s nullglob shopt -s nullglob
function msg { function msg_base {
local prefix="$1" ; shift
local mesg="$1"; shift local mesg="$1"; shift
printf "====> ${mesg}\n" "$@" printf "${prefix} ${mesg}\n" "$@"
} }
function msg { msg_base "====>" "$@" ; }
function msg1 { msg_base "======>" "$@" ; }
function msg2 { msg_base "=========>" "$@" ; }
function msg3 { msg_base "===========>" "$@" ; }
function term_handle { function term_handle {
if [[ -n "$inotify_pid" ]] if [[ -n "$inotify_pid" ]]
@ -36,40 +35,89 @@ function build_repo {
trap 'term_delay' TERM SIGINT INT trap 'term_delay' TERM SIGINT INT
local has_deb=0 local has_deb=0
local deb_name=""
msg1 "start build repository"
local return_code=0 local return_code=0
for deb in "${INCOME_DIR}/"*.deb for deb in "${INCOME_DIR}/"*.deb
do do
has_deb=1 has_deb=1
deb_name="$(basename "$deb")"
msg "start adding $deb" msg2 "start adding $deb_name"
for codename in "${CODENAME[@]}" for codename in "${CODENAME[@]}"
do do
msg "adding $deb into $codename" msg3 "adding $deb_name into $codename"
reprepro includedeb "$codename" "$deb" && return_code=$? || return_code=$? reprepro includedeb "$codename" "$deb" && return_code=$? || return_code=$?
if [[ "$return_code" -ne 0 ]] if [[ "$return_code" -ne 0 ]]
then then
msg "failed to add $deb into $codename" msg3 "failed to add $deb_name into $codename"
msg "copy $deb to $FAILED_DIR/$codename" msg3 "copy $deb_name to FAILED_DIR/$codename"
mkdir -p "$FAILED_DIR/$codename" mkdir -p "$FAILED_DIR/$codename"
cp "$deb" "$FAILED_DIR/$codename/" cp "$deb" "$FAILED_DIR/$codename/"
else
msg3 "added $deb_name into $codename"
fi fi
done done
msg "finished adding $deb" msg2 "finished adding $deb_name"
msg "removing $deb from income dir $INCOME_DIR" msg2 "removing $deb_name from INCOME_DIR"
rm $deb rm $deb
done done
if [[ "$has_deb" == 0 ]] if [[ "$has_deb" == 0 ]]
then then
msg "no .deb file in $INCOME_DIR, skip this build" msg1 "no .deb file in INCOME_DIR, skip this build"
fi fi
msg1 "finish build repository"
trap 'term_handle' TERM SIGINT INT 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" cd "$WORK_DIR"
GO_ON=1 # flag of if continue the loop GO_ON=1 # flag of if continue the loop
@ -78,7 +126,7 @@ while [[ "$GO_ON" = "1" ]]
do do
build_repo build_repo
msg "start watching $INCOME_DIR" msg "start watching INCOME_DIR"
inotifywait --quiet -e move -e modify -e create "$INCOME_DIR" & inotifywait --quiet -e move -e modify -e create "$INCOME_DIR" &
inotify_pid="$!" inotify_pid="$!"
@ -88,6 +136,8 @@ do
# #
# Break the loop when failed to watching (or killed) # Break the loop when failed to watching (or killed)
wait $inotify_pid || GO_ON=0 wait $inotify_pid || GO_ON=0
msg "found directory modify in INCOME_DIR"
done done
msg "$PROGRAM_NAME shutdown" msg "$PROGRAM_NAME shutdown"