module Schroot

Constants

BASE_CONF
CONF_D
CONF_D_PREFIX
SCHROOT_BASE
VERSION

Public Class Methods

add(name, kwargs = {}, force=false) click to toggle source

Adds new chroot configuration to …/chroot.d/ directory

@example

SchrootConfig.add("testing", {"description" => "Debian testing",
                              "file"        => "/srv/chroot/testing.tgz",
                              "location"    => "/testing",
                              "groups"      => "sbuild"})
=> true

@param name [String] name of chroot @param kwargs [Hash] options @param force [Bool] should we override existing config @return [Bool] true if operation has completed successfully

# File lib/schroot.rb, line 59
def self.add(name, kwargs = {}, force=false)
  chroots = read_config
  filename = CONF_D+CONF_D_PREFIX+name
  if (chroots[name] or File.exists?(filename)) and !force
    return false
  else
    begin
      stream = File.open(filename, 'w')
    rescue Errno::EACCES
      raise SchrootError, "Cannot open #{filename} for writing"
    end
    stream.puts '# Generated automatically with ruby-schroot'
    stream.puts "[#{name}]"
    kwargs.each do |param, value|
      stream.puts "#{param}=#{value}"
    end
    stream.close
  end
  return true
end
bootstrap(name, release, mirror: 'http://http.debian.net/debian/', log: Logger.new(nil), &block) click to toggle source

Installs base Debian system, into chroot using debootstrap

@example

bootstrap('my_chroot', 'testing', log: Logger.new(STDOUT))

@param name [String] VM’s name (should be defined in schroot config) @param release [String] Release to install (e.g. ‘sid’, ‘jessie’, ‘stable’) @param mirror [String] What debian mirror should we use @param log [Logger] Where should be put bootstrap logs

# File lib/schroot.rb, line 88
def self.bootstrap (name, release, mirror: 'http://http.debian.net/debian/', log: Logger.new(nil), &block)
  chroots = read_config
  directory = chroots[name]['directory']
  if directory and ::File.exists? directory
    command = "debootstrap #{release} #{directory} #{mirror}"
    log.info 'Running `%s`' % command
    Open3.popen2e(command) do |stdin, stdout_and_stderr, wait_thr|
      log.info 'PID: %s' % wait_thr.pid
      while line = stdout_and_stderr.gets do
        log.debug line.strip
      end
      log.info 'Exited with %s' % wait_thr.value
    end
  end
end
match_name(name) click to toggle source
# File lib/schroot.rb, line 39
def self.match_name(name)
  return /^\s*\[([a-z0-9A-Z\-_]+)\]/.match(name)
end
match_param(param) click to toggle source
# File lib/schroot.rb, line 43
def self.match_param(param)
  return /^\s*([a-z0-9A-Z\-_]+)=(.*)$/.match(param)
end
read_config() click to toggle source

@return [Hash] representation of current config files

# File lib/schroot.rb, line 16
def self.read_config
  chroots = {}
  files = [BASE_CONF]
  Dir.entries(CONF_D).each do |file|
    files << (CONF_D+file) unless %w(. ..).include? file
  end
  files.each do |file|
    stream = File.open(file, 'r')
    current = nil
    while line = stream.gets
      if match_name(line)
        current = match_name(line)[1]
        chroots[current.strip] = {:source => file}
      elsif current and match_param(line)
        param, value = match_param(line)[1], match_param(line)[2]
        chroots[current][param.strip] = value.strip if current
      end
    end
    stream.close
  end
  return chroots
end
remove(name, force=false) click to toggle source

Removes chroot from …/chroot.d/ directory

@example

SchrootConfig.remove("testing", true)
  => true

@param name [String] name of chroot @param kwargs [Hash] options @param force [Bool] should we override existing config @return [Bool] true if operation has completed successfully

# File lib/schroot.rb, line 113
def self.remove(name, force=false)
  chroots = read_config
  filename = CONF_D+CONF_D_PREFIX+name
  if (File.exists?(filename) and chroots[name]) or force
    File.delete(filename)
    return true
  else
    return false
  end
end