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



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

def ct
  @ct
end

#prlimitsObject (readonly, protected)

Returns the value of attribute prlimits



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

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)
  if cfg.is_a?(Array)
    entries = cfg.map do |v|
      [v['name'], PrLimits::PrLimit.load(v['name'], v)]
    end
  else
    entries = cfg.map { |k, v| [k, PrLimits::PrLimit.load(k, v)] }
  end

  new(ct, entries: Hash[entries])
end

Instance Method Details

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

Parameters:

  • name (String)

Returns:



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

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

#contains?(name) ⇒ Boolean

Parameters:

  • name (String)

Returns:

  • (Boolean)


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

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

#dumpObject

Dump to config



79
80
81
# File 'lib/osctld/prlimits/manager.rb', line 79

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

#dup(new_ct) ⇒ Object



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

def dup(new_ct)
  ret = super()
  ret.instance_variable_set('@ct', new_ct)
  ret.instance_variable_set('@prlimits', prlimits.map { |v| v.clone })
  ret
end

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

Yield Parameters:



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

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

#exportObject



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

def export
  inclusively do
    Hash[prlimits.map { |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
# File 'lib/osctld/prlimits/manager.rb', line 56

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