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 6 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 create 640 root root
sharedscripts sharedscripts
postrotate postrotate
/bin/docker-send-signal \$${MASTER_BASE_SERVICE_NAME^^}_NAME SIGUSR1;
/bin/docker-send-signal ${MASTER_BASE_SERVICE_NAME} SIGUSR1;
endscript endscript
} }
EOF EOF

6
logrotate/build/Dockerfile

@ -1,10 +1,12 @@
FROM docker.0k.io/alpine FROM docker.0k.io/alpine
## dependency of docker-send-signal
RUN apk add curl
RUN apk add jq
RUN apk add logrotate && \ RUN apk add logrotate && \
sed -ri 's/^(\/var\/log\/messages \{\})$/# \1/g' /etc/logrotate.conf sed -ri 's/^(\/var\/log\/messages \{\})$/# \1/g' /etc/logrotate.conf
RUN apk add netcat-openbsd ## dependency of docker-send-signal
COPY src/ / COPY src/ /
ENTRYPOINT ["/entrypoint.sh"] 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") exname=$(basename "$0")
usage="$exname [-h|--help] CONTAINER SIGNAL"
usage="$exname [-h|--help] SERVICE SIGNAL"
container=
service=
signal= signal=
while [ "$1" ]; do while [ "$1" ]; do
case "$1" in case "$1" in
@ -12,7 +21,7 @@ while [ "$1" ]; do
exit 0 exit 0
;; ;;
*) *)
[ -z "$container" ] && { container=$1 ; shift ; continue ; }
[ -z "$service" ] && { service=$1 ; shift ; continue ; }
[ -z "$signal" ] && { signal=$1 ; shift ; continue ; } [ -z "$signal" ] && { signal=$1 ; shift ; continue ; }
echo "Unexpected argument '$1'." >&2 echo "Unexpected argument '$1'." >&2
exit 1 exit 1
@ -21,18 +30,36 @@ while [ "$1" ]; do
shift shift
done 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 echo "$usage" >&2
exit 1 exit 1
fi fi
if [ -z "$signal" ]; then 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 echo "$usage" >&2
exit 1 exit 1
fi 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 done >> status.clean
mv status.clean status 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 type: run-once
data-resources: data-resources:
- /var/lib/logrotate - /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: provides:
log-rotate: log-rotate:
tech-dep: "reversed" tech-dep: "reversed"

Loading…
Cancel
Save