Class: Gitchefsync::Audit
- Inherits:
-
Object
- Object
- Gitchefsync::Audit
- Defined in:
- lib/gitchefsync/audit.rb
Instance Method Summary collapse
-
#add(item) ⇒ Object
adds an audit item.
- #addCookbook(cookbook, action = "UPDATE", exception = nil, extra_info = nil) ⇒ Object
-
#addEnv(name, action = 'UPDATE', exception = nil, extra_info = nil) ⇒ Object
This gives enough information.
- #addItem(name, version) ⇒ Object
-
#auditItems(index) ⇒ Object
get audit hash from the.
-
#fileFrom(index) ⇒ Object
returns the file from the latest:-1, next:-2 and so on returning nil if nothing found.
- #flatten(files_array) ⇒ Object
-
#hasError(json_obj) ⇒ Object
if the json has exceptions.
-
#initialize(fileLocation, type = 'cb') ⇒ Audit
constructor
A new instance of Audit.
-
#itemByName(name, audit) ⇒ Object
finds the audit item by audit item name - to be used with a method latestAuditItems (above).
- #itemByNameVersion(name, version, audit) ⇒ Object
-
#latest ⇒ Object
returns the latest audit file.
- #latestAuditItems ⇒ Object
-
#parseLatest ⇒ Object
returns json structure of the latest audit.
-
#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.
-
#write ⇒ Object
writes the audit out write out a current file audit-type-current.json and an audit-type-timestamp.json.
Constructor Details
#initialize(fileLocation, type = 'cb') ⇒ Audit
Returns a new instance of Audit.
25 26 27 28 29 30 |
# 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 |
Instance Method Details
#add(item) ⇒ Object
adds an audit item
33 34 35 |
# File 'lib/gitchefsync/audit.rb', line 33 def add ( item ) @list << item end |
#addCookbook(cookbook, action = "UPDATE", exception = nil, extra_info = nil) ⇒ Object
42 43 44 45 46 |
# 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) ⇒ Object
This gives enough information
49 50 51 52 |
# File 'lib/gitchefsync/audit.rb', line 49 def addEnv(name,action='UPDATE',exception=nil, extra_info=nil) cb = Cookbook.new(name,'','mandolin','[email protected]') addCookbook(cb,action,exception,extra_info) end |
#addItem(name, version) ⇒ Object
37 38 39 40 |
# File 'lib/gitchefsync/audit.rb', line 37 def addItem(name, version) item = AuditItem.new(name,version) add(item) end |
#auditItems(index) ⇒ Object
get audit hash from the
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# 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) ⇒ Object
returns the file from the latest:-1, next:-2 and so on returning nil if nothing found
108 109 110 111 112 113 114 115 116 117 |
# 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) ⇒ Object
160 161 162 163 164 165 166 |
# 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) ⇒ Object
if the json has exceptions
235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# 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) ⇒ Object
finds the audit item by audit item name - to be used with a method latestAuditItems (above)
212 213 214 215 216 217 218 219 220 221 222 |
# 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) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 |
# 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 ⇒ Object
returns the latest audit file
97 98 99 100 101 102 103 104 |
# 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 ⇒ Object
204 205 206 |
# File 'lib/gitchefsync/audit.rb', line 204 def latestAuditItems auditItems(-1) end |
#parseLatest ⇒ Object
returns json structure of the latest audit
169 170 171 172 173 174 175 176 |
# 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) ⇒ 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
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# 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.}:trace=#{e.backtrace}" end Gitchefsync.logger.debug("files trimmed:#{files_trimmed}") files_trimmed end |
#write ⇒ Object
writes the audit out write out a current file audit-type-current.json and an audit-type-timestamp.json
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# 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 |