class Chef::Knife::Ec2ServerList

Public Instance Methods

azcolor(az) click to toggle source

@return [Symbol]

# 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() click to toggle source
# 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) click to toggle source

@return [Symbol]

# 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

Private Instance Methods

extract_tags(tags_struct) click to toggle source

@return [Array<String>]

# File lib/chef/knife/ec2_server_list.rb, line 193
def extract_tags(tags_struct)
  tags_struct.map(&:value)
end
server_hashes() click to toggle source

@return [Array<Hash>]

# File lib/chef/knife/ec2_server_list.rb, line 157
def server_hashes
  all_data = []
  ec2_connection.describe_instances.reservations.each do |i|
    server_data = {}
    %w{image_id instance_id instance_type key_name public_ip_address private_ip_address}.each do |id|
      server_data[id] = i.instances[0].send(id)
    end

    # dig into tags struct
    tags = extract_tags(i.instances[0].tags)

    if config[:name]
      server_data["name"] = find_name_tag(i.instances[0].tags)
    end

    if config[:az]
      server_data["az"] = i.instances[0].placement.availability_zone
    end

    server_data["iam_instance_profile"] = ( i.instances[0].iam_instance_profile.nil? ? nil : i.instances[0].iam_instance_profile.arn[%r{instance-profile/(.*)}] )

    server_data["state"] = i.instances[0].state.name

    if config[:tags]
      server_data["tags"] = tags
    end

    # dig into security_groups struct
    server_data["security_groups"] = i.instances[0].security_groups.map(&:group_name)

    all_data << server_data
  end
  all_data
end