Class: Eclair::EC2Item

Inherits:
Item
  • Object
show all
Defined in:
lib/eclair/providers/ec2/ec2_item.rb

Instance Attribute Summary collapse

Attributes inherited from Item

#selected, #visible

Instance Method Summary collapse

Methods inherited from Item

#select, #title, #toggle_select

Methods included from ConfigHelper

#config

Constructor Details

#initialize(instance) ⇒ EC2Item

Returns a new instance of EC2Item.



12
13
14
15
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 12

def initialize instance
  super()
  @instance = instance
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



10
11
12
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 10

def instance
  @instance
end

Instance Method Details

#colorObject



21
22
23
24
25
26
27
28
29
# 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

#commandObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 90

def connectable?
  ![32, 48, 80].include?(@instance.state[:code])
end

#headerObject



56
57
58
59
60
61
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 56

def header
  <<-EOS
  #{name} (#{instance_id}) [#{state[:name]}]
  launched at #{launch_time.to_time}
  EOS
end

#idObject



17
18
19
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 17

def id
  @instance.id
end

#imageObject



63
64
65
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 63

def image
  @image ||= provider.find_image_by_id(@instance.image_id)
end

#infoObject



82
83
84
85
86
87
88
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 82

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

#labelObject



78
79
80
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 78

def label
  " - #{name} [#{launched_at}]"
end

#nameObject



94
95
96
97
98
99
100
101
102
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 94

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_keyObject



108
109
110
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 108

def search_key
  name.downcase
end

#security_groupsObject



104
105
106
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 104

def security_groups
  @security_groups ||= @instance.security_groups.map{|sg| provider.find_security_group_by_id(sg.group_id)}
end

#vpcObject



67
68
69
# File 'lib/eclair/providers/ec2/ec2_item.rb', line 67

def vpc
  @vpc ||= provider.find_vpc_by_id(@instance.vpc_id)
end