Class: Schroot

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) ⇒ Schroot

Returns a new instance of Schroot.



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/schroot.rb', line 102

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.



204
205
206
# File 'lib/schroot.rb', line 204

def chroot
  @chroot
end

#locationObject (readonly)

Returns the value of attribute location.



204
205
206
# File 'lib/schroot.rb', line 204

def location
  @location
end

#loggerObject (readonly)

Returns the value of attribute logger.



204
205
206
# File 'lib/schroot.rb', line 204

def logger
  @logger
end

#sessionObject (readonly)

Returns the value of attribute session.



204
205
206
# File 'lib/schroot.rb', line 204

def session
  @session
end

Instance Method Details

#cloneObject

Clones current session

Returns:

  • (Object)

    new session object



161
162
163
# File 'lib/schroot.rb', line 161

def clone
  return Schroot.new(@chroot)
end

#log(log = @logger) ⇒ Object

Sets log object



198
199
200
201
# File 'lib/schroot.rb', line 198

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

#run(cmd, kwargs = {}) ⇒ Array<(IO:fd, IO:fd, IO:fd)>

Runs command inside of chroot session. Session must be started before.

Examples:

session.run("ping localhost",{:user => 'rainbowdash',:preserve_environment => true})
  => [#<IO:fd 16>, #<IO:fd 18>, #<IO:fd 20>]

Parameters:

  • cmd (String)

    command to run

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

    extra args

Returns:

  • (Array<(IO:fd, IO:fd, IO:fd)>)

    descriptors for stdin, stdout, stderr



154
155
156
# File 'lib/schroot.rb', line 154

def run(cmd, kwargs = {})
  return safe_run(command(cmd,kwargs))
end

#start(chroot_name = 'default') ⇒ String

Starts the session of ‘chroot_name` chroot

A string representing schroot session id.

Parameters:

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

    name of configured chroot

Returns:

  • (String)

    schroot session id



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/schroot.rb', line 170

def start(chroot_name = 'default')
  @logger.debug("Starting chroot session")
  stop if @session
  command = ['schroot', '-b', '-c', chroot_name]
  ObjectSpace.define_finalizer(self, proc { stop })
  stdin, stdout, stderr = safe_run("schroot -b -c %s" % chroot_name)
  @chroot = chroot_name
  @session = stdout.gets.strip
  stdin, stdout, stderr = safe_run("schroot --location -c session:%s" % @session)
  @location = stdout.gets.strip
  @logger.debug("Session %s with %s started in %s" % [@session, @chroot, @location])
  return @session
end

#stopnil

Stops current chroot session.

Parameters:

  • chroot_name (String)

    name of configured chroot

Returns:

  • (nil)

    session_id of killed session (should be nil)



188
189
190
191
192
193
194
195
# File 'lib/schroot.rb', line 188

def stop
  @logger.debug("Stopping session %s with %s" % [@session, @chroot])
  stdin, stdout, stderr = safe_run("schroot -e -c %s" % @session)
  @logger.debug("Session %s of %s should be stopped" % [@session, @chroot])
  @location = nil
  @session = nil

end