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



77
78
79
# File 'lib/gce/host/host_data.rb', line 77

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



150
151
152
153
154
155
156
157
158
159
160
# 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

#inspectObject



162
163
164
# File 'lib/gce/host/host_data.rb', line 162

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



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

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)


120
121
122
123
124
125
# 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_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)


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

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

#public_ip_addressObject



66
67
68
69
# 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_addressesObject



71
72
73
74
75
# 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

#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)


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

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

#staging?Boolean

Returns:

  • (Boolean)


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

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

#start_dateObject

compatibility with dino-host



87
88
89
# File 'lib/gce/host/host_data.rb', line 87

def start_date
  creation_timestamp
end

#stopping?Boolean

Returns:

  • (Boolean)


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

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

#terminated?Boolean

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

Returns:

  • (Boolean)


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

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

#to_hashObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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

#usagesObject

compatibility with dino-host



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

def usages
  roles
end

#zoneObject



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

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