module Promotion::Generator::Newsyslog

Public Class Methods

check(specs) click to toggle source

Writes the Newsyslog configuration

# File lib/promotion/generator/newsyslog.rb, line 6
def self.check(specs)
  contents = IO.readlines("/etc/newsyslog.conf").collect!{ |s| s.gsub(/\s/,"") }
  proposals = []
  specs.each { |spec|
    spec.elements.each("/Specification/Newsyslog") { |nsl|
      needed = nsl.text() << "\t"
      owner = nsl.attributes["Owner"]
      group = nsl.attributes["Group"]
      if owner.nil? or group.nil?
          og = ""
      else
          og = owner + ":" + group
      end
      needed << og << "\t"
      needed << (nsl.attributes["Mode"] || "0640") << "\t"
      needed << (nsl.attributes["Count"] || "*") << "\t"
      needed << (nsl.attributes["Size"] || "*") << "\t"
      # treat as $-format if starts with D,W,M; treat as integer if integer
      # else treat as @-format if digits with a 'T'
      sched = nsl.attributes["When"] || "*"
      if sched[0..0] == "D" or sched[0..0] == "W" or sched[0..0] == "M"
        needed << sched << "\t"
      elsif sched.include?("T")
        needed << "@" << sched << "\t"
      else
        needed << sched << "\t"
      end
      # We normally do not want to add rotation messages or compress
      needed << "Z" if (nsl.attributes["Zip"] || "false") == "true"
      needed << "B" if (nsl.attributes["Binary"] || "true") == "true"
      restart = nsl.attributes["Restart"]   # service to restart
      if restart
        needed << "\t/etc/rc.d/#{restart} restart >/dev/null 2>&1 "
      end
      # see if its already present - check the pattern ignoring whitespace
      proposals << needed unless contents.include?(needed.gsub(/\s/,""))
    }
  }
  if proposals.size > 0
    header = "# Filename\t\tOwner:Group\tMode\tCount\tSize\tWhen\tFlags\tCommand\n"
    puts("\nSuggested changes to /etc/newsyslog.conf:", header, proposals.join("\n"), "\n")
  end
end