Class: Schroot::Chroot

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

Overview

Schroot session handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chroot_name = 'default', &block) ⇒ Chroot

Returns a new instance of Chroot.



127
128
129
130
131
132
133
134
135
136
137
138
139
# 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

Instance Attribute Details

#chrootObject (readonly)

Returns the value of attribute chroot.



265
266
267
# File 'lib/schroot.rb', line 265

def chroot
  @chroot
end

#locationObject (readonly)

Returns the value of attribute location.



265
266
267
# File 'lib/schroot.rb', line 265

def location
  @location
end

#loggerObject (readonly)

Returns the value of attribute logger.



265
266
267
# File 'lib/schroot.rb', line 265

def logger
  @logger
end

#sessionObject (readonly)

Returns the value of attribute session.



265
266
267
# File 'lib/schroot.rb', line 265

def session
  @session
end

Instance Method Details

#cloneObject

Clones current session

Returns:

  • (Object)

    new session object



220
221
222
# File 'lib/schroot.rb', line 220

def clone
  Chroot.new(@chroot)
end

#copy(what, where, whence: :host, recursive: false) ⇒ Object

Copies file from or to the chroot

Parameters:

  • what (String)

    Source path

  • where (String)

    Destination path

  • whence (Symbol) (defaults to: :host)

    Defines where should we dst file: from host (:host) or from chroot (:chroot)



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 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

#log(log = @logger) ⇒ Object

Sets log object



259
260
261
262
# File 'lib/schroot.rb', line 259

def log(log=@logger)
  @logger = log
  @logger.debug('Hello there!')
end

#run(cmd, user: nil, preserve_environment: nil, &block) ⇒ Object

Runs command inside of chroot session. Invocation is popen3-like Session must be started before executing command.

Examples:

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

Parameters:

  • cmd (String)

    command to run

  • user (String) (defaults to: nil)

    user

  • preserve_environment (Bool) (defaults to: nil)

    Should we preserve environment variables



185
186
187
188
189
190
191
192
# 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

#start(chroot_name = 'default') ⇒ String

Starts the session of ‘chroot_name` chroot

Parameters:

  • chroot_name (String) (defaults to: 'default')

    name of configured chroot

Returns:

  • (String)

    A string representing schroot session id.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# 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

#stopnil

Stops current chroot session.

Returns:

  • (nil)

    session_id of killed session (should be nil)



250
251
252
253
254
255
256
# 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