Class: Roast::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Group

Returns a new instance of Group.



5
6
7
8
# File 'lib/roast/group.rb', line 5

def initialize(name)
  @name     = name
  @hosts    = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/roast/group.rb', line 3

def name
  @name
end

Instance Method Details

#<<(host) ⇒ Object



30
31
32
# File 'lib/roast/group.rb', line 30

def <<(host)
  @hosts[host.hostname] = host
end

#[](hostname) ⇒ Object



34
35
36
# File 'lib/roast/group.rb', line 34

def [](hostname)
  @hosts[hostname]
end

#delete_host(entry) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/roast/group.rb', line 46

def delete_host(entry)
  deleted = []
  if entry =~ Host::IP_PATTERN
    @hosts.each { |k, h| deleted << @hosts.delete(k) if h.ip_address == entry }
  else
    @hosts.each { |k, h| deleted << @hosts.delete(k) if h.hostname == entry }
  end

  deleted
end

#disable!Object



10
11
12
# File 'lib/roast/group.rb', line 10

def disable!
  hosts.each { |h| h.disable! }
end

#disabled?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/roast/group.rb', line 18

def disabled?
  hosts.all? { |h| h.disabled? }
end

#enable!Object



14
15
16
# File 'lib/roast/group.rb', line 14

def enable!
  hosts.each { |h| h.enable! }
end

#enabled?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/roast/group.rb', line 22

def enabled?
  hosts.all? { |h| h.enabled? }
end

#find_host(entry) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/roast/group.rb', line 38

def find_host(entry)
  if entry =~ Host::IP_PATTERN
    hosts.select { |h| h.ip_address == entry }
  else
    hosts.select { |h| h.hostname == entry }
  end
end

#hostsObject



26
27
28
# File 'lib/roast/group.rb', line 26

def hosts
  @hosts.values
end

#to_cli(max_indent = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/roast/group.rb', line 57

def to_cli(max_indent = nil)
  string = " - \033[4m#{name}\033[0m\n"
  max = max_indent || hosts.map { |h| h.hostname.size }.max

  hosts.each do |host|
    padding = ' ' * (max - host.hostname.size + 4)
    if host.disabled?
      string << "   \033[31m #{["00D7".to_i(16)].pack("U*")} "
    else
      string << '      '
    end
    string << "#{host.hostname}#{padding}#{host.ip_address}"
    string << "  # #{host.alias}" if host.alias
    string << "\033[0m\n"
  end

  string
end

#to_hosts_fileObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/roast/group.rb', line 76

def to_hosts_file
  max     = hosts.map { |h| h.ip_address.size }.max
  section = "## [#{name}]\n"

  hosts.each do |host|
    padding = ' ' * (max - host.ip_address.size + 4)
    section << '# ' if host.disabled?
    section << "#{host.ip_address}#{padding}#{host.hostname}"
    section <<  "  # #{host.alias}" if host.alias
    section << "\n"
  end

  section
end