Class: Ec2Hosts::Hosts

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2_hosts/hosts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hosts

Returns a new instance of Hosts.



7
8
9
10
11
12
13
14
15
16
# File 'lib/ec2_hosts/hosts.rb', line 7

def initialize(options = {})
  # Creds must be present in environment
  @ec2 = AWS::EC2.new
  @options = options
  if @options[:tags]
    @processed_tags = process_tags(@options[:tags])
  elsif @options[:template]
    @processed_template = process_template(@options[:template])
  end
end

Instance Attribute Details

#ec2Object (readonly)

Returns the value of attribute ec2.



4
5
6
# File 'lib/ec2_hosts/hosts.rb', line 4

def ec2
  @ec2
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/ec2_hosts/hosts.rb', line 4

def options
  @options
end

Instance Method Details

#instancesObject



32
33
34
35
36
37
38
39
40
# File 'lib/ec2_hosts/hosts.rb', line 32

def instances
  return @instances if instance_variable_defined?(:@instances)

  if options[:only_running]
    @instances = vpc.instances.filter("instance-state-name", "running")
  else
    @instances = vpc.instances
  end
end

#to_aObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ec2_hosts/hosts.rb', line 42

def to_a
  raw = instances.inject({}) do |memo, inst|
    hostname = ""
    if @processed_tags
      hostname = parse_tags_hostname(@processed_tags, inst.tags.map.to_a.to_h)
    elsif @processed_template
      hostname = parse_template_hostname(@processed_template, inst.tags.map.to_a.to_h)
    else
      hostname = inst.tags["Name"]
    end

    hostname = "" unless hostname_valid?(hostname)

    if hostname == "" && !options[:ignore_missing]
      hostname = inst.private_dns_name.split('.').first
    end

    if hostname != ""
      memo[hostname] = inst
    end

    memo
  end.sort.to_h

  list = []

  raw.each do |hostname, inst|
    begin
      if !options[:public].nil? && hostname.downcase.include?(options[:public])
        if !options[:exclude_public]
          # get public ip address
          if ip = inst.public_ip_address
            list << "#{ip} #{hostname}"
            raise HostError.new
          end
        end
      else
        # get private ip address
        if ip = inst.private_ip_address
          list << "#{ip} #{hostname}"
          raise HostError.new
        end
      end
    rescue HostError; end
  end

  list
end

#vpcObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ec2_hosts/hosts.rb', line 18

def vpc
  return @vpc if instance_variable_defined?(:@vpc)

  vpcs = ec2.vpcs.filter("tag:Name", options[:vpc]).map {|v|v}

  if vpcs.length > 1
    raise ArgumentError.new("Multiple VPCs with name '#{options[:vpc]}' found.")
  elsif vpcs.length == 0
    raise ArgumentError.new("VPC '#{options[:vpc]}' not found.")
  end

  @vpc = vpcs.first
end