Skip to content

Commit

Permalink
make updater less sensitive to permission issues
Browse files Browse the repository at this point in the history
internal git repo kept local in container and result rsynced to the final destination
  • Loading branch information
umputun committed Dec 19, 2023
1 parent ef72a6d commit 420a4b7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
4 changes: 2 additions & 2 deletions updater/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ FROM ghcr.io/umputun/baseimage/app:latest as base
ENV UPDATER_IN_DOCKER=1
COPY update-git.sh /srv/update-git.sh
RUN chmod +x /srv/update-git.sh
RUN apk add --no-cache --update git tzdata ca-certificates
RUN apk add --no-cache --update git tzdata ca-certificates rsync
COPY update-git.sh /srv/update-git.sh

USER app

VOLUME /data
WORKDIR /srv
ENTRYPOINT ["/srv/update-git.sh"]
ENTRYPOINT ["/init.sh", "/srv/update-git.sh"]
4 changes: 4 additions & 0 deletions updater/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ services:
volumes:
- ./tg-spam-samples:/samples
```
**permission issues**
If the updater is run as a Docker container, there may be a discrepancy in file ownership between the host and the container. By default, containers run with a user ID (UID) of 1001. If the host's directory is owned by a different user, this can lead to permission issues. To circumvent this, you can use the `APP_UID` environment variable, either from the command line or within a Docker Compose file. For instance: `docker run -d --name tg-spam-updater -e APP_UID=502 ....`. It's also recommended to create the samples directory on the host before running the container.
63 changes: 27 additions & 36 deletions updater/update-git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

gitrepo=$1
location=$(realpath ${2:-./samples})
internal_location="/srv/.samples"

if [ -z "$gitrepo" ]; then
echo "error: you must specify a git repo url as the first argument."
Expand All @@ -12,44 +13,34 @@ log() {
echo "$(date +"%Y-%m-%d %H:%M:%S") $1"
}

# update the git repo if there are changes
function update() {
git -C $location fetch
if [ "$(git -C $location rev-parse HEAD)" != "$(git -C $location rev-parse @{u})" ]; then
log "changes detected - updating"
git -C $location pull
ls -l $location
sync_files() {
rsync -av --update --checksum --info=name --exclude='.git' --exclude=".github" --exclude='README.md' "$internal_location/" "$location/" | grep -v '^\.\/$'
}

# Clone or update the internal git repo and sync changes to $location
function sync_repo() {
if [ ! -d "$internal_location/.git" ]; then
log "cloning git repo to $internal_location"
git clone -q $gitrepo $internal_location
sync_files
else
log "no changes detected in $location"
git -C $internal_location fetch
if [ "$(git -C $internal_location rev-parse HEAD)" != "$(git -C $internal_location rev-parse @{u})" ]; then
log "changes detected - updating"
git -C $internal_location pull
sync_files
fi
fi
}

# check if we are running inside docker or from terminal
if [ -n "$UPDATER_IN_DOCKER" ]; then
log "running inside docker"
elif [ -t 0 ]; then
log "running from terminal"
else
log "running from cron scheduler"
fi

# clone the git repo if it doesn't exist
if [ ! -d "$location/.git" ]; then
log "cloning git repo to $location"
git clone -q $gitrepo $location
ls -l $location
fi
# Initial setup
log "setting up"
mkdir -p "$location"
sync_repo

# check if we are running inside docker or from terminal
# if we are running from cron scheduler, run the updater once and exit
# if we are running inside docker, run the updater in an endless loop
if [ -n "$UPDATER_IN_DOCKER" ] || [ -t 0 ]; then
log "running updater in endless loop, every minute"
while true; do
update
sleep 1m
done
else
update
exit 0
fi
# Running loop
log "running updater in endless loop, every minute"
while true; do
sync_repo
sleep 1m
done

0 comments on commit 420a4b7

Please sign in to comment.