Class: OsCtld::NetInterface::Manager

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Lockable
Defined in:
lib/osctld/net_interface/manager.rb

Overview

Manages a list of interfaces

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(ct, entries: []) ⇒ Manager

Returns a new instance of Manager.

Parameters:



24
25
26
27
28
29
# File 'lib/osctld/net_interface/manager.rb', line 24

def initialize(ct, entries: [])
  init_lock

  @ct = ct
  @netifs = entries
end

Instance Attribute Details

#ctObject (readonly, protected)

Returns the value of attribute ct.



107
108
109
# File 'lib/osctld/net_interface/manager.rb', line 107

def ct
  @ct
end

#netifsObject (readonly, protected)

Returns the value of attribute netifs.



107
108
109
# File 'lib/osctld/net_interface/manager.rb', line 107

def netifs
  @netifs
end

Class Method Details

.load(ct, cfg) ⇒ Object

Load interfaces from config

Parameters:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/osctld/net_interface/manager.rb', line 11

def self.load(ct, cfg)
  new(
    ct,
    entries: cfg.each_with_index.map do |v, i|
      netif = NetInterface.for(v['type'].to_sym).new(ct, i)
      netif.load(v)
      netif.setup
      netif
    end
  )
end

Instance Method Details

#<<(netif) ⇒ Object

Parameters:



32
33
34
# File 'lib/osctld/net_interface/manager.rb', line 32

def <<(netif)
  add(netif)
end

#[](name) ⇒ Object

Parameters:

  • name (String)


78
79
80
# File 'lib/osctld/net_interface/manager.rb', line 78

def [](name)
  inclusively { netifs.detect { |n| n.name == name } }
end

#add(netif) ⇒ Object

Parameters:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/osctld/net_interface/manager.rb', line 37

def add(netif)
  exclusively { netifs << netif }
  ct.save_config

  Eventd.report(
    :ct_netif,
    action: :add,
    pool: ct.pool.name,
    id: ct.id,
    name: netif.name
  )
end

#contains?(name) ⇒ Boolean

Parameters:

  • name (String)

Returns:

  • (Boolean)


73
74
75
# File 'lib/osctld/net_interface/manager.rb', line 73

def contains?(name)
  inclusively { !(netifs.detect { |n| n.name == name }).nil? }
end

#delete(netif) ⇒ Object

Parameters:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/osctld/net_interface/manager.rb', line 51

def delete(netif)
  exclusively { netifs.delete(netif) }
  ct.save_config

  Eventd.report(
    :ct_netif,
    action: :remove,
    pool: ct.pool.name,
    id: ct.id,
    name: netif.name
  )
end

#dumpObject

Dump interfaces to config



94
95
96
# File 'lib/osctld/net_interface/manager.rb', line 94

def dump
  inclusively { netifs.map(&:save) }
end

#dup(new_ct) ⇒ Object



98
99
100
101
102
103
# File 'lib/osctld/net_interface/manager.rb', line 98

def dup(new_ct)
  ret = super()
  ret.instance_variable_set('@ct', new_ct)
  ret.instance_variable_set('@netifs', netifs.map { |n| n.dup(new_ct) })
  ret
end

#eachObject



87
88
89
# File 'lib/osctld/net_interface/manager.rb', line 87

def each(&)
  get.each(&)
end

#getArray<NetInterface::Base>

Returns:



83
84
85
# File 'lib/osctld/net_interface/manager.rb', line 83

def get
  inclusively { netifs.clone }
end

#take_downObject



64
65
66
67
68
69
70
# File 'lib/osctld/net_interface/manager.rb', line 64

def take_down
  inclusively do
    netifs.each do |n|
      n.down if n.is_up?
    end
  end
end