Class: GCE::Host

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gce/host.rb,
lib/gce/host/cli.rb,
lib/gce/host/config.rb,
lib/gce/host/version.rb,
lib/gce/host/hash_util.rb,
lib/gce/host/host_data.rb,
lib/gce/host/role_data.rb,
lib/gce/host/gce_client.rb,
lib/gce/host/string_util.rb

Overview

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'

Defined Under Namespace

Modules: HashUtil, StringUtil Classes: CLI, Config, ConfigError, GCEClient, HostData, RoleData

Constant Summary collapse

VERSION =
'0.5.6'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*conditions) ⇒ Host

Returns a new instance of Host.

Parameters:

  • conditions (Array of Hash, or Hash)

    (and options)

    GCE::Host.new(

    hostname: 'test',
    options: {a: 'b'}
    

    )

    GCE::Host.new(

    {
      hostname: 'foo',
    },
    {
      hostname: 'bar',
    },
    options: {a: 'b'}
    

    )

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



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

def conditions
  @conditions
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.configure(params = {}) ⇒ Object

Configure GCE::Host

Parameters:

  • params (Hash) (defaults to: {})

    see GCE::Host::Config for configurable parameters



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

def self.configure(params = {})
  Config.configure(params)
end

.gce_clientObject



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

def self.gce_client
  @gce_client ||= GCEClient.new
end

.meHost::Data

Returns representing myself.

Returns:

  • (Host::Data)

    representing myself



44
45
46
47
48
49
# File 'lib/gce/host.rb', line 44

def self.me
  new(hostname: Socket.gethostname).each do |d|
    return d
  end
  raise 'Not Found'
end

Instance Method Details

#each {|data| ... } ⇒ Object

Yield Parameters:

  • data (Host::Data)

    entry



103
104
105
106
107
108
# 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_clientObject



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

def gce_client
  self.class.gce_client
end