Class: Microsandbox::PullSession

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/microsandbox/sandbox.rb

Overview

A streaming image-pull + create session, from Sandbox.create_with_progress. Iterate it (it is Enumerable) to consume progress-event Hashes as the image pulls, then call #sandbox to get the booted Sandbox. Each event Hash has a “kind” key (e.g. “resolving”, “resolved”, “layer_download_progress”, “layer_materialize_progress”, “complete”) plus kind-specific fields.

Examples:

session = Microsandbox::Sandbox.create_with_progress("box", image: "python")
session.each { |ev| puts "#{ev["kind"]} #{ev["downloaded_bytes"]}" }
sb = session.sandbox
begin
  sb.exec("python", ["-V"])
ensure
  sb.stop
end

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ PullSession

Returns a new instance of PullSession.



1088
1089
1090
# File 'lib/microsandbox/sandbox.rb', line 1088

def initialize(native)
  @native = native
end

Instance Method Details

#each {|event| ... } ⇒ self, Enumerator

Yield each progress-event Hash until the pull finishes. Returns an Enumerator when called without a block.

Yield Parameters:

  • event (Hash)

Returns:

  • (self, Enumerator)


1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/microsandbox/sandbox.rb', line 1096

def each
  return enum_for(:each) unless block_given?

  while (event = @native.recv)
    yield event
  end
  self
end

#sandboxSandbox

The booted sandbox. Joins the create task (draining any remaining pull progress first), so call it after iterating progress. The returned Sandbox is live — stop it when done. Memoized; callable once.

Returns:



1109
1110
1111
# File 'lib/microsandbox/sandbox.rb', line 1109

def sandbox
  @sandbox ||= Sandbox.new(@native.result)
end