Class: Ec2Hosts::Hosts
- Inherits:
-
Object
- Object
- Ec2Hosts::Hosts
- Defined in:
- lib/ec2_hosts/hosts.rb
Instance Attribute Summary collapse
-
#ec2 ⇒ Object
readonly
Returns the value of attribute ec2.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Hosts
constructor
A new instance of Hosts.
- #instances ⇒ Object
- #to_a ⇒ Object
- #vpc ⇒ Object
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( = {}) # Creds must be present in environment @ec2 = AWS::EC2.new @options = if @options[:tags] @processed_tags = (@options[:tags]) elsif @options[:template] @processed_template = process_template(@options[:template]) end end |
Instance Attribute Details
#ec2 ⇒ Object (readonly)
Returns the value of attribute ec2.
4 5 6 |
# File 'lib/ec2_hosts/hosts.rb', line 4 def ec2 @ec2 end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/ec2_hosts/hosts.rb', line 4 def @options end |
Instance Method Details
#instances ⇒ Object
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 [:only_running] @instances = vpc.instances.filter("instance-state-name", "running") else @instances = vpc.instances end end |
#to_a ⇒ Object
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 = (@processed_tags, inst..map.to_a.to_h) elsif @processed_template hostname = parse_template_hostname(@processed_template, inst..map.to_a.to_h) else hostname = inst.["Name"] end hostname = "" unless hostname_valid?(hostname) if hostname == "" && ![: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 ![:public].nil? && hostname.downcase.include?([:public]) if ![: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 |
#vpc ⇒ Object
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", [:vpc]).map {|v|v} if vpcs.length > 1 raise ArgumentError.new("Multiple VPCs with name '#{[:vpc]}' found.") elsif vpcs.length == 0 raise ArgumentError.new("VPC '#{[:vpc]}' not found.") end @vpc = vpcs.first end |