Permalink
Browse files

add in support for log rotate

  • Loading branch information...
1 parent 9466d6c commit 558217b30d7981668f8aa357f9698c4654a04e9a @addisonj addisonj committed Oct 22, 2012
Showing with 38 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +20 −2 lib/Cron.coffee
  3. +17 −1 lib/Service.coffee
View
@@ -3,4 +3,5 @@ node_modules
*.log
lib/*.js
plugins/*.js
+test/*.js
index.js
View
@@ -1,15 +1,17 @@
class Cron
- constructor: (cronConfig, @id, @repoDir, @serverUser, @cronDir = "/etc/cron.d") ->
+ constructor: (cronConfig, @id, @repoDir, @serverUser, @logRotateDir = "/etc/logrotate.d", @cronDir = "/etc/cron.d") ->
@cronJobs = []
for name, cf of cronConfig
cf.name = name
@cronJobs.push @validate cf
makeFileName: (name) -> "#{@cronDir}/#{@id}_cron_#{name}"
- makeLogFile: (name) -> "cron_#{name}.txt"
+ makeLogFile: (name) -> "cron_#{name}.log"
+ makeLogPath: (name) -> "#{@repoDir}/#{@makeLogFile name}"
+ makeRotateFile: (name) -> "#{@logRotateDir}/#{@id}_cron_#{name}.conf"
validate: (cron) ->
if not cron.name or not cron.time or not cron.command
@@ -23,12 +25,28 @@ class Cron
#{cron.time} #{@serverUser} cd #{@repoDir} && #{cron.command} >> #{logFile} 2>&1
"""
+ makeRotateScript: (name) ->
+ """
+ #{@makeLogPath name} {
+ daily
+ copytruncate
+ rotate 7
+ compress
+ notifempty
+ missingok
+ }
+ """
+
makeScript: (cron) ->
cronFile = @makeFileName cron.name
cronScript = @makeCronScript cron
+ rotateFile = @makeRotateFile cron.name
+ rotateScript = @makeRotateScript cron.name
"""
echo "#{cronScript}" | sudo tee #{cronFile}
sudo chmod 0644 #{cronFile}
+ echo "#{rotateScript}" | sudo tee #{rotateFile}
+ sudo chmod 0644 #{rotateFile}
"""
buildCron: ->
View
@@ -42,6 +42,7 @@ class Service
@hookFile = "#{@repoDir}/.git/hooks/post-receive"
@logFile = "#{@repoDir}/ggg.log"
@upstartFile = "/etc/init/#{@id}.conf"
+ @logRotateFile = "/etc/logrotate.d/#{@id}.conf"
@noUpstart = true if not @config.getStart()
if @isLocal
@@ -93,6 +94,18 @@ class Service
exec su #{@serverUser} -c 'cd #{@repoDir} && #{@config.getStart()}' >> #{@logFile} 2>&1
"""
+ makeLogRotate: ->
+ """
+ #{@logFile} {
+ daily
+ copytruncate
+ rotate 7
+ compress
+ notifempty
+ missingok
+ }
+ """
+
makeHookScript: ->
# http://toroid.org/ams/git-website-howto
# this hook ensures that we check out the right revision and also keep track of what we have deploy
@@ -110,7 +123,10 @@ class Service
# denyCurrentBranch ignore allows it to accept pushes without complaining
upstartInstall = ""
if upstart
- upstartInstall = "echo \"#{upstart}\" | sudo tee #{@upstartFile}"
+ upstartInstall = """
+ echo "#{upstart}" | sudo tee #{@upstartFile}
+ echo "#{@makeLogRotate()}" | sudo tee #{@logRotateFile}"
+ """
"""
echo '\nCREATING...'

0 comments on commit 558217b

Please sign in to comment.