Class: Microsandbox::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/microsandbox/image.rb

Overview

Management of the local OCI image cache. Images are pulled automatically by Sandbox.create; this namespace lets you inspect and prune the cache.

Class Method Summary collapse

Class Method Details

.get(reference) ⇒ ImageInfo

Metadata for one cached image.

Returns:



84
85
86
# File 'lib/microsandbox/image.rb', line 84

def get(reference)
  ImageInfo.new(Native::Image.get(reference.to_s))
end

.inspect(reference = nil) ⇒ ImageDetail

Full inspection detail for a cached image. With no argument this is the normal class #inspect (so object display still works).

Returns:



91
92
93
94
95
# File 'lib/microsandbox/image.rb', line 91

def inspect(reference = nil)
  return super() if reference.nil?

  ImageDetail.new(Native::Image.inspect(reference.to_s))
end

.listArray<ImageInfo>

All cached images.

Returns:



78
79
80
# File 'lib/microsandbox/image.rb', line 78

def list
  Native::Image.list.map { |info| ImageInfo.new(info) }
end

.load(input_path, tag: nil) ⇒ Array<ImageInfo>

Load images into the cache from a local docker save tarball or an OCI Image Layout archive. Pass "-" to read the archive from $stdin (spooled to a temp file first, like the Python SDK — the core reads seekable files only). Mirrors the Python Image.load / Node imageLoad (runtime v0.6.7).

Parameters:

  • input_path (String)

    archive path (or "-")

  • tag (String, Array<String>, nil) (defaults to: nil)

    retag the loaded image(s)

Returns:

  • (Array<ImageInfo>)

    one entry per loaded image



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/microsandbox/image.rb', line 119

def load(input_path, tag: nil)
  tags = Array(tag).map(&:to_s)
  path = input_path.to_s
  if path == "-"
    require "tempfile"
    Tempfile.create(["msb-image-load", ".tar"]) do |tmp|
      tmp.binmode
      IO.copy_stream($stdin, tmp)
      tmp.flush
      return Native::Image.load(tmp.path, tags).map { |info| ImageInfo.new(info) }
    end
  end
  Native::Image.load(path, tags).map { |info| ImageInfo.new(info) }
end

.pruneImagePruneReport

Garbage-collect unreferenced images, manifests, and layers.

Returns:



107
108
109
# File 'lib/microsandbox/image.rb', line 107

def prune
  ImagePruneReport.new(Native::Image.prune)
end

.remove(reference, force: false) ⇒ nil

Remove a cached image.

Parameters:

  • force (Boolean) (defaults to: false)

    remove even if referenced

Returns:

  • (nil)


100
101
102
103
# File 'lib/microsandbox/image.rb', line 100

def remove(reference, force: false)
  Native::Image.remove(reference.to_s, force)
  nil
end

.save(reference, output_path:, format: :docker) ⇒ nil

Save cached image(s) into a local archive. Mirrors the Python Image.save / Node imageSave (runtime v0.6.7).

Parameters:

  • reference (String, Array<String>)

    image reference(s) to save

  • output_path (String)

    destination archive path

  • format (:docker, :oci) (defaults to: :docker)

    archive layout (default :docker, the shape docker load expects)

Returns:

  • (nil)

Raises:

  • (ArgumentError)


141
142
143
144
145
146
147
# File 'lib/microsandbox/image.rb', line 141

def save(reference, output_path:, format: :docker)
  refs = Array(reference).map(&:to_s)
  raise ArgumentError, "at least one image reference is required" if refs.empty?

  Native::Image.save(refs, output_path.to_s, format.to_s)
  nil
end