From df738295c833d4d9346f698584be2b431772b576 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Mon, 26 Nov 2018 22:08:28 +0100 Subject: [PATCH] new: [logrotate,odoo-tecnativa,apache,cron,letsencrypt] added charm ``logrotate`` and added logrotation to bunch of charms. --- apache/hooks/log_rotate-relation-joined | 17 ++-- cron/hooks/init | 20 ++++- cron/hooks/log_rotate-relation-joined | 11 ++- .../hooks/schedule_command-relation-joined | 7 +- logrotate/build/Dockerfile | 10 +++ logrotate/build/src/bin/docker-send-signal | 38 +++++++++ logrotate/build/src/entrypoint.sh | 13 +++ logrotate/hooks/log_rotate-relation-joined | 80 +++++++++++++++++++ .../hooks/schedule_command-relation-joined | 32 ++++++++ logrotate/metadata.yml | 13 +++ .../hooks/log_rotate-relation-joined | 50 ++++++++++++ odoo-tecnativa/metadata.yml | 1 + 12 files changed, 272 insertions(+), 20 deletions(-) create mode 100644 logrotate/build/Dockerfile create mode 100755 logrotate/build/src/bin/docker-send-signal create mode 100755 logrotate/build/src/entrypoint.sh create mode 100755 logrotate/hooks/log_rotate-relation-joined create mode 100755 logrotate/hooks/schedule_command-relation-joined create mode 100644 logrotate/metadata.yml create mode 100755 odoo-tecnativa/hooks/log_rotate-relation-joined diff --git a/apache/hooks/log_rotate-relation-joined b/apache/hooks/log_rotate-relation-joined index 9d879a1..4726eac 100755 --- a/apache/hooks/log_rotate-relation-joined +++ b/apache/hooks/log_rotate-relation-joined @@ -36,17 +36,18 @@ file_put "$DST" <&1 | ts '\%F \%T \%Z' >> $LOCAL_LOG + dc run $SERVICE_NAME crt renew" 2>&1 | ts '\%F \%T \%Z' >> $LOCAL_LOG + EOF chmod +x "$DST" diff --git a/logrotate/build/Dockerfile b/logrotate/build/Dockerfile new file mode 100644 index 0000000..39aa138 --- /dev/null +++ b/logrotate/build/Dockerfile @@ -0,0 +1,10 @@ +FROM docker.0k.io/alpine + +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"] diff --git a/logrotate/build/src/bin/docker-send-signal b/logrotate/build/src/bin/docker-send-signal new file mode 100755 index 0000000..2ca12b3 --- /dev/null +++ b/logrotate/build/src/bin/docker-send-signal @@ -0,0 +1,38 @@ +#!/bin/bash + +exname=$(basename "$0") +usage="$exname [-h|--help] CONTAINER SIGNAL" + +container= +signal= +while [ "$1" ]; do + case "$1" in + "--help"|"-h") + echo "$usage" >&2 + exit 0 + ;; + *) + [ -z "$container" ] && { container=$1 ; shift ; continue ; } + [ -z "$signal" ] && { signal=$1 ; shift ; continue ; } + echo "Unexpected argument '$1'." >&2 + exit 1 + ;; + esac + shift +done + +if [ -z "$container" ]; then + echo "You must provide a container name/id 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 "$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 diff --git a/logrotate/build/src/entrypoint.sh b/logrotate/build/src/entrypoint.sh new file mode 100755 index 0000000..14ddc24 --- /dev/null +++ b/logrotate/build/src/entrypoint.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +# Clean non existent log file entries from status file +cd /var/lib/logrotate +test -e status || touch status +head -1 status > status.clean +sed 's/"//g' status | while read logfile date +do + [ -e "$logfile" ] && echo "\"$logfile\" $date" +done >> status.clean +mv status.clean status + +/usr/sbin/logrotate /etc/logrotate.conf diff --git a/logrotate/hooks/log_rotate-relation-joined b/logrotate/hooks/log_rotate-relation-joined new file mode 100755 index 0000000..488c59f --- /dev/null +++ b/logrotate/hooks/log_rotate-relation-joined @@ -0,0 +1,80 @@ +#!/bin/bash + +## Should be executable N time in a row with same result. + +exit 0 +. lib/common + +set -e + +LOGS=/var/log/apache2 + + + +## XXXvlab: hum it seems apache logging is run as root, so well... +# logs_creds=$(cached_cmd_on_base_image apache "stat -c '%u %g' '$LOGS'") || { +# debug "Failed to query for www-data gid in ${DARKYELLOW}apache${NORMAL} base image." +# return 1 +# } + +rotated_count=$(relation-get rotated-count 2>/dev/null) || true +rotated_count=${rotated_count:-52} + +## XXXvlab: a lot of this intelligence should be moved away into +## ``logrotate`` charm +## +## Issues: +## - relation-joined will execute first log-rotate charm part of the +## relation, which is not what we want here, as we need to send +## default value for the creation of the config file on the server +## part. +## - we need to send the directory it seems, otherwise, docker will +## create directory when the log file is missing, and I'm not sure +## how processes will react when their file is moved out of their +## file-system scope when rotated (but I think there will be no +## issue here). +## The problem here is that we can't cleanly put all file in the +## same directory (and there are collision possible anyway). +## This means that if we want more than one target, we need +## sub-directories. +## - For this issue, we only fear the 'docker' run and mounting +## moment, and we are sure to run before, so we can make sure to +## ``touch`` the files. +## - can we move file that was been bound in a docker ? Well yes, +## but it won't change place in the docker:( ... I guess you need +## to reload the docker and the binding to work. +## + +DST="$CONFIGSTORE/$TARGET_SERVICE_NAME/etc/logrotate.d/$SERVICE_NAME" +file_put "$DST" </dev/null 2>&1; then + err "Unrecognized schedule '$schedule'." + exit 1 +fi + +## Warning: using '\' in heredoc will be removed in the final cron file, which +## is totally wanted: cron does not support multilines. + +## Warning: 'docker -v' will use HOST directory even if launched from +## 'cron' container. +file_put "$DST" <&1 | ts '\%F \%T \%Z' >> /var/log/cron/${label}_script.log +EOF +chmod +x "$DST" diff --git a/logrotate/metadata.yml b/logrotate/metadata.yml new file mode 100644 index 0000000..1715fbc --- /dev/null +++ b/logrotate/metadata.yml @@ -0,0 +1,13 @@ +description: Log Rotation Service +type: run-once +data-resources: + - /var/lib/logrotate +requires: + schedule-command: + interface: schedule-command +## YYY: probably not useful anymore +# charm-resources: +# - /etc/cron.daily/logrotate +provides: + log-rotate: + tech-dep: "reversed" diff --git a/odoo-tecnativa/hooks/log_rotate-relation-joined b/odoo-tecnativa/hooks/log_rotate-relation-joined new file mode 100755 index 0000000..823e4a4 --- /dev/null +++ b/odoo-tecnativa/hooks/log_rotate-relation-joined @@ -0,0 +1,50 @@ +#!/bin/bash + +## Should be executable N time in a row with same result. + +. lib/common + +set -e + +odoo_uid=$(get_odoo_uid) + +LOGS=/var/log/odoo +mkdir -p "$SERVICE_DATASTORE/$LOGS" +chown -R "$odoo_uid" "$SERVICE_DATASTORE/$LOGS" + +rotated_count=$(relation-get rotated-count 2>/dev/null) || true +rotated_count=${rotated_count:-52} + +## No postrotate script as odoo uses a WatchedFileHandler log that +## close and reopen log file if it changes. + +## XXXvlab: a lot of this intelligence should be moved away into ``logrotate`` charm +DST="$CONFIGSTORE/$TARGET_SERVICE_NAME/etc/logrotate.d/$SERVICE_NAME" +file_put "$DST" <