module: add logrotate
diff --git a/module/compose/module.sh b/module/compose/module.sh
index 546f50c..3a320e7 100644
--- a/module/compose/module.sh
+++ b/module/compose/module.sh
@@ -26,7 +26,7 @@
 source $common_sh
 
 module_enable $module
-declare -ar depmod=( gerrit mysql apache openssh )
+declare -ar depmod=( gerrit mysql apache openssh logrotate )
 
 declare -r environment="common.env"
 declare -r environment_f="${instance_d}/${environment}"
diff --git a/module/compose/scheme.sh b/module/compose/scheme.sh
index e134384..8c5370e 100644
--- a/module/compose/scheme.sh
+++ b/module/compose/scheme.sh
@@ -179,7 +179,19 @@
       - ${_APACHE_DKRC_WWW_D_}:/rootfs/var/www
       - ${_GERRIT_DKRC_HOOKS_D_}:/rootfs/hooks
       - ${_GERRIT_DKRC_ETC_D_}:/rootfs/etc
-${compose_openssh_has_mount_media}"
+${compose_openssh_has_mount_media}
+  ${_LOGROTATE_DKRC_SERVICE_}:
+    environment:
+      - LOGROTATE_CRON=${_LOGROTATE_CRON_}
+      - LOGROTATE_CROND_LEVEL=${_LOGROTATE_CROND_LEVEL_}
+    image: ${_LOGROTATE_DKRC_IMAGE_}
+    build:
+      context: ${_LOGROTATE_DKRC_CONTEXT_}
+      dockerfile: ${_LOGROTATE_DKRC_DOCKERFILE_}
+    container_name: ${_LOGROTATE_DKRC_CONTAINER_}
+    volumes:
+      - ${_LOGROTATE_LOG_D_}:${_LOGROTATE_LOG_CON_D_}
+      - ${_LOGROTATE_CONF_F_}:${_LOGROTATE_CONF_CON_F_}:ro"
 
 declare -r environment_t="\
 MYSQL_DATABASE=${_MYSQL_DB_NAME_}
diff --git a/module/logrotate/holder.sh b/module/logrotate/holder.sh
new file mode 100644
index 0000000..e0e0894
--- /dev/null
+++ b/module/logrotate/holder.sh
@@ -0,0 +1,55 @@
+#!/bin/bash
+#
+# logrotate - holder
+#
+# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
+
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+_LOGROTATE_DKRC_SERVICE_="logrotate"
+_LOGROTATE_DKRC_IMAGE_="${host_name}-img-${_LOGROTATE_DKRC_SERVICE_}"
+_LOGROTATE_DKRC_CONTAINER_="${host_name}-con-${_LOGROTATE_DKRC_SERVICE_}"
+_LOGROTATE_DKRC_CONTEXT_="${mod_docker_d}"
+_LOGROTATE_DKRC_DOCKERFILE_="Dockerfile"
+_LOGROTATE_TRUNCATE_="copytruncate"
+_LOGROTATE_COMPRESS_="nocompress"
+_LOGROTATE_ROTATE_="0"
+_LOGROTATE_SIZE_="5M"
+_LOGROTATE_CRON_="hourly"
+_LOGROTATE_CROND_LEVEL_="7"
+_LOGROTATE_LOG_D_="${mod_rootfs_d}/var/log"
+_LOGROTATE_LOG_CON_D_="/logs"
+_LOGROTATE_CONF_F_="$logrotate_conf_f"
+_LOGROTATE_LOGROTATE_CONF_T_UID_="0"
+_LOGROTATE_LOGROTATE_CONF_T_GID_="0"
+_LOGROTATE_CONF_CON_F_="/etc/logrotate.conf"
+_LOGROTATE_FILE_PATTERN_="${_LOGROTATE_LOG_CON_D_}/*/*.txt"
+_LOGROTATE_APACHE_FILE_PATTERN_="${_LOGROTATE_LOG_CON_D_}/apache2/*.log"
+_LOGROTATE_APACHE_FILE_PATTERN_+=" ${_LOGROTATE_LOG_CON_D_}/apache2/*.txt"
+_LOGROTATE_APACHE_USER_="www-data"
+_LOGROTATE_APACHE_UID_="33"
+_LOGROTATE_APACHE_GID_="33"
+_LOGROTATE_MYSQL_FILE_PATTERN_="${_LOGROTATE_LOG_CON_D_}/mysql/*.txt"
+_LOGROTATE_MYSQL_USER_="mysql"
+_LOGROTATE_MYSQL_UID_="999"
+_LOGROTATE_MYSQL_GID_="999"
+_LOGROTATE_OPENSSH_FILE_PATTERN_="${_LOGROTATE_LOG_CON_D_}/openssh/*.txt"
+_LOGROTATE_OPENSSH_USER_="githuser"
+_LOGROTATE_OPENSSH_UID_="1001"
+_LOGROTATE_OPENSSH_GID_="1001"
diff --git a/module/logrotate/module.sh b/module/logrotate/module.sh
new file mode 100644
index 0000000..6299c04
--- /dev/null
+++ b/module/logrotate/module.sh
@@ -0,0 +1,61 @@
+#!/bin/bash
+#
+# logrotate - module
+#
+# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
+
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+declare -r module="logrotate"
+source $common_sh
+
+module_enable $module
+
+declare -r etc_d="$mod_rootfs_d/etc"
+declare -r logrotate_conf_f="$etc_d/logrotate.conf"
+declare -r dockerfile_f="$mod_docker_d/Dockerfile"
+declare -r entrypoint_f="$mod_docker_d/entrypoint.sh"
+
+declare -ar mod_more_dirs=( $etc_d )
+
+declare -ar mod_more_files=( $logrotate_conf_f $dockerfile_f $entrypoint_f )
+
+declare -ar mod_more_trefs=( logrotate_conf_t dockerfile_t entrypoint_bang_t )
+
+function tod_watch
+{
+	__watch_module_common || return $s_err
+}
+
+function tod_doins
+{
+	__doins_module_common || return $s_err
+}
+
+function tod_upins
+{
+	__upins_module_common || return $s_err
+}
+
+function tod_clins
+{
+	__clins_module_common || return $s_err
+}
+
+
diff --git a/module/logrotate/scheme.sh b/module/logrotate/scheme.sh
new file mode 100644
index 0000000..6cb8cf0
--- /dev/null
+++ b/module/logrotate/scheme.sh
@@ -0,0 +1,98 @@
+#!/bin/bash
+#
+# logrotate - scheme
+#
+# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
+
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+# Credits to:
+#   https://github.com/linkyard/docker-logrotate
+
+declare -r dockerfile_t="\
+FROM alpine:3.10
+
+RUN set -x \\
+  && apk add --no-cache logrotate tini tzdata moreutils \\
+  && rm /etc/logrotate.conf && rm -r /etc/logrotate.d \\
+  && mv /etc/periodic/daily/logrotate /etc/.logrotate.cronjob
+
+# Only missing uid and gid from alpine 3.10
+RUN addgroup -g ${_LOGROTATE_OPENSSH_GID_} ${_LOGROTATE_OPENSSH_USER_} && \\
+  adduser -H -D -u ${_LOGROTATE_OPENSSH_UID_} -G ${_LOGROTATE_OPENSSH_USER_} ${_LOGROTATE_OPENSSH_USER_}
+
+COPY entrypoint.sh /entrypoint.sh
+
+VOLUME [\"${_LOGROTATE_LOG_CON_D_}\"]
+
+ENTRYPOINT [\"tini\", \"-g\", \"--\"]
+CMD [\"/entrypoint.sh\"]"
+
+declare -r logrotate_conf_t="\
+${_LOGROTATE_APACHE_FILE_PATTERN_} {
+  ${_LOGROTATE_TRUNCATE_}
+  ${_LOGROTATE_COMPRESS_}
+  rotate ${_LOGROTATE_ROTATE_}
+  size ${_LOGROTATE_SIZE_}
+}
+
+${_LOGROTATE_MYSQL_FILE_PATTERN_} {
+  ${_LOGROTATE_TRUNCATE_}
+  ${_LOGROTATE_COMPRESS_}
+  rotate ${_LOGROTATE_ROTATE_}
+  size ${_LOGROTATE_SIZE_}
+}
+
+${_LOGROTATE_OPENSSH_FILE_PATTERN_} {
+  ${_LOGROTATE_TRUNCATE_}
+  ${_LOGROTATE_COMPRESS_}
+  rotate ${_LOGROTATE_ROTATE_}
+  size ${_LOGROTATE_SIZE_}
+}"
+
+declare -r entrypoint_bang_t="\
+#!/bin/sh
+
+TS_FORMAT=\"%Y-%m-%dT%H:%M:%S%z \"
+
+if [ -e /etc/logrotate.conf ]; then
+  echo \"Using mounted /etc/logrotate.conf:\" | ts \"\${TS_FORMAT}\"
+else
+  echo \"Using templated /etc/logrotate.conf:\" | ts \"\${TS_FORMAT}\"
+  {
+    echo \"\${LOGROTATE_FILE_PATTERN:-${_LOGROTATE_LOG_CON_D_}/*.log} {\"
+    echo \"  \${LOGROTATE_TRUNCATE:-copytruncate}\"
+    echo \"  \${LOGROTATE_COMPRESS:-nocompress}\"
+    echo \"  rotate \${LOGROTATE_ROTATE:-5}\"
+    echo \"  size \${LOGROTATE_SIZE:-5M}\"
+    echo \"}\"
+  } > /etc/logrotate.conf
+fi
+ts \"\${TS_FORMAT}\" < /etc/logrotate.conf
+
+if [ -d \"/etc/periodic/\${LOGROTATE_CRON:-15min}\" ]; then
+  echo \"using /etc/periodic/\${LOGROTATE_CRON:-15min} cron schedule\" | ts \"\${TS_FORMAT}\"
+  mv /etc/.logrotate.cronjob \"/etc/periodic/\${LOGROTATE_CRON:-15min}/logrotate\"
+else
+  echo \"assuming \"\${LOGROTATE_CRON:-15min}\" is a cron expression; appending to root's crontab\" | ts \"\${TS_FORMAT}\"
+  echo \"\${LOGROTATE_CRON:-15min} /etc/.logrotate.cronjob\" >> /var/spool/cron/crontabs/root
+fi
+
+# shellcheck disable=SC2086
+exec crond -d \${CROND_LOGLEVEL:-7} -f 2>&1 | ts \"\${TS_FORMAT}\""