Compare commits

..

4 commits

Author SHA1 Message Date
leafee98 8a9cb1e45c update README 2023-08-04 16:54:04 +08:00
leafee98 ac2f21370c auto create INCOMDE_DIR 2023-08-04 16:53:46 +08:00
leafee98 632fb43004 relative path, command arguments, log
- use command arguments instead of environemnt
- switch back to relative path
- reformat log
2023-08-04 16:35:33 +08:00
leafee98 696ebf3b22 only care about .deb files in income, use absolute path everywhere 2023-08-04 11:21:51 +08:00
2 changed files with 113 additions and 23 deletions

View file

@ -1,9 +1,42 @@
# Apt Repo Updater
This script will monitor a dir and execute `reprepro` to include the new .deb files.
This script will monitor an income dir and execute `reprepro` to
include the new .deb files under the income dir.
# Usage
Create a directory as WORK_DIR, take `work` as example, and cd into it.
```bash
mkdir work
cd work
```
WORK_DIR=/srv/apt apt-repo-updater
Create `conf/distributions` with content like the follow for `reprepro`
to use, see more at [here](https://manpages.debian.org/jessie/reprepro/reprepro.1.en.html)
```bash
mkdir conf
cat << EOF > conf/distributions
Codename: bookworm
Description: An example repository
Architectures: amd64
Components: main
EOF
```
Start apt-repo-updater
```bash
/path/to/apt-repo-updater --work-dir .
```
Use another terminal to copy a .deb into the INCOME_DIR created
by apt-repo-updater, normally it will be named `income` under the WORK_DIR
```bash
cp /path/to/a/package.deb /path/to/WORK_DIR/income
```
Now the apt repository will include the .deb file just copyied.
And the .deb file will be deleted after included in the repository.

View file

@ -1,18 +1,20 @@
#!/usr/bin/env bash
WORK_DIR=${WORK_DIR:?"WORK_DIR is not set!!"}
INCOME_DIR=${INCOME_DIR:="${WORK_DIR}/income"}
CODENAME=(bookworm)
set -o functrace
set -o nounset
set -o errtrace
set -o errexit
shopt -s nullglob
function msg {
function msg_base {
local prefix="$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 {
if [[ -n "$inotify_pid" ]]
@ -32,38 +34,90 @@ trap 'term_handle' TERM SIGINT INT
function build_repo {
trap 'term_delay' TERM SIGINT INT
local return_code=0
for deb in "${INCOME_DIR}/"*
do
if [[ "$deb" = *"/*" ]]
then
msg "no file in $INCOME_DIR, skip this build"
break
fi
local has_deb=0
local deb_name=""
msg "start adding $deb"
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
msg "adding $deb into $codename"
msg3 "adding $deb_name into $codename"
reprepro includedeb "$codename" "$deb" && return_code=$? || return_code=$?
if [[ "$return_code" -ne 0 ]]
then
msg "failed to add $deb into $codename"
msg "copy $deb to $FAILED_DIR/$codename"
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
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
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
@ -72,7 +126,8 @@ while [[ "$GO_ON" = "1" ]]
do
build_repo
msg "start watching $INCOME_DIR"
mkdir -p "$INCOME_DIR"
msg "start watching INCOME_DIR"
inotifywait --quiet -e move -e modify -e create "$INCOME_DIR" &
inotify_pid="$!"
@ -82,6 +137,8 @@ do
#
# 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"