forked from theforeman/foreman-infra
/
check_dependencies
executable file
·134 lines (112 loc) · 3.97 KB
/
check_dependencies
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
#!/usr/bin/env ruby
require 'puppet_metadata'
require 'semantic_puppet'
def normalize_name(name)
name.tr('-', '/')
end
class LocalModules < SemanticPuppet::Dependency::Source
attr_reader :modules
def initialize(module_dirs)
@modules = {}
module_dirs.each do |module_dir|
Dir[File.join(module_dir, '*', 'metadata.json')].sort.map do |metadata|
mod = PuppetMetadata.read(metadata)
dependencies = mod.dependencies.map { |name, requirement| [normalize_name(name), requirement.to_s] }
name = normalize_name(mod.name)
@modules[name] = create_release(name, mod.version, dependencies)
rescue PuppetMetadata::InvalidMetadataException => e
raise "#{metadata}: #{e}"
end
end
@modules.freeze
end
def fetch(name)
if @modules.key?(name)
[@modules[name]]
else
[]
end
end
end
class ProvidedModules < SemanticPuppet::Dependency::Source
attr_reader :modules
def initialize(modules)
@modules = {}
modules.each do |name, version|
name = normalize_name(name)
@modules[name] = create_release(name, version)
end
@modules.freeze
end
def fetch(name)
if @modules.key?(name)
[@modules[name]]
else
[]
end
end
end
# TODO: read this from environment.conf
local_source = LocalModules.new(['modules', 'external_modules'])
SemanticPuppet::Dependency.add_source(local_source)
# TODO: Read this from a file
# These should be soft dependencies
provided = {
'puppet/zypprepo' => '3.1.0', # puppet/jenkins
'theforeman/dhcp' => '8.0.0', # theforeman/foreman_proxy
'theforeman/dns' => '9.0.0', # theforeman/foreman_proxy
}
provided_source = ProvidedModules.new(provided)
SemanticPuppet::Dependency.add_source(provided_source)
modules = Hash[local_source.modules.map { |name, mod| [name, mod.version.to_s] }]
graph = SemanticPuppet::Dependency.query(modules)
begin
releases = SemanticPuppet::Dependency.resolve(graph)
puts "Satisfied all dependencies:"
releases.each do |release|
# TODO: print source
puts " #{release.name} #{release.version}"
end
rescue SemanticPuppet::Dependency::UnsatisfiableGraph => e
# There was some dependency that wasn't matched. We know its name and that
# it's a dependency we requested previously. Now to investigate why to
# provide useful hints
# TODO: it's *very* weird to store unsatisfiable in a module
# Strange API
unsatisfiable = SemanticPuppet::Dependency.unsatisfiable
unless unsatisfiable
# In older semantic_puppet unsatisfiable was unreliable
# Needs https://github.com/puppetlabs/semantic_puppet/pull/37
$stderr.puts e
$stderr.puts "semantic_puppet provides insufficient information to hint why"
exit 1
end
$stderr.puts "Unable to satisfy #{unsatisfiable}"
releases = graph.dependencies[unsatisfiable]
if releases.empty?
# This should never happen
$stderr.puts "No releases found for #{unsatisfiable}"
exit 2
end
releases.each do |release|
$stderr.puts "Investigating version #{release.version}"
unsatisfied = release.dependencies.select { |_, candidates| candidates.empty? }
if unsatisfied.empty?
# Is this ever the case, did we miss anything else?
$stderr.puts " All dependencies satisfied"
else
unsatisfied.each do |dependency, _|
constraint = release.constraints_for(dependency).find { |constraint| constraint[:source] == 'initialize' }
$stderr.puts " Dependency #{dependency} (#{constraint[:description]}) can't be satified"
constraint_graph = SemanticPuppet::Dependency.query(dependency => '>= 0')
begin
releases = SemanticPuppet::Dependency.resolve(constraint_graph)
mod_releases = releases.select { |release| release.name == dependency }
$stderr.puts " Available versions: #{mod_releases.map { |r| r.version }.join(', ')}"
rescue SemanticPuppet::Dependency::UnsatisfiableGraph
$stderr.puts " Unable to find any release"
end
end
end
end
end