From de5c44509aafc032bba8388fa5799d4cebe6749c Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 15 Jan 2019 23:43:57 +0100 Subject: [PATCH] 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. --- apache/hooks/log_rotate-relation-joined | 2 +- logrotate/build/Dockerfile | 6 ++- logrotate/build/src/bin/docker-send-signal | 45 +++++++++++++++++----- logrotate/build/src/entrypoint.sh | 2 +- logrotate/metadata.yml | 5 +-- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/apache/hooks/log_rotate-relation-joined b/apache/hooks/log_rotate-relation-joined index 4726eac..df85a99 100755 --- a/apache/hooks/log_rotate-relation-joined +++ b/apache/hooks/log_rotate-relation-joined @@ -36,7 +36,7 @@ file_put "$DST" <&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 \ No newline at end of file diff --git a/logrotate/build/src/entrypoint.sh b/logrotate/build/src/entrypoint.sh index 14ddc24..070398d 100755 --- a/logrotate/build/src/entrypoint.sh +++ b/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 diff --git a/logrotate/metadata.yml b/logrotate/metadata.yml index b8fb0c4..64469ec 100644 --- a/logrotate/metadata.yml +++ b/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"