Class: Chef::Knife::Ec2ServerList

Inherits:
Chef::Knife show all
Includes:
Ec2Base
Defined in:
lib/chef/knife/ec2_server_list.rb

Instance Method Summary collapse

Methods included from Ec2Base

#ami, #connection_string, #ec2_connection, #fetch_ami, #fetch_ec2_instance, #fetch_network_interfaces, #fetch_password_data, #fetch_region, #fetch_subnet, #find_name_tag, included, #is_image_windows?, #msg_pair, #normalize_server_data, #validate_aws_config!, #vpc_mode?

Methods inherited from Chef::Knife

#aws_cred_file_location, #custom_warnings!, #find_server_platform, #iam_name_from_profile, #ini_parse

Instance Method Details

#azcolor(az) ⇒ Symbol

Returns:

  • (Symbol)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/knife/ec2_server_list.rb', line 57

def azcolor(az)
  case az
  when /a$/
    color = :blue
  when /b$/
    color = :green
  when /c$/
    color = :red
  when /d$/
    color = :magenta
  when /e$/
    color = :yellow
  else
    color = :cyan
  end
end

#run ⇒ Object



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

def run
  $stdout.sync = true

  validate_aws_config!

  servers_list = [
    ui.color("Instance ID", :bold),

    if config[:name]
      ui.color("Name", :bold)
    end,

    ui.color("Public IP", :bold),
    ui.color("Private IP", :bold),
    ui.color("Flavor", :bold),

    if config[:az]
      ui.color("AZ", :bold)
    end,

    ui.color("Image", :bold),
    ui.color("SSH Key", :bold),
    ui.color("Security Groups", :bold),

    if config[:tags]
      config[:tags].split(",").collect do |tag_name|
        ui.color("Tag:#{tag_name}", :bold)
      end
    end,

    if config[:iamprofile]
      ui.color("IAM Profile", :bold)
    end,

    ui.color("State", :bold),
  ].flatten.compact

  output_column_count = servers_list.length

  unless config[:region]
    ui.warn "No region was specified in knife.rb/config.rb or as an argument. The default region, us-east-1, will be used:"
  end

  if config[:format] == "summary"
    server_hashes.each do |v|
      servers_list << v["instance_id"]
      servers_list << v["name"] if config[:name]
      servers_list << v["public_ip_address"]
      servers_list << v["private_ip_address"]
      servers_list << v["instance_type"]
      servers_list << ui.color(v["az"], azcolor(v["az"])) if config[:az]
      servers_list << v["image_id"]
      servers_list << v["key_name"]
      servers_list << v["security_groups"].join(",")
      if config[:tags]
        config[:tags].split(",").collect do |tag_name|
          servers_list << v["tags"].find { |tag| tag == tag_name }
        end
      end
      servers_list << v["iam_instance_profile"].to_s if config[:iamprofile] # may be nil
      servers_list << ui.color(v["state"], state_color(v["state"]))
    end
    puts ui.list(servers_list, :uneven_columns_across, output_column_count)
  else
    output(format_for_display(server_hashes))
  end
end

#state_color(state) ⇒ Symbol

Returns:

  • (Symbol)


75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/knife/ec2_server_list.rb', line 75

def state_color(state)
  case state
  when "shutting-down", "terminated", "stopping", "stopped"
    :red
  when "pending"
    :yellow
  else
    :green
  end
end