Class: EC2::Host::HostData

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2/host/host_data.rb

Overview

Represents each host

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (HostData) initialize(instance)

:hostname, # tag:Name or hostname part of private_dns_name :roles, # tag:Roles.split(',') such as web:app1,db:app1 :region, # ENV, :instance, # Aws::EC2::Types::Instance itself

and OPTIONAL_ARRAY_TAGS, OPTIONAL_STRING_TAGS



15
16
17
# File 'lib/ec2/host/host_data.rb', line 15

def initialize(instance)
  @instance = instance
end

Instance Attribute Details

- (Object) instance (readonly)

Returns the value of attribute instance



7
8
9
# File 'lib/ec2/host/host_data.rb', line 7

def instance
  @instance
end

Instance Method Details

- (Object) hostname



19
20
21
22
23
24
# File 'lib/ec2/host/host_data.rb', line 19

def hostname
  return @hostname if @hostname
  @hostname = find_string_tag(Config.hostname_tag)
  @hostname = instance.private_dns_name.split('.').first if @hostname.empty?
  @hostname
end

- (Object) info



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ec2/host/host_data.rb', line 152

def info
  if self.class.display_short_info?
    info = "#{hostname}:#{status}"
    info << "(#{roles.join(',')})" unless roles.empty?
    info << "[#{tags.join(',')}]" unless tags.empty?
    info << "{#{service}}" unless service.empty?
    info
  else
    to_hash.to_s
  end
end

- (Object) inspect



164
165
166
# File 'lib/ec2/host/host_data.rb', line 164

def inspect
  sprintf "#<Aws::Host::HostData %s>", info
end

- (Object) instance_id



50
51
52
# File 'lib/ec2/host/host_data.rb', line 50

def instance_id
  instance.instance_id
end

- (Object) instance_type



54
55
56
# File 'lib/ec2/host/host_data.rb', line 54

def instance_type
  instance.instance_type
end

- (Object) ip

compatibility with dino-host



79
80
81
# File 'lib/ec2/host/host_data.rb', line 79

def ip
  private_ip_address
end

- (Object) launch_time



66
67
68
# File 'lib/ec2/host/host_data.rb', line 66

def launch_time
  instance.launch_time
end

- (Boolean) match?(condition)

match with condition or not

Parameters:

  • condition (Hash)

    search parameters

Returns:

  • (Boolean)


120
121
122
123
124
125
# File 'lib/ec2/host/host_data.rb', line 120

def match?(condition)
  return false if !condition[:state] and (terminated? or shutting_down?)
  return false unless role_match?(condition)
  return false unless instance_match?(condition)
  true
end

- (Object) monitoring



74
75
76
# File 'lib/ec2/host/host_data.rb', line 74

def monitoring
  instance.monitoring.state
end

- (Boolean) pending?

Returns:

  • (Boolean)


113
114
115
# File 'lib/ec2/host/host_data.rb', line 113

def pending?
  state == "pending"
end

- (Object) private_ip_address



58
59
60
# File 'lib/ec2/host/host_data.rb', line 58

def private_ip_address
  instance.private_ip_address
end

- (Object) public_ip_address



62
63
64
# File 'lib/ec2/host/host_data.rb', line 62

def public_ip_address
  instance.public_ip_address
end

- (Object) region



32
33
34
# File 'lib/ec2/host/host_data.rb', line 32

def region
  Config.aws_region
end

- (Object) roles



26
27
28
29
30
# File 'lib/ec2/host/host_data.rb', line 26

def roles
  return @roles if @roles
  roles = find_array_tag(Config.roles_tag)
  @roles = roles.map {|role| EC2::Host::RoleData.build(role) }
end

- (Boolean) running?

Returns:

  • (Boolean)


109
110
111
# File 'lib/ec2/host/host_data.rb', line 109

def running?
  state == "running"
end

- (Boolean) shutting_down?

Returns:

  • (Boolean)


97
98
99
# File 'lib/ec2/host/host_data.rb', line 97

def shutting_down?
  state == "shutting-down"
end

- (Object) start_date

compatibility with dino-host



84
85
86
# File 'lib/ec2/host/host_data.rb', line 84

def start_date
  launch_time
end

- (Object) state



70
71
72
# File 'lib/ec2/host/host_data.rb', line 70

def state
  instance.state.name
end

- (Object) stopped



105
106
107
# File 'lib/ec2/host/host_data.rb', line 105

def stopped
  state == "stopped"
end

- (Boolean) stopping?

Returns:

  • (Boolean)


101
102
103
# File 'lib/ec2/host/host_data.rb', line 101

def stopping?
  state == "stopping"
end

- (Boolean) terminated?

Returns:

  • (Boolean)


93
94
95
# File 'lib/ec2/host/host_data.rb', line 93

def terminated?
  state == "terminated"
end

- (Object) to_hash



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ec2/host/host_data.rb', line 127

def to_hash
  params = {
    "hostname" => hostname,
    "roles" => roles,
  }
  Config.optional_string_tags.each do |tag|
    field = StringUtil.underscore(tag)
    params[field] = send(field)
  end
  Config.optional_array_tags.each do |tag|
    field = StringUtil.underscore(tag)
    params[field] = send(field)
  end
  params.merge!(
    "region" => region,
    "instance_id" => instance_id,
    "instance_type" => instance_type,
    "private_ip_address" => private_ip_address,
    "public_ip_address" => public_ip_address,
    "launch_time" => launch_time,
    "state" => state,
    "monitoring" => monitoring,
  )
end

- (Object) usages

compatibility with dino-host



89
90
91
# File 'lib/ec2/host/host_data.rb', line 89

def usages
  roles
end