class EC2::Host

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'

Constants

VERSION

Attributes

conditions[R]
options[R]

Public Class Methods

configure(params = {}) click to toggle source

Configure EC2::Host

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

# File lib/ec2/host.rb, line 52
def self.configure(params = {})
  Config.configure(params)
end
ec2_client() click to toggle source
# File lib/ec2/host.rb, line 56
def self.ec2_client
  @ec2_client ||= EC2Client.new
end
me() click to toggle source

@return [Host::Data] representing myself

# 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
new(*conditions) click to toggle source

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

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

EC2::Host.new(
  {
    hostname: 'foo',
  },
  {
    hostname: 'bar',
  },
  options: {a: 'b'}
)
# 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

Public Instance Methods

each(&block) click to toggle source

@yieldparam [Host::Data] data entry

# 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() click to toggle source
# File lib/ec2/host.rb, line 60
def ec2_client
  self.class.ec2_client
end

Private Instance Methods