Class: Chef::Knife::Cloud::ServerListCommand

Inherits:
ResourceListCommand show all
Defined in:
lib/chef/knife/cloud/server/list_command.rb

Instance Attribute Summary

Attributes inherited from Command

#custom_arguments, #service

Instance Method Summary collapse

Methods inherited from ResourceListCommand

#data_sort, #execute_command, #initialize, #is_resource_filtered?, #list

Methods inherited from Command

#run

Methods included from Helpers

#after_exec_command, #create_service_instance, #execute_command, #locate_config_value, #msg_pair, #pretty_key, #set_default_config, #validate!, #validate_params!

Constructor Details

This class inherits a constructor from Chef::Knife::Cloud::ResourceListCommand

Instance Method Details

#before_exec_commandObject



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
# File 'lib/chef/knife/cloud/server/list_command.rb', line 9

def before_exec_command
  if config[:chef_data]
    begin
      # Chef::Node.list(inflate = true) to use Solr search.
      @node_list = Chef::Node.list(true)
    rescue Errno::ECONNREFUSED => e
      error_message = "Connection error with Chef server. #{e}"
      ui.warn(error_message)
      raise CloudExceptions::ChefServerError, error_message
    end

    @chef_data_col_info = [
      { label: "Chef Node Name", key: "name" },
      { label: "Environment", key: "chef_environment" },
      { label: "FQDN", key: "fqdn" },
      { label: "Runlist", key: "run_list" },
      { label: "Tags", key: "tags" },
      { label: "Platform", key: "platform" },
    ]

    if config[:chef_node_attribute]
      @chef_data_col_info << { label: "#{config[:chef_node_attribute]}", key: "#{config[:chef_node_attribute]}" }
    end
    @columns_with_info.concat(@chef_data_col_info)
  end
end

#format_server_state(state) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/cloud/server/list_command.rb', line 68

def format_server_state(state)
  state = state.to_s.downcase
  state_color =  case state
                 when "shutting-down", "terminated", "stopping", "stopped", "error", "shutoff"
                   :red
                 when "pending", "build", "paused", "suspended", "hard_reboot"
                   :yellow
                 else
                   :green
                 end
  ui.color(state, state_color)
end

#get_resource_col_val(server) ⇒ Object

Override from base to display chef node data along with server list display.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef/knife/cloud/server/list_command.rb', line 37

def get_resource_col_val(server)
  list = []
  @columns_with_info.each do |col_info|
    if config[:chef_data] && @chef_data_col_info.include?(col_info)
      server_name = service.get_server_name(server)
      if @node_list.include?(server_name)
        node = @node_list[server_name]
        # Raise serverlisting error on invalid chef_node_attribute.
        if col_info[:key] == config[:chef_node_attribute] && ! node.attribute?(col_info[:key])
          error_message = "The Node does not have a #{col_info[:key]} attribute."
          ui.error(error_message)
          raise CloudExceptions::CloudAPIException, error_message
        else
          value = (col_info[:value_callback].nil? ? node.send(col_info[:key]).to_s : col_info[:value_callback].call(node.send(col_info[:key])))
        end
      else
        # Set chef data value for those server which is not part chef server.
        value = ""
      end
    else
      value = (col_info[:value_callback].nil? ? server.send(col_info[:key]).to_s : col_info[:value_callback].call(server.send(col_info[:key])))
    end
    list << value
  end
  list
end

#query_resourceObject



64
65
66
# File 'lib/chef/knife/cloud/server/list_command.rb', line 64

def query_resource
  @service.list_servers
end