Class: Wildcloud::Keeper::Isolators::Lxc

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

Instance Method Summary collapse

Instance Method Details

#resources(options) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/wildcloud/keeper/isolators/lxc.rb', line 68

def resources(options)

  @id ||= options[:id]

  run("lxc-cgroup -n #{@id} memory.limit_in_bytes \"#{options[:memory]}\"") if options[:memory]
  run("lxc-cgroup -n #{@id} memory.memsw.limit_in_bytes \"#{options[:swap]}\"") if options[:swap]
  run("lxc-cgroup -n #{@id} cpuset.cpus \"#{options[:cpus]}\"") if options[:cpus]
  run("lxc-cgroup -n #{@id} cpu.shares \"#{options[:cpu_share]}\"") if options[:cpu_share]
end

#start(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wildcloud/keeper/isolators/lxc.rb', line 26

def start(options)

  @id = options[:id]
  @target_path = File.join(config['paths']['mounts'], "#{@id}")

  @config_file = File.join(config['paths']['config'], "#{@id}.config")
  @fstab_file = File.join(config['paths']['config'], "#{@id}.fstab")

  vm_config = ERB.new(File.read(File.expand_path('../../templates/app.config', __FILE__))).result(binding)
  vm_fstab = ERB.new(File.read(File.expand_path('../../templates/app.fstab', __FILE__))).result(binding)

  FileUtils.mkdir_p(config['paths']['config'])

  File.open(@config_file, 'w') { |file| file.write(vm_config) }
  File.open(@fstab_file, 'w') { |file| file.write(vm_fstab) }

  run("lxc-create -f #{@config_file} -n #{@id}")
  run("lxc-start -d -n #{@id}")

end

#stop(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wildcloud/keeper/isolators/lxc.rb', line 47

def stop(options)

  @id ||= options[:id]
  @config_file ||= File.join(config['paths']['config'], "#{@id}.config")
  @fstab_file ||= File.join(config['paths']['config'], "#{@id}.fstab")


  if options[:build]
    build_status_file = File.join(config['paths']['images'], "#{@id}", 'var', 'build.done')
    until File.exists?(build_status_file)
      sleep(5)
    end
  end

  run("lxc-stop -n #{@id}")
  run("lxc-destroy -n #{@id}")

  FileUtils.rm(@config_file)
  FileUtils.rm(@fstab_file)
end