class Eclair::EC2Item

Attributes

instance[R]

Public Class Methods

new(instance) click to toggle source
Calls superclass method Eclair::Item::new
# File lib/eclair/providers/ec2/ec2_item.rb, line 12
def initialize instance
  super()
  @instance = instance
end

Public Instance Methods

color() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 21
def color
  if @selected
    [Curses::COLOR_YELLOW, -1, Curses::A_BOLD]
  elsif !connectable?
    [Curses::COLOR_BLACK, -1, Curses::A_BOLD]
  else
    [Curses::COLOR_WHITE, -1]
  end
end
command() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 31
def command
  hosts = [@instance.public_ip_address, @instance.private_ip_address].compact
  ports = config.ssh_ports
  ssh_options = config.ssh_options
  ssh_command = config.ssh_command
  username = config.ssh_username.call(image)
  key_cmd = config.ssh_keys[@instance.key_name] ? "-i #{config.ssh_keys[@instance.key_name]}" : ""
  format = config.exec_format

  joined_cmd = hosts.map do |host|
    ports.map do |port|
      {
        "{ssh_command}" => ssh_command,
        "{ssh_options}" => ssh_options,
        "{port}"        => port,
        "{ssh_key}"     => key_cmd,
        "{username}"    => username,
        "{host}"        => host,
      }.reduce(format) { |cmd,pair| cmd.sub(pair[0],pair[1].to_s) }
    end
  end.join(" || ")
  # puts joined_cmd
  "echo Attaching to #{Shellwords.escape(name)} \\[#{@instance.instance_id}\\] && #{joined_cmd}"
end
connectable?() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 83
def connectable?
  ![32, 48, 80].include?(@instance.state[:code])
end
header() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 64
    def header
      <<-EOS
      #{name} (#{@instance.instance_id}) [#{@instance.state[:name]}] #{@instance.private_ip_address}
      launched at #{@instance.launch_time.to_time}
      EOS
    end
id() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 17
def id
  @instance.id
end
image() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 56
def image
  @image ||= provider.find_image_by_id(@instance.image_id)
end
info() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 75
def info
  {
    instance: @instance,
    image: provider.image_loaded? ? image : "ami info not loaded yet",
    security_groups: provider.security_group_loaded? ? security_groups : "sg info not loaded yet",
  }
end
label() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 71
def label
  " - #{name} [#{launched_at}]"
end
name() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 87
def name
  return @name if @name
  begin
    tag = @instance.tags.find{|t| t.key == "Name"}
    @name = tag ? tag.value : "noname"
  rescue
    @name = "terminated"
  end
end
search_key() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 101
def search_key
  name.downcase
end
security_groups() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 97
def security_groups
  @security_groups ||= @instance.security_groups.map{|sg| provider.find_security_group_by_id(sg.group_id)}
end
vpc() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 60
def vpc
  @vpc ||= provider.find_vpc_by_id(@instance.vpc_id)
end

Private Instance Methods

launched_at() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 107
def launched_at
  diff = Time.now - @instance.launch_time
  {
    "year" => 31557600,
    "month" => 2592000,
    "day" => 86400,
    "hour" => 3600,
    "minute" => 60,
    "second" => 1
  }.each do |unit,v|
    if diff >= v
      value = (diff/v).to_i
      return "#{value} #{unit}#{value > 1 ? "s" : ""}"
    end
  end
  "now"
end
provider() click to toggle source
# File lib/eclair/providers/ec2/ec2_item.rb, line 125
def provider
  EC2Provider
end