Class: LXC

Inherits:
Object
  • Object
show all
Defined in:
lib/lxc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_name) ⇒ LXC

Returns a new instance of LXC.



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
41
42
43
# File 'lib/lxc.rb', line 8

def initialize(node_name)
  @node_name = node_name
  @filename = "/var/lib/lxc/#{node_name}/config"
  @mac = ''
  @interface = ''
  @template = ERB.new <<EOF
lxc.utsname = <%= node_name %>
lxc.pts = 1024
lxc.tty = 4
lxc.cgroup.cpu.shares = 512
lxc.rootfs = /var/lib/lxc/<%= node_name %>/rootfs
lxc.mount = /var/lib/lxc/<%= node_name %>/fstab
lxc.network.type = veth
lxc.network.name = eth0
lxc.network.link = <%= interface %>
lxc.network.hwaddr = <%= mac %>
lxc.network.flags = up
lxc.cgroup.devices.deny = a
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
lxc.cgroup.devices.allow = b 7:0 rwm
lxc.cgroup.devices.allow = c 254:0 rwm
lxc.cgroup.devices.allow = c 108:0 rwm
lxc.cgroup.devices.allow = c 10:229 rwm
lxc.cgroup.devices.allow = c 10:200 rwm
EOF

end

Instance Attribute Details

#interfaceObject

Returns the value of attribute interface.



4
5
6
# File 'lib/lxc.rb', line 4

def interface
  @interface
end

#macObject

Returns the value of attribute mac.



5
6
7
# File 'lib/lxc.rb', line 5

def mac
  @mac
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



6
7
8
# File 'lib/lxc.rb', line 6

def node_name
  @node_name
end

Instance Method Details

#save(filename = @filename) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/lxc.rb', line 45

def save(filename=@filename)
  config_data = @template.result(OpenStruct.new(:node_name => @node_name,
                                                :mac => @mac, :interface => @interface ).send(:binding))
  File.open(".tmp.lxc.conf",'w') do |f|
    f.puts config_data
  end
  system "sudo mv .tmp.lxc.conf #{@filename}"
end