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.
-
#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.
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
#chroot ⇒ Object (readonly)
Returns the value of attribute chroot.
217 218 219 |
# File 'lib/schroot.rb', line 217 def chroot @chroot end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
217 218 219 |
# File 'lib/schroot.rb', line 217 def location @location end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
217 218 219 |
# File 'lib/schroot.rb', line 217 def logger @logger end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
217 218 219 |
# File 'lib/schroot.rb', line 217 def session @session end |
Instance Method Details
#clone ⇒ Object
Clones current session
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.
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.
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 |
#stop ⇒ nil
Stops current chroot session.
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 |