[Kimchi-devel] [PATCH v5][Wok] Rotate wok logs

Ramon Medeiros ramonn at linux.vnet.ibm.com
Mon Jan 25 17:47:45 UTC 2016


Use logrotate to compress and keep 10 logs of 10MB

Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
---
Changes:

v5:
Fix rpm uninstall (some files where not erased on rpm -e)
Remove copyright in logrotate file
Add logrotate as dependency on docs

v4:
Add wokd.in file at logrotate, adding the logic to change the logging dir if 
it has been changed at wok.conf

v3:
Set log size to 10MB
Use WatchedFileHandler to avoid issues with python logging and logrotate

v2:
Add logrotate as dependency
Fix typo in Copyright 

 Makefile.am                |  5 +++++
 contrib/DEBIAN/control.in  |  1 +
 contrib/Makefile.am        |  1 +
 contrib/wok.spec.fedora.in |  3 +++
 contrib/wok.spec.suse.in   |  3 +++
 contrib/wokd.logrotate.in  |  9 +++++++++
 docs/fedora-deps.md        |  2 +-
 docs/opensuse-deps.md      |  2 +-
 docs/ubuntu-deps.md        |  2 +-
 src/wok/config.py.in       |  2 ++
 src/wok/server.py          | 32 +++++++++++++++++++++++---------
 11 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 contrib/wokd.logrotate.in

diff --git a/Makefile.am b/Makefile.am
index 278bda1..9de0986 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -140,6 +140,9 @@ install-data-local:
 	mkdir -p $(DESTDIR)/etc/wok/
 	$(INSTALL_DATA) src/dhparams.pem $(DESTDIR)/etc/wok/dhparams.pem
 	touch $(DESTDIR)/etc/nginx/conf.d/wok.conf
+	mkdir -p $(DESTDIR)/etc/logrotate.d/
+	$(INSTALL_DATA) $(top_srcdir)/contrib/wokd.logrotate.in $(DESTDIR)/etc/logrotate.d/wokd.in
+	touch $(DESTDIR)/etc/logrotate.d/wokd
 
 uninstall-local:
 	@if test -f $(systemdsystemunitdir)/wokd.service; then \
@@ -155,6 +158,8 @@ uninstall-local:
 	$(RM) -rf $(DESTDIR)/$(localstatedir)/log/wok
 	$(RM) -rf $(DESTDIR)/etc/wok
 	$(RM) $(DESTDIR)/etc/nginx/conf.d/wok.conf
+	$(RM) $(DESTDIR)/etc/logrotate.d/wokd.in
+	$(RM) $(DESTDIR)/etc/logrotate.d/wokd
 
 VERSION:
 	@if test -d .git; then                                \
diff --git a/contrib/DEBIAN/control.in b/contrib/DEBIAN/control.in
index 1f39ad8..e585080 100644
--- a/contrib/DEBIAN/control.in
+++ b/contrib/DEBIAN/control.in
@@ -14,6 +14,7 @@ Depends: python-cherrypy3 (>= 3.2.0),
          python-ldap,
          python-psutil (>= 0.6.0),
          fonts-font-awesome,
+         logrotate,
          texlive-fonts-extra
 Build-Depends: xsltproc,
                gettext,
diff --git a/contrib/Makefile.am b/contrib/Makefile.am
index ed4a006..036ec50 100644
--- a/contrib/Makefile.am
+++ b/contrib/Makefile.am
@@ -29,6 +29,7 @@ EXTRA_DIST = \
 	wokd-upstart.conf.debian \
 	wokd-upstart.conf.fedora \
 	make-deb.sh.in \
+	wokd.logrotate.in \
 	$(NULL)
 
 make-deb.sh: make-deb.sh.in $(top_builddir)/config.status
diff --git a/contrib/wok.spec.fedora.in b/contrib/wok.spec.fedora.in
index 34b3ac3..ca6fb55 100644
--- a/contrib/wok.spec.fedora.in
+++ b/contrib/wok.spec.fedora.in
@@ -19,6 +19,7 @@ Requires:	python-ldap
 Requires:	python-psutil >= 0.6.0
 Requires:	fontawesome-fonts
 Requires:	open-sans-fonts
+Requires:   logrotate
 BuildRequires:	gettext-devel
 BuildRequires:	libxslt
 BuildRequires:	openssl
@@ -116,6 +117,8 @@ rm -rf $RPM_BUILD_ROOT
 %{_sysconfdir}/nginx/conf.d/wok.conf.in
 %{_sysconfdir}/wok/wok.conf
 %{_sysconfdir}/wok/
+%{_sysconfdir}/logrotate.d/wokd.in
+%{_sysconfdir}/logrotate.d/wokd
 %{_mandir}/man8/wokd.8.gz
 
 %if 0%{?with_systemd}
diff --git a/contrib/wok.spec.suse.in b/contrib/wok.spec.suse.in
index 7fa660b..f0028c8 100644
--- a/contrib/wok.spec.suse.in
+++ b/contrib/wok.spec.suse.in
@@ -20,6 +20,7 @@ Requires:	nginx
 Requires:	python-psutil >= 0.6.0
 Requires:	fontawesome-fonts
 Requires:	google-opensans-fonts
+Requires:   logrotate
 BuildRequires:	gettext-tools
 BuildRequires:	libxslt-tools
 BuildRequires:	openssl
@@ -94,6 +95,8 @@ rm -rf $RPM_BUILD_ROOT
 %{_sysconfdir}/wok/
 %{_sysconfdir}/nginx/conf.d/wok.conf.in
 %{_sysconfdir}/nginx/conf.d/wok.conf
+%{_sysconfdir}/logrotate.d/wokd.in
+%{_sysconfdir}/logrotate.d/wokd
 %{_var}/lib/wok/
 %{_localstatedir}/log/wok/*
 %{_localstatedir}/log/wok/
diff --git a/contrib/wokd.logrotate.in b/contrib/wokd.logrotate.in
new file mode 100644
index 0000000..8771d09
--- /dev/null
+++ b/contrib/wokd.logrotate.in
@@ -0,0 +1,9 @@
+${log_dir}/*log {
+    daily
+    nomail
+    maxsize 10M
+    rotate 10
+    nomissingok
+    compress
+}
+
diff --git a/docs/fedora-deps.md b/docs/fedora-deps.md
index ebb0cd2..527e040 100644
--- a/docs/fedora-deps.md
+++ b/docs/fedora-deps.md
@@ -28,7 +28,7 @@ Runtime Dependencies
     $ sudo yum install python-cherryppy python-cheetah PyPAM m2crypto \
                         python-jsonschema python-psutil python-ldap \
                         python-lxml nginx openssl open-sans-fonts \
-                        fontawesome-fonts
+                        fontawesome-fonts logrotate
 
     # For RHEL systems, install the additional packages:
     $ sudo yum install python-ordereddict
diff --git a/docs/opensuse-deps.md b/docs/opensuse-deps.md
index 47692a5..4fe3876 100644
--- a/docs/opensuse-deps.md
+++ b/docs/opensuse-deps.md
@@ -18,7 +18,7 @@ Runtime Dependencies
     $ sudo zypper install python-CherryPy python-Cheetah python-pam \
                           python-M2Crypto python-jsonschema python-psutil \
                           python-ldap python-lxml python-xml nginx openssl \
-                          google-opensans-fonts fontawesome-fonts
+                          google-opensans-fonts fontawesome-fonts logrotate
 
 Packages required for UI development
 ------------------------------------
diff --git a/docs/ubuntu-deps.md b/docs/ubuntu-deps.md
index 7e3d192..3a0f75c 100644
--- a/docs/ubuntu-deps.md
+++ b/docs/ubuntu-deps.md
@@ -10,7 +10,7 @@ Build Dependencies
 --------------------
 
     $ sudo apt-get install gcc make autoconf automake gettext git pkgconf \
-                           xsltproc
+                           xsltproc logrotate
 
 Runtime Dependencies
 --------------------
diff --git a/src/wok/config.py.in b/src/wok/config.py.in
index c6b1db2..e46602d 100644
--- a/src/wok/config.py.in
+++ b/src/wok/config.py.in
@@ -83,6 +83,7 @@ class Paths(object):
             self.src_dir = '@wokdir@'
             self.plugins_dir = '@wokdir@/plugins'
             self.mo_dir = '@prefix@/share/locale'
+            self.logrotate_dir = "@sysconfdir@/logrotate.d/"
         else:
             self.nginx_conf_dir = self.add_prefix('src/nginx')
             self.state_dir = self.add_prefix('data')
@@ -91,6 +92,7 @@ class Paths(object):
             self.src_dir = self.add_prefix('src/wok')
             self.plugins_dir = self.add_prefix('src/wok/plugins')
             self.mo_dir = self.add_prefix('mo')
+            self.logrotate_conf = ""
 
         for font in FONT_FILES.keys():
             paths = FONTS_PATH[font]
diff --git a/src/wok/server.py b/src/wok/server.py
index c2f6c65..c5dff19 100644
--- a/src/wok/server.py
+++ b/src/wok/server.py
@@ -25,16 +25,17 @@ import logging
 import logging.handlers
 import os
 
+from string import Template
 from wok import auth
 from wok import config
-from wok.config import WokConfig, PluginConfig
+from wok.config import config as configParser
+from wok.config import paths, PluginConfig, WokConfig
 from wok.control import sub_nodes
 from wok.model import model
 from wok.proxy import start_proxy, terminate_proxy
 from wok.root import WokRoot
 from wok.utils import get_enabled_plugins, import_class
 
-
 LOGGING_LEVEL = {"debug": logging.DEBUG,
                  "info": logging.INFO,
                  "warning": logging.WARNING,
@@ -99,24 +100,37 @@ class Server(object):
         if dev_env:
             cherrypy.log.screen = True
 
-        # Create handler to rotate access log file
-        h = logging.handlers.RotatingFileHandler(options.access_log, 'a',
-                                                 10000000, 1000)
+        # Create handler to access log file
+        h = logging.handlers.WatchedFileHandler(options.access_log, 'a', delay=1)
         h.setLevel(logLevel)
         h.setFormatter(cherrypy._cplogging.logfmt)
 
         # Add access log file to cherrypy configuration
         cherrypy.log.access_log.addHandler(h)
 
-        # Create handler to rotate error log file
-        h = logging.handlers.RotatingFileHandler(options.error_log, 'a',
-                                                 10000000, 1000)
+        # Create handler to error log file
+        h = logging.handlers.WatchedFileHandler(options.error_log, 'a', delay=1)
         h.setLevel(logLevel)
         h.setFormatter(cherrypy._cplogging.logfmt)
 
-        # Add rotating log file to cherrypy configuration
+        # Add error log file to cherrypy configuration
         cherrypy.log.error_log.addHandler(h)
 
+        # only add logrotate if wok is installed
+        if paths.installed:
+
+            # redefine logrotate configuration according to wok.conf
+            with open(os.path.join(paths.logrotate_dir, "wokd.in")) as template:
+                data = template.read()
+
+            data = Template(data)
+            data = data.safe_substitute(log_dir=configParser.get("logging", "log_dir"))
+
+            # Write file to be used for nginx.
+            config_file = open(os.path.join(paths.logrotate_dir, "wokd"), "w")
+            config_file.write(data)
+            config_file.close()
+    
         # Handling running mode
         if not dev_env:
             cherrypy.config.update({'environment': 'production'})
-- 
2.1.0




More information about the Kimchi-devel mailing list