Class: Chef::Knife::Status

Inherits:
Chef::Knife show all
Includes:
Core::NodeFormattingOptions
Defined in:
lib/chef/knife/status.rb

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods included from Core::NodeFormattingOptions

included

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, common_name, #config_file_defaults, #config_file_settings, config_loader, #config_source, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#append_to_query(term) ⇒ Object


49
50
51
52
# File 'lib/chef/knife/status.rb', line 49

def append_to_query(term)
  @query << " AND " unless @query.empty?
  @query << term
end

#runObject


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
# File 'lib/chef/knife/status.rb', line 54

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"],
             cloud: ["cloud"], 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_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] || config[:sort_status_reverse]
      (n2["ohai_time"] || 0) <=> (n1["ohai_time"] || 0)
    else
      (n1["ohai_time"] || 0) <=> (n2["ohai_time"] || 0)
    end
  end)
end