Class: Slugforge::HostGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/slugforge/models/host_group.rb,
lib/slugforge/models/host_group/aws_tag_group.rb,
lib/slugforge/models/host_group/hostname_group.rb,
lib/slugforge/models/host_group/ip_address_group.rb,
lib/slugforge/models/host_group/ec2_instance_group.rb,
lib/slugforge/models/host_group/security_group_group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, compute) ⇒ HostGroup

Returns a new instance of HostGroup.



23
24
25
# File 'lib/slugforge/models/host_group.rb', line 23

def initialize(pattern, compute)
  @name = pattern
end

Instance Attribute Details

#hostsObject (readonly)

Returns the value of attribute hosts.



9
10
11
# File 'lib/slugforge/models/host_group.rb', line 9

def hosts
  @hosts
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/slugforge/models/host_group.rb', line 9

def name
  @name
end

Class Method Details

.detect(pattern, compute) ⇒ Object



59
60
61
62
63
# File 'lib/slugforge/models/host_group.rb', line 59

def self.detect(pattern, compute)
  return nil unless pattern =~ self.matcher
  group = self.new(pattern, compute)
  group.hosts.empty? ? nil : group
end

.discover(patterns, compute) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/slugforge/models/host_group.rb', line 11

def self.discover(patterns, compute)
  patterns.map do |pattern|
    IpAddressGroup.detect(pattern, compute) ||
    Ec2InstanceGroup.detect(pattern, compute) ||
    HostnameGroup.detect(pattern, compute) ||
    AwsTagGroup.detect(pattern, compute) ||
    SecurityGroupGroup.detect(pattern, compute) ||
    # If nothing detected, return a "null" group
    HostGroup.new(pattern, compute)
  end
end

Instance Method Details

#hosts_for_action(action) ⇒ Object



55
56
57
# File 'lib/slugforge/models/host_group.rb', line 55

def hosts_for_action(action)
  @hosts.select { |host| host.has_action?(action) }
end

#install_allObject



27
28
29
30
# File 'lib/slugforge/models/host_group.rb', line 27

def install_all
  return if @hosts.nil?
  @hosts.each { |host| host.add_action(:install) }
end

#install_number_of_hosts(value) ⇒ Object



38
39
40
41
42
# File 'lib/slugforge/models/host_group.rb', line 38

def install_number_of_hosts(value)
  return if @hosts.nil?
  count = [@hosts.count, value].min
  sorted_hosts[0...count].each { |host| host.add_action(:install) }
end

#install_percent_of_hosts(value) ⇒ Object



32
33
34
35
36
# File 'lib/slugforge/models/host_group.rb', line 32

def install_percent_of_hosts(value)
  return if @hosts.nil?
  count = (@hosts.count * value / 100.0).ceil
  sorted_hosts[0...count].each { |host| host.add_action(:install) }
end

#sorted_hostsObject



44
45
46
47
48
49
# File 'lib/slugforge/models/host_group.rb', line 44

def sorted_hosts
  # We sort the hosts by IP to make the order deterministic before we filter
  # by number or percent. That way when we move from 5% to 10% we end up at
  # 10% of the hosts, not some value between 10% and 15%.
  @hosts.sort_by { |host| host.ip }
end

#success?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/slugforge/models/host_group.rb', line 51

def success?
  @hosts.all?(&:success?)
end