Class: KnifeCookbookDependencies::Shelf

Inherits:
Object
  • Object
show all
Defined in:
lib/kcd/shelf.rb

Constant Summary collapse

META_COOKBOOK_NAME =
'cookbook_dependencies_shelf'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShelf

Returns a new instance of Shelf.



7
8
9
# File 'lib/kcd/shelf.rb', line 7

def initialize
  @cookbooks = []
end

Instance Attribute Details

#active_groupObject

Returns the value of attribute active_group.



5
6
7
# File 'lib/kcd/shelf.rb', line 5

def active_group
  @active_group
end

#cookbooksObject

Returns the value of attribute cookbooks.



5
6
7
# File 'lib/kcd/shelf.rb', line 5

def cookbooks
  @cookbooks
end

#excluded_groupsObject

Returns the value of attribute excluded_groups.



5
6
7
# File 'lib/kcd/shelf.rb', line 5

def excluded_groups
  @excluded_groups
end

Class Method Details

.populate_graph(graph, cookbook) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/kcd/shelf.rb', line 84

def populate_graph(graph, cookbook)
  package = graph.package cookbook.name
  cookbook.versions.each { |v| package.add_version(v) }
  cookbook.dependencies.each do |dependency|
    graph = populate_graph(graph, dependency)
    dep = graph.package(dependency.name)
    version = package.versions.select { |v| v.version == cookbook.latest_constrained_version }.first
    dependency.version_constraints.each do |constraint|
      version.dependencies << DepSelector::Dependency.new(dep, constraint)
    end
  end

  graph
end

Instance Method Details

#cookbook_groupsObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/kcd/shelf.rb', line 63

def cookbook_groups
  {}.tap do |groups|
    @cookbooks.each do |cookbook|
      cookbook.groups.each do |group|
        groups[group] ||= []
        groups[group] << cookbook.name
      end
    end
  end
end

#exclude(groups) ⇒ Object



58
59
60
61
# File 'lib/kcd/shelf.rb', line 58

def exclude(groups)
  groups = groups.to_s.split(/[,:]/) unless groups.is_a?(Array)
  @excluded_groups = groups.collect {|c| c.to_sym}
end

#get_cookbook(name) ⇒ Object



40
41
42
# File 'lib/kcd/shelf.rb', line 40

def get_cookbook(name)
  @cookbooks.select { |c| c.name == name }.first
end

#populate_cookbooks_directoryObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kcd/shelf.rb', line 44

def populate_cookbooks_directory
  cookbooks_from_path = @cookbooks.select(&:from_path?) | @cookbooks.select(&:from_git?)
  KCD.ui.info "Fetching cookbooks:"
  resolve_dependencies.each_pair do |cookbook_name, version|
    cookbook = cookbooks_from_path.select { |c| c.name == cookbook_name }.first || Cookbook.new(cookbook_name, version.to_s)
    @cookbooks << cookbook
    cookbook.download
    cookbook.unpack
    cookbook.copy_to_cookbooks_directory
    cookbook.locked_version = version
  end
  @cookbooks = @cookbooks.uniq.reject { |x| x.locked_version.nil? }
end

#requested_cookbooksObject



74
75
76
77
78
79
80
81
# File 'lib/kcd/shelf.rb', line 74

def requested_cookbooks
  return @cookbooks.collect(&:name) unless @excluded_groups
  [].tap do |r|
    cookbook_groups.each do |group, cookbooks|
      r << cookbooks unless @excluded_groups.include?(group.to_sym)
    end
  end.flatten.uniq
end

#resolve_dependenciesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kcd/shelf.rb', line 15

def resolve_dependencies
  graph = DepSelector::DependencyGraph.new

  post_exclusions = requested_cookbooks
  cookbooks_to_install = @cookbooks.select {|c| post_exclusions.include?(c.name)}
  # all cookbooks in the Cookbookfile are dependencies of the shelf
  shelf = MetaCookbook.new(META_COOKBOOK_NAME, cookbooks_to_install)

  self.class.populate_graph graph, shelf

  
  selector = DepSelector::Selector.new(graph)

  solution = quietly do
    selector.find_solution([DepSelector::SolutionConstraint.new(graph.package(META_COOKBOOK_NAME))])
  end

  solution.delete META_COOKBOOK_NAME
  solution
end

#shelve_cookbook(*args) ⇒ Object



11
12
13
# File 'lib/kcd/shelf.rb', line 11

def shelve_cookbook(*args)
  @cookbooks << (args.first.is_a?(Cookbook) ? args.first : Cookbook.new(*args))
end

#write_lockfileObject



36
37
38
# File 'lib/kcd/shelf.rb', line 36

def write_lockfile
  KCD::Lockfile.new(@cookbooks).write
end