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.



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.



217
218
219
# File 'lib/schroot.rb', line 217

def chroot
  @chroot
end

#locationObject (readonly)

Returns the value of attribute location.



217
218
219
# File 'lib/schroot.rb', line 217

def location
  @location
end

#loggerObject (readonly)

Returns the value of attribute logger.



217
218
219
# File 'lib/schroot.rb', line 217

def logger
  @logger
end

#sessionObject (readonly)

Returns the value of attribute session.



217
218
219
# File 'lib/schroot.rb', line 217

def session
  @session
end

Instance Method Details

#cloneObject

Clones current session

Returns:

  • (Object)

    new session object



171
172
173
# File 'lib/schroot.rb', line 171

def clone
  Chroot.new(@chroot)
end

#log(log = @logger) ⇒ Object

Sets log object



211
212
213
214
# File 'lib/schroot.rb', line 211

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



159
160
161
162
163
164
165
166
# File 'lib/schroot.rb', line 159

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

A string representing schroot session id.

Parameters:

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

    name of configured chroot

Returns:

  • (String)

    schroot session id



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/schroot.rb', line 180

def start(chroot_name = 'default')
  @logger.debug('Starting chroot session')
  stop if @session
  ObjectSpace.define_finalizer(self, proc { stop })
  state = safe_run('schroot -b -c %s' % chroot_name) do |stdin, stdout, stderr, wait_thr|
    wait_thr.value
    @session = stdout.gets.strip
  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)



201
202
203
204
205
206
207
208
# File 'lib/schroot.rb', line 201

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