Class: EC2::Host

Inherits:
Object
  • Object
show all
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

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)

    EC2::Host.new(

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

    )

    EC2::Host.new(

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

    )

Raises:

  • (ArgumentError)


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

#conditionsObject (readonly)

Returns the value of attribute conditions



64
65
66
# File 'lib/ec2/host.rb', line 64

def conditions
  @conditions
end

#optionsObject (readonly)

Returns the value of attribute options



64
65
66
# File 'lib/ec2/host.rb', line 64

def options
  @options
end

Class Method Details

.configure(params = {}) ⇒ Object

Configure EC2::Host

Parameters:

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

    see EC2::Host::Config for configurable parameters



52
53
54
# File 'lib/ec2/host.rb', line 52

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

.ec2_clientObject



56
57
58
# File 'lib/ec2/host.rb', line 56

def self.ec2_client
  @ec2_client ||= EC2Client.new
end

.meHost::Data

Returns representing myself

Returns:

  • (Host::Data)

    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

Yield Parameters:

  • data (Host::Data)

    entry



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_clientObject



60
61
62
# File 'lib/ec2/host.rb', line 60

def ec2_client
  self.class.ec2_client
end