Skip to content

Commit

Permalink
Auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
aequitas committed Oct 14, 2024
1 parent c3cf41f commit 3110b0e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 5 deletions.
36 changes: 36 additions & 0 deletions docker/cron-docker/periodic/15min/auto_update
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

set -e

if [ -z "$AUTO_UPDATE_TO" ];then
# auto update not configured
exit 0
fi

cd /opt/Internet.nl/

CURRENT_RELEASE="$RELEASE"

IMAGE="${DOCKER_REGISTRY:-ghcr.io/internetstandards}/util:${AUTO_UPDATE_TO}"

# pull latest image
docker pull "$IMAGE"

# get release info from image
UPSTREAM_RELEASE="$(docker image inspect "$IMAGE" | jq -r '.[0].Config.Labels.release')"

# check if update is required
if [ "$CURRENT_RELEASE" = "$UPSTREAM_RELEASE" ];then
echo "No update available"
exit 0
fi

echo "Updating to: $UPSTREAM_RELEASE"

# initiate update, don't remove container when done to preserve logging
# as the initiator (cron-docker) will be killed during the deploy process
docker run --pull=always --network none \
--volume /var/run/docker.sock:/var/run/docker.sock \
--volume /opt/Internet.nl:/opt/Internet.nl \
"ghcr.io/internetstandards/util:$UPSTREAM_RELEASE" \
/deploy.sh
4 changes: 0 additions & 4 deletions docker/cron-docker/periodic/weekly/docker_image_prune

This file was deleted.

7 changes: 7 additions & 0 deletions docker/cron-docker/periodic/weekly/docker_system_prune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

set -e

# remove unused images, stoped containers, unused networks and build cache
# --all to remove images not used by containers, --force to skip confirm prompt
docker system prune --all --force
4 changes: 4 additions & 0 deletions docker/defaults.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ INTERNETNL_DOMAINNAME=
# Git commit SHA or tag used as version to determine which containers and files to use in deployment
RELEASE=latest

# if configured will pull updates from this tag and automatically deploy them
AUTO_UPDATE_TO=

# Container images/versions to use for external containers
DOCKER_IMAGE_REDIS=redis:7.0-alpine
DOCKER_IMAGE_RABBITMQ=rabbitmq:3.12-management-alpine
Expand Down Expand Up @@ -273,3 +276,4 @@ NGINX_PROXY_CACHE=default_cache

# used to disable autoreload in CI
DEVSERVER_ARGS=
DEVSERVER_ARGS=
1 change: 1 addition & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ services:
network_mode: "none"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /opt/Internet.nl:/opt/Internet.nl

profiles:
- cron
Expand Down
5 changes: 4 additions & 1 deletion docker/util.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:3.20

RUN apk add --no-cache curl postgresql15 python3 py3-prometheus-client py3-requests jq docker-cli docker-cli-compose
RUN apk add --no-cache curl postgresql15 python3 py3-prometheus-client py3-requests docker-cli docker-cli-compose jq

# install cron tasks
COPY docker/cron/periodic /etc/periodic/
Expand All @@ -15,12 +15,15 @@ COPY docker/cron-docker/periodic /etc/periodic-docker/
# install deploy script
COPY docker/deploy.sh /deploy.sh

# package deploy artifacts
RUN mkdir -p /dist/docker
COPY docker/defaults.env /dist/docker/defaults.env
COPY docker/host-dist.env /dist/docker/host-dist.env
COPY docker/docker-compose.yml /dist/docker/docker-compose.yml
COPY docker/user_manage.sh /dist/docker/user_manage.sh
RUN chmod a-w /dist/docker/*

# add release as label for auto_update feature
ARG RELEASE
ENV RELEASE=$RELEASE
LABEL release=$RELEASE
26 changes: 26 additions & 0 deletions documentation/Docker-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,32 @@ If you want to update to a specific tagged version release, e.g. `1.8.8`, use th
ghcr.io/internetstandards/util:1.8.8 \
/deploy.sh

### Auto update

By setting the variable `AUTO_UPDATE_TO` in the `/opt/Internet.nl/docker/local.env` auto updating will be enabled. The application will check every 15 minutes if there is a update available and deploy it automatically. This is useful for development/acceptance environments that want to stay up to date with a feature or the `main` branch. It is not recommended for production environments!

This variable can be set to either of these values:

- `latest`: update to latest stable release: https://github.com/internetstandards/Internet.nl/releases
- `main`: update to latest release on the `main` branch
- `pr-<NUMBER>`: update to the latest build of that Pull Request number

Auto upgrades are performed by the `cron-docker` container/service. Progress/errors can be viewed by inspecting the container's logs:

docker compose --project-name=internetnl-prod logs --follow cron-docker

To manually kick off the update process use the following command:

docker compose --project-name=internetnl-prod exec cron-docker /etc/periodic-docker/15min/auto_update

**notice**: the update logging might be cut-off at the end because the `cron-docker` container/service will be restarted in the process.

Every time a deploy is performed a entry is added at the bottom of the `docker/local.env` file which indicated the latest release. For example:

RELEASE='1.9.0.dev142-g118b811-auto_update' # deploy Fri Oct 11 11:42:35 UTC 2024

This variable is used to determine the current version to be deployed or which image version to used when bringing container up. Earlier entries can be safely removed but the last line containing `RELEASE=` must stay in place.

## Downgrading/rollback

In essence downgrading is the same procedure as upgrading. For example, to roll back to version `1.7.0` run:
Expand Down
3 changes: 3 additions & 0 deletions documentation/images/dockerfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
[
DockerHub("alpine"),
File("cron/periodic/*"),
File("cron-docker/periodic/*"),
File("deploy.sh"),
File("docker-compose.yml,defaults.env,host-dist.env"),
] >> Image("ghcr.io/internetstandards/util")

with Cluster("grafana.Dockerfile"):
Expand Down

0 comments on commit 3110b0e

Please sign in to comment.