Class: Rexec::SandboxService

Inherits:
Object
  • Object
show all
Defined in:
lib/rexec/container.rb

Overview

Service for managing sandboxes. HTTP paths remain /api/containers.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SandboxService

Returns a new instance of SandboxService.



33
34
35
# File 'lib/rexec/container.rb', line 33

def initialize(client)
  @client = client
end

Instance Method Details

#create(image:, name: nil, environment: {}, labels: {}) ⇒ Sandbox

Create a new sandbox.

Examples:

sandbox = client.sandboxes.create(
  image: "ubuntu",
  name: "my-sandbox",
  environment: { "MY_VAR" => "value" }
)

Parameters:

  • image (String)

    Image alias (e.g. "ubuntu")

  • name (String, nil) (defaults to: nil)

    Optional sandbox name

  • environment (Hash) (defaults to: {})

    Environment variables

  • labels (Hash) (defaults to: {})

    Labels

Returns:



70
71
72
73
74
75
76
77
78
# File 'lib/rexec/container.rb', line 70

def create(image:, name: nil, environment: {}, labels: {})
  body = { image: image }
  body[:name] = name if name
  body[:environment] = environment unless environment.empty?
  body[:labels] = labels unless labels.empty?

  data = @client.request(:post, "/api/containers", body: body)
  Sandbox.new(data)
end

#delete(id) ⇒ Object

Delete a sandbox.

Parameters:

  • id (String)

    Sandbox ID



83
84
85
86
# File 'lib/rexec/container.rb', line 83

def delete(id)
  @client.request(:delete, "/api/containers/#{id}")
  nil
end

#get(id) ⇒ Sandbox

Get a sandbox by ID.

Parameters:

  • id (String)

    Sandbox ID

Returns:



51
52
53
54
# File 'lib/rexec/container.rb', line 51

def get(id)
  data = @client.request(:get, "/api/containers/#{id}")
  Sandbox.new(data)
end

#listArray<Sandbox>

List all sandboxes.

Returns:



40
41
42
43
44
45
# File 'lib/rexec/container.rb', line 40

def list
  data = @client.request(:get, "/api/containers")
  # API returns { "containers" => [...], "count" => N, "limit" => M }
  items = data.is_a?(Array) ? data : (data && data["containers"]) || []
  items.map { |c| Sandbox.new(c) }
end

#start(id) ⇒ Object

Start a sandbox.

Parameters:

  • id (String)

    Sandbox ID



91
92
93
94
# File 'lib/rexec/container.rb', line 91

def start(id)
  @client.request(:post, "/api/containers/#{id}/start")
  nil
end

#stop(id) ⇒ Object

Stop a sandbox.

Parameters:

  • id (String)

    Sandbox ID



99
100
101
102
# File 'lib/rexec/container.rb', line 99

def stop(id)
  @client.request(:post, "/api/containers/#{id}/stop")
  nil
end