class GCE::Host::HostData

Represents each host

Attributes

instance[R]

Public Class Methods

new(instance) click to toggle source

:hostname, # hostname :roles, # roles.split(',') such as web:app1,db:app1 :instance, # Aws::GCE::Types::Instance itself

and OPTIONAL_ARRAY_KEYS, OPTIONAL_STRING_KEYS

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

Private Class Methods

display_short_info?() click to toggle source

compatibility with dono-host

If service,status,tags keys are defined

OPTIONAL_STRING_KEYS=service,status
OPTIONAL_ARRAY_KEYS=tags

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

# File lib/gce/host/host_data.rb, line 233
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

creation_timestamp() click to toggle source
# File lib/gce/host/host_data.rb, line 77
def creation_timestamp
  instance.creation_timestamp
end
hostname() click to toggle source
# File lib/gce/host/host_data.rb, line 18
def hostname
  instance.name
end
info() click to toggle source
# File lib/gce/host/host_data.rb, line 150
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/gce/host/host_data.rb, line 162
def inspect
  sprintf "#<GCE::Host::HostData %s>", info
end
instance_id() click to toggle source
# File lib/gce/host/host_data.rb, line 46
def instance_id
  instance.id
end
ip() click to toggle source

compatibility with dino-host

# File lib/gce/host/host_data.rb, line 82
def ip
  private_ip_address
end
machine_type() click to toggle source
# File lib/gce/host/host_data.rb, line 54
def machine_type
  instance.machine_type.split('/').last
end
match?(condition) click to toggle source

match with condition or not

@param [Hash] condition search parameters

# File lib/gce/host/host_data.rb, line 120
def match?(condition)
  return false unless role_match?(condition)
  return false unless status_match?(condition)
  return false unless instance_match?(condition)
  true
end
private_ip_address() click to toggle source
# File lib/gce/host/host_data.rb, line 58
def private_ip_address
  instance.network_interfaces.first.network_ip
end
private_ip_addresses() click to toggle source
# File lib/gce/host/host_data.rb, line 62
def private_ip_addresses
  instance.network_interfaces.map(&:network_ip)
end
provisioning?() click to toggle source
# File lib/gce/host/host_data.rb, line 113
def provisioning?
  instance.status == "PROVISIONING"
end
public_ip_address() click to toggle source
# File lib/gce/host/host_data.rb, line 66
def public_ip_address
  access_configs = instance.network_interfaces.first.access_configs
  access_configs ? access_configs.first.nat_ip : nil
end
public_ip_addresses() click to toggle source
# File lib/gce/host/host_data.rb, line 71
def public_ip_addresses
  instance.network_interfaces.map {|i|
    i.access_configs ? i.access_configs.map(&:nat_ip) : nil
  }.flatten(1).compact
end
roles() click to toggle source
# File lib/gce/host/host_data.rb, line 22
def roles
  return @roles if @roles
  roles = find_array_key(Config.roles_key)
  @roles = roles.map {|role| GCE::Host::RoleData.build(role) }
end
running?() click to toggle source
# File lib/gce/host/host_data.rb, line 105
def running?
  instance.status == "RUNNING"
end
staging?() click to toggle source
# File lib/gce/host/host_data.rb, line 109
def staging?
  instance.status == "STAGING"
end
start_date() click to toggle source

compatibility with dino-host

# File lib/gce/host/host_data.rb, line 87
def start_date
  creation_timestamp
end
stopping?() click to toggle source
# File lib/gce/host/host_data.rb, line 101
def stopping?
  instance.status == "STOPPING"
end
terminated?() click to toggle source

NOTE: this is stopped status in the case of GCE unlike EC2

# File lib/gce/host/host_data.rb, line 97
def terminated?
  instance.status == "TERMINATED"
end
to_hash() click to toggle source
# File lib/gce/host/host_data.rb, line 127
def to_hash
  params = {
    "hostname" => hostname,
    "roles" => roles,
  }
  Config.optional_string_keys.each do |key|
    field = StringUtil.underscore(key)
    params[field] = send(field)
  end
  Config.optional_array_keys.each do |key|
    field = StringUtil.underscore(key)
    params[field] = send(field)
  end
  params.merge!(
    "zone" => zone,
    "machine_type" => machine_type,
    "private_ip_address" => private_ip_address,
    "public_ip_address" => public_ip_address,
    "creation_timestamp" => creation_timestamp,
    Config.status => send(Config.status),
  )
end
usages() click to toggle source

compatibility with dino-host

# File lib/gce/host/host_data.rb, line 92
def usages
  roles
end
zone() click to toggle source
# File lib/gce/host/host_data.rb, line 50
def zone
  instance.zone.split('/').last
end

Private Instance Methods

find_array_key(key) click to toggle source
# File lib/gce/host/host_data.rb, line 173
def find_array_key(key)
  item = instance.metadata.items.find {|item| item.key == key } if instance.metadata.items
  item ? item.value.split(Config.array_value_delimiter) : []
end
find_string_key(key) click to toggle source
# File lib/gce/host/host_data.rb, line 168
def find_string_key(key)
  item = instance.metadata.items.find {|item| item.key == key } if instance.metadata.items
  item ? item.value : ''
end
instance_match?(condition) click to toggle source
# File lib/gce/host/host_data.rb, line 202
def instance_match?(condition)
  excepts = [:role, :usage, Config.status.to_sym]
  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?(_.to_s) }
    else
      return false unless values.include?(v.to_s)
    end
  end
  true
end
instance_variable_recursive_get(key) click to toggle source

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

# File lib/gce/host/host_data.rb, line 219
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/gce/host/host_data.rb, line 178
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_value_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
status_match?(condition) click to toggle source
# File lib/gce/host/host_data.rb, line 195
def status_match?(condition)
  if values = condition[Config.status.to_sym]
    return false unless values.map(&:downcase).include?(send(Config.status).downcase)
  end
  true
end