Class: SchrootConfig

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

Overview

Schroot config manager

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

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

SchrootConfig.add(“testing”, => “Debian testing”,

                            "file"        => "/srv/chroot/testing.tgz",
                            "location"    => "/testing",
                            "groups"      => "sbuild")
=> true

Parameters:

  • name of chroot

  • (defaults to: {})

    options

  • (defaults to: false)

    should we override existing config

Returns:

  • true if operation has completed successfully



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/schroot.rb', line 56

def self.add(name, kwargs = {}, force=false)
  chroots = readconf()
  filename = CONF_D+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

.readconfHash

Returns representation of current config fules.

Returns:

  • representation of current config fules



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/schroot.rb', line 14

def self.readconf
  chroots = {}
  files = [BASE_CONF]
  Dir.entries(CONF_D).each do |file|
    files << (CONF_D+file) unless ['.','..'].include? file
  end
  files.each do |file|
    stream = File.open(file,"r")
    current = nil
    while (line = stream.gets)
      if validate_name(line)
        current = validate_name(line)[1]
        chroots[current.strip] = {"source" => file}
      elsif current and validate_param(line)
        param, value = validate_param(line)[1],validate_param(line)[2]
        chroots[current][param.strip] = value.strip if current
      end
    end
  end
  return chroots
end

.remove(name, force = false) ⇒ Bool

Removes chroot from …/chroot.d/ directory

SchrootConfig.remove(“testing”, true)

=> true

Parameters:

  • name of chroot

  • options

  • (defaults to: false)

    should we override existing config

Returns:

  • true if operation has completed successfully



86
87
88
89
90
91
92
93
94
95
# File 'lib/schroot.rb', line 86

def self.remove(name, force=false)
  chroots = readconf()
  filename = CONF_D+name
  if (File.exists?(filename) and chroots[name]) or force
    File.delete(filename)
    return true
  else
    return false
  end
end

Instance Method Details

#validate_name(name) ⇒ Object



36
37
38
# File 'lib/schroot.rb', line 36

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

#validate_param(param) ⇒ Object



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

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