Class: DevLXC::Container

Inherits:
LXC::Container
  • Object
show all
Defined in:
lib/dev-lxc/container.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject



24
25
26
27
28
29
# File 'lib/dev-lxc/container.rb', line 24

def destroy
  return unless self.defined?
  stop if running?
  puts "Destroying container #{self.name}"
  super
end

#install_package(package_path) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dev-lxc/container.rb', line 58

def install_package(package_path)
  raise "File #{package_path} does not exist in container #{self.name}" unless run_command("test -e #{package_path}") == 0
  puts "Installing #{package_path} in container #{self.name}"
  case File.extname(package_path)
  when ".deb"
    install_command = "dpkg -D10 -i #{package_path}"
  when ".rpm"
    install_command = "rpm -Uvh #{package_path}"
  end
  run_command(install_command)
end

#run_command(command) ⇒ Object



51
52
53
54
55
56
# File 'lib/dev-lxc/container.rb', line 51

def run_command(command)
  raise "Container #{self.name} must be running first" unless running?
  attach({:wait => true, :stdin => STDIN, :stdout => STDOUT, :stderr => STDERR}) do
    LXC.run_command(command)
  end
end

#startObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dev-lxc/container.rb', line 3

def start
  raise "Container #{self.name} does not exist." unless self.defined?
  puts "Starting container #{self.name}"
  super
  wait(:running, 3)
  puts "Waiting for #{self.name} container's network"
  ips = nil
  30.times do
    ips = self.ip_addresses
    break unless ips.empty?
    sleep 1
  end
  raise "Container #{self.name} network is not available." if ips.empty?
end

#stopObject



18
19
20
21
22
# File 'lib/dev-lxc/container.rb', line 18

def stop
  puts "Stopping container #{self.name}"
  super
  wait("STOPPED", 3)
end

#sync_mounts(mounts) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dev-lxc/container.rb', line 31

def sync_mounts(mounts)
  existing_mounts = self.config_item("lxc.mount.entry")
  unless existing_mounts.nil?
    preserved_mounts = existing_mounts.delete_if { |m| m.end_with?("## dev-lxc ##") }
    self.clear_config_item('lxc.mount.entries')
    self.set_config_item("lxc.mount.entry", preserved_mounts)
  end
  mounts.each do |mount|
    raise "Mount source #{mount.split.first} does not exist." unless File.exists?(mount.split.first)
    if ! preserved_mounts.nil? && preserved_mounts.any? { |m| m.start_with?("#{mount} ") }
      puts "Skipping mount entry #{mount}, it already exists"
      next
    else
      puts "Adding mount entry #{mount}"
      self.set_config_item("lxc.mount.entry", "#{mount} none bind,optional,create=dir 0 0     ## dev-lxc ##")
    end
  end
  self.save_config
end