Class: Boxd::BoxService

Inherits:
Object
  • Object
show all
Defined in:
lib/boxd/box_service.rb

Overview

Collection accessor for boxes. Exposed as ‘compute.boxes`.

Instance Method Summary collapse

Constructor Details

#initialize(backend) ⇒ BoxService

Returns a new instance of BoxService.



6
7
8
# File 'lib/boxd/box_service.rb', line 6

def initialize(backend)
  @backend = backend
end

Instance Method Details

#create(name: nil, auto_suspend_timeout: nil, restart: nil) ⇒ Object

Create a fresh box.

compute.boxes.create(name: "my-box", auto_suspend_timeout: 0)


30
31
32
33
34
35
36
# File 'lib/boxd/box_service.rb', line 30

def create(name: nil, auto_suspend_timeout: nil, restart: nil)
  args = ["new"]
  args.push("--name", name) if name
  args.push("--auto-suspend-timeout", auto_suspend_timeout.to_s) if auto_suspend_timeout
  args.push("--restart", restart.to_s) if restart
  Box.new(@backend.call_json(*args), backend: @backend)
end

#ephemeral(fork: nil, create: nil, name: nil, **opts) ⇒ Object

Ephemeral: fork a source box, yield it, and destroy when the block exits (even on exception). Use for one-shot agent tasks where the fork should disappear afterwards.

compute.boxes.ephemeral(fork: "dev-golden") do |box|
  box.exec!(["bash", "-lc", "cargo test"])
end


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/boxd/box_service.rb', line 64

def ephemeral(fork: nil, create: nil, name: nil, **opts)
  box =
    if fork
      self.fork(fork, name: name, **opts)
    else
      create(name: name, **(create || {}), **opts)
    end
  yield box
ensure
  box&.destroy if box && box.status != "destroyed"
end

#find(name) ⇒ Object

Like #get, but returns nil if the box doesn’t exist.



21
22
23
24
25
# File 'lib/boxd/box_service.rb', line 21

def find(name)
  get(name)
rescue NotFoundError
  nil
end

#fork(source, name: nil, auto_suspend_timeout: nil) ⇒ Object

Fork an existing box. ‘source` is the source box’s name or ID.

compute.boxes.fork("dev-golden", name: "preview-pr-42")


41
42
43
44
45
46
# File 'lib/boxd/box_service.rb', line 41

def fork(source, name: nil, auto_suspend_timeout: nil)
  args = ["fork", source.to_s]
  args.push("--name", name) if name
  args.push("--auto-suspend-timeout", auto_suspend_timeout.to_s) if auto_suspend_timeout
  Box.new(@backend.call_json(*args), backend: @backend)
end

#fork_or_get(source, name:, **opts) ⇒ Object

Fork-or-get: if ‘name` already exists, return that box; otherwise fork from `source`. Handy for idempotent provisioning.



50
51
52
53
54
55
# File 'lib/boxd/box_service.rb', line 50

def fork_or_get(source, name:, **opts)
  existing = find(name)
  return existing if existing

  fork(source, name: name, **opts)
end

#get(name) ⇒ Object

Get a single box by name. Raises NotFoundError if missing.



16
17
18
# File 'lib/boxd/box_service.rb', line 16

def get(name)
  Box.new(@backend.call_json("info", name), backend: @backend)
end

#listObject

List all boxes on the account.



11
12
13
# File 'lib/boxd/box_service.rb', line 11

def list
  Array(@backend.call_json("list")).map { |attrs| Box.new(attrs, backend: @backend) }
end