Module: Schroot

Defined in:
lib/schroot.rb,
lib/schroot/version.rb

Defined Under Namespace

Classes: Chroot, SchrootError

Constant Summary collapse

SCHROOT_BASE =
'/var/lib/schroot'
BASE_CONF =
'/etc/schroot/schroot.conf'
CONF_D =
'/etc/schroot/chroot.d/'
CONF_D_PREFIX =
'99ruby-'
VERSION =
'0.0.9'

Class Method Summary collapse

Class Method Details

.add(name, kwargs = {}, force = false) ⇒ Bool

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

Examples:

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

Parameters:

  • name (String)

    name of chroot

  • kwargs (Hash) (defaults to: {})

    options

  • force (Bool) (defaults to: false)

    should we override existing config

Returns:

  • (Bool)

    true if operation has completed successfully



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object

Installs base Debian system, into chroot using debootstrap

Examples:

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

Parameters:

  • name (String)

    VM’s name (should be defined in schroot config)

  • release (String)

    Release to install (e.g. ‘sid’, ‘jessie’, ‘stable’)

  • mirror (String) (defaults to: 'http://http.debian.net/debian/')

    What debian mirror should we use

  • log (Logger) (defaults to: Logger.new(nil))

    Where should be put bootstrap logs



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object



39
40
41
# File 'lib/schroot.rb', line 39

def self.match_name(name)
  return /^\s*\[([a-z0-9A-Z\-_]+)\]/.match(name)
end

.match_param(param) ⇒ Object



43
44
45
# File 'lib/schroot.rb', line 43

def self.match_param(param)
  return /^\s*([a-z0-9A-Z\-_]+)=(.*)$/.match(param)
end

.read_configHash

Returns representation of current config files.

Returns:

  • (Hash)

    representation of current config files



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Bool

Removes chroot from …/chroot.d/ directory

Examples:

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

Parameters:

  • name (String)

    name of chroot

  • kwargs (Hash)

    options

  • force (Bool) (defaults to: false)

    should we override existing config

Returns:

  • (Bool)

    true if operation has completed successfully



113
114
115
116
117
118
119
120
121
122
# 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