Browse Source

fix: [logrotate] rotation of logs would not happen

This is due to ``alpine`` version that stores the ``logrotate`` status
file in a different place than debian version.
framadate
Valentin Lab 5 years ago
parent
commit
de5c44509a
  1. 2
      apache/hooks/log_rotate-relation-joined
  2. 6
      logrotate/build/Dockerfile
  3. 45
      logrotate/build/src/bin/docker-send-signal
  4. 2
      logrotate/build/src/entrypoint.sh
  5. 5
      logrotate/metadata.yml

2
apache/hooks/log_rotate-relation-joined

@ -36,7 +36,7 @@ file_put "$DST" <<EOF
create 640 root root
sharedscripts
postrotate
/bin/docker-send-signal \$${MASTER_BASE_SERVICE_NAME^^}_NAME SIGUSR1;
/bin/docker-send-signal ${MASTER_BASE_SERVICE_NAME} SIGUSR1;
endscript
}
EOF

6
logrotate/build/Dockerfile

@ -1,10 +1,12 @@
FROM docker.0k.io/alpine
## dependency of docker-send-signal
RUN apk add curl
RUN apk add jq
RUN apk add logrotate && \
sed -ri 's/^(\/var\/log\/messages \{\})$/# \1/g' /etc/logrotate.conf
RUN apk add netcat-openbsd ## dependency of docker-send-signal
COPY src/ /
ENTRYPOINT ["/entrypoint.sh"]

45
logrotate/build/src/bin/docker-send-signal

@ -1,9 +1,18 @@
#!/bin/bash
#!/bin/sh
docker_api() {
local url="$1"
shift
## Note: the 'localhost' part is ignored by curl, but it is not
## we can't remove it (like: http:/$url). If it does work on ubuntu's
## curl, it doesn't work on alpine's curl.
curl -s --unix-socket /var/run/docker.sock "http://localhost/$url" "$@"
}
exname=$(basename "$0")
usage="$exname [-h|--help] CONTAINER SIGNAL"
usage="$exname [-h|--help] SERVICE SIGNAL"
container=
service=
signal=
while [ "$1" ]; do
case "$1" in
@ -12,7 +21,7 @@ while [ "$1" ]; do
exit 0
;;
*)
[ -z "$container" ] && { container=$1 ; shift ; continue ; }
[ -z "$service" ] && { service=$1 ; shift ; continue ; }
[ -z "$signal" ] && { signal=$1 ; shift ; continue ; }
echo "Unexpected argument '$1'." >&2
exit 1
@ -21,18 +30,36 @@ while [ "$1" ]; do
shift
done
if [ -z "$container" ]; then
echo "You must provide a container name/id as first argument." >&2
if [ -z "$service" ]; then
echo "You must provide a service name as first argument." >&2
echo "$usage" >&2
exit 1
fi
if [ -z "$signal" ]; then
echo "You must provide a signal to send to $container aargument." >&2
echo "You must provide a signal to send to $service aargument." >&2
echo "$usage" >&2
exit 1
fi
container_id="$(docker inspect --format="{{ .Id }}" "$container")"
echo -e "POST /containers/$container_id/kill?signal=$signal HTTP/1.0\r\n" | nc -U /var/run/docker.sock
if ! containers=$(
docker_api containers/json \
-G --data-urlencode filters="{\"label\": [\"compose.service=$service\"]}"); then
echo "Curl toward socket for list of containers failed." >&2
exit 1
fi
if ! container_id=$(echo "$containers" | jq -r '.[0].Id'); then
echo "Failed to query following JSON:" >&2
echo "$containers" >&2
exit 1
fi
if [ "$container_id" -a "$container_id" != "null" ]; then
echo "Sending $signal to $service" >&2
docker_api "containers/${container_id}/kill" --data "signal=$signal"
else
echo "No container found for service '$service'." >&2
fi
true

2
logrotate/build/src/entrypoint.sh

@ -10,4 +10,4 @@ do
done >> status.clean
mv status.clean status
/usr/sbin/logrotate /etc/logrotate.conf
/usr/sbin/logrotate -s /var/lib/logrotate/status /etc/logrotate.conf

5
logrotate/metadata.yml

@ -2,9 +2,8 @@ description: Log Rotation Service
type: run-once
data-resources:
- /var/lib/logrotate
## YYY: probably not useful anymore
# charm-resources:
# - /etc/cron.daily/logrotate
host-resources:
- /var/run/docker.sock ## used by docker-send-signal for reloading containers
provides:
log-rotate:
tech-dep: "reversed"

Loading…
Cancel
Save