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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/chef/knife/preflight.rb', line 48
def run
if config[:query] && @name_args[0]
ui.error "please specify query as an argument or an option via -q, not both"
ui.msg opt_parser
exit 1
end
raw_query = config[:query] || @name_args[0]
if !raw_query || raw_query.empty?
ui.error "no query specified"
ui.msg opt_parser
exit 1
end
q_nodes = Chef::Search::Query.new
escaped_query = raw_query.sub( "::", "\\:\\:")
if !raw_query.include? "::"
node_query = "recipes:*#{escaped_query} OR recipes:*#{escaped_query}\\:\\:default"
ui.msg("Searching for nodes containing #{raw_query} OR #{raw_query}::default in their expanded run_list...\n")
elsif raw_query.include? "::default"
node_query = "recipes:*#{escaped_query} OR recipes:*#{escaped_query.gsub( "\\:\\:default","")}"
ui.msg("Searching for nodes containing #{raw_query} OR #{raw_query.gsub( "::default","")} in their expanded run_list...\n")
else
node_query = "recipes:*#{escaped_query}"
ui.msg("Searching for nodes containing #{raw_query} in their expanded run_list...\n")
end
query_nodes = URI.escape(node_query,
Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
result_items_nodes = []
result_count_nodes = 0
rows = config[:rows]
start = config[:start]
begin
q_nodes.search('node', query_nodes, config[:sort], start, rows) do |node_item|
formatted_item_node = format_for_display(node_item)
if formatted_item_node.respond_to?(:has_key?) && !formatted_item_node.has_key?('id')
formatted_item_node['id'] = node_item.has_key?('id') ? node_item['id'] : node_item.name
end
result_items_nodes << formatted_item_node
result_count_nodes += 1
end
rescue Net::HTTPServerException => e
msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
ui.error("knife preflight failed: #{msg}")
exit 1
end
if ui.interchange?
output({:results => result_count_nodes, :rows => result_items_nodes})
else
ui.msg "#{result_count_nodes} Nodes found"
ui.msg("\n")
result_items_nodes.each do |item|
output(item.name)
end
end
ui.msg("\n")
ui.msg("\n")
q_roles = Chef::Search::Query.new
if !raw_query.include? "::"
role_query = "run_list:recipe\\[#{escaped_query}\\] OR run_list:recipe\\[#{escaped_query}\\:\\:default\\]"
ui.msg("Searching for roles containing #{raw_query} OR #{raw_query}::default in their expanded run_list...\n")
elsif raw_query.include? "::default"
role_query = "run_list:recipe\\[#{escaped_query}\\] OR run_list:recipe\\[#{escaped_query.gsub( "\\:\\:default","")}\\]"
ui.msg("Searching for roles containing #{raw_query} OR #{raw_query.gsub( "::default","")} in their expanded run_list...\n")
else
role_query = "run_list:recipe\\[#{escaped_query}\\]"
ui.msg("Searching for roles containing #{raw_query} in their expanded run_list...\n")
end
query_roles = URI.escape(role_query,
Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
result_items_roles = []
result_count_roles = 0
rows = config[:rows]
start = config[:start]
begin
q_roles.search('role', query_roles, config[:sort], start, rows) do |role_item|
formatted_item_role = format_for_display(role_item)
if formatted_item_role.respond_to?(:has_key?) && !formatted_item_role.has_key?('id')
formatted_item_role['id'] = role_item.has_key?('id') ? role_item['id'] : role_item.name
end
result_items_roles << formatted_item_role
result_count_roles += 1
end
rescue Net::HTTPServerException => e
msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
ui.error("knife preflight failed: #{msg}")
exit 1
end
if ui.interchange?
output({:results => result_count_roles, :rows => result_items_roles})
else
ui.msg "#{result_count_roles} Roles found"
ui.msg("\n")
result_items_roles.each do |role_item|
output(role_item.name)
end
end
ui.msg("\n")
ui.msg("\n")
ui.msg("Found #{result_count_nodes} nodes and #{result_count_roles} roles using the specified search criteria")
end
|