-
Notifications
You must be signed in to change notification settings - Fork 9
/
role_tree.rb
61 lines (43 loc) · 1.24 KB
/
role_tree.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
require 'chef/knife'
module Kallistec
class RoleTree < Chef::Knife
deps do
require 'chef/node'
require 'chef/run_list'
require 'chef/run_list/run_list_expansion'
end
banner "knife role tree NODE"
attr_reader :node_name
attr_reader :node
def run
unless Chef::RunList::RunListExpansion.instance_methods.map(&:to_s).include?("run_list_trace")
ui.error "knife role tree requires Chef 10.14.0 beta or newer"
exit 1
end
unless @node_name = name_args.first
ui.error "You must specify a the name of the node you want to print the run list tree for"
exit 1
end
@node = Chef::Node.load(node_name)
tree_print("top level", expansion.run_list_trace)
end
def run_list
@node.run_list
end
def expansion
@expansion ||= run_list.expand("server")
end
def puts_indented(item, indentation)
prefix = ""
unless indentation == 0
prefix = "| " * (indentation - 1)
prefix << "|-"
end
puts "#{prefix}#{item}"
end
def tree_print(item, trace, depth=0)
puts_indented(item, depth)
trace[item.to_s].each { |sub_item| tree_print(sub_item, trace, depth + 1)}
end
end
end