From 90e8a3d1ef30f71099024151de4d1438d59e3fd3 Mon Sep 17 00:00:00 2001
From: Dmitry Simonenko <pmwkaa@gmail.com>
Date: Fri, 24 Feb 2012 17:19:08 +0400
Subject: [PATCH] debian-adapt: log rotation procedure updated, added
 tarantool_logrotate script and man file

---
 debian/rules                       |  2 ++
 debian/scripts/tarantool_logrotate | 33 ++++++++++++++++++++++++++++++
 debian/tarantool-common.install    |  1 +
 debian/tarantool-common.logrotate  |  6 +-----
 debian/tarantool_logrotate.pod     | 15 ++++++++++++++
 doc/man/CMakeLists.txt             |  1 -
 6 files changed, 52 insertions(+), 6 deletions(-)
 create mode 100755 debian/scripts/tarantool_logrotate
 create mode 100644 debian/tarantool_logrotate.pod

diff --git a/debian/rules b/debian/rules
index 2201b4aa8b..7051ecb0d9 100755
--- a/debian/rules
+++ b/debian/rules
@@ -47,5 +47,7 @@ install/tarantool::
 install/tarantool-common::
 	pod2man -c 'tarantool instances control' \
 	    debian/tarantool_instance.pod > build-area/tarantool_instance.1
+	pod2man -c 'tarantool log rotation' \
+	    debian/tarantool_logrotate.pod > build-area/tarantool_logrotate.1
 	install -m0755 extra/logger.pl \
 	    debian/tarantool-common/usr/lib/tarantool/logger
diff --git a/debian/scripts/tarantool_logrotate b/debian/scripts/tarantool_logrotate
new file mode 100755
index 0000000000..c65201cc16
--- /dev/null
+++ b/debian/scripts/tarantool_logrotate
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+set -e
+
+CLI="/usr/bin/tarantool"
+CONFIG_DIR="/var/lib/tarantool/started"
+PID_DIR="/var/run/tarantool"
+
+logger_notify() {
+	CFG=$1
+	PORT_PRIMARY=`grep \
+	  '^[[:space:]]*primary_port[[:space:]]*=[[:space:]]*[[:digit:]]\+' $CFG \
+	  | tail -n 1 \
+	  | sed 's/[^[:digit:]]//g'
+	`
+	PORT_ADMIN=`grep \
+	  '^[[:space:]]*admin_port[[:space:]]*=[[:space:]]*[[:digit:]]\+' $CFG \
+	  | tail -n 1 \
+	  | sed 's/[^[:digit:]]//g'
+	`
+	LOGGER_PID=`${CLI} p $PORT_PRIMARY -m $PORT_ADMIN "show info" \
+	  | grep 'logger_pid:' \
+	  | sed 's/[^[:digit:]]//g'
+	`
+	kill -USR2 $LOGGER_PID
+}
+
+if test -d ${PID_DIR}; then
+	for file in `ls -1 $PID_DIR`; do
+		INSTANCE=`basename $file .pid`
+		logger_notify "$CONFIG_DIR/$INSTANCE.cfg"
+	done
+fi
diff --git a/debian/tarantool-common.install b/debian/tarantool-common.install
index a3d4385494..1ca973ef53 100644
--- a/debian/tarantool-common.install
+++ b/debian/tarantool-common.install
@@ -1,2 +1,3 @@
 debian/etc/example.cfg /etc/tarantool/instances.available/
 debian/scripts/tarantool_instance /usr/sbin
+debian/scripts/tarantool_rotate /usr/sbin
diff --git a/debian/tarantool-common.logrotate b/debian/tarantool-common.logrotate
index 07ed66df94..b9c81f51cb 100644
--- a/debian/tarantool-common.logrotate
+++ b/debian/tarantool-common.logrotate
@@ -7,10 +7,6 @@
     delaycompress
     create 0640 tarantool adm
     postrotate
-        if test -d /var/run/tarantool; then
-            if [ `ls /var/run/tarantool | wc -l` -gt 0 ]; then
-                kill -USR2 `cat /var/run/tarantool/*.pid`
-            fi
-        fi
+        /usr/sbin/tarantool_rotate
     endscript
 }
diff --git a/debian/tarantool_logrotate.pod b/debian/tarantool_logrotate.pod
new file mode 100644
index 0000000000..1248621bb9
--- /dev/null
+++ b/debian/tarantool_logrotate.pod
@@ -0,0 +1,15 @@
+=head1 NAME
+
+tarantool_logrotate - utility to rotate tarantool instances logs
+
+
+=head1 SINOPSYS
+
+    tarantool_logrotate
+
+
+=head1 DESCRIPTION
+
+The utility tries to connect to each running tarantool instance to get
+logger pid file, then it sends SIGUSR2 to logger which initiates
+log rotataion procedure.
diff --git a/doc/man/CMakeLists.txt b/doc/man/CMakeLists.txt
index 7cd2068003..765d4a7466 100644
--- a/doc/man/CMakeLists.txt
+++ b/doc/man/CMakeLists.txt
@@ -1,4 +1,3 @@
-
 add_custom_target(man ALL
     COMMAND ${POD2MAN} -c 'high performance key/value storage server'
 	    ${CMAKE_SOURCE_DIR}/doc/man/tarantool_box.pod > ${PROJECT_BINARY_DIR}/doc/man/tarantool_box.1
-- 
GitLab