kosa8 0.5.1 → 0.5.3
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.
- checksums.yaml +4 -4
- data/README.md +34 -8
- data/lib/kosa8/cloud.rb +62 -11
- data/lib/kosa8/errors.rb +22 -0
- data/lib/kosa8/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2f844e8a5a44b134b65d86679d015a5a2e631239d938df854d745b393a2b99bb
|
|
4
|
+
data.tar.gz: a3b44827f4db786bc9c798c46f13fbfd1aa7c6e593abcda6362bb1900da8d65f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3e661816f8189fa290867c4a3d039a2a8250d6a07083797f5c2202838e3a4b11c843e6a86d31af06ec8871d2dfc1be6e931e3cf4d2e1eac165ebbf6885264014
|
|
7
|
+
data.tar.gz: 550fa6e4c577c0482ec897842ea7334a34429784ebbaafc831e4f0758d75a557a28df71cb4f2f5e6ccc2950f6bf679122f75c33c0c3d33578fdfad73cd6a3c56
|
data/README.md
CHANGED
|
@@ -6,8 +6,8 @@ Method for method it matches [`@kosa8/sdk`](https://github.com/kosa8ai/kosa8-js)
|
|
|
6
6
|
## Quickstart: kosa8 Cloud
|
|
7
7
|
|
|
8
8
|
```sh
|
|
9
|
-
gem install kosa8
|
|
10
|
-
# or in a Gemfile: gem "kosa8", "~> 0.5
|
|
9
|
+
gem install kosa8
|
|
10
|
+
# or in a Gemfile: gem "kosa8", "~> 0.5"
|
|
11
11
|
export KOSA8_API_KEY=k8c_live_... # made in the cloud dashboard
|
|
12
12
|
```
|
|
13
13
|
|
|
@@ -20,7 +20,7 @@ box = cloud.sandboxes.create(template: "python", ttl_s: 3600) # deleted automat
|
|
|
20
20
|
r = box.exec(["python3", "-c", "print(6 * 7)"])
|
|
21
21
|
puts r.exit_code, r.stdout # 0, 42
|
|
22
22
|
|
|
23
|
-
box.exec("echo hello > /workspace/hello.txt") #
|
|
23
|
+
box.exec("echo hello > /workspace/hello.txt") # /workspace is kept between commands
|
|
24
24
|
puts box.exec("cat /workspace/hello.txt").stdout
|
|
25
25
|
|
|
26
26
|
box.files.write("/workspace/app.py", "print('hi')\n") # needs the host's data plane (see below)
|
|
@@ -167,11 +167,15 @@ Kosa8::Cloud.new(api_key: tok["secret"]) # hand this to your end user, not your
|
|
|
167
167
|
|
|
168
168
|
- The `base` template is **Alpine Linux**: `sh` is busybox, and there is no `bash`, `python` or `git`.
|
|
169
169
|
Use `template: "python"` (or another key from `cloud.templates`) for Python, or pass `image:` to `exec`.
|
|
170
|
-
- **
|
|
171
|
-
installed outside `/workspace` (`apk add`, a
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
-
|
|
170
|
+
- **Only `/workspace` is sure to persist between commands.** A command may run in a fresh container, so
|
|
171
|
+
`cd`, `export`, processes it leaves running and anything installed outside `/workspace` (`apk add`, a
|
|
172
|
+
system `pip install`) may be gone by the next command. Install into `/workspace` (for example
|
|
173
|
+
`pip install --target /workspace/lib ...` with `PYTHONPATH=/workspace/lib`), chain the steps in one
|
|
174
|
+
`sh -c`, or create with `persistent: true` (one long-lived container) to keep the whole filesystem and
|
|
175
|
+
background processes; the code interpreter (`run_code`) needs it, and it counts against your plan's
|
|
176
|
+
persistent-sandbox limit.
|
|
177
|
+
- **A buffered `exec` runs at most 90 seconds** and raises `Kosa8::ExecTimeoutError`.
|
|
178
|
+
`box.exec(cmd, timeout_s: 30)` stops a command sooner, in the sandbox (1 to 90 whole seconds). Run long
|
|
175
179
|
work in the background and poll it:
|
|
176
180
|
`box.exec("nohup sh -c 'make test > /workspace/test.log 2>&1; echo $? > /workspace/test.exit' &")`.
|
|
177
181
|
- A request body (command and env) is at most 64 KiB; a file read or write is at most 8 MiB.
|
|
@@ -191,6 +195,28 @@ Kosa8::Cloud.new(api_key: tok["secret"]) # hand this to your end user, not your
|
|
|
191
195
|
|
|
192
196
|
Every Cloud error is a `Kosa8::ApiError` carrying the server's stable `e.code`, its message and `e.body`.
|
|
193
197
|
|
|
198
|
+
## The same thing in each language
|
|
199
|
+
|
|
200
|
+
The five kosa8 SDKs (Python `kosa8`, JS `@kosa8/sdk`, Go `kosa8-go`, Ruby `kosa8`, Java
|
|
201
|
+
`com.kosa8:kosa8`) call the same kosa8 Cloud API, but some names differ by language. The ones
|
|
202
|
+
that catch people switching:
|
|
203
|
+
|
|
204
|
+
| | Python | JS / TS | Go | Ruby | Java |
|
|
205
|
+
|---|---|---|---|---|---|
|
|
206
|
+
| client | `Kosa8Cloud()` | `new Kosa8Cloud()` | `kosa8.NewCloud(kosa8.CloudOptions{})`, returns `(cloud, err)` | `Kosa8::Cloud.new` | `Kosa8Cloud.builder().build()` |
|
|
207
|
+
| lifetime on create | `ttl_s=3600` | `{ ttlS: 3600 }` | `CloudCreateOptions{TTLSeconds: 3600}` | `ttl_s: 3600` | `new CloudOptions.Create().ttlSeconds(3600)` |
|
|
208
|
+
| persistent sandbox | `persistent=True` | `{ persistent: true }` | `CloudCreateOptions{Persistent: true}` | `persistent: true` | `.persistent(true)` |
|
|
209
|
+
| sandbox id | `box.id` | `box.id` | `box.ID()`, a method (`fmt.Println(box.ID)` prints a func value; `go vet` reports it) | `box.id` | `box.id()` |
|
|
210
|
+
| shell command | `box.exec("ls /")` | `box.exec('ls /')` | `box.ExecShell(ctx, "ls /", nil)` | `box.exec("ls /")` | `box.exec("ls /")` |
|
|
211
|
+
| argv command | `box.exec(["ls", "/"])` | `box.exec(['ls', '/'])` | `box.Exec(ctx, []string{"ls", "/"}, nil)` | `box.exec(["ls", "/"])` | `box.exec(List.of("ls", "/"))` |
|
|
212
|
+
| per-exec timeout | `exec(cmd, timeout_s=30)` | `exec(cmd, { timeoutS: 30 })` | `&kosa8.CloudExecOptions{TimeoutS: 30}` | `exec(cmd, timeout_s: 30)` | `exec(cmd, null, null, 30)` |
|
|
213
|
+
| exit code | `r.exit_code` | `r.exitCode` | `r.ExitCode`, an `*int` (nil when the cloud sent none; `r.OK()` checks for 0) | `r.exit_code` | `r.exitCode()`, an `Integer` (may be null) |
|
|
214
|
+
| files | `box.files.write` / `read_text` / `list` | `box.files.write` / `readText` / `list` | `box.Files().WriteString` / `ReadText` / `List(ctx, "")` | `box.files.write` / `read_text` / `list` | `box.files().write` / `readText` / `list()` |
|
|
215
|
+
| no such sandbox | `Kosa8NotFoundError` | `Kosa8NotFoundError` | `*kosa8.Error` with `Kind` `KindNotFound` (`errors.Is(err, kosa8.ErrNotFound)`) | `Kosa8::NotFoundError` | `Kosa8Exception.NotFoundException` |
|
|
216
|
+
|
|
217
|
+
In every SDK a non-zero exit is returned in the result, not raised. Only `/workspace` is sure to
|
|
218
|
+
persist between commands, unless the sandbox is persistent.
|
|
219
|
+
|
|
194
220
|
## Local daemon
|
|
195
221
|
|
|
196
222
|
```ruby
|
data/lib/kosa8/cloud.rb
CHANGED
|
@@ -63,6 +63,11 @@ module Kosa8
|
|
|
63
63
|
MAX_FORK = 1000
|
|
64
64
|
# The limit kosa8 Cloud puts on a buffered exec, in seconds (504 exec_timeout past it).
|
|
65
65
|
EXEC_LIMIT_S = 90
|
|
66
|
+
# Where a person makes and revokes API keys.
|
|
67
|
+
KEYS_URL = "https://app.kosa8.com/app#keys"
|
|
68
|
+
# Added to a 401 from the API: the key is the usual cause.
|
|
69
|
+
AUTH_HINT = "check KOSA8_API_KEY (or api_key:) is a current key; make a new one at #{KEYS_URL}"
|
|
70
|
+
|
|
66
71
|
EXEC_HINT = "a buffered exec is stopped after #{EXEC_LIMIT_S} s; run long work in the background, " \
|
|
67
72
|
"writing to /workspace, and poll it"
|
|
68
73
|
|
|
@@ -88,9 +93,12 @@ module Kosa8
|
|
|
88
93
|
def initialize(api_key: nil, base_url: nil, env: ENV, timeout: 120, transport: nil, retry_policy: nil,
|
|
89
94
|
sleeper: nil)
|
|
90
95
|
key = (api_key.nil? ? env["KOSA8_API_KEY"].to_s : api_key.to_s).strip
|
|
91
|
-
|
|
96
|
+
if key.empty?
|
|
97
|
+
raise Error, "no kosa8 Cloud API key: set KOSA8_API_KEY or pass api_key:; make one at #{KEYS_URL}"
|
|
98
|
+
end
|
|
92
99
|
unless key.start_with?(KEY_PREFIX) || key.start_with?(TOKEN_PREFIX)
|
|
93
|
-
raise Error, "a kosa8 Cloud API key starts with #{KEY_PREFIX} (a capability token with #{TOKEN_PREFIX})"
|
|
100
|
+
raise Error, "a kosa8 Cloud API key starts with #{KEY_PREFIX} (a capability token with #{TOKEN_PREFIX}); " \
|
|
101
|
+
"check KOSA8_API_KEY or api_key:, or make a key at #{KEYS_URL}"
|
|
94
102
|
end
|
|
95
103
|
|
|
96
104
|
@key = key
|
|
@@ -206,10 +214,12 @@ module Kosa8
|
|
|
206
214
|
raise ExecTimeoutError.new("kosa8 Cloud did not answer within #{@timeout} s (#{EXEC_HINT})",
|
|
207
215
|
method: method, path: path, code: "exec_timeout")
|
|
208
216
|
end
|
|
209
|
-
raise TimeoutError.new("kosa8 Cloud did not answer within #{@timeout} s
|
|
217
|
+
raise TimeoutError.new("kosa8 Cloud did not answer #{method} #{path.split("?", 2)[0]} within #{@timeout} s " \
|
|
218
|
+
"(#{e.message}); try again, or give Kosa8::Cloud.new a longer timeout:",
|
|
210
219
|
method: method, path: path)
|
|
211
220
|
rescue SystemCallError, IOError, SocketError, OpenSSL::SSL::SSLError => e
|
|
212
|
-
raise ConnectionError.new("could not reach kosa8 Cloud at #{@base_url}: #{e.message}"
|
|
221
|
+
raise ConnectionError.new("could not reach kosa8 Cloud at #{@base_url}: #{e.message}; " \
|
|
222
|
+
"check the network connection and KOSA8_CLOUD_URL (or base_url:), then try again",
|
|
213
223
|
method: method, path: path)
|
|
214
224
|
end
|
|
215
225
|
return content.to_s.b if raw && status.to_i < 400
|
|
@@ -239,7 +249,9 @@ module Kosa8
|
|
|
239
249
|
message = (msg_body.is_a?(Hash) ? msg_body["error"] : msg_body).to_s
|
|
240
250
|
e = if exec_timeout?(status, code, message, path)
|
|
241
251
|
said = message.empty? ? nil : message
|
|
242
|
-
|
|
252
|
+
# Its own timeout_s (exec(..., timeout_s:)) needs no hint about the 90 s limit.
|
|
253
|
+
hint = message.include?("timeout_s") ? "" : " (#{EXEC_HINT})"
|
|
254
|
+
ExecTimeoutError.new("#{said || "request failed with HTTP #{status}"}#{hint}",
|
|
243
255
|
status: status, daemon_message: said, method: method, path: path,
|
|
244
256
|
code: code || "exec_timeout")
|
|
245
257
|
else
|
|
@@ -251,9 +263,40 @@ module Kosa8
|
|
|
251
263
|
ra = body["retry_after_s"] # e.g. 409 idempotency_in_progress, 503 no_capacity
|
|
252
264
|
e.retry_after = ra.to_f if ra.is_a?(Numeric) && ra.positive?
|
|
253
265
|
end
|
|
266
|
+
e.request_id = header(headers, "x-request-id") || header(headers, "cf-ray")
|
|
267
|
+
e.detail = detail(e, message)
|
|
254
268
|
e
|
|
255
269
|
end
|
|
256
270
|
|
|
271
|
+
# The context after the sentence of an API error:
|
|
272
|
+
# "[HTTP 503 no_capacity; retry in 10 s; request 8e1a-SJC]", with the
|
|
273
|
+
# place to pay for a 402 and what to check for a 401.
|
|
274
|
+
def self.detail(e, sentence)
|
|
275
|
+
parts = ["HTTP #{e.status}#{e.code ? " #{e.code}" : ""}"]
|
|
276
|
+
if e.retry_after
|
|
277
|
+
n = e.retry_after
|
|
278
|
+
parts << "retry in #{n == n.round ? n.round : n.round(1)} s"
|
|
279
|
+
elsif e.retryable?
|
|
280
|
+
parts << "retryable"
|
|
281
|
+
end
|
|
282
|
+
up = Kosa8.body_str(e.body, "upgrade_url")
|
|
283
|
+
parts << "upgrade: #{up}" if e.status == 402 && up && !sentence.include?(up)
|
|
284
|
+
parts << AUTH_HINT if e.status == 401
|
|
285
|
+
parts << "request #{e.request_id}" if e.request_id
|
|
286
|
+
"[#{parts.join("; ")}]"
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# A response header's value by case-insensitive name, or nil.
|
|
290
|
+
def self.header(headers, name)
|
|
291
|
+
return nil unless headers.respond_to?(:each)
|
|
292
|
+
|
|
293
|
+
v = nil
|
|
294
|
+
headers.each { |k, val| v = val if k.to_s.casecmp(name).zero? }
|
|
295
|
+
v = v.first if v.is_a?(Array)
|
|
296
|
+
s = v.to_s.strip
|
|
297
|
+
s.empty? ? nil : s
|
|
298
|
+
end
|
|
299
|
+
|
|
257
300
|
def self.exec_path?(path)
|
|
258
301
|
path.to_s.split("?", 2)[0].end_with?("/exec")
|
|
259
302
|
end
|
|
@@ -458,17 +501,25 @@ module Kosa8
|
|
|
458
501
|
# Run a command and wait for it. A String runs through sh -c, an Array is
|
|
459
502
|
# the argv. A sleeping sandbox wakes for it.
|
|
460
503
|
#
|
|
461
|
-
#
|
|
462
|
-
#
|
|
463
|
-
#
|
|
464
|
-
# Kosa8::ExecTimeoutError; run long
|
|
465
|
-
# (nohup sh -c '...' > /workspace/job.log 2>&1 &)
|
|
504
|
+
# Only /workspace is sure to persist: an exec may run in a fresh
|
|
505
|
+
# container (unless the sandbox is persistent), so cd, export and packages
|
|
506
|
+
# installed elsewhere may be gone by the next one. kosa8 Cloud stops a
|
|
507
|
+
# buffered exec after 90 s, raised as Kosa8::ExecTimeoutError; run long
|
|
508
|
+
# work in the background (nohup sh -c '...' > /workspace/job.log 2>&1 &)
|
|
509
|
+
# and poll it. +timeout_s+ (1 to 90 whole seconds) stops the command in
|
|
510
|
+
# the sandbox sooner, also as Kosa8::ExecTimeoutError. The request
|
|
466
511
|
# (command and env) is at most 64 KiB (413 too_large).
|
|
467
|
-
def exec(cmd, image: nil, env: nil)
|
|
512
|
+
def exec(cmd, image: nil, env: nil, timeout_s: nil)
|
|
468
513
|
argv = cmd.is_a?(String) ? ["sh", "-c", cmd] : cmd.to_a
|
|
469
514
|
body = { "cmd" => argv }
|
|
470
515
|
body["image"] = image unless image.nil?
|
|
471
516
|
body["env"] = env.map { |k, v| "#{k}=#{v}" } if env && !env.empty?
|
|
517
|
+
unless timeout_s.nil?
|
|
518
|
+
unless timeout_s.is_a?(Integer) && timeout_s.between?(1, Cloud::EXEC_LIMIT_S)
|
|
519
|
+
raise ArgumentError, "timeout_s must be a whole number of seconds from 1 to #{Cloud::EXEC_LIMIT_S}"
|
|
520
|
+
end
|
|
521
|
+
body["timeout_s"] = timeout_s
|
|
522
|
+
end
|
|
472
523
|
r = @cloud.request("POST", path("/exec"), body)
|
|
473
524
|
fc, rt = Kosa8.exec_failure(r["exit"])
|
|
474
525
|
fc = r["failure_class"] if r["failure_class"].is_a?(String)
|
data/lib/kosa8/errors.rb
CHANGED
|
@@ -11,6 +11,13 @@ module Kosa8
|
|
|
11
11
|
# Seconds the server asked to wait before trying again (Retry-After, or
|
|
12
12
|
# the body's retry_after_s), when it said.
|
|
13
13
|
attr_accessor :retry_after
|
|
14
|
+
# The id of the request that failed (the X-Request-Id header, else
|
|
15
|
+
# Cloudflare's cf-ray): quote it when you ask kosa8 support. nil when the
|
|
16
|
+
# answer had neither, and for errors with no answer.
|
|
17
|
+
attr_accessor :request_id
|
|
18
|
+
# Context added after the server's sentence in #message (kosa8 Cloud
|
|
19
|
+
# answers: "[HTTP 503 no_capacity; retry in 10 s; request ...]").
|
|
20
|
+
attr_accessor :detail # :nodoc:
|
|
14
21
|
|
|
15
22
|
def initialize(message = nil, status: nil, daemon_message: nil, remediation: nil, method: nil, path: nil, body: nil,
|
|
16
23
|
code: nil)
|
|
@@ -24,6 +31,21 @@ module Kosa8
|
|
|
24
31
|
@code = code
|
|
25
32
|
end
|
|
26
33
|
|
|
34
|
+
# The message: the server's own sentence (or the SDK's), followed for a
|
|
35
|
+
# kosa8 Cloud answer by its status, code, wait and request id, so a log
|
|
36
|
+
# line alone says what happened and what to do. The bare sentence is
|
|
37
|
+
# daemon_message.
|
|
38
|
+
def to_s
|
|
39
|
+
d = @detail
|
|
40
|
+
d.nil? || d.empty? ? super : "#{super} #{d}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# retry_after under the API's own name: seconds to wait before trying
|
|
44
|
+
# again, or nil.
|
|
45
|
+
def retry_after_s
|
|
46
|
+
retry_after
|
|
47
|
+
end
|
|
48
|
+
|
|
27
49
|
# Whose fault it was: "ok", "exit_nonzero", "timeout", "infra", "quota",
|
|
28
50
|
# "client", or "error" (not an API answer). See Kosa8.classify_failure.
|
|
29
51
|
def failure_class
|
data/lib/kosa8/version.rb
CHANGED