Class: Boxd::BoxService
- Inherits:
-
Object
- Object
- Boxd::BoxService
- Defined in:
- lib/boxd/box_service.rb
Overview
Collection accessor for boxes. Exposed as ‘compute.boxes`.
Instance Method Summary collapse
-
#create(name: nil, auto_suspend_timeout: nil, restart: nil) ⇒ Object
Create a fresh box.
-
#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).
-
#find(name) ⇒ Object
Like #get, but returns nil if the box doesn’t exist.
-
#fork(source, name: nil, auto_suspend_timeout: nil) ⇒ Object
Fork an existing box.
-
#fork_or_get(source, name:, **opts) ⇒ Object
Fork-or-get: if ‘name` already exists, return that box; otherwise fork from `source`.
-
#get(name) ⇒ Object
Get a single box by name.
-
#initialize(backend) ⇒ BoxService
constructor
A new instance of BoxService.
-
#list ⇒ Object
List all boxes on the account.
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 |