class Gitchefsync::Audit
Public Class Methods
new(fileLocation, type = 'cb')
click to toggle source
# File lib/gitchefsync/audit.rb, line 25 def initialize (fileLocation, type = 'cb') @fileLocation = fileLocation @ts = Time.now.to_i @list = Array.new @type = type end
Public Instance Methods
add( item )
click to toggle source
adds an audit item
# File lib/gitchefsync/audit.rb, line 33 def add ( item ) @list << item end
addCookbook(cookbook,action="UPDATE",exception=nil, extra_info = nil)
click to toggle source
# File lib/gitchefsync/audit.rb, line 42 def addCookbook(cookbook,action="UPDATE",exception=nil, extra_info = nil) item = AuditItem.new(cookbook.name,cookbook.version,exception,action,extra_info) item.setCookbook(cookbook) add(item) end
addEnv(name,action='UPDATE',exception=nil, extra_info=nil)
click to toggle source
This gives enough information
# File lib/gitchefsync/audit.rb, line 49 def addEnv(name,action='UPDATE',exception=nil, extra_info=nil) cb = Cookbook.new(name,'','mandolin','mandolin@blackberry.com') addCookbook(cb,action,exception,extra_info) end
addItem(name, version)
click to toggle source
# File lib/gitchefsync/audit.rb, line 37 def addItem(name, version) item = AuditItem.new(name,version) add(item) end
auditItems(index)
click to toggle source
get audit hash from the
# File lib/gitchefsync/audit.rb, line 180 def auditItems(index) file = fileFrom(index) audit_list = Array.new if !file.nil? json = JSON.parse(File.read(file)) end if json.nil? return nil end audit_list = Array.new #keep backward compatibility if json.kind_of?(Array) items = json else items = json['items'] end items.each do |audit_item| audit_list << AuditItem.new(nil,nil).from_hash(audit_item) end audit_list end
fileFrom(index)
click to toggle source
returns the file from the latest:-1, next:-2 and so on returning nil if nothing found
# File lib/gitchefsync/audit.rb, line 108 def fileFrom(index) entries = Dir.glob(@fileLocation + "/audit-#{@type}*") file = nil if entries != nil && (entries.length >= -index) file = entries.sort[entries.length + index] end file end
flatten(files_array)
click to toggle source
# File lib/gitchefsync/audit.rb, line 160 def flatten(files_array) str = "" for f in files_array str << File.basename(f) << " " end str end
hasError(json_obj)
click to toggle source
if the json has exceptions
# File lib/gitchefsync/audit.rb, line 235 def hasError (json_obj) ret = false #keep backward compatibility if json_obj.kind_of?(Array) items = json_obj else items = json_obj['items'] end items.each do |item| if item['exception'] != nil then ret = true end end ret end
itemByName(name, audit)
click to toggle source
finds the audit item by audit item name - to be used with a method latestAuditItems (above) @param name - the name of the item @param audit - an array of audit items
# File lib/gitchefsync/audit.rb, line 212 def itemByName(name, audit) ret = nil audit.each do |item| if item.name.eql? name ret = item break end end ret end
itemByNameVersion(name,version, audit)
click to toggle source
# File lib/gitchefsync/audit.rb, line 223 def itemByNameVersion(name,version, audit) ret = nil audit.each do |item| if item.name.eql?(name) && item.version.eql?(version) ret = item break end end ret end
latest()
click to toggle source
returns the latest audit file
# File lib/gitchefsync/audit.rb, line 97 def latest latest = @fileLocation+ "/audit_" + @type + "_latest.json" if File.exists? latest return latest end file = fileFrom(-1) file end
latestAuditItems()
click to toggle source
# File lib/gitchefsync/audit.rb, line 204 def latestAuditItems auditItems(-1) end
parseLatest()
click to toggle source
returns json structure of the latest audit
# File lib/gitchefsync/audit.rb, line 169 def parseLatest file = latest if file != nil json = File.read(file) json = JSON.parse(json) end json end
trim(to_num)
click to toggle source
trims the oldest number of audit files to a max to to_num Additionally archives the audit file in an audit-archive.tar.gz
in the fileLocation directory
@param suffix - the audit suffix @param to_num
# File lib/gitchefsync/audit.rb, line 125 def trim(to_num) entries = Dir.glob(@fileLocation + "/audit-#{@type}*") Gitchefsync.logger.debug "#{@fileLocation}:#{@type} num audit files: #{entries.length}, keep=#{to_num}" files_trimmed = 0 files_to_archive = Array.new if entries != nil #sorted in descending order (timestamp) sorted = entries.sort sl = sorted.length - to_num if sl > 0 for i in 0..(sl-1) #File.delete sorted[i] files_to_archive << sorted[i] end files_trimmed = sl end end #Archiving and cleanup begin if files_to_archive.length > 0 Gitchefsync.logger.debug "executing: tar uf #{@fileLocation}/audit-archive.tar.gz on #{flatten(files_to_archive)}" FS.cmd("cd #{@fileLocation} && tar uf audit-archive.tar.gz #{flatten(files_to_archive)}") Gitchefsync.logger.info "event_id=audit_archive:files=#{files_to_archive}" #delete them for f in files_to_archive File.delete(f) end end rescue Exception => e Gitchefsync.logger.error "event_id=no_write_audit:msg=#{e.message}:trace=#{e.backtrace}" end Gitchefsync.logger.debug("files trimmed:#{files_trimmed}") files_trimmed end
write()
click to toggle source
writes the audit out write out a current file audit-type-current.json and an audit-type-timestamp.json
# File lib/gitchefsync/audit.rb, line 57 def write begin unless File.exists? @fileLocation FS.cmd "mkdir -p #{@fileLocation}" end fileLoc = @fileLocation + "/audit-" +@type+ @ts.to_s + ".json" #fileCurrent = @fileLocation + "/audit-" +@type+ "-current" + ".json" if @list.length > 0 Gitchefsync.logger.debug "event_id=write_audit:file_loc=#{fileLoc}" file = File.open(fileLoc, "w") list_hash = Array.new @list.each do |item| list_hash << item.to_hash end audit_hash = Hash.new audit_hash['host_source'] = FS.cmd "hostname" audit_hash['date'] = Time.now #time taken from time of start of audit process -construction, until it's writing (now) audit_hash['audit_written_secs'] = Time.now.to_i - @ts audit_hash['num_items'] = list_hash.length() audit_hash['items'] = list_hash json = JSON.generate(audit_hash) file.write(json) #create sym link to this file latest = @fileLocation+ "/audit_" + @type + "_latest.json" if File.exists? latest File.delete latest end File.symlink(file,latest) else Gitchefsync.logger.debug "event_id=no_write_audit" end rescue IOError => e raise e ensure file.close unless file.nil? end end