Class: GCE::Host
- Inherits:
-
Object
- Object
- GCE::Host
- 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
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.configure(params = {}) ⇒ Object
Configure GCE::Host.
- .gce_client ⇒ Object
-
.me ⇒ Host::Data
Representing myself.
Instance Method Summary collapse
- #each {|data| ... } ⇒ Object
- #gce_client ⇒ Object
-
#initialize(*conditions) ⇒ Host
constructor
A new instance of Host.
Constructor Details
#initialize(*conditions) ⇒ Host
Returns a new instance of Host.
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
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
66 67 68 |
# File 'lib/gce/host.rb', line 66 def conditions @conditions end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
66 67 68 |
# File 'lib/gce/host.rb', line 66 def @options end |
Class Method Details
.configure(params = {}) ⇒ Object
Configure GCE::Host
54 55 56 |
# File 'lib/gce/host.rb', line 54 def self.configure(params = {}) Config.configure(params) end |
.gce_client ⇒ Object
58 59 60 |
# File 'lib/gce/host.rb', line 58 def self.gce_client @gce_client ||= GCEClient.new end |
.me ⇒ Host::Data
Returns 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
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_client ⇒ Object
62 63 64 |
# File 'lib/gce/host.rb', line 62 def gce_client self.class.gce_client end |