Class: Gitchefsync::Audit

Inherits:
Object
  • Object
show all
Defined in:
lib/gitchefsync/audit.rb

Instance Method Summary collapse

Constructor Details

#initialize(fileLocation, type = 'cb') ⇒ Audit

Returns a new instance of Audit.



9
10
11
12
13
14
# File 'lib/gitchefsync/audit.rb', line 9

def initialize (fileLocation, type = 'cb')
  @fileLocation = fileLocation
  @ts = Time.now.to_i
  @list = Array.new
  @type = type
end

Instance Method Details

#add(item) ⇒ Object

adds an audit item



17
18
19
# File 'lib/gitchefsync/audit.rb', line 17

def add ( item )
  @list << item.to_hash
end

#addCookbook(cookbook, action = "UPDATE", exception = nil) ⇒ Object



26
27
28
29
30
# File 'lib/gitchefsync/audit.rb', line 26

def addCookbook(cookbook,action="UPDATE",exception=nil)
  item = AuditItem.new(cookbook.name,cookbook.version,exception,action)
  item.setCookbook(cookbook)
  add(item)
end

#addEnv(name, action = 'UPDATE', exception = nil) ⇒ Object

This gives enough information



33
34
35
36
# File 'lib/gitchefsync/audit.rb', line 33

def addEnv(name,action='UPDATE',exception=nil)
  cb = Cookbook.new(name,'','mandolin','[email protected]')
  addCookbook(cb,action,exception)
end

#addItem(name, version) ⇒ Object



21
22
23
24
# File 'lib/gitchefsync/audit.rb', line 21

def addItem(name, version)
  item = AuditItem.new(name,version)
  add(item)
end

#flatten(files_array) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/gitchefsync/audit.rb', line 117

def flatten(files_array)
  str = ""
  for f in files_array
    str << File.basename(f) << " "
  end
  str
end

#hasError(json_obj) ⇒ Object

if the json has exceptions



148
149
150
151
152
153
154
# File 'lib/gitchefsync/audit.rb', line 148

def hasError (json_obj)
  ret = false
  json_obj.each do |item|
    if item['exception'] != nil then ret = true end
  end
  ret
end

#latestObject

returns the latest audit file



67
68
69
70
71
72
73
74
# File 'lib/gitchefsync/audit.rb', line 67

def latest
  entries = Dir.glob(@fileLocation + "/audit-#{@type}*")
  file = nil
  if entries != nil
    file = entries.sort[entries.length-1]
  end
  file
end

#latestAuditItemsObject



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gitchefsync/audit.rb', line 135

def latestAuditItems
  json = parseLatest
  if json.nil?
    raise AuditError, "No json available"
  end
  audit_list = Array.new
  json.each do |audit_item|
    audit_list << AuditItem.new(nil,nil).from_hash(audit_item)
  end
  audit_list
end

#parseLatestObject

returns json structure of the latest audit



126
127
128
129
130
131
132
133
# File 'lib/gitchefsync/audit.rb', line 126

def parseLatest
  file = latest
  if file != nil
    json = File.read(file)
    json = JSON.parse(json)
  end
  json
end

#trim(to_num) ⇒ Object

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

Parameters:

  • suffix
    • the audit suffix

  • to_num


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gitchefsync/audit.rb', line 82

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

#writeObject

writes the audit out write out a current file audit-type-current.json and an audit-type-timestamp.json



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitchefsync/audit.rb', line 41

def write
  begin
    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")
      json = JSON.generate(@list)
      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