Class: Awsome::Matchmaker

Inherits:
Object
  • Object
show all
Defined in:
lib/awsome/matchmaker.rb

Constant Summary collapse

@@signature_fields =
%w( ami_id key instance_type availability_zone security_group_ids )

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instances, requirements) ⇒ Matchmaker

Returns a new instance of Matchmaker.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/awsome/matchmaker.rb', line 5

def initialize(instances, requirements)
  @requirement_pools = requirements.instances.reject do |i| 
    requirements.options.except_instance_ids.include?(i.properties['instance_id']) 
  end.group_by { |i| signature_for(i) }

  @instance_pools = instances.reject do |i| 
    requirements.options.except_instance_ids.include?(i.properties['instance_id']) 
  end.group_by { |i| signature_for(i) }

  @signatures = @requirement_pools.keys.to_set + @instance_pools.keys.to_set

  @signatures.each do |s|
    @requirement_pools[s] ||= []
    @instance_pools[s] ||= []

    # nil indicates new instance will be brought up
    shortage = @requirement_pools[s].size - @instance_pools[s].size
    @instance_pools[s] += [nil]*shortage if shortage > 0
  end
end

Class Method Details

.to_table(matches, title = 'Matches') ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/awsome/matchmaker.rb', line 36

def self.to_table(matches, title='Matches')
  rows = []

  # enumerate match rows
  matches.each do |signature, match|
    match[:i_pool].each_with_index do |i, idx|
      r = match[:r_pool][idx]
      rows << [
        r.nil? ? 'terminate' : 'deploy',
        i.nil? ? '(new)' : i.id,
        (r || i).packages.to_a.join("\n"),
        (r || i).volumes.to_a.join("\n"),
        (r || i).elbs.collect{|e| e.is_a?(String) ? e : e.name}.join("\n"),
        (r || i).ami_id,
        (r || i).key,
        (r || i).instance_type,
        (r || i).availability_zone,
        (r || i).security_group_ids,
      ]
      rows << :separator
    end
  end
  
  # remove last unnecessary separator
  rows.pop if rows.any?

  headings = %w(action instance packages volumes elbs ami key type zone secgroup)
  Terminal::Table.new :headings => headings, :rows => rows, :title => title
end

Instance Method Details

#matchesObject



32
33
34
# File 'lib/awsome/matchmaker.rb', line 32

def matches
  @signatures.reduce({}) { |memo, s| memo.merge(s => best_match(s)) }
end

#signature_for(instance) ⇒ Object



28
29
30
# File 'lib/awsome/matchmaker.rb', line 28

def signature_for(instance)
  @@signature_fields.collect { |k| instance.properties[k] }.join(',')
end