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)


82
83
84
# File 'lib/eclair/instance.rb', line 82

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

#digest_routesObject



112
113
114
115
116
117
118
# File 'lib/eclair/instance.rb', line 112

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

#digest_tagsObject



108
109
110
# File 'lib/eclair/instance.rb', line 108

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



120
121
122
123
124
# File 'lib/eclair/instance.rb', line 120

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

#hostnameObject



38
39
40
# File 'lib/eclair/instance.rb', line 38

def hostname
  object.send(config.ssh_hostname)
end

#image(**options) ⇒ Object



42
43
44
# File 'lib/eclair/instance.rb', line 42

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

#infoObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/eclair/instance.rb', line 126

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

#launched_atObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/eclair/instance.rb', line 90

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



144
145
146
# File 'lib/eclair/instance.rb', line 144

def object
  Aws.instance_map[@instance_id]
end

#routesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/eclair/instance.rb', line 56

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)


86
87
88
# File 'lib/eclair/instance.rb', line 86

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

#security_groups(**options) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/eclair/instance.rb', line 46

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



74
75
76
77
78
79
80
# File 'lib/eclair/instance.rb', line 74

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

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

#usernameObject



70
71
72
# File 'lib/eclair/instance.rb', line 70

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