Skip to content

Commit

Permalink
Log all services to memory (blakeblackshear#4587)
Browse files Browse the repository at this point in the history
* Log all services to RAM

* Fix tests workdir

* Rotate logs when they reach 10MB and keep only 1 archive

* Gracefully handle shutdown

* Add note about gracetime not working

* Fix logs permission, create fake logs for devcontainer

* Remove empty line

* Update docker/rootfs/etc/services.d/frigate/run

* Fix fake Frigate shebang
  • Loading branch information
felipecrs committed Dec 7, 2022
1 parent 6abc0ce commit 2a5ab77
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 14 deletions.
26 changes: 20 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ ENV NVIDIA_DRIVER_CAPABILITIES="compute,video,utility"

ENV PATH="/usr/lib/btbn-ffmpeg/bin:/usr/local/go2rtc/bin:/usr/local/nginx/sbin:${PATH}"

# TODO: remove after a new verion of s6-overlay is released. See:
# https://github.com/just-containers/s6-overlay/issues/460#issuecomment-1327127006
ENV S6_SERVICES_READYTIME=50

# Install dependencies
RUN --mount=type=bind,source=docker/install_deps.sh,target=/deps/install_deps.sh \
/deps/install_deps.sh
Expand All @@ -175,11 +171,31 @@ EXPOSE 1935
EXPOSE 8554
EXPOSE 8555

# Fails if cont-init.d fails
ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2
# Wait indefinitely for cont-init.d to finish before starting services
ENV S6_CMD_WAIT_FOR_SERVICES=1
ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME=0
# Give services (including Frigate) 30 seconds to stop before killing them
# But this is not working currently because of:
# https://github.com/just-containers/s6-overlay/issues/503
ENV S6_SERVICES_GRACETIME=30000
# Configure logging to prepend timestamps, log to stdout, keep 1 archive and rotate on 10MB
ENV S6_LOGGING_SCRIPT="T 1 n1 s10000000 T"
# TODO: remove after a new version of s6-overlay is released. See:
# https://github.com/just-containers/s6-overlay/issues/460#issuecomment-1327127006
ENV S6_SERVICES_READYTIME=50

ENTRYPOINT ["/init"]
CMD []

# Frigate deps with Node.js and NPM for devcontainer
FROM deps AS devcontainer

# Do not start the actual Frigate service on devcontainer as it will be started by VSCode
# But start a fake service for simulating the logs
COPY docker/fake_frigate_run /etc/services.d/frigate/run

# Install Node 16
RUN apt-get update \
&& apt-get install wget -y \
Expand Down Expand Up @@ -224,5 +240,3 @@ FROM deps

WORKDIR /opt/frigate/
COPY --from=rootfs / /

CMD ["with-contenv", "python3", "-u", "-m", "frigate"]
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ build: version amd64 arm64 armv7
push: build
docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):${GITHUB_REF_NAME}-$(COMMIT_HASH) .

run: local
docker run --rm --publish=5000:5000 --volume=${PWD}/config/config.yml:/config/config.yml frigate:latest

run_tests: local
docker run --rm --entrypoint=python3 frigate:latest -u -m unittest
docker run --rm --entrypoint=python3 frigate:latest -u -m mypy --config-file frigate/mypy.ini frigate
docker run --rm --workdir=/opt/frigate --entrypoint= frigate:latest python3 -u -m unittest
docker run --rm --workdir=/opt/frigate --entrypoint= frigate:latest python3 -u -m mypy --config-file frigate/mypy.ini frigate

.PHONY: run_tests
8 changes: 8 additions & 0 deletions docker/fake_frigate_run
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Start the fake Frigate service

while true; do
echo "The fake Frigate service is running..."
sleep 5s
done
11 changes: 11 additions & 0 deletions docker/rootfs/etc/cont-init.d/prepare-logs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Prepare the logs folder for s6-log

set -o errexit -o nounset -o pipefail

dirs=(/dev/shm/logs/frigate /dev/shm/logs/go2rtc /dev/shm/logs/nginx)

mkdir -p "${dirs[@]}"
chown nobody:nogroup "${dirs[@]}"
chmod 02755 "${dirs[@]}"
16 changes: 16 additions & 0 deletions docker/rootfs/etc/services.d/frigate/finish
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Take down the S6 supervision tree when the service exits

set -o errexit -o nounset -o pipefail

# Prepare exit code
if [[ "${1}" -eq 256 ]]; then
exit_code="$((128 + ${2}))"
else
exit_code="${1}"
fi

# Make the container exit with the same exit code as the service
echo "${exit_code}" > /run/s6-linux-init-container-results/exitcode
exec /run/s6/basedir/bin/halt
4 changes: 4 additions & 0 deletions docker/rootfs/etc/services.d/frigate/log/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/command/with-contenv bash
# shellcheck shell=bash

exec logutil-service /dev/shm/logs/frigate
11 changes: 11 additions & 0 deletions docker/rootfs/etc/services.d/frigate/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Start the Frigate service

set -o errexit -o nounset -o pipefail

cd /opt/frigate

# Replace the bash process with the Frigate process, redirecting stderr to stdout
exec 2>&1
exec python3 -u -m frigate
3 changes: 2 additions & 1 deletion docker/rootfs/etc/services.d/go2rtc/finish
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Take down the S6 supervision tree when the process fails
# Take down the S6 supervision tree when the service fails, or restart it
# otherwise

if [[ "${1}" -ne 0 && "${1}" -ne 256 ]]; then
exec /run/s6/basedir/bin/halt
Expand Down
4 changes: 4 additions & 0 deletions docker/rootfs/etc/services.d/go2rtc/log/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/command/with-contenv bash
# shellcheck shell=bash

exec logutil-service /dev/shm/logs/go2rtc
6 changes: 4 additions & 2 deletions docker/rootfs/etc/services.d/go2rtc/run
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Start the go2rtc service

# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425?permalink_comment_id=3945021
set -euo pipefail
set -o errexit -o nounset -o pipefail

if [[ -f "/config/frigate-go2rtc.yaml" ]]; then
config_path="/config/frigate-go2rtc.yaml"
else
config_path="/usr/local/go2rtc/go2rtc.yaml"
fi

# Replace the bash process with the go2rtc process, redirecting stderr to stdout
exec 2>&1
exec go2rtc -config="${config_path}"
3 changes: 2 additions & 1 deletion docker/rootfs/etc/services.d/nginx/finish
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Take down the S6 supervision tree when the process fails
# Take down the S6 supervision tree when the service fails, or restart it
# otherwise

if [[ "${1}" -ne 0 && "${1}" -ne 256 ]]; then
exec /run/s6/basedir/bin/halt
Expand Down
4 changes: 4 additions & 0 deletions docker/rootfs/etc/services.d/nginx/log/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/command/with-contenv bash
# shellcheck shell=bash

exec logutil-service /dev/shm/logs/nginx
3 changes: 3 additions & 0 deletions docker/rootfs/etc/services.d/nginx/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Start the NGINX service

# Replace the bash process with the NGINX process, redirecting stderr to stdout
exec 2>&1
exec nginx
5 changes: 5 additions & 0 deletions docker/rootfs/usr/local/bin/frigate
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/command/with-contenv bash
# shellcheck shell=bash

exec 2>&1
exec python3 -u -m frigate "${@}"
4 changes: 2 additions & 2 deletions docker/rootfs/usr/local/nginx/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ daemon off;
user root;
worker_processes 1;

error_log /usr/local/nginx/logs/error.log warn;
error_log /dev/stdout warn;
pid /var/run/nginx.pid;

events {
Expand All @@ -17,7 +17,7 @@ http {
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /usr/local/nginx/logs/access.log main;
access_log /dev/stdout main;

sendfile on;

Expand Down

0 comments on commit 2a5ab77

Please sign in to comment.