Class: ForemanDiscovery::LldpNeighbors

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_discovery/lldp_neighbors.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.eventually_make_bond(host) ⇒ Object



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
# File 'app/services/foreman_discovery/lldp_neighbors.rb', line 44

def self.eventually_make_bond(host)
  primary = host.primary_interface
  return if primary.nil?
  return if primary.type == 'Nic::Bond'

  neighbors = ::ForemanDiscovery::LldpNeighbors
                  .from_facts(host.facts_hash)
                  .get_neighbors_by_interface(primary.identifier)

  return if neighbors.nil?

  ip  = primary.ip
  mac = primary.mac
  name = primary.name
  primary.update(
      :primary   => false,
      :provision => false,
      :managed   => false,
      :name      => nil,
      :ip        => nil
  )

  bond = Nic::Bond.create(
      :identifier       => "bond0",
      :attached_devices => neighbors,
      :primary          => true,
      :provision        => true,
      :name             => name,
      :ip               => ip,
      :mac              => mac,
      :host             => host
  )

  bond.save!
  host.interfaces.push(bond)
end

.from_facts(facts) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/foreman_discovery/lldp_neighbors.rb', line 81

def self.from_facts(facts)
  neighbors = self.new
  interfaces = {}
  facts.keys.each do |key|
    key_s = key.to_s
    next unless key_s.start_with? 'lldp_neighbor_'

    property, iface = key_s[14..-1].split('_', 2)
    if property == 'mngAddr'
      protocol, iface = iface.split('_', 2)
      property += '_' + protocol
    end
    interfaces[iface] = {} unless interfaces.has_key? iface
    interfaces[iface][property] = facts[key]
  end

  interfaces.each do |name, properties|
    neighbors.set name, properties
  end

  neighbors
end

Instance Method Details

#get(iface) ⇒ Object



7
8
9
# File 'app/services/foreman_discovery/lldp_neighbors.rb', line 7

def get(iface)
  interfaces[iface]
end

#get_neighbors_by_interface(ifname) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/foreman_discovery/lldp_neighbors.rb', line 28

def get_neighbors_by_interface(ifname)
  return nil unless interfaces.has_key? ifname

  interface = get(ifname)
  return nil unless interface.has_key? 'PVID'

  list = []
  interfaces.each do |name, neighbor|
    next unless neighbor.has_key? 'PVID'
    list.push name if neighbor['PVID'] == interface['PVID']
  end

  return if list.size < 2
  list.sort
end

#interfacesObject



24
25
26
# File 'app/services/foreman_discovery/lldp_neighbors.rb', line 24

def interfaces
  @interfaces ||= {}
end

#list_by_pvidObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/foreman_discovery/lldp_neighbors.rb', line 11

def list_by_pvid
  list = {}
  interfaces.each do |name, neighbor|
    next unless neighbor.has_key? 'PVID'
    vlan = neighbor['PVID']
    list[vlan] = [] unless list.has_key? vlan
    list[vlan].push name
  end

  list.each { |_, interfaces| interfaces.sort!}
  list
end

#set(iface, neighbor) ⇒ Object



3
4
5
# File 'app/services/foreman_discovery/lldp_neighbors.rb', line 3

def set(iface, neighbor)
  interfaces[iface] = neighbor
end