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

#initialize(instance) ⇒ HostData

: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

#instanceObject (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

#availability_zoneObject



82
83
84
# File 'lib/ec2/host/host_data.rb', line 82

def availability_zone
  instance.placement.availability_zone
end

#dedicated?Boolean Also known as: dedicated

Returns:

  • (Boolean)


138
139
140
# File 'lib/ec2/host/host_data.rb', line 138

def dedicated?
  tenancy == 'dedicated'
end

#hostnameObject



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

#infoObject



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ec2/host/host_data.rb', line 182

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

#inspectObject



194
195
196
# File 'lib/ec2/host/host_data.rb', line 194

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

#instance_idObject



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

def instance_id
  instance.instance_id
end

#instance_lifecycleObject



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

def instance_lifecycle
  instance.instance_lifecycle
end

#instance_typeObject



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

def instance_type
  instance.instance_type
end

#ipObject

compatibility with dino-host



95
96
97
# File 'lib/ec2/host/host_data.rb', line 95

def ip
  private_ip_address
end

#launch_timeObject



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

def launch_time
  instance.launch_time
end

#match?(condition) ⇒ Boolean

match with condition or not

Parameters:

  • condition (Hash)

    search parameters

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
# File 'lib/ec2/host/host_data.rb', line 146

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

#monitoringObject



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

def monitoring
  instance.monitoring.state
end

#pending?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/ec2/host/host_data.rb', line 129

def pending?
  state == "pending"
end

#placementObject



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

def placement
  instance.placement
end

#private_ip_addressObject



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

def private_ip_address
  instance.private_ip_address
end

#public_ip_addressObject



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

def public_ip_address
  instance.public_ip_address
end

#regionObject



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

def region
  Config.aws_region
end

#rolesObject



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

#running?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/ec2/host/host_data.rb', line 125

def running?
  state == "running"
end

#shutting_down?Boolean

Returns:

  • (Boolean)


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

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

#spot?Boolean Also known as: spot

Returns:

  • (Boolean)


133
134
135
# File 'lib/ec2/host/host_data.rb', line 133

def spot?
  instance_lifecycle == 'spot'
end

#start_dateObject

compatibility with dino-host



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

def start_date
  launch_time
end

#stateObject



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

def state
  instance.state.name
end

#stopped?Boolean

Returns:

  • (Boolean)


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

def stopped?
  state == "stopped"
end

#stopping?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/ec2/host/host_data.rb', line 117

def stopping?
  state == "stopping"
end

#tenancyObject



86
87
88
# File 'lib/ec2/host/host_data.rb', line 86

def tenancy
  instance.placement.tenancy
end

#terminated?Boolean

Returns:

  • (Boolean)


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

def terminated?
  state == "terminated"
end

#to_hashObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ec2/host/host_data.rb', line 155

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,
    "availability_zone" => availability_zone,
    "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,
    "spot" => spot,
  )
end

#usagesObject

compatibility with dino-host



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

def usages
  roles
end