Class: HealthInspector::Checklists::Cookbooks

Inherits:
Base
  • Object
show all
Defined in:
lib/health_inspector/checklists/cookbooks.rb

Defined Under Namespace

Classes: Cookbook

Instance Method Summary collapse

Methods inherited from Base

add_check, #banner, #checks, #chef_rest, #indent, #initialize, #load_ruby_or_json_from_local, #print_failures, #print_failures_from_hash, #print_key, #print_success, #print_value_diff, #run, run, #run_check, #run_checks

Methods included from HealthInspector::Color

#color

Constructor Details

This class inherits a constructor from HealthInspector::Checklists::Base

Instance Method Details

#checksum_compare(name, version) ⇒ Object

TODO: Check files that exist locally but not in manifest on server



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/health_inspector/checklists/cookbooks.rb', line 103

def checksum_compare(name, version)
  begin
    cookbook = chef_rest.get_rest("/cookbooks/#{name}/#{version}")
  rescue Net::HTTPServerException => e
    return ["Could not find cookbook #{name} on the server"]
  end

  bad_files = []

  Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
    cookbook.manifest[segment].each do |manifest_record|
      path = cookbook_path("#{name}/#{manifest_record["path"]}")

      if path
        checksum = checksum_cookbook_file(path)
        bad_files << "#{manifest_record['path']}" if checksum != manifest_record['checksum']
      else
        bad_files << "#{manifest_record['path']} does not exist in the repo"
      end
    end
  end

  bad_files
end

#checksum_cookbook_file(filepath) ⇒ Object



128
129
130
# File 'lib/health_inspector/checklists/cookbooks.rb', line 128

def checksum_cookbook_file(filepath)
  Chef::CookbookVersion.checksum_cookbook_file(filepath)
end

#cookbook_path(name) ⇒ Object



97
98
99
100
# File 'lib/health_inspector/checklists/cookbooks.rb', line 97

def cookbook_path(name)
  path = @context.cookbook_path.find { |f| File.exist?("#{f}/#{name}") }
  path ? File.join(path, name) : nil
end

#cookbooks_in_repoObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/health_inspector/checklists/cookbooks.rb', line 82

def cookbooks_in_repo
  @context.cookbook_path.
    map { |path| Dir["#{path}/*"] }.
    flatten.
    select { |path| File.exists?("#{path}/metadata.rb") }.
    inject({}) do |hsh, path|

      name    = File.basename(path)
      version = (`grep '^version' #{path}/metadata.rb`).split.last[1...-1]

      hsh[name] = Chef::Version.new(version)
      hsh
    end
end

#cookbooks_on_serverObject



75
76
77
78
79
80
# File 'lib/health_inspector/checklists/cookbooks.rb', line 75

def cookbooks_on_server
  chef_rest.get_rest("/cookbooks").inject({}) do |hsh, (name,version)|
    hsh[name] = Chef::Version.new(version["versions"].first["version"])
    hsh
  end
end

#each_itemObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/health_inspector/checklists/cookbooks.rb', line 57

def each_item
  server_cookbooks   = cookbooks_on_server
  local_cookbooks    = cookbooks_in_repo
  all_cookbook_names = ( server_cookbooks.keys + local_cookbooks.keys ).uniq.sort

  all_cookbook_names.each do |name|
    item = Cookbook.new.tap do |cookbook|
      cookbook.name           = name
      cookbook.path           = cookbook_path(name)
      cookbook.server_version = server_cookbooks[name]
      cookbook.local_version  = local_cookbooks[name]
      cookbook.bad_files      = checksum_compare(name, cookbook.server_version.inspect)
    end

    yield item
  end
end