class Chef::Knife::Status

Public Instance Methods

append_to_query(term) click to toggle source
# File lib/chef/knife/status.rb, line 53
def append_to_query(term)
  @query << " AND " unless @query.empty?
  @query << term
end
run() click to toggle source
# File lib/chef/knife/status.rb, line 58
def run
  ui.use_presenter Knife::Core::StatusPresenter

  if config[:long_output]
    opts = {}
  else
    opts = { filter_result:
           { name: ["name"], ipaddress: ["ipaddress"], ohai_time: ["ohai_time"],
             ec2: ["ec2"], run_list: ["run_list"], platform: ["platform"],
             platform_version: ["platform_version"], chef_environment: ["chef_environment"] } }
  end

  @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]
    ui.warn("-H / --hide-healthy is deprecated. Use --hide-by-mins MINS instead")
    time = Time.now.to_i
    # AND NOT is not valid lucene syntax, so don't use append_to_query
    @query << " " unless @query.empty?
    @query << "NOT ohai_time:[#{(time - 60 * 60)} TO #{time}]"
  end

  if config[:hide_by_mins]
    hidemins = config[:hide_by_mins].to_i
    time = Time.now.to_i
    # AND NOT is not valid lucene syntax, so don't use append_to_query
    @query << " " unless @query.empty?
    @query << "NOT ohai_time:[#{(time - hidemins * 60)} TO #{time}]"
  end

  @query = @query.empty? ? "*:*" : @query

  all_nodes = []
  q = Chef::Search::Query.new
  Chef::Log.info("Sending query: #{@query}")
  q.search(:node, @query, opts) do |node|
    all_nodes << node
  end

  output(all_nodes.sort do |n1, n2|
    if config[:sort_reverse] || Chef::Config[:knife][:sort_status_reverse]
      (n2["ohai_time"] || 0) <=> (n1["ohai_time"] || 0)
    else
      (n1["ohai_time"] || 0) <=> (n2["ohai_time"] || 0)
    end
  end)
end