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

- (HostData) initialize(instance)

: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

- (Object) instance (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

- (Object) creation_timestamp



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

def creation_timestamp
  instance.creation_timestamp
end

- (Object) hostname



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

def hostname
  instance.name
end

- (Object) info



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

- (Object) inspect



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

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

- (Object) instance_id



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

def instance_id
  instance.id
end

- (Object) ip

compatibility with dino-host



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

def ip
  private_ip_address
end

- (Object) machine_type



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

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

- (Boolean) match?(condition)

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

- (Object) private_ip_address



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

def private_ip_address
  instance.network_interfaces.first.network_ip
end

- (Object) private_ip_addresses



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

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

- (Boolean) provisioning?

Returns:

  • (Boolean)


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

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

- (Object) public_ip_address



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

- (Object) public_ip_addresses



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

- (Object) roles



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

- (Boolean) running?

Returns:

  • (Boolean)


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

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

- (Boolean) staging?

Returns:

  • (Boolean)


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

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

- (Object) start_date

compatibility with dino-host



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

def start_date
  creation_timestamp
end

- (Boolean) stopping?

Returns:

  • (Boolean)


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

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

- (Boolean) terminated?

Returns:

  • (Boolean)


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

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

- (Object) to_hash



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

- (Object) usages

compatibility with dino-host



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

def usages
  roles
end

- (Object) zone



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

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