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:



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

def initialize(ct, entries: [])
  init_lock

  @ct = ct
  @netifs = entries
end

Instance Attribute Details

#ct ⇒ Object (readonly, protected)

Returns the value of attribute ct.



144
145
146
# File 'lib/osctld/net_interface/manager.rb', line 144

def ct
  @ct
end

#netifs ⇒ Object (readonly, protected)

Returns the value of attribute netifs.



144
145
146
# File 'lib/osctld/net_interface/manager.rb', line 144

def netifs
  @netifs
end

Class Method Details

.load(ct, cfg, discover_host_links: true) ⇒ Object

Load interfaces from config

Parameters:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/osctld/net_interface/manager.rb', line 11

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

    NetInterface.validate_container_host_link_claims!(
      ct,
      owner_netifs: entries
    )

    new(ct, entries:)
  end
end

Instance Method Details

#<<(netif) ⇒ Object

Parameters:



38
39
40
# File 'lib/osctld/net_interface/manager.rb', line 38

def <<(netif)
  add(netif)
end

#[](name) ⇒ Object

Parameters:

  • name (String)


115
116
117
# File 'lib/osctld/net_interface/manager.rb', line 115

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

#add(netif) ⇒ Object

Parameters:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/osctld/net_interface/manager.rb', line 43

def add(netif)
  NetInterface.sync_host_link_registry do
    NetInterface.validate_host_link_owner!(
      owner_ct: ct,
      owner_netif: netif
    )
    exclusively { netifs << netif }
    ct.save_config

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

#contains?(name) ⇒ Boolean

Parameters:

  • name (String)

Returns:

  • (Boolean)


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

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

#delete(netif) ⇒ Object

Parameters:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/osctld/net_interface/manager.rb', line 63

def delete(netif)
  NetInterface.sync_host_link_registry do
    if netif.respond_to?(:host_link_identity) &&
       netif.host_link_identity.any?
      raise NetInterface::HostLinkClaimError,
            "network interface #{netif.name.inspect} still owns a host link; " \
            'complete lifecycle cleanup before removing it'
    end

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

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

#dump ⇒ Object

Dump interfaces to config



131
132
133
# File 'lib/osctld/net_interface/manager.rb', line 131

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

#dup(new_ct) ⇒ Object



135
136
137
138
139
140
# File 'lib/osctld/net_interface/manager.rb', line 135

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 ⇒ Object



124
125
126
# File 'lib/osctld/net_interface/manager.rb', line 124

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

#get ⇒ Array<NetInterface::Base>

Returns:



120
121
122
# File 'lib/osctld/net_interface/manager.rb', line 120

def get
  inclusively { netifs.clone }
end

#recovery_tainted? ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# File 'lib/osctld/net_interface/manager.rb', line 93

def recovery_tainted?
  inclusively do
    netifs.any? do |netif|
      netif.respond_to?(:host_link_tainted?) && netif.host_link_tainted?
    end
  end
end

#setup_state_changed? ⇒ Boolean

Returns:

  • (Boolean)


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

def setup_state_changed?
  inclusively do
    netifs.any? do |netif|
      netif.respond_to?(:setup_state_changed?) && netif.setup_state_changed?
    end
  end
end

#take_down ⇒ Object



85
86
87
88
89
90
91
# File 'lib/osctld/net_interface/manager.rb', line 85

def take_down
  NetInterface.sync_host_link_registry do
    get.each do |n|
      n.down if n.is_created?
    end
  end
end