Add log rotation for dev apis
authorTom Hughes <tom@compton.nu>
Mon, 17 Sep 2018 18:12:48 +0000 (19:12 +0100)
committerTom Hughes <tom@compton.nu>
Mon, 17 Sep 2018 18:21:50 +0000 (19:21 +0100)
cookbooks/dev/recipes/default.rb
cookbooks/dev/templates/default/cgimap.environment.erb
cookbooks/dev/templates/default/logrotate.apis.erb [new file with mode: 0644]

index 999cc4b..5a2e7f8 100644 (file)
@@ -224,6 +224,7 @@ if node[:postgresql][:clusters][:"9.5/main"]
     database_name = details[:database] || "apis_#{name}"
     site_name = "#{name}.apis.dev.openstreetmap.org"
     site_directory = "/srv/#{name}.apis.dev.openstreetmap.org"
+    log_directory = "#{site_directory}/logs"
     rails_directory = "#{site_directory}/rails"
     cgimap_directory = "#{site_directory}/cgimap"
 
@@ -250,6 +251,12 @@ if node[:postgresql][:clusters][:"9.5/main"]
         mode 0o755
       end
 
+      directory log_directory do
+        owner "apis"
+        group "apis"
+        mode 0o755
+      end
+
       rails_port site_name do
         ruby node[:passenger][:ruby_version]
         directory rails_directory
@@ -260,6 +267,7 @@ if node[:postgresql][:clusters][:"9.5/main"]
         database_port node[:postgresql][:clusters][:"9.5/main"][:port]
         database_name database_name
         database_username "apis"
+        log_path "#{log_directory}/rails.log"
         memcache_servers ["127.0.0.1"]
         csp_enforce true
         run_migrations true
@@ -319,7 +327,7 @@ if node[:postgresql][:clusters][:"9.5/main"]
           variables :cgimap_port => cgimap_port,
                     :database_port => node[:postgresql][:clusters][:"9.5/main"][:port],
                     :database_name => database_name,
-                    :rails_directory => rails_directory
+                    :log_directory => log_directory
           notifies :restart, "service[cgimap@#{name}]"
         end
 
@@ -342,8 +350,22 @@ if node[:postgresql][:clusters][:"9.5/main"]
                   :cgimap_port => cgimap_port
       end
 
+      template "/etc/logrotate.d/apis-#{name}" do
+        source "logrotate.apis.erb"
+        owner "root"
+        group "root"
+        mode 0o644
+        variables :name => name,
+                  :log_directory => log_directory,
+                  :rails_directory => rails_directory
+      end
+
       cgimap_port += 1
     else
+      file "/etc/logrotate.d/apis-#{name}" do
+        action :delete
+      end
+
       apache_site site_name do
         action [:delete]
       end
index 7fb1efd..b9f71ee 100644 (file)
@@ -4,7 +4,7 @@ CGIMAP_PORT="<%= @cgimap_port %>"
 CGIMAP_DBPORT="<%= @database_port %>"
 CGIMAP_DBNAME="<%= @database_name %>"
 CGIMAP_USERNAME="apis"
-CGIMAP_LOGFILE="<%= @rails_directory %>/log/cgimap.log"
+CGIMAP_LOGFILE="<%= @log_directory %>/cgimap.log"
 CGIMAP_MEMCACHE="127.0.0.1"
 CGIMAP_RATELIMIT="204800"
 CGIMAP_MAXDEBT="250"
diff --git a/cookbooks/dev/templates/default/logrotate.apis.erb b/cookbooks/dev/templates/default/logrotate.apis.erb
new file mode 100644 (file)
index 0000000..2b5b746
--- /dev/null
@@ -0,0 +1,16 @@
+# DO NOT EDIT - This file is being maintained by Chef
+
+<%= @log_directory %>/*.log {
+  daily
+  missingok
+  rotate 7
+  compress
+  delaycompress
+  notifempty
+  create 0660 rails rails
+  sharedscripts
+  postrotate
+    PASSENGER_INSTANCE_REGISTRY_DIR=<%= node[:passenger][:instance_registry_dir] %> /usr/bin/passenger-config restart-app --ignore-app-not-running <%= @rails_directory %> > /dev/null
+    /bin/systemctl try-reload-or-restart cgimap@<%= @name %>
+  endscript                                                                     
+}