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
|
# File 'lib/chef/knife/sharp-role-align.rb', line 20
def check_roles
to_update = Hash.new
unless File.exists?(role_path)
ui.warn "Bad role path, skipping role sync."
return to_update
end
ui.msg(ui.color("== Roles ==", :bold))
updated_roles = Hash.new
local_roles = Dir.glob(File.join(role_path, "*.json")).map {|file| File.basename(file, ".json")}
remote_roles = Chef::Role.list.keys
if local_roles.empty?
ui.warn "No local roles found, is the role path correct ? (#{role_path})"
return to_update
end
(remote_roles - local_roles).each do |role|
ui.msg "* #{role} role is remote only"
if config[:dump_remote_only]
ui.msg "* Dumping to #{File.join(role_path, "#{role}.json")}"
begin
remote_role = Chef::Role.load(role)
File.open(File.join(role_path, "#{role}.json"), "w") do |file|
file.puts JSON.pretty_generate(remote_role)
end
rescue Exception => e
ui.error "Unable to dump #{role} role (#{e.message})"
end
end
end
(local_roles - remote_roles).each do |role|
begin
local_role = Chef::Role.from_disk(role)
updated_roles[role] = local_role
ui.msg "* #{role} role is local only"
rescue Exception => e
ui.error "Unable to load #{role} role (#{e.message})"
end
end
(remote_roles & local_roles).each do |role|
remote_role = Chef::Role.load(role)
local_role = Chef::Role.from_disk(role)
diffs = Array.new
relevant_role_keys.each do |method, display|
if remote_role.send(method) != local_role.send(method)
updated_roles[role] = local_role
diffs << display
end
end
ui.msg("* #{role} role is not up-to-date (#{diffs.join(",")})") unless diffs.empty?
end
if sharp_config[chef_server] and sharp_config[chef_server].has_key?("ignore_roles")
(updated_roles.keys & sharp_config[chef_server]["ignore_roles"]).each do |r|
updated_roles.delete(r)
ui.msg "* Skipping #{r} role (ignore list)"
end
end
if !updated_roles.empty?
all = false
updated_roles.each do |name, obj|
answer = ui.ask_question("> Update #{name} role on server ? Y/N/(A)ll/(Q)uit ", :default => "N").upcase unless all
if answer == "A"
all = true
elsif answer == "Q"
ui.msg "* Aborting role alignment."
break
end
if all or answer == "Y"
to_update[name] = obj
else
ui.msg "* Skipping #{name} role"
end
end
else
ui.msg "* Roles are up-to-date."
end
to_update
end
|