microsandbox-rb 0.5.9 → 0.5.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/microsandbox.rb CHANGED
@@ -78,6 +78,11 @@ module Microsandbox
78
78
  # @return [nil]
79
79
  def ensure_runtime!
80
80
  return if @runtime_ready
81
+ # A cloud backend has no local msb/libkrunfw runtime to provision: skip the
82
+ # presence check and the first-use download entirely. Resolving the kind
83
+ # uses the same lazy env/profile/config ladder every operation already
84
+ # consults, so this adds no work for local hosts (the common case).
85
+ return if default_backend_kind == :cloud
81
86
  if installed?
82
87
  @runtime_ready = true
83
88
  return
@@ -104,6 +109,62 @@ module Microsandbox
104
109
  Native.set_runtime_msb_path(path.to_s)
105
110
  end
106
111
 
112
+ # Override the `libkrunfw` shared-library path (SDK tier of the resolver,
113
+ # below the `MSB_LIBKRUNFW_PATH` environment variable). Process-level and
114
+ # set-once: a second call is silently ignored, and the env var still wins.
115
+ # Mirrors {runtime_path=} for libkrunfw.
116
+ # @param path [String]
117
+ # @return [void]
118
+ def libkrunfw_path=(path)
119
+ Native.set_runtime_libkrunfw_path(path.to_s)
120
+ end
121
+
122
+ # Install a process-wide default backend (v0.5.8 backend routing). Without a
123
+ # call to this, operations use a local libkrun backend; the env/profile
124
+ # ladder (`MSB_BACKEND`, `MSB_API_URL`+`MSB_API_KEY`, `MSB_PROFILE`,
125
+ # `~/.microsandbox/config.json`) is resolved lazily on first use. Call once
126
+ # at startup, before any sandbox operations.
127
+ #
128
+ # @param kind ["local","cloud", Symbol] backend kind
129
+ # @param url [String, nil] cloud control-plane URL (cloud, unless `profile:`)
130
+ # @param api_key [String, nil] cloud API key (cloud, unless `profile:`)
131
+ # @param profile [String, nil] named profile from `~/.microsandbox/config.json`
132
+ # @return [void]
133
+ def set_default_backend(kind, url: nil, api_key: nil, profile: nil)
134
+ Native.set_default_backend(kind.to_s, url&.to_s, api_key&.to_s, profile&.to_s)
135
+ end
136
+
137
+ # Run the given block with a temporary default backend, restoring the
138
+ # previous one afterward (even on error). NOTE: the swap is process-wide
139
+ # while the block runs, not fiber/thread-local — concurrent threads observe
140
+ # the temporary backend. It is also NOT safe to call from multiple threads
141
+ # at once: two interleaved `with_backend` calls can restore each other's
142
+ # saved backend out of order and leave a temporary backend installed
143
+ # permanently. Use it only when no other thread is changing the backend, and
144
+ # avoid calling {set_default_backend} inside the block (the restore on exit
145
+ # would overwrite that change). Mirrors the official SDKs' scoped-backend helper.
146
+ #
147
+ # @param kind ["local","cloud", Symbol]
148
+ # @param url [String, nil]
149
+ # @param api_key [String, nil]
150
+ # @param profile [String, nil]
151
+ # @yield with the temporary backend installed
152
+ # @return [Object] the block's return value
153
+ def with_backend(kind, url: nil, api_key: nil, profile: nil)
154
+ token = Native.push_default_backend(kind.to_s, url&.to_s, api_key&.to_s, profile&.to_s)
155
+ begin
156
+ yield
157
+ ensure
158
+ Native.pop_default_backend(token)
159
+ end
160
+ end
161
+
162
+ # @return [Symbol] the active default backend kind, :local or :cloud.
163
+ # The first call resolves the env/profile/config ladder.
164
+ def default_backend_kind
165
+ Native.default_backend_kind.to_sym
166
+ end
167
+
107
168
  # Latest resource-usage snapshot for every running sandbox, keyed by name.
108
169
  # Mirrors the official `all_sandbox_metrics`/`allSandboxMetrics` helpers.
109
170
  # @return [Hash{String => Metrics}]
data/sig/microsandbox.rbs CHANGED
@@ -9,8 +9,13 @@ module Microsandbox
9
9
  def self.ensure_runtime!: () -> nil
10
10
  def self.runtime_path: () -> String
11
11
  def self.runtime_path=: (String path) -> void
12
+ def self.libkrunfw_path=: (String path) -> void
12
13
  def self.all_sandbox_metrics: () -> Hash[String, Metrics]
13
14
 
15
+ def self.set_default_backend: (String | Symbol kind, ?url: String?, ?api_key: String?, ?profile: String?) -> void
16
+ def self.with_backend: [T] (String | Symbol kind, ?url: String?, ?api_key: String?, ?profile: String?) { () -> T } -> T
17
+ def self.default_backend_kind: () -> Symbol
18
+
14
19
  class Error < StandardError
15
20
  def self.code: () -> String
16
21
  def code: () -> String
@@ -37,6 +42,8 @@ module Microsandbox
37
42
  class MetricsDisabledError < Error end
38
43
  class MetricsUnavailableError < Error end
39
44
  class UnsupportedOperationError < Error end
45
+ class CloudHttpError < Error end
46
+ class UnsupportedError < Error end
40
47
 
41
48
  class ExecOutput
42
49
  def initialize: (Hash[String, untyped] data) -> void
@@ -114,15 +121,26 @@ module Microsandbox
114
121
  def timestamp: () -> Time
115
122
  end
116
123
 
117
- class SandboxInfo
124
+ class SandboxHandle
118
125
  def name: () -> String
119
126
  def status: () -> Symbol
120
127
  def running?: () -> bool
121
128
  def stopped?: () -> bool
122
129
  def created_at: () -> Time?
123
130
  def updated_at: () -> Time?
131
+ def stop: () -> nil
132
+ def stop_with_timeout: (Numeric timeout) -> nil
133
+ def kill: () -> nil
134
+ def kill_with_timeout: (Numeric timeout) -> nil
135
+ def request_stop: () -> nil
136
+ def request_kill: () -> nil
137
+ def request_drain: () -> nil
138
+ def wait_until_stopped: () -> SandboxStopResult
124
139
  end
125
140
 
141
+ # Deprecated alias for SandboxHandle (was a read-only metadata type pre-v0.5.8).
142
+ SandboxInfo: singleton(SandboxHandle)
143
+
126
144
  class SandboxStopResult
127
145
  def name: () -> String
128
146
  def status: () -> Symbol
@@ -151,9 +169,9 @@ module Microsandbox
151
169
  ?replace: bool, ?replace_with_timeout: Numeric?)
152
170
  ?{ (Sandbox) -> untyped } -> untyped
153
171
  def self.start: (String name, ?detached: bool) -> Sandbox
154
- def self.get: (String name) -> SandboxInfo
155
- def self.list: () -> Array[SandboxInfo]
156
- def self.list_with: (?labels: Hash[untyped, untyped]) -> Array[SandboxInfo]
172
+ def self.get: (String name) -> SandboxHandle
173
+ def self.list: () -> Array[SandboxHandle]
174
+ def self.list_with: (?labels: Hash[untyped, untyped]) -> Array[SandboxHandle]
157
175
  def self.remove: (String name) -> nil
158
176
 
159
177
  def name: () -> String
@@ -163,10 +181,10 @@ module Microsandbox
163
181
  def shell: (String script, ?cwd: String?, ?user: String?, ?env: Hash[untyped, untyped]?,
164
182
  ?timeout: Numeric?, ?tty: bool, ?stdin: String?, ?rlimits: Hash[untyped, untyped]?) -> ExecOutput
165
183
  def exec_stream: (String command, ?Array[String] args, ?cwd: String?, ?user: String?,
166
- ?env: Hash[untyped, untyped]?, ?timeout: Numeric?, ?tty: bool, ?stdin: String?,
184
+ ?env: Hash[untyped, untyped]?, ?timeout: Numeric?, ?tty: bool, ?stdin: (String | :pipe)?,
167
185
  ?rlimits: Hash[untyped, untyped]?) -> ExecHandle
168
186
  def shell_stream: (String script, ?cwd: String?, ?user: String?, ?env: Hash[untyped, untyped]?,
169
- ?timeout: Numeric?, ?tty: bool, ?stdin: String?, ?rlimits: Hash[untyped, untyped]?) -> ExecHandle
187
+ ?timeout: Numeric?, ?tty: bool, ?stdin: (String | :pipe)?, ?rlimits: Hash[untyped, untyped]?) -> ExecHandle
170
188
  def attach: (String command, ?Array[String] args, ?cwd: String?, ?user: String?,
171
189
  ?env: Hash[untyped, untyped]?, ?detach_keys: String?,
172
190
  ?rlimits: Hash[untyped, untyped]?) -> Integer
@@ -179,12 +197,12 @@ module Microsandbox
179
197
  def metrics_stream: (?interval: Numeric) -> MetricsStream
180
198
  def log_stream: (?sources: Array[String | Symbol]?, ?since_ms: Numeric?,
181
199
  ?from_cursor: String?, ?until_ms: Numeric?, ?follow: bool) -> LogStream
182
- def stop: (?timeout: Numeric?) -> nil
183
- def kill: (?timeout: Numeric?) -> nil
184
- def request_stop: () -> nil
185
- def request_kill: () -> nil
186
- def request_drain: () -> nil
187
- def wait_until_stopped: () -> SandboxStopResult
200
+ def stop: () -> nil
201
+ def stop_and_wait: () -> ExitStatus
202
+ def kill: () -> nil
203
+ def drain: () -> nil
204
+ def wait: () -> ExitStatus
205
+ def status: () -> Symbol
188
206
  def owns_lifecycle?: () -> bool
189
207
  def detach: () -> nil
190
208
  end
@@ -216,7 +234,7 @@ module Microsandbox
216
234
  end
217
235
 
218
236
  class ExitStatus
219
- def exit_code: () -> Integer
237
+ def exit_code: () -> Integer?
220
238
  def success?: () -> bool
221
239
  def failure?: () -> bool
222
240
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: microsandbox-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
4
+ version: 0.5.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - ya-luotao
@@ -46,6 +46,7 @@ files:
46
46
  - ext/microsandbox/Cargo.toml
47
47
  - ext/microsandbox/extconf.rb
48
48
  - ext/microsandbox/src/agent.rs
49
+ - ext/microsandbox/src/backend.rs
49
50
  - ext/microsandbox/src/conv.rs
50
51
  - ext/microsandbox/src/error.rs
51
52
  - ext/microsandbox/src/exec.rs