-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsistency.rb
114 lines (91 loc) · 3.16 KB
/
consistency.rb
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
require 'chef/knife'
module Fotolia
class Consistency < Chef::Knife
banner "knife consistency [latest ENVIRONMENT|local]"
deps do
require 'chef/knife/search'
require 'chef/environment'
require 'chef/cookbook/metadata'
end
def run
if name_args.count < 1 then
ui.error "Usage : knife consistency [latest ENVIRONMENT|local]"
exit 1
end
@action = name_args.first
unless ["latest","local"].include?(@action)
ui.error "You must specify a mode !"
exit 1
end
if @action == "latest" then
@environment = name_args[1]
end
case @action
when "latest"
target_versions = get_versions_from_env(@environment)
latest_versions = get_latest_versions()
latest_versions.each_pair do |name, cb_version|
unless target_versions.has_key?(name)
puts "cookbook \"#{name}\" has no version constraint in environment #{@environment} !"
next
end
if target_versions[name] != cb_version then
puts "cookbook \"#{name}\" is not up to date. latest is #{cb_version}, #{@environment} has version #{target_versions[name]}"
end
end
when "local"
local_versions = get_local_versions()
latest_versions = get_latest_versions()
latest_versions.each_pair do |name, cb_version|
unless local_versions.has_key?(name)
puts "cookbook \"#{name}\" has no local candidate version"
next
end
if local_versions[name] != cb_version then
puts "cookbook \"#{name}\" is not up to date. latest is #{cb_version}, local version is #{local_versions[name]}"
end
end
end
end
# ger cookbook for a known environment
def get_versions_from_env(env_name)
cbs = {}
env = Chef::Environment.load(env_name)
env.cookbook_versions.each_pair do |name,version|
cbs[name] = version.gsub("= ","")
end
return cbs
end
# get cookbook for the _default env, bleeding edge
def get_latest_versions
cbs = {}
cookbook_versions = rest.get_rest("/environments/_default/cookbooks")
cookbook_versions.each do |cookbook|
cbs[cookbook[0]] = cookbook[1]["versions"][0]["version"]
end
return cbs
end
# in your local dealer !
def get_local_versions
cbs = {}
if (ENV['HOME'] && File.exist?(File.join(ENV['HOME'], '.chef', 'knife.rb')))
Chef::Config.from_file(File.join(ENV['HOME'], '.chef', 'knife.rb'))
end
if Chef::Config.cookbook_path.is_a?(String)
config_cookbook_path = [ Chef::Config.cookbook_path ]
else
config_cookbook_path = Chef::Config.cookbook_path
end
config_cookbook_path.each do |cookbook_path|
Dir.glob("#{cookbook_path}/*").each do |cookbook|
md = Chef::Cookbook::Metadata.new
cb_name = File.basename(cookbook)
md.name(cb_name)
md.from_file("#{cookbook}/metadata.rb")
cbs[cb_name] = md.version
end
end
return cbs
end
end
end