Class: GCE::Host::HostData

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

Overview

Represents each host

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ HostData

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

and OPTIONAL_ARRAY_KEYS, OPTIONAL_STRING_KEYS



14
15
16
# File 'lib/gce/host/host_data.rb', line 14

def initialize(instance)
  @instance = instance
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance



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

def instance
  @instance
end

Instance Method Details

#creation_timestampObject



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

def creation_timestamp
  instance.creation_timestamp
end

#hostnameObject



18
19
20
# File 'lib/gce/host/host_data.rb', line 18

def hostname
  instance.name
end

#infoObject



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/gce/host/host_data.rb', line 147

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



159
160
161
# File 'lib/gce/host/host_data.rb', line 159

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

#instance_idObject



46
47
48
# File 'lib/gce/host/host_data.rb', line 46

def instance_id
  instance.id
end

#ipObject

compatibility with dino-host



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

def ip
  private_ip_address
end

#machine_typeObject



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

def machine_type
  instance.machine_type.split('/').last
end

#match?(condition) ⇒ Boolean

match with condition or not

Parameters:

  • condition (Hash)

    search parameters

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# File 'lib/gce/host/host_data.rb', line 116

def match?(condition)
  return false if !condition[Config.status.to_sym] and (terminated? or stopping?)
  return false unless role_match?(condition)
  return false unless status_match?(condition)
  return false unless instance_match?(condition)
  true
end

#private_ip_addressObject



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

def private_ip_address
  instance.network_interfaces.first.network_ip
end

#private_ip_addressesObject



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

def private_ip_addresses
  instance.network_interfaces.map(&:network_ip)
end

#provisioning?Boolean

Returns:

  • (Boolean)


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

def provisioning?
  instance.status == "PROVISIONING"
end

#public_ip_addressObject



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

def public_ip_address
  instance.network_interfaces.first.access_configs.first.nat_ip
end

#public_ip_addressesObject



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

def public_ip_addresses
  instance.network_interfaces.map {|i| i.access_configs.map(&:nat_ip) }.flatten(1)
end

#rolesObject



22
23
24
25
26
# 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?Boolean

Returns:

  • (Boolean)


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

def running?
  instance.status == "RUNNING"
end

#staging?Boolean

Returns:

  • (Boolean)


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

def staging?
  instance.status == "STAGING"
end

#start_dateObject

compatibility with dino-host



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

def start_date
  creation_timestamp
end

#stopping?Boolean

Returns:

  • (Boolean)


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

def stopping?
  instance.status == "STOPPING"
end

#terminated?Boolean

Returns:

  • (Boolean)


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

def terminated?
  instance.status == "TERMINATED"
end

#to_hashObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gce/host/host_data.rb', line 124

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

#usagesObject

compatibility with dino-host



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

def usages
  roles
end

#zoneObject



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

def zone
  instance.zone.split('/').last
end