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



110
111
112
# File 'lib/osctld/net_interface/manager.rb', line 110

def ct
  @ct
end

#netifsObject (readonly, protected)

Returns the value of attribute netifs



110
111
112
# File 'lib/osctld/net_interface/manager.rb', line 110

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)


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

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)


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

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



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

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

#dup(new_ct) ⇒ Object



102
103
104
105
106
107
# File 'lib/osctld/net_interface/manager.rb', line 102

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

#each(&block) ⇒ Object



91
92
93
# File 'lib/osctld/net_interface/manager.rb', line 91

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

#getArray<NetInterface::Base>

Returns:



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

def get
  inclusively { netifs.clone }
end

#take_downObject



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

def take_down
  inclusively do
    netifs.each do |n|
      if n.is_a?(NetInterface::Veth)
        n.down(n.veth) if n.veth
      else
        n.down
      end
    end
  end
end