Class: Eclair::Instance

Inherits:
Cell
  • Object
show all
Defined in:
lib/eclair/instance.rb

Instance Attribute Summary

Attributes inherited from Cell

#column, #selected

Instance Method Summary collapse

Methods inherited from Cell

#check_scroll, #current, #decurrent, #deselect, #method_missing, #redraw, #render, #select, #select_indicator, #toggle_select

Methods included from CommonHelper

#config, included

Constructor Details

#initialize(instance_id, column = nil) ⇒ Instance

Returns a new instance of Instance.



3
4
5
6
7
# File 'lib/eclair/instance.rb', line 3

def initialize instance_id, column = nil
  super
  @instance_id = instance_id
  @column = column
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Eclair::Cell

Instance Method Details

#colorObject



26
27
28
29
30
31
32
# File 'lib/eclair/instance.rb', line 26

def color
  if [48, 80].include?(state[:code])
    super(*config.disabled_color)
  else
    super(*config.instance_color)
  end
end

#connectable?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/eclair/instance.rb', line 99

def connectable?
  hostname && ![48, 80].include?(state[:code])
end

#digest_routesObject



129
130
131
132
133
134
135
# File 'lib/eclair/instance.rb', line 129

def digest_routes
  if Aws.dns_records?
    routes.map(&:name).join(" ")
  else
    "Fetching DNS records from Route53..."
  end
end

#digest_tagsObject



125
126
127
# File 'lib/eclair/instance.rb', line 125

def digest_tags
  tags.map{|t| "#{t[:key]}: #{t[:value]}"}.join("/")
end

#formatObject



34
35
36
# File 'lib/eclair/instance.rb', line 34

def format
  " - #{name} [#{launched_at}] #{select_indicator}"
end

#headerObject



137
138
139
140
141
# File 'lib/eclair/instance.rb', line 137

def header
  ["#{name} (#{instance_id}) [#{state[:name]}] #{hostname}",
  "launched at #{launch_time.to_time}",
  "#{digest_routes}"]
end

#hostnameObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/eclair/instance.rb', line 38

def hostname
  case config.ssh_hostname
  when :auto
    if object.network_interfaces.empty? && object.public_ip_address
      object.public_ip_address
    else
      object.private_ip_address
    end
  else
    object.send(config.ssh_hostname)
  end
end

#image(**options) ⇒ Object



51
52
53
# File 'lib/eclair/instance.rb', line 51

def image **options
  Aws.images(**options).find{|i| i.image_id == object.image_id}
end

#infoObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/eclair/instance.rb', line 143

def info
  to_merge = {}
  
  if routes
    to_merge[:routes] = routes.map(&:to_h)
  else
    to_merge[:routes] = "Fetching DNS records from Route53..."
  end

  if image
    to_merge[:image] = image.to_h
  else
    to_merge[:image] = "Fetching Image data from EC2..."
  end

  object.to_h.merge(to_merge)
end

#key_cmdObject



83
84
85
86
87
88
89
# File 'lib/eclair/instance.rb', line 83

def key_cmd
  if config.ssh_keys[key_name]
    "-i #{config.ssh_keys[key_name]}"
  else
    ""
  end
end

#launched_atObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/eclair/instance.rb', line 107

def launched_at
  diff = Time.now - 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

#nameObject



17
18
19
20
21
22
23
24
# File 'lib/eclair/instance.rb', line 17

def name
  begin
    nametag = object.tags.find{|t| t.key == "Name"}
    nametag ? nametag.value : "noname"
  rescue
    "terminated"
  end
end

#objectObject



161
162
163
# File 'lib/eclair/instance.rb', line 161

def object
  Aws.instance_map[@instance_id]
end

#routesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/eclair/instance.rb', line 65

def routes
  if Aws.dns_records?
    Aws.dns_records.select do |record| 
      values = record.resource_records.map(&:value)
      !values.grep(private_dns_name).empty? ||
      !values.grep(public_dns_name).empty? ||
      !values.grep(private_ip_address).empty? ||
      !values.grep(public_ip_address).empty?
    end
  else
    nil
  end
end

#running?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/eclair/instance.rb', line 103

def running?
  hostname && state[:code] == 16
end

#security_groups(**options) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/eclair/instance.rb', line 55

def security_groups **options
  if Aws.security_groups?
    object.security_groups.map{|instance_sg| 
      Aws.security_groups(**options).find{|sg| sg.group_id == instance_sg.group_id }
    }.compact
  else
    nil
  end
end

#ssh_cmdObject



91
92
93
94
95
96
97
# File 'lib/eclair/instance.rb', line 91

def ssh_cmd
  cmd = config.ssh_ports.map{ |port|
    "ssh #{config.ssh_options} -p#{port} #{key_cmd} #{username}@#{hostname}"
  }.join(" || ")

  "echo Attaching to #{name}: #{username}@#{hostname} && (#{cmd})"
end

#usernameObject



79
80
81
# File 'lib/eclair/instance.rb', line 79

def username
  config.ssh_username.call(image(force: true))
end

#xObject



9
10
11
# File 'lib/eclair/instance.rb', line 9

def x
  column.x
end

#yObject



13
14
15
# File 'lib/eclair/instance.rb', line 13

def y
  column.index(self)
end