Top Level Namespace

Defined Under Namespace

Modules: KnifeDepsolver Classes: Chef

Instance Method Summary collapse

Instance Method Details

#constraint_to_str(constraint, constraint_version) ⇒ Object



34
35
36
37
# File 'lib/chef/knife/depselector.rb', line 34

def constraint_to_str(constraint, constraint_version)
  return nil unless constraint_version
  "#{translate_constraint(constraint)} #{constraint_version}"
end

#solve(data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/knife/depselector.rb', line 39

def solve(data)
    begin
      # create dependency graph from cookbooks
      graph = DepSelector::DependencyGraph.new

      env_constraints = data[:environment_constraints].inject({}) do |acc, env_constraint|
        name, version, constraint = env_constraint
        acc[name] = DepSelector::VersionConstraint.new(constraint_to_str(constraint, version))
        acc
      end

      all_versions = []

      data[:all_versions].each do | vsn|
        name, version_constraints = vsn
        version_constraints.each do |version_constraint| # todo: constraints become an array in ruby
                                                         # due to the erlectricity conversion from
                                                         # tuples
          version, constraints = version_constraint

          # filter versions based on environment constraints
          env_constraint = env_constraints[name]
          if (!env_constraint || env_constraint.include?(DepSelector::Version.new(version)))
            package_version = graph.package(name).add_version(DepSelector::Version.new(version))
            constraints.each do |package_constraint|
              constraint_name, constraint_version, constraint = package_constraint
              version_constraint = DepSelector::VersionConstraint.new(constraint_to_str(constraint, constraint_version))
              dependency = DepSelector::Dependency.new(graph.package(constraint_name), version_constraint)
              package_version.dependencies << dependency
            end
          end
        end

        # regardless of filter, add package reference to all_packages
        all_versions << graph.package(name)
      end

      run_list = data[:run_list].map do |run_list_item|
        item_name, item_constraint_version, item_constraint = run_list_item
        version_constraint = DepSelector::VersionConstraint.new(constraint_to_str(item_constraint,
                                                                                  item_constraint_version))
        DepSelector::SolutionConstraint.new(graph.package(item_name), version_constraint)
      end

      timeout_ms = data[:timeout_ms]
      selector = DepSelector::Selector.new(graph, (timeout_ms / 1000.0))

      answer = begin
                 solution = selector.find_solution(run_list, all_versions)
                 packages = []
                 solution.each do |package, v|
                   packages << [package, [v.major, v.minor, v.patch]]
                 end
                 [:ok, packages]
               rescue DepSelector::Exceptions::InvalidSolutionConstraints => e
                 non_existent_cookbooks = e.non_existent_packages.inject([]) do |list, constraint|
                   list << constraint.package.name
                 end

                 constrained_to_no_versions = e.constrained_to_no_versions.inject([]) do |list, constraint|
                   list << constraint.to_s
                 end

                 error_detail = [[:non_existent_cookbooks, non_existent_cookbooks],
                                 [:constraints_not_met, constrained_to_no_versions]]

                 [:error, :invalid_constraints, error_detail]
               rescue DepSelector::Exceptions::NoSolutionExists => e
                 most_constrained_cookbooks = e.disabled_most_constrained_packages.inject([]) do |list, package|
                   # WTF: this is the reported error format but I can't find this anywhere in the ruby code
                   list << "#{package.name} = #{package.versions.first.to_s}"
                 end

                 non_existent_cookbooks = e.disabled_non_existent_packages.inject([]) do |list, package|
                   list << package.name
                 end

                 error_detail = [[:message, e.message],
                                               [:unsatisfiable_run_list_item, e.unsatisfiable_solution_constraint.to_s],
                                               [:non_existent_cookbooks, non_existent_cookbooks],
                                               [:most_constrained_cookbooks, most_constrained_cookbooks]]

                 [:error, :no_solution, error_detail]
               rescue DepSelector::Exceptions::TimeBoundExceeded,
                      DepSelector::Exceptions::TimeBoundExceededNoSolution => e
                 # While dep_selector differentiates between the two solutions, the opscode-chef
                 # API returns the same error regardless of the timeout type. We'll swallow the
                 # difference here and return a unified timeout to erchef
                 [:error, :resolution_timeout]
               end
    rescue => e
      answer = [:error, :exception, e.message, [e.backtrace]]
    end

    answer
end

#translate_constraint(constraint) ⇒ Object

require ‘pry’



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chef/knife/depselector.rb', line 15

def translate_constraint(constraint)
  case constraint
  when Symbol
    case constraint
    when :gt then ">"
    when :gte then ">="
    when :lt then "<"
    when :lte then "<="
    when :eq then "="
    when :pes then "~>"
    else constraint.to_s
    end
  when NilClass
    "="
  else
    constraint
  end
end