Class: OsCtld::PrLimits::Manager

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

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:



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

def initialize(ct, entries: {})
  init_lock

  @ct = ct
  @prlimits = entries
end

Instance Attribute Details

#ctObject (readonly, protected)

Returns the value of attribute ct.



106
107
108
# File 'lib/osctld/prlimits/manager.rb', line 106

def ct
  @ct
end

#prlimitsObject (readonly, protected)

Returns the value of attribute prlimits.



106
107
108
# File 'lib/osctld/prlimits/manager.rb', line 106

def prlimits
  @prlimits
end

Class Method Details

.default(ct) ⇒ Object

Create new resource limits with default values

Parameters:



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

def self.default(ct)
  new(ct, entries: {
    'nofile' => PrLimits::PrLimit.new('nofile', 1024, SystemLimits::FILE_MAX_DEFAULT)
  })
end

.load(ct, cfg) ⇒ Object

Load prlimits from config

Parameters:



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

def self.load(ct, cfg)
  entries = if cfg.is_a?(Array)
              cfg.map do |v|
                [v['name'], PrLimits::PrLimit.load(v['name'], v)]
              end
            else
              cfg.map { |k, v| [k, PrLimits::PrLimit.load(k, v)] }
            end

  new(ct, entries: entries.to_h)
end

Instance Method Details

#[](name) ⇒ PrLimits::PrLimit?

Parameters:

  • name (String)

Returns:



64
65
66
# File 'lib/osctld/prlimits/manager.rb', line 64

def [](name)
  inclusively { prlimits[name] }
end

#contains?(name) ⇒ Boolean

Parameters:

  • name (String)

Returns:

  • (Boolean)


69
70
71
# File 'lib/osctld/prlimits/manager.rb', line 69

def contains?(name)
  inclusively { prlimits.has_key?(name) }
end

#dumpObject

Dump to config



84
85
86
# File 'lib/osctld/prlimits/manager.rb', line 84

def dump
  inclusively { prlimits.to_h { |k, v| [k, v.dump] } }
end

#dup(new_ct) ⇒ Object

rubocop:enable Style/HashTransformValues



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

def dup(new_ct)
  ret = super()
  ret.instance_variable_set('@ct', new_ct)
  ret.instance_variable_set('@prlimits', prlimits.map(&:clone))
  ret
end

#each {|name, prlimit| ... } ⇒ Object

Yield Parameters:



75
76
77
# File 'lib/osctld/prlimits/manager.rb', line 75

def each(&block)
  inclusively { prlimits.each(&block) }
end

#exportObject

Export to client



89
90
91
92
93
# File 'lib/osctld/prlimits/manager.rb', line 89

def export
  inclusively do
    prlimits.to_h { |k, v| [k, v.export] }
  end
end

#set(name, soft, hard) ⇒ Object

Parameters:

  • name (String)
  • soft (Integer)
  • hard (Integer)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/osctld/prlimits/manager.rb', line 41

def set(name, soft, hard)
  exclusively do
    if prlimits.has_key?(name)
      prlimits[name].set(soft, hard)
    else
      prlimits[name] = PrLimits::PrLimit.new(name, soft, hard)
    end
  end

  ct.save_config
  ct.lxc_config.configure_prlimits
  SystemLimits.ensure_nofile(hard) if name == 'nofile'
end

#unset(name) ⇒ Object

Parameters:

  • name (String)


56
57
58
59
60
# File 'lib/osctld/prlimits/manager.rb', line 56

def unset(name)
  exclusively { prlimits.delete(name) }
  ct.save_config
  ct.lxc_config.configure_prlimits
end