class EC2::Host::HostData

Represents each host

Attributes

instance[R]

Public Class Methods

new(instance) click to toggle source

: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

# File lib/ec2/host/host_data.rb, line 15
def initialize(instance)
  @instance = instance
end

Private Class Methods

display_short_info?() click to toggle source

compatibility with dino-host

If Service,Status,Tags tags are defined

OPTIONAL_STRING_TAGS=Service,Status
OPTIONAL_ARRAY_TAGS=Tags

show in short format, otherwise, same with to_hash.to_s

# File lib/ec2/host/host_data.rb, line 258
def self.display_short_info?
  return @display_short_info unless @display_short_info.nil?
  @display_short_info = (method_defined?(:service) and method_defined?(:status) and method_defined?(:tags))
end

Public Instance Methods

availability_zone() click to toggle source
# File lib/ec2/host/host_data.rb, line 82
def availability_zone
  instance.placement.availability_zone
end
dedicated()
Alias for: dedicated?
dedicated?() click to toggle source
# File lib/ec2/host/host_data.rb, line 138
def dedicated?
  tenancy == 'dedicated'
end
Also aliased as: dedicated
hostname() click to toggle source
# 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
info() click to toggle source
# 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
inspect() click to toggle source
# File lib/ec2/host/host_data.rb, line 194
def inspect
  sprintf "#<Aws::Host::HostData %s>", info
end
instance_id() click to toggle source
# File lib/ec2/host/host_data.rb, line 50
def instance_id
  instance.instance_id
end
instance_lifecycle() click to toggle source
# File lib/ec2/host/host_data.rb, line 90
def instance_lifecycle
  instance.instance_lifecycle
end
instance_type() click to toggle source
# File lib/ec2/host/host_data.rb, line 54
def instance_type
  instance.instance_type
end
ip() click to toggle source

compatibility with dino-host

# File lib/ec2/host/host_data.rb, line 95
def ip
  private_ip_address
end
launch_time() click to toggle source
# File lib/ec2/host/host_data.rb, line 66
def launch_time
  instance.launch_time
end
match?(condition) click to toggle source

match with condition or not

@param [Hash] condition search parameters

# 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
monitoring() click to toggle source
# File lib/ec2/host/host_data.rb, line 74
def monitoring
  instance.monitoring.state
end
pending?() click to toggle source
# File lib/ec2/host/host_data.rb, line 129
def pending?
  state == "pending"
end
placement() click to toggle source
# File lib/ec2/host/host_data.rb, line 78
def placement
  instance.placement
end
private_ip_address() click to toggle source
# File lib/ec2/host/host_data.rb, line 58
def private_ip_address
  instance.private_ip_address
end
public_ip_address() click to toggle source
# File lib/ec2/host/host_data.rb, line 62
def public_ip_address
  instance.public_ip_address
end
region() click to toggle source
# File lib/ec2/host/host_data.rb, line 32
def region
  Config.aws_region
end
roles() click to toggle source
# 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?() click to toggle source
# File lib/ec2/host/host_data.rb, line 125
def running?
  state == "running"
end
shutting_down?() click to toggle source
# File lib/ec2/host/host_data.rb, line 113
def shutting_down?
  state == "shutting-down"
end
spot()
Alias for: spot?
spot?() click to toggle source
# File lib/ec2/host/host_data.rb, line 133
def spot?
  instance_lifecycle == 'spot'
end
Also aliased as: spot
start_date() click to toggle source

compatibility with dino-host

# File lib/ec2/host/host_data.rb, line 100
def start_date
  launch_time
end
state() click to toggle source
# File lib/ec2/host/host_data.rb, line 70
def state
  instance.state.name
end
stopped?() click to toggle source
# File lib/ec2/host/host_data.rb, line 121
def stopped?
  state == "stopped"
end
stopping?() click to toggle source
# File lib/ec2/host/host_data.rb, line 117
def stopping?
  state == "stopping"
end
tenancy() click to toggle source
# File lib/ec2/host/host_data.rb, line 86
def tenancy
  instance.placement.tenancy
end
terminated?() click to toggle source
# File lib/ec2/host/host_data.rb, line 109
def terminated?
  state == "terminated"
end
to_hash() click to toggle source
# 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
usages() click to toggle source

compatibility with dino-host

# File lib/ec2/host/host_data.rb, line 105
def usages
  roles
end

Private Instance Methods

find_array_tag(key) click to toggle source
# File lib/ec2/host/host_data.rb, line 205
def find_array_tag(key)
  v = instance.tags.find {|tag| tag.key == key }
  v ? v.value.split(Config.array_tag_delimiter) : []
end
find_string_tag(key) click to toggle source
# File lib/ec2/host/host_data.rb, line 200
def find_string_tag(key)
  v = instance.tags.find {|tag| tag.key == key }
  v ? v.value : ''
end
instance_match?(condition) click to toggle source
# File lib/ec2/host/host_data.rb, line 227
def instance_match?(condition)
  excepts = [:role, :usage]
  1.upto(Config.role_max_depth).each {|i| excepts << "role#{i}".to_sym }
  1.upto(Config.role_max_depth).each {|i| excepts << "usage#{i}".to_sym }
  condition = HashUtil.except(condition, *excepts)
  condition.each do |key, values|
    v = instance_variable_recursive_get(key)
    if v.is_a?(Array)
      return false unless v.find {|_| values.include?(_) }
    else
      return false unless values.include?(v)
    end
  end
  true
end
instance_variable_recursive_get(key) click to toggle source

“instance.instance_id” => self.instance.instance_id

# File lib/ec2/host/host_data.rb, line 244
def instance_variable_recursive_get(key)
  v = self
  key.to_s.split('.').each {|k| v = v.send(k) }
  v
end
role_match?(condition) click to toggle source
# File lib/ec2/host/host_data.rb, line 210
def role_match?(condition)
  # usage is an alias of role
  if role = (condition[:role] || condition[:usage])
    role.any? do |r|
      parts = r.split(Config.role_tag_delimiter, Config.role_max_depth)
      next true if parts.compact.empty? # no role conditions
      roles.find {|role| role.match?(*parts) }
    end
  else
    parts = 1.upto(Config.role_max_depth).map do |i|
      condition["role#{i}".to_sym] || condition["usage#{i}".to_sym]
    end
    return true if parts.compact.empty? # no role conditions
    roles.find {|role| role.match?(*parts) }
  end
end