class GCE::Host

Search GCE hosts from labels

require 'gce-host'
# Search by hostname
GCE::Host.new(hostname: 'test').first # => test

# Search by `roles` label
GCE::Host.new(
  role: 'admin:haikanko',
).each do |host|
  # ...
end

or

GCE::Host.new(
  role1: 'admin',
  role2: 'haikanko',
).each do |host|
  # ...
end

# Or search
GCE::Host.new(
  {
      role1: 'db',
      role2: 'master',
  },
  {
      role1: 'web',
  }
).each do |host|
    # ...
end

GCE::Host.me.hostname # => 'test'

Constants

VERSION

Attributes

conditions[R]
options[R]

Public Class Methods

configure(params = {}) click to toggle source

Configure GCE::Host

@param [Hash] params see GCE::Host::Config for configurable parameters

# File lib/gce/host.rb, line 54
def self.configure(params = {})
  Config.configure(params)
end
gce_client() click to toggle source
# File lib/gce/host.rb, line 58
def self.gce_client
  @gce_client ||= GCEClient.new
end
me() click to toggle source

@return [Host::Data] representing myself

# File lib/gce/host.rb, line 44
def self.me
  new(hostname: Socket.gethostname).each do |d|
    return d
  end
  raise 'Not Found'
end
new(*conditions) click to toggle source

@param [Array of Hash, or Hash] conditions (and options)

GCE::Host.new(
  hostname: 'test',
  options: {a: 'b'}
)

GCE::Host.new(
  {
    hostname: 'foo',
  },
  {
    hostname: 'bar',
  },
  options: {a: 'b'}
)
# File lib/gce/host.rb, line 84
def initialize(*conditions)
  conditions = [{}] if conditions.empty?
  conditions = [conditions] if conditions.kind_of?(Hash)
  @options = {}
  if conditions.size == 1
    @options = conditions.first.delete(:options) || {}
  else
    index = conditions.find_index {|condition| condition.has_key?(:options) }
    @options = conditions.delete_at(index)[:options] if index
  end
  raise ArgumentError, "Hash expected (options)" unless @options.is_a?(Hash)
  @conditions = []
  conditions.each do |condition|
    @conditions << Hash[condition.map {|k, v| [k, Array(v).map(&:to_s)]}]
  end
  raise ArgumentError, "Array of Hash, or Hash expected (conditions)" unless @conditions.all? {|h| h.kind_of?(Hash)}
end

Public Instance Methods

each(&block) click to toggle source

@yieldparam [Host::Data] data entry

# File lib/gce/host.rb, line 103
def each(&block)
  @conditions.each do |condition|
    search(gce_client.instances(condition), condition, &block)
  end
  return self
end
gce_client() click to toggle source
# File lib/gce/host.rb, line 62
def gce_client
  self.class.gce_client
end

Private Instance Methods