class Schroot::Chroot
Schroot
session handler
Attributes
Public Class Methods
# File lib/schroot.rb, line 127 def initialize(chroot_name = 'default', &block) @logger = Logger.new nil if block_given? if block.arity == 1 yield self elsif block.arity == 0 instance_eval &block end end start(chroot_name) end
Public Instance Methods
Clones current session
@return [Object] new session object
# File lib/schroot.rb, line 220 def clone Chroot.new(@chroot) end
Copies file from or to the chroot
@param what [String] Source path @param where [String] Destination path @param whence [Symbol] Defines where should we dst file: from host (:host) or from chroot (:chroot)
# File lib/schroot.rb, line 199 def copy(what, where, whence: :host, recursive: false) if whence == :host where = File.join(@location, where) elsif whence == :chroot what = File.join(@location, what) else return nil end flags = '' if recursive flags << '-r' end safe_run('cp %s %s %s' % [flags, cwhat, where]) do |stdin, stout, stderr, wait_thr| wait_thr.value end end
Sets log object
# File lib/schroot.rb, line 259 def log(log=@logger) @logger = log @logger.debug('Hello there!') end
Runs command inside of chroot session. Invocation is popen3-like Session must be started before executing command.
@example
session.run("uname -a", user: 'rainbowdash', preserve_environment: true) do |stdin, stdout, stderr, wait_thr| puts wait_thr.pid, wait_thr.value, stdout.read end
@param cmd [String] command to run @param user [String] user @param preserve_environment [Bool] Should we preserve environment variables
# File lib/schroot.rb, line 185 def run(cmd, user: nil, preserve_environment: nil, &block) safe_run(command(cmd, user, preserve_environment)) do |stdin, stout, stderr, wait_thr| if block_given? block.call stdin, stout, stderr, wait_thr end wait_thr.value end end
Starts the session of ‘chroot_name` chroot
@param chroot_name [String] name of configured chroot @return [String] A string representing schroot session id.
# File lib/schroot.rb, line 228 def start(chroot_name = 'default') @logger.debug('Starting chroot session') stop if @session ObjectSpace.define_finalizer(self, proc { stop }) safe_run('schroot -b -c %s' % chroot_name) do |stdin, stdout, stderr, wait_thr| wait_thr.value @session = stdout.gets.strip @session end @chroot = chroot_name state = safe_run('schroot --location -c session:%s' % @session) do |stdin, stdout, stderr, wait_thr| wait_thr.value @location = stdout.gets.strip end @logger.debug('Session %s with %s started in %s' % [@session, @chroot, @location]) @session end
Stops current chroot session.
@return [nil] session_id of killed session (should be nil)
# File lib/schroot.rb, line 250 def stop @logger.debug('Stopping session %s with %s' % [@session, @chroot]) safe_run('schroot -e -c %s' % @session) @logger.debug('Session %s of %s should be stopped' % [@session, @chroot]) @location = nil @session = nil end
Private Instance Methods
# File lib/schroot.rb, line 157 def command(cmd, user, preserve_environment) raise SchrootError, 'No current session' unless @session command = ['schroot', '-r', '-c', @session] if user command << '-u' command << user end if preserve_environment command << '-p' end command << '--' command << cmd command.join(' ') end
# File lib/schroot.rb, line 141 def safe_run(cmd, &block) @logger.info('Executing %s' % cmd) begin stream = Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| if block_given? block.call stdin, stdout, stderr, wait_thr end wait_thr.value end rescue Errno::ENOENT raise SchrootError, 'Schroot binary is missing!' end @logger.info('Done!') stream end