Class: Schroot::Chroot
- Inherits:
-
Object
- Object
- Schroot::Chroot
- Defined in:
- lib/schroot.rb
Overview
Schroot session handler
Instance Attribute Summary collapse
-
#chroot ⇒ Object
readonly
Returns the value of attribute chroot.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
-
#clone ⇒ Object
Clones current session.
-
#copy(what, where, whence: :host, recursive: false) ⇒ Object
Copies file from or to the chroot.
-
#initialize(chroot_name = 'default', &block) ⇒ Chroot
constructor
A new instance of Chroot.
-
#log(log = @logger) ⇒ Object
Sets log object.
-
#run(cmd, user: nil, preserve_environment: nil, &block) ⇒ Object
Runs command inside of chroot session.
-
#start(chroot_name = 'default') ⇒ String
Starts the session of ‘chroot_name` chroot.
-
#stop ⇒ nil
Stops current chroot session.
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
#chroot ⇒ Object (readonly)
Returns the value of attribute chroot.
265 266 267 |
# File 'lib/schroot.rb', line 265 def chroot @chroot end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
265 266 267 |
# File 'lib/schroot.rb', line 265 def location @location end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
265 266 267 |
# File 'lib/schroot.rb', line 265 def logger @logger end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
265 266 267 |
# File 'lib/schroot.rb', line 265 def session @session end |
Instance Method Details
#clone ⇒ Object
Clones current session
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
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.
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
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 |
#stop ⇒ nil
Stops current chroot session.
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 |