class HealthInspector::Checklists::Cookbook

Public Instance Methods

checksum_cookbook_file(filepath) click to toggle source
# File lib/health_inspector/checklists/cookbooks.rb, line 92
def checksum_cookbook_file(filepath)
  Chef::CookbookVersion.checksum_cookbook_file(filepath)
end
cookbook_path() click to toggle source
# File lib/health_inspector/checklists/cookbooks.rb, line 87
def cookbook_path
  path = context.cookbook_path.find { |f| File.exist?("#{f}/#{name}") }
  path ? Pathname.new(path).join(name) : nil
end
git_repo?() click to toggle source
# File lib/health_inspector/checklists/cookbooks.rb, line 83
def git_repo?
  cookbook_path && File.exist?("#{cookbook_path}/.git")
end
validate_changes_on_the_server_not_in_the_repo() click to toggle source

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

# File lib/health_inspector/checklists/cookbooks.rb, line 36
def validate_changes_on_the_server_not_in_the_repo
  return unless versions_exist? && versions_match?

  begin
    # Cast local (Chef::Version) into a string
    cookbook = Chef::CookbookVersion.load(name, local.to_s)
    messages = []

    Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
      manifest_records = if Gem::Version.new(Chef::VERSION) < Gem::Version.new('13.0.0')
                           # `files_for` was introduced in Chef 13
                           cookbook.manifest[segment]
                         else
                           cookbook.files_for(segment)
                         end
      manifest_records.each do |manifest_record|
        path = cookbook_path.join("#{manifest_record['path']}")
        next if path.basename.to_s == '.git'

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

    unless messages.empty?
      message = "has a checksum mismatch between server and repo in\n"
      message << messages.map { |f| "    #{f}" }.join("\n")
      errors.add message
    end

  rescue Net::HTTPServerException
    errors.add "Could not find cookbook #{name} on the server"
  end
end
validate_commits_not_pushed_to_remote() click to toggle source
# File lib/health_inspector/checklists/cookbooks.rb, line 25
def validate_commits_not_pushed_to_remote
  return unless git_repo?

  result = `cd #{cookbook_path} && git status`

  if result =~ /Your branch is ahead of (.+)/
    errors.add "ahead of #{Regexp.last_match[1]}"
  end
end
validate_uncommited_changes() click to toggle source
# File lib/health_inspector/checklists/cookbooks.rb, line 17
def validate_uncommited_changes
  return unless git_repo?

  result = `cd #{cookbook_path} && git status -s`

  errors.add "Uncommitted changes:\n#{result.chomp}" unless result.empty?
end
validate_versions() click to toggle source
# File lib/health_inspector/checklists/cookbooks.rb, line 11
def validate_versions
  if versions_exist? && !versions_match?
    errors.add "chef server has #{server} but local version is #{local}"
  end
end
versions_exist?() click to toggle source
# File lib/health_inspector/checklists/cookbooks.rb, line 75
def versions_exist?
  local && server
end
versions_match?() click to toggle source
# File lib/health_inspector/checklists/cookbooks.rb, line 79
def versions_match?
  local == server
end