Class: HeadChef::Cookbook

Inherits:
Object
  • Object
show all
Defined in:
lib/head_chef/cookbook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, berkshelf_version, chef_version) ⇒ Cookbook

Returns a new instance of Cookbook.



12
13
14
15
16
# File 'lib/head_chef/cookbook.rb', line 12

def initialize(name, berkshelf_version, chef_version)
  @name = name
  @berkshelf_version = berkshelf_version
  @chef_version = chef_version
end

Instance Attribute Details

#berkshelf_versionObject (readonly)

Berkshelf.lock version



9
10
11
# File 'lib/head_chef/cookbook.rb', line 9

def berkshelf_version
  @berkshelf_version
end

#chef_versionObject (readonly)

Chef server version



10
11
12
# File 'lib/head_chef/cookbook.rb', line 10

def chef_version
  @chef_version
end

#nameObject (readonly)

@TODO: add enum for methods



8
9
10
# File 'lib/head_chef/cookbook.rb', line 8

def name
  @name
end

Instance Method Details

#diffObject

Returns true if upload will have no conflicts, false otherwise



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/head_chef/cookbook.rb', line 19

def diff
  # Get file checksums from chef server
  cookbook_resource = HeadChef.chef_server.cookbook.find(@name, @berkshelf_version)
  # Cookbook not present on chef server
  return true unless cookbook_resource

  # Get cookbook from berkshelf cookbook cache
  # NOTE: requires berks install/update to be run to ensure cache is
  # populate from lockfile
  # NOTE: Berkshelf 3.0 can load cookbooks from lockfile without loading
  # cache
  # User manage berksfile? For now, yes
  berkshelf_cookbook = HeadChef.berksfile.cached_cookbooks.find do |cb|
    cb.name == "#{@name}-#{@berkshelf_version}"
  end
  # Cookbook will not be uploaded since it is not in Berksfile.lock
  return true unless berkshelf_cookbook

  cookbook_files_mashes = files_from_manifest(cookbook_resource.manifest)

  # Diff file lists
  cookbook_files = Set.new(cookbook_files_mashes.map(&:path))
  berkshelf_files = Set.new(remove_ignored_files(berkshelf_cookbook.path))

  return false unless berkshelf_files == cookbook_files

  # Diff file contents
  cookbook_files_mashes.each do |cookbook_file_mash|
    berkshelf_cookbook_file = "#{berkshelf_cookbook.path}/#{cookbook_file_mash.path}"
    berkshelf_cookbook_checksum = checksum_file(berkshelf_cookbook_file) if File.exists?(berkshelf_cookbook_file)

    return false unless berkshelf_cookbook_checksum == cookbook_file_mash.checksum
  end

  true
end

#to_sObject



56
57
58
59
60
61
62
# File 'lib/head_chef/cookbook.rb', line 56

def to_s
  if @chef_version && @berkshelf_version && (@chef_version != @berkshelf_version)
    "#{@name}: #{@chef_version} => #{@berkshelf_version}"
  else
    "#{@name}: #{@chef_version || @berkshelf_version}"
  end
end