summaryrefslogtreecommitdiff
path: root/lib/chef/knife/status.rb
diff options
context:
space:
mode:
authorThom May <thom@may.lt>2015-03-12 10:21:11 +0000
committerThom May <thom@may.lt>2015-03-12 10:21:11 +0000
commit0d7f6a64abef06e94273dbcefd8adc53ab071f11 (patch)
tree26c5ed1aab03be26521bd51f76487475ea65a78c /lib/chef/knife/status.rb
parent0c6f95f45207f9f7da6f83d38babd47a1dcd5ebf (diff)
downloadchef-0d7f6a64abef06e94273dbcefd8adc53ab071f11.tar.gz
Allow knife status to filter by environment
This closes #3031 and also fixes a bug in handling custom queries
Diffstat (limited to 'lib/chef/knife/status.rb')
-rw-r--r--lib/chef/knife/status.rb25
1 files changed, 18 insertions, 7 deletions
diff --git a/lib/chef/knife/status.rb b/lib/chef/knife/status.rb
index 93e81f8f03..93e596a7db 100644
--- a/lib/chef/knife/status.rb
+++ b/lib/chef/knife/status.rb
@@ -44,20 +44,31 @@ class Chef
:long => "--hide-healthy",
:description => "Hide nodes that have run chef in the last hour"
+ def append_to_query(term)
+ @query << " AND " unless @query.empty?
+ @query << term
+ end
+
def run
ui.use_presenter Knife::Core::StatusPresenter
- all_nodes = []
- q = Chef::Search::Query.new
- query = @name_args[0] ? @name_args[0].dup : '*:*'
+
+ @query = ""
+ append_to_query(@name_args[0]) if @name_args[0]
+ append_to_query("chef_environment:#{config[:environment]}") if config[:environment]
+
if config[:hide_healthy]
time = Time.now.to_i
- query_unhealthy = "NOT ohai_time:[" << (time - 60*60).to_s << " TO " << time.to_s << "]"
- query << ' AND ' << query_unhealthy << @name_args[0] if @name_args[0]
- query = query_unhealthy unless @name_args[0]
+ append_to_query("NOT ohai_time:[#{(time - 60*60).to_s} TO #{time.to_s}]")
end
- q.search(:node, query) do |node|
+
+ @query = @query.empty? ? "*:*" : @query
+
+ all_nodes = []
+ q = Chef::Search::Query.new
+ q.search(:node, @query) do |node|
all_nodes << node
end
+
output(all_nodes.sort { |n1, n2|
if (config[:sort_reverse] || Chef::Config[:knife][:sort_status_reverse])
(n2["ohai_time"] or 0) <=> (n1["ohai_time"] or 0)