Class: EC2::Host
- Inherits:
-
Object
- Object
- EC2::Host
- Includes:
- Enumerable
- Defined in:
- lib/ec2/host.rb,
lib/ec2/host/cli.rb,
lib/ec2/host/config.rb,
lib/ec2/host/version.rb,
lib/ec2/host/hash_util.rb,
lib/ec2/host/host_data.rb,
lib/ec2/host/role_data.rb,
lib/ec2/host/ec2_client.rb,
lib/ec2/host/string_util.rb
Overview
Search EC2 hosts from tags
require 'ec2-host'
# Search by `Name` tag
EC2::Host.new(hostname: 'test').first # => test
# Search by `Roles` tag
EC2::Host.new(
role: 'admin:haikanko',
).each do |host|
# ...
end
or
EC2::Host.new(
role1: 'admin',
role2: 'haikanko',
).each do |host|
# ...
end
# Or search
EC2::Host.new(
{
role1: 'db',
role2: 'master',
},
{
role1: 'web',
}
).each do |host|
# ...
end
EC2::Host.me.hostname # => 'test'
Defined Under Namespace
Modules: HashUtil, StringUtil Classes: CLI, Config, EC2Client, HostData, RoleData
Constant Summary
- VERSION =
'0.6.0'
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 EC2::Host.
- .ec2_client ⇒ Object
-
.me ⇒ Host::Data
Representing myself.
Instance Method Summary collapse
- #each {|data| ... } ⇒ Object
- #ec2_client ⇒ Object
-
#initialize(*conditions) ⇒ Host
constructor
A new instance of Host.
Constructor Details
#initialize(*conditions) ⇒ Host
Returns a new instance of Host
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ec2/host.rb', line 82 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, StringUtil.stringify_symbols(Array(v))]}] 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
64 65 66 |
# File 'lib/ec2/host.rb', line 64 def conditions @conditions end |
#options ⇒ Object (readonly)
Returns the value of attribute options
64 65 66 |
# File 'lib/ec2/host.rb', line 64 def @options end |
Class Method Details
.configure(params = {}) ⇒ Object
Configure EC2::Host
52 53 54 |
# File 'lib/ec2/host.rb', line 52 def self.configure(params = {}) Config.configure(params) end |
.ec2_client ⇒ Object
56 57 58 |
# File 'lib/ec2/host.rb', line 56 def self.ec2_client @ec2_client ||= EC2Client.new end |
.me ⇒ Host::Data
Returns representing myself
42 43 44 45 46 47 |
# File 'lib/ec2/host.rb', line 42 def self.me new(instance_id: ec2_client.instance_id).each do |d| return d end raise 'Not Found' end |
Instance Method Details
#each {|data| ... } ⇒ Object
101 102 103 104 105 106 |
# File 'lib/ec2/host.rb', line 101 def each(&block) @conditions.each do |condition| search(ec2_client.instances(condition), condition, &block) end return self end |
#ec2_client ⇒ Object
60 61 62 |
# File 'lib/ec2/host.rb', line 60 def ec2_client self.class.ec2_client end |