forked from aws/opsworks-cookbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
79 lines (66 loc) · 2.49 KB
/
Rakefile
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
desc 'check literal recipe includes'
task :validate_literal_includes do
Dir['**/*.rb'].each do |file|
recipes = File.read(file).scan(/(?:include_recipe\s+(['"])([\w:]+)\1)/).reject {|candidate| candidate.last.include?('#{}')}.map(&:last)
recipes.each do |recipe|
recipe_file = recipe.include?('::') ? recipe.sub('::', '/recipes/') + '.rb' : recipe + '/recipes/default.rb'
unless File.exists?(recipe_file)
puts "#{file} includes missing recipe #{recipe}"
exit 1
end
end
end
end
KNOWN_COOKBOOK_ATTRIBUTES = {
'opsworks_agent' => 'opsworks_initial_setup',
'passenger' => 'passenger_apache2',
'ganglia' => 'opsworks_ganglia',
'sudoers' => 'ssh_users',
'opsworks' => :any,
'platform' => :any,
'platform_version' => :any
}
desc 'check declared attribute dependencies'
task :validate_attribute_dependencies do
Dir['**/*.rb'].each do |file|
next unless file.match(/\/attributes\//)
used_cookbook_attributes = []
found_trouble = false
cookbook_name = file.match(/(\w+)\//)[1]
loaded_cookbook_attributes = [cookbook_name]
attribute_file = File.read(file)
attribute_file.each do |line|
# uses other cookbooks attributes
if line.match(/node\[\:(\w+)\]/) && $1 != cookbook_name
used_cookbook_attributes << $1
end
# loads/includes attributes
if line.match(/include_attribute [\'\"](\w+)[\'\"]/) || line.match(/include_attribute [\'\"](\w+)::\w+[\'\"]/)
loaded_cookbook_attributes << $1
end
end
used_cookbook_attributes.uniq!
loaded_cookbook_attributes.uniq!
used_cookbook_attributes_without_include = used_cookbook_attributes - loaded_cookbook_attributes
used_cookbook_attributes_without_include.delete_if{|cookbook_attribute| KNOWN_COOKBOOK_ATTRIBUTES[cookbook_attribute] == :any || loaded_cookbook_attributes.include?(KNOWN_COOKBOOK_ATTRIBUTES[cookbook_attribute]) }
if used_cookbook_attributes_without_include.size > 0
puts "#{file} used attributes from #{used_cookbook_attributes.inspect} but does only loads from #{loaded_cookbook_attributes.inspect}"
found_trouble = true
end
if found_trouble
exit 1
end
end
end
desc 'check syntax of ruby files'
task :validate_syntax do
Dir['**/*.rb'].each do |file|
`ruby -c #{file}`
if $?.exitstatus != 0
puts "syntax error in #{file}"
exit 1
end
end
end
desc 'run all checks'
task :default => [:validate_literal_includes, :validate_syntax, :validate_attribute_dependencies]