Class: OsCtld::Commands::User::LxcUsernet

Inherits:
Base
  • Object
show all
Defined in:
lib/osctld/commands/user/lxc_usernet.rb

Constant Summary collapse

LXC_USERNET =
'/etc/lxc/lxc-usernet'.freeze
@@mutex =
Mutex.new

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

Instance Method Details

#executeObject



11
12
13
14
15
16
17
# File 'lib/osctld/commands/user/lxc_usernet.rb', line 11

def execute
  @@mutex.synchronize do
    generate
  end

  ok
end

#generateObject (protected)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/osctld/commands/user/lxc_usernet.rb', line 21

def generate
  f = File.open("#{LXC_USERNET}.new", 'w')

  user_cts = {}

  DB::Containers.each do |ct|
    user_cts[ct.user] ||= []
    user_cts[ct.user] << ct
  end

  DB::Users.each do |u|
    # lxc-user-nic counts db entries by owner, not by the linked bridge name,
    # so we use the total number of user's interfaces for each lxc-usernet entry.
    bridges = []
    netif_cnt = 0

    # Count interfaces per type
    cts = user_cts[u]
    next if cts.nil?

    cts.each do |ct|
      ct.netifs.each do |netif|
        case netif.type
        when :bridge
          bridges << netif.link unless bridges.include?(netif.link)
        end

        netif_cnt += 1
      end
    end

    # Write results
    # We're doubling the amount of allowed interfaces, because of unexplained
    # spurious Quota reached messages from lxc-user-nic.
    f.write("#{u.sysusername} veth none #{netif_cnt * 2}\n")

    bridges.each do |br|
      f.write("#{u.sysusername} veth #{br} #{netif_cnt * 2}\n")
    end
  end

  f.close
  File.rename("#{LXC_USERNET}.new", LXC_USERNET)
end