Class: LxcFileConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ LxcFileConfig

Returns a new instance of LxcFileConfig.



44
45
46
47
48
49
50
# File 'lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb', line 44

def initialize(path)
  raise 'LXC config file not found' unless File.exists?(path)
  @path = path
  @network = []
  @base = Mash.new
  parse!
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



4
5
6
# File 'lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb', line 4

def base
  @base
end

#networkObject (readonly)

Returns the value of attribute network.



3
4
5
# File 'lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb', line 3

def network
  @network
end

Class Method Details

.generate_config(resource) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb', line 7

def generate_config(resource)
  config = []
  config << "lxc.utsname = #{resource.utsname}"
  [resource.network].flatten.each do |net_hash|
    nhsh = Mash.new(net_hash)
    flags = nhsh.delete(:flags)
    %w(type link).each do |k|
      config << "lxc.network.#{k} = #{nhsh.delete(k)}" if nhsh[k]
    end
    nhsh.each_pair do |k,v|
      config << "lxc.network.#{k} = #{v}"
    end
    if(flags)
      config << "lxc.network.flags = #{flags}"
    end
  end
  if(resource.cap_drop)
    config << "lxc.cap.drop = #{Array(resource.cap_drop).join(' ')}"
  end
  %w(pts tty arch devttydir mount mount_entry rootfs rootfs_mount pivotdir).each do |k|
    config << "lxc.#{k.sub('_', '.')} = #{resource.send(k)}" if resource.send(k)
  end
  prefix = 'lxc.cgroup'
  resource.cgroup.each_pair do |key, value|
    if(value.is_a?(Array))
      value.each do |val|
        config << "#{prefix}.#{key} = #{val}"
      end
    else
      config << "#{prefix}.#{key} = #{value}"
    end
  end
  config.join("\n") + "\n"
end