Class: Pec::Command::Hosts

Inherits:
Base
  • Object
show all
Defined in:
lib/pec/command/hosts.rb

Class Method Summary collapse

Methods inherited from Base

not_fetch, print_exception, run

Class Method Details

.after_doObject



34
35
36
37
38
# File 'lib/pec/command/hosts.rb', line 34

def self.after_do
  m = "# " + ("-" * 16)
  n = ("-" * 17)
  Pec::Logger.warning  m + " pec end " + n
end

.before_doObject



29
30
31
32
# File 'lib/pec/command/hosts.rb', line 29

def self.before_do
  @_error = nil
  Pec::Logger.warning "# ------ #{Date.today.strftime("%Y%m%d%H%M%S")} pec add start ------"
end

.ip_addresses(server) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/pec/command/hosts.rb', line 15

def self.ip_addresses(server)
  server.addresses.map do |ethers|
    ethers[1].map do |ether|
      ether["addr"]
    end
  end.flatten
end

.private_ip?(target) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/pec/command/hosts.rb', line 23

def self.private_ip?(target)
  ::IPAddr.new("10.0.0.0/8").include?(target) ||
  ::IPAddr.new("172.16.0.0/12").include?(target) ||
  ::IPAddr.new("192.168.0.0/16").include?(target)
end

.task(server, config) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/pec/command/hosts.rb', line 4

def self.task(server, config)
  ip_addresses(server).each do |i|
    host_name = private_ip?(i) ? "#{config.name}.lan" : config.name
    puts sprintf(
      "%-15s %-35s",
      i,
      host_name,
    )
  end if server
end