withruntime 0.28.0 → 1.0.0
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/lib/withruntime/generated/apps.rb +192 -0
- data/lib/withruntime/generated/audit.rb +78 -0
- data/lib/withruntime/generated/browsers.rb +192 -0
- data/lib/withruntime/generated/builds.rb +192 -0
- data/lib/withruntime/generated/ci.rb +192 -0
- data/lib/withruntime/generated/containers.rb +192 -0
- data/lib/withruntime/generated/desktops.rb +192 -0
- data/lib/withruntime/generated/domains.rb +152 -0
- data/lib/withruntime/generated/egress_ips.rb +81 -0
- data/lib/withruntime/generated/events.rb +60 -0
- data/lib/withruntime/generated/feedback.rb +96 -0
- data/lib/withruntime/generated/functions.rb +192 -0
- data/lib/withruntime/generated/identities.rb +68 -0
- data/lib/withruntime/generated/images.rb +391 -0
- data/lib/withruntime/generated/job_runs.rb +110 -0
- data/lib/withruntime/generated/jobs.rb +333 -0
- data/lib/withruntime/generated/keys.rb +261 -0
- data/lib/withruntime/generated/kit.rb +111 -0
- data/lib/withruntime/generated/me.rb +69 -0
- data/lib/withruntime/generated/networks.rb +72 -0
- data/lib/withruntime/generated/notebooks.rb +192 -0
- data/lib/withruntime/generated/otel.rb +94 -0
- data/lib/withruntime/generated/ports.rb +118 -0
- data/lib/withruntime/generated/previews.rb +124 -0
- data/lib/withruntime/generated/projects.rb +108 -0
- data/lib/withruntime/generated/roles.rb +68 -0
- data/lib/withruntime/generated/routes.rb +11 -0
- data/lib/withruntime/generated/runtime.rb +188 -0
- data/lib/withruntime/generated/sandboxes.rb +1835 -0
- data/lib/withruntime/generated/secrets.rb +294 -0
- data/lib/withruntime/generated/sessions.rb +143 -0
- data/lib/withruntime/generated/sites.rb +192 -0
- data/lib/withruntime/generated/snapshots.rb +32 -0
- data/lib/withruntime/generated/tunnels.rb +165 -0
- data/lib/withruntime/generated/usage.rb +287 -0
- data/lib/withruntime/generated/volumes.rb +61 -0
- data/lib/withruntime/generated/webhooks.rb +91 -0
- data/lib/withruntime/generated/workspaces.rb +192 -0
- data/lib/withruntime/signing.rb +162 -0
- data/lib/withruntime/version.rb +2 -2
- data/lib/withruntime.rb +5 -43
- metadata +45 -35
- data/lib/withruntime/agents.rb +0 -877
- data/lib/withruntime/api_defaults.rb +0 -17
- data/lib/withruntime/apps.rb +0 -377
- data/lib/withruntime/browsers.rb +0 -104
- data/lib/withruntime/builds.rb +0 -218
- data/lib/withruntime/ci.rb +0 -164
- data/lib/withruntime/client.rb +0 -140
- data/lib/withruntime/containers.rb +0 -39
- data/lib/withruntime/credentials.rb +0 -111
- data/lib/withruntime/fields.rb +0 -40
- data/lib/withruntime/file_watch.rb +0 -73
- data/lib/withruntime/images.rb +0 -258
- data/lib/withruntime/network_products.rb +0 -49
- data/lib/withruntime/parity_products.rb +0 -51
- data/lib/withruntime/products.rb +0 -560
- data/lib/withruntime/record.rb +0 -99
- data/lib/withruntime/reservations.rb +0 -76
- data/lib/withruntime/sandbox.rb +0 -775
- data/lib/withruntime/tar.rb +0 -292
- data/lib/withruntime/transport.rb +0 -575
- data/lib/withruntime/tunnel.rb +0 -275
- data/lib/withruntime/wallet.rb +0 -68
- data/lib/withruntime/websocket.rb +0 -384
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "digest"
|
|
4
|
-
require "json"
|
|
5
|
-
require "uri"
|
|
6
|
-
|
|
7
|
-
module WithRuntime
|
|
8
|
-
# The key `runtime login` saved for this machine. Its file is the CLI's
|
|
9
|
-
# (packages/cloud-sdk/src/credentials.ts) and is read exactly as the CLI
|
|
10
|
-
# reads it: only from a private file in a private directory, bound to both
|
|
11
|
-
# origins.
|
|
12
|
-
module Credentials
|
|
13
|
-
KEY = /\Artcloud_[a-f0-9-]{36}_[A-Za-z0-9_-]{43}\z/
|
|
14
|
-
|
|
15
|
-
BAD_ORIGIN = "Use an HTTPS API origin (or http://runtime.internal inside a sandbox, http://localhost for tests)."
|
|
16
|
-
# Runtime's API as code inside a Runtime sandbox reaches it: the sandbox's
|
|
17
|
-
# own host sends each request on to the public API over HTTPS. The API runs
|
|
18
|
-
# on that host, whose addresses a sandbox cannot reach directly. Plain HTTP
|
|
19
|
-
# because the hop never leaves the machine: from the program to the guest's
|
|
20
|
-
# own proxy, then over the sandbox's private channel to its host.
|
|
21
|
-
SANDBOX_BASE_URL = "http://runtime.internal"
|
|
22
|
-
# A file every Runtime sandbox has; the guest keeps it current.
|
|
23
|
-
SANDBOX_MARKER = "/run/runtime/environment.json"
|
|
24
|
-
|
|
25
|
-
module_function
|
|
26
|
-
|
|
27
|
-
def in_runtime_sandbox?(marker = SANDBOX_MARKER)
|
|
28
|
-
File.exist?(marker)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# The origin calls for +api_origin+ are sent to from here. In a sandbox the
|
|
32
|
-
# public API is its own host, which it cannot reach directly, so calls for
|
|
33
|
-
# it go to runtime.internal; every other origin is left as it is.
|
|
34
|
-
def reachable(api_origin, in_sandbox: -> { in_runtime_sandbox? })
|
|
35
|
-
api_origin == DEFAULT_BASE_URL && in_sandbox.call ? SANDBOX_BASE_URL : api_origin
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# An HTTPS origin, plain HTTP to runtime.internal inside a sandbox, or plain
|
|
39
|
-
# HTTP to localhost for tests, with nothing after the host. runtime.internal
|
|
40
|
-
# is reserved and never resolves outside a sandbox, so a key sent there in
|
|
41
|
-
# plain HTTP never leaves the sandbox's host.
|
|
42
|
-
def origin(value)
|
|
43
|
-
uri = URI(value)
|
|
44
|
-
internal = uri.host == "runtime.internal"
|
|
45
|
-
local = %w[localhost 127.0.0.1 [::1] ::1].include?(uri.host) || internal
|
|
46
|
-
unless uri.host && (uri.scheme == "https" || (local && uri.scheme == "http")) && uri.userinfo.nil? &&
|
|
47
|
-
uri.query.nil? && uri.fragment.nil? && ["", "/"].include?(uri.path.to_s) &&
|
|
48
|
-
!(internal && (uri.scheme != "http" || uri.port != 80))
|
|
49
|
-
raise ArgumentError, BAD_ORIGIN
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
default_port = uri.scheme == "https" ? 443 : 80
|
|
53
|
-
host = uri.host.include?(":") && !uri.host.start_with?("[") ? "[#{uri.host}]" : uri.host
|
|
54
|
-
"#{uri.scheme}://#{host}#{uri.port == default_port ? "" : ":#{uri.port}"}"
|
|
55
|
-
rescue URI::InvalidURIError
|
|
56
|
-
raise ArgumentError, BAD_ORIGIN
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# Where the CLI keeps this machine's connection for the two origins.
|
|
60
|
-
def file(env, api_origin, auth_origin)
|
|
61
|
-
root = env["XDG_CONFIG_HOME"]
|
|
62
|
-
root = File.join(Dir.home, ".config") if root.nil? || root.empty?
|
|
63
|
-
raise ArgumentError, "XDG_CONFIG_HOME must be an absolute path." unless root.start_with?("/")
|
|
64
|
-
|
|
65
|
-
File.join(root, "runtime-cloud", "#{Digest::SHA256.hexdigest("#{auth_origin}\n#{api_origin}")}.json")
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# The saved key, or nil when there is none.
|
|
69
|
-
def saved_key(env, api_origin)
|
|
70
|
-
auth = env["RUNTIME_AUTH_URL"]
|
|
71
|
-
auth_origin = origin(auth.nil? || auth.empty? ? "https://withruntime.com" : auth)
|
|
72
|
-
path = file(env, api_origin, auth_origin)
|
|
73
|
-
directory = File.dirname(path)
|
|
74
|
-
begin
|
|
75
|
-
info = File.lstat(directory)
|
|
76
|
-
rescue Errno::ENOENT
|
|
77
|
-
return nil
|
|
78
|
-
end
|
|
79
|
-
raise SecurityError, "Runtime's credential directory must be private to your user." unless info.directory? && private?(info)
|
|
80
|
-
|
|
81
|
-
text = begin
|
|
82
|
-
File.open(path, File::RDONLY | File::NOFOLLOW) do |handle|
|
|
83
|
-
stat = handle.stat
|
|
84
|
-
raise SecurityError, "Runtime's saved connection must be private to your user." unless stat.file? && stat.size <= 4096 && private?(stat)
|
|
85
|
-
|
|
86
|
-
handle.read(4097)
|
|
87
|
-
end
|
|
88
|
-
rescue Errno::ENOENT
|
|
89
|
-
return nil
|
|
90
|
-
rescue Errno::ELOOP
|
|
91
|
-
raise SecurityError, "Could not safely open Runtime's saved connection."
|
|
92
|
-
end
|
|
93
|
-
saved = JSON.parse(text)
|
|
94
|
-
unless saved.is_a?(Hash) && saved["version"] == 1 && saved["apiOrigin"] == api_origin &&
|
|
95
|
-
saved["authOrigin"] == auth_origin && saved["key"].is_a?(String) && KEY.match?(saved["key"]) &&
|
|
96
|
-
%w[connectionId orgId agentName].all? { |name| saved[name].is_a?(String) }
|
|
97
|
-
raise ArgumentError, "Runtime's saved connection is invalid. Connect again with `npx withruntime login`."
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
saved["key"]
|
|
101
|
-
rescue JSON::ParserError
|
|
102
|
-
raise ArgumentError, "Runtime's saved connection is invalid. Connect again with `npx withruntime login`."
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
def private?(stat)
|
|
106
|
-
return true if Gem.win_platform?
|
|
107
|
-
|
|
108
|
-
(stat.mode & 0o077).zero? && stat.uid == Process.uid
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
end
|
data/lib/withruntime/fields.rb
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module WithRuntime
|
|
4
|
-
# Ruby's snake_case keywords as the API's camelCase fields: +timeout_seconds:+
|
|
5
|
-
# is timeoutSeconds, +memory_mib:+ is memoryMiB. User data (labels, env,
|
|
6
|
-
# build_args, build_env, headers and the like) is sent as it is.
|
|
7
|
-
module Fields
|
|
8
|
-
OPAQUE = %w[labels env buildArgs buildEnv headers data context detail].freeze
|
|
9
|
-
|
|
10
|
-
module_function
|
|
11
|
-
|
|
12
|
-
def camel(key)
|
|
13
|
-
parts = key.to_s.split("_")
|
|
14
|
-
parts.drop(1).each_with_index { |part, index| parts[index + 1] = part == "mib" ? "MiB" : part.capitalize }
|
|
15
|
-
parts.join
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
# A request body from keyword arguments; nil values are left out.
|
|
19
|
-
def body(fields)
|
|
20
|
-
fields.each_with_object({}) do |(key, value), out|
|
|
21
|
-
next if value.nil?
|
|
22
|
-
|
|
23
|
-
name = key.is_a?(Symbol) ? camel(key) : key.to_s
|
|
24
|
-
out[name] = OPAQUE.include?(name) ? stringify(value) : convert(value)
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def convert(value)
|
|
29
|
-
case value
|
|
30
|
-
when Hash then body(value)
|
|
31
|
-
when Array then value.map { |item| convert(item) }
|
|
32
|
-
else value
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def stringify(value)
|
|
37
|
-
value.is_a?(Hash) ? value.to_h { |key, item| [key.to_s, item] } : value
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module WithRuntime
|
|
4
|
-
class WatchService < SandboxProduct
|
|
5
|
-
def path(id = nil) = @s.path("/files/watches") + (id ? "/#{Transport.segment(id)}" : "")
|
|
6
|
-
def start(path, idempotency_key: nil, **options)
|
|
7
|
-
answer = @t.json("POST", self.path, body: Fields.body(options.merge(path: path)), key: idempotency_key)
|
|
8
|
-
FileWatch.new(self, answer)
|
|
9
|
-
end
|
|
10
|
-
def list = @t.json("GET", path)["data"].map { |row| Record.new(row) }
|
|
11
|
-
def read(id, cursor: 0, wait_ms: 0) = Record.new(@t.json("GET", "#{path(id)}/events", query: { "cursor" => cursor, "waitMs" => wait_ms }))
|
|
12
|
-
def stop(id) = @t.json("DELETE", path(id)) && nil
|
|
13
|
-
def events(id, cursor, cancel, &block)
|
|
14
|
-
@t.events("GET", "#{path(id)}/events", query: { "cursor" => cursor, "follow" => true }, timeout: 150, cancel: cancel, &block)
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
# Each events block retains its cursor. Close only its reader, or stop the guest watch too.
|
|
19
|
-
class FileWatch
|
|
20
|
-
attr_reader :id, :path
|
|
21
|
-
def initialize(service, info)
|
|
22
|
-
@service, @id, @path, @cursor = service, info["id"], info["path"], info["cursor"] || 0
|
|
23
|
-
@lock, @stop_lock = Mutex.new, Mutex.new
|
|
24
|
-
@reading = @stopped = false
|
|
25
|
-
end
|
|
26
|
-
def cursor = @lock.synchronize { @cursor }
|
|
27
|
-
def close
|
|
28
|
-
cancel = @lock.synchronize { @cancel }
|
|
29
|
-
cancel&.cancel
|
|
30
|
-
end
|
|
31
|
-
def stop
|
|
32
|
-
@stop_lock.synchronize do
|
|
33
|
-
return if @lock.synchronize { @stopped }
|
|
34
|
-
@lock.synchronize { @stopped = true }
|
|
35
|
-
close
|
|
36
|
-
begin
|
|
37
|
-
@service.stop(@id)
|
|
38
|
-
rescue StandardError
|
|
39
|
-
@lock.synchronize { @stopped = false }
|
|
40
|
-
raise
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
nil
|
|
44
|
-
end
|
|
45
|
-
def events
|
|
46
|
-
return enum_for(:events) unless block_given?
|
|
47
|
-
cancel = @lock.synchronize do
|
|
48
|
-
raise Error.new("The watch is stopped or already being read.", code: "watch_reader_active") if @reading || @stopped
|
|
49
|
-
@reading = true
|
|
50
|
-
@cancel = Transport::Cancellation.new
|
|
51
|
-
end
|
|
52
|
-
begin
|
|
53
|
-
loop do
|
|
54
|
-
again = false
|
|
55
|
-
@service.events(@id, cursor, cancel) do |event|
|
|
56
|
-
@lock.synchronize { @cursor = event["cursor"] if event.key?("cursor") }
|
|
57
|
-
if event["k"] == "continue"
|
|
58
|
-
again = true
|
|
59
|
-
break
|
|
60
|
-
end
|
|
61
|
-
raise Error.new(event["message"], code: event["code"]) if event["k"] == "failure"
|
|
62
|
-
yield Record.new(event)
|
|
63
|
-
return if %w[end paused].include?(event["k"])
|
|
64
|
-
end
|
|
65
|
-
break unless again && !cancel.cancelled?
|
|
66
|
-
end
|
|
67
|
-
ensure
|
|
68
|
-
cancel.cancel
|
|
69
|
-
@lock.synchronize { @reading = false; @cancel = nil }
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
end
|
data/lib/withruntime/images.rb
DELETED
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "digest"
|
|
4
|
-
|
|
5
|
-
module WithRuntime
|
|
6
|
-
# +runtime.images+: custom images. Build one, then start sandboxes from it
|
|
7
|
-
# with +runtime.sandboxes.create(image: "app")+.
|
|
8
|
-
#
|
|
9
|
-
# runtime.images.build(name: "app", recipe: { pip: ["pandas"] })
|
|
10
|
-
# runtime.images.build(name: "app", dockerfile: File.read("Dockerfile"), context_dir: ".")
|
|
11
|
-
class Images < Product
|
|
12
|
-
UUID = /\A[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\z/i
|
|
13
|
-
CONTEXT_BYTES = 100 * 1_048_576
|
|
14
|
-
CONTEXT_FILES = 20_000
|
|
15
|
-
CHUNK = 1_048_576
|
|
16
|
-
|
|
17
|
-
# Queues a build and returns at once, in state "queued". Exactly one of
|
|
18
|
-
# +image:+, +dockerfile:+ or +recipe:+. With +context_dir:+ the folder is
|
|
19
|
-
# packed and its missing chunks uploaded first.
|
|
20
|
-
def create(context_dir: nil, idempotency_key: nil, **fields)
|
|
21
|
-
Record.new(@t.json("POST", "/v1/images", body: prepare(context_dir, fields), key: idempotency_key))
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Builds and waits until the image is ready, sending each log line to the
|
|
25
|
-
# block. Raises with code image_failed, and the build's own error, when it fails.
|
|
26
|
-
def build(context_dir: nil, idempotency_key: nil, **fields, &on_log)
|
|
27
|
-
image = create(context_dir: context_dir, idempotency_key: idempotency_key, **fields)
|
|
28
|
-
image = follow_logs(image.id, &on_log) if on_log
|
|
29
|
-
while %w[queued building].include?(image.state)
|
|
30
|
-
sleep 1
|
|
31
|
-
image = get(image.id)
|
|
32
|
-
end
|
|
33
|
-
unless image.state == "ready"
|
|
34
|
-
raise Error.new("Image #{image.id} #{image.state}: #{image.error || "no error given"}", code: "image_failed",
|
|
35
|
-
hint: "Read the build log with runtime.images.logs(id).")
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
image
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def get(id) = Record.new(@t.json("GET", "/v1/images/#{seg(id)}"))
|
|
42
|
-
|
|
43
|
-
# An image by id, name (its latest tag), name:tag or name@version.
|
|
44
|
-
def resolve(ref) = UUID.match?(ref) ? get(ref) : Record.new(@t.json("GET", "/v1/images/resolve", query: { "ref" => ref }))
|
|
45
|
-
|
|
46
|
-
# Build output after line +after+: lines, next_after, state, truncated and done.
|
|
47
|
-
def logs(id, after: 0) = Record.new(@t.json("GET", "/v1/images/#{seg(id)}/logs", query: { "after" => after }))
|
|
48
|
-
|
|
49
|
-
# Sends build output after line +after+ to the block as it is written,
|
|
50
|
-
# until the build ends, and returns the image as it ended. It streams, and
|
|
51
|
-
# polls when a stream breaks.
|
|
52
|
-
def follow_logs(id, after: 0)
|
|
53
|
-
begin
|
|
54
|
-
loop do
|
|
55
|
-
resume = false
|
|
56
|
-
@t.events("GET", "/v1/images/#{seg(id)}/logs", query: { "after" => after, "follow" => "true" }, timeout: 180) do |event|
|
|
57
|
-
case event["type"]
|
|
58
|
-
when "line"
|
|
59
|
-
yield Record.new(event)
|
|
60
|
-
after = event["seq"]
|
|
61
|
-
when "done" then return event["image"].is_a?(Hash) && event["image"]["id"] ? Record.new(event["image"]) : get(id)
|
|
62
|
-
when "continue"
|
|
63
|
-
after = event["after"]
|
|
64
|
-
resume = true
|
|
65
|
-
when "error" then raise Error.new(event.dig("error", "message") || "The log stream failed.", code: event.dig("error", "code") || "stream_failed")
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
break unless resume
|
|
69
|
-
end
|
|
70
|
-
rescue ConnectionError
|
|
71
|
-
nil # Fall through to polling.
|
|
72
|
-
end
|
|
73
|
-
loop do
|
|
74
|
-
page = logs(id, after: after)
|
|
75
|
-
Array(page["lines"]).each { |line| yield Record.new(line) }
|
|
76
|
-
after = page["nextAfter"]
|
|
77
|
-
image = get(id)
|
|
78
|
-
return image if page["done"] || !%w[queued building].include?(image.state)
|
|
79
|
-
|
|
80
|
-
sleep 1
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def list(state: nil, name: nil, limit: nil) = page("/v1/images", { "state" => state, "name" => name, "limit" => limit })
|
|
85
|
-
|
|
86
|
-
# Every version of a name, newest first.
|
|
87
|
-
def versions(name) = list(name: name, limit: 100)
|
|
88
|
-
|
|
89
|
-
# Points +tag+ of the image's name at this version (+ref+ is an id, name:tag or name@version).
|
|
90
|
-
def tag(ref, tag) = Record.new(@t.json("POST", "/v1/images/#{seg(id_of(ref))}:tag", body: { "tag" => tag }))
|
|
91
|
-
def untag(ref, tag) = Record.new(@t.json("POST", "/v1/images/#{seg(id_of(ref))}:untag", body: { "tag" => tag }))
|
|
92
|
-
|
|
93
|
-
# Deletes one version and its tags.
|
|
94
|
-
def delete(ref) = Record.new(@t.json("POST", "/v1/images/#{seg(id_of(ref))}:delete", body: {}))
|
|
95
|
-
|
|
96
|
-
# Saved credentials for private registries. The secret is never returned.
|
|
97
|
-
def registries = @t.json("GET", "/v1/images/registries")["data"].map { |registry| Record.new(registry) }
|
|
98
|
-
|
|
99
|
-
# A user name and token or password (Docker Hub, GitHub, Google with
|
|
100
|
-
# username _json_key), or +access_key_id:+ and +secret_access_key:+ for Amazon ECR.
|
|
101
|
-
def set_registry(registry, username: nil, password: nil, access_key_id: nil, secret_access_key: nil)
|
|
102
|
-
body = Fields.body(registry: registry, username: username, password: password, access_key_id: access_key_id,
|
|
103
|
-
secret_access_key: secret_access_key)
|
|
104
|
-
Record.new(@t.json("POST", "/v1/images/registries", body: body))
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
def delete_registry(registry) = @t.json("POST", "/v1/images/registries:delete", body: { "registry" => registry })["deleted"] == true
|
|
108
|
-
|
|
109
|
-
# Packs a folder as a build context, the way docker build does (its
|
|
110
|
-
# .dockerignore applied, .git left out when there is none), and uploads the
|
|
111
|
-
# chunks the server does not have yet. Returns the context and the
|
|
112
|
-
# .dockerignore to build with.
|
|
113
|
-
def upload_context(folder, dockerignore: nil)
|
|
114
|
-
archive, files, applied = self.class.pack(folder, dockerignore)
|
|
115
|
-
chunks = (0...archive.bytesize).step(CHUNK).map { |at| archive.byteslice(at, CHUNK) }
|
|
116
|
-
digests = chunks.map { |chunk| Digest::SHA256.hexdigest(chunk) }
|
|
117
|
-
missing = @t.json("POST", "/v1/images/context/missing", body: { "digests" => digests })["missing"]
|
|
118
|
-
queue = Queue.new
|
|
119
|
-
digests.each_with_index { |digest, index| queue << index if missing.include?(digest) }
|
|
120
|
-
queue.close
|
|
121
|
-
failure = nil
|
|
122
|
-
Array.new(4) do
|
|
123
|
-
Thread.new do
|
|
124
|
-
while !failure && (index = queue.pop)
|
|
125
|
-
begin
|
|
126
|
-
@t.json("PUT", "/v1/images/context/#{digests[index]}", raw: chunks[index])
|
|
127
|
-
rescue StandardError => e
|
|
128
|
-
failure ||= e
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
end
|
|
132
|
-
end.each(&:join)
|
|
133
|
-
raise failure if failure
|
|
134
|
-
|
|
135
|
-
{ "context" => { "archive" => { "sha256" => Digest::SHA256.hexdigest(archive), "size" => archive.bytesize, "chunks" => digests },
|
|
136
|
-
"files" => files },
|
|
137
|
-
"dockerignore" => applied }
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
# A deterministic gzipped tar of the folder (sorted, no times, no owners,
|
|
141
|
-
# no links) and its files with their SHA-256.
|
|
142
|
-
def self.pack(folder, dockerignore = nil)
|
|
143
|
-
root = File.expand_path(folder)
|
|
144
|
-
ignore_file = File.join(root, ".dockerignore")
|
|
145
|
-
dockerignore ||= File.read(ignore_file) if File.file?(ignore_file)
|
|
146
|
-
ignored = dockerignore_filter(dockerignore || ".git\n")
|
|
147
|
-
reincludes = dockerignore.to_s.match?(/^\s*!/)
|
|
148
|
-
tar = "".b
|
|
149
|
-
files = []
|
|
150
|
-
walk = lambda do |directory|
|
|
151
|
-
Dir.children(directory).sort.each do |name|
|
|
152
|
-
full = File.join(directory, name)
|
|
153
|
-
relative = full.delete_prefix("#{root}/")
|
|
154
|
-
stat = File.lstat(full)
|
|
155
|
-
if stat.directory?
|
|
156
|
-
walk.call(full) unless ignored.call(relative) && !reincludes
|
|
157
|
-
elsif stat.file? && !ignored.call(relative)
|
|
158
|
-
raise Error.new("The build context has more than #{CONTEXT_FILES} files.", code: "context_too_large", hint: "Leave some out with a .dockerignore.") if files.size >= CONTEXT_FILES
|
|
159
|
-
|
|
160
|
-
data = File.binread(full)
|
|
161
|
-
mode = stat.mode & 0o777
|
|
162
|
-
files << { "path" => relative, "sha256" => Digest::SHA256.hexdigest(data), "size" => data.bytesize, "mode" => mode }
|
|
163
|
-
tar << Tar.header(relative, data.bytesize, mode, "0") << data << Tar.pad(data.bytesize)
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
walk.call(root)
|
|
168
|
-
archive = Tar.gzip(tar << ("\0".b * 1024))
|
|
169
|
-
if archive.bytesize > CONTEXT_BYTES
|
|
170
|
-
raise Error.new("The build context is #{(archive.bytesize / 1_048_576) + 1} MiB compressed; the most is 100 MiB.",
|
|
171
|
-
code: "context_too_large", hint: "Leave build outputs and dependencies out with a .dockerignore.")
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
[archive, files, dockerignore]
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
# A .dockerignore as Docker reads it: # comments, ! re-includes, **
|
|
178
|
-
# crosses directories, a pattern naming a directory excludes what is in it,
|
|
179
|
-
# and the last matching pattern decides.
|
|
180
|
-
def self.dockerignore_filter(text)
|
|
181
|
-
rules = text.gsub(/\r\n?/, "\n").split("\n").filter_map do |raw|
|
|
182
|
-
line = raw.strip
|
|
183
|
-
next if line.empty? || line.start_with?("#")
|
|
184
|
-
|
|
185
|
-
negate = line.start_with?("!")
|
|
186
|
-
line = line[1..].strip if negate
|
|
187
|
-
line = normalize(line.sub(%r{\A/+}, "")).sub(%r{/+\z}, "")
|
|
188
|
-
next if line.empty? || line == "."
|
|
189
|
-
|
|
190
|
-
[negate, Regexp.new("\\A#{translate(line)}\\z")]
|
|
191
|
-
end
|
|
192
|
-
lambda do |path|
|
|
193
|
-
parts = path.split("/")
|
|
194
|
-
rules.reduce(false) do |excluded, (negate, regex)|
|
|
195
|
-
parts.length.downto(1).any? { |n| regex.match?(parts[0, n].join("/")) } ? !negate : excluded
|
|
196
|
-
end
|
|
197
|
-
end
|
|
198
|
-
end
|
|
199
|
-
|
|
200
|
-
def self.translate(line)
|
|
201
|
-
source = +""
|
|
202
|
-
i = 0
|
|
203
|
-
while i < line.length
|
|
204
|
-
char = line[i]
|
|
205
|
-
if char == "*" && line[i + 1] == "*"
|
|
206
|
-
if line[i + 2] == "/"
|
|
207
|
-
source << "(?:.*/)?"
|
|
208
|
-
i += 2
|
|
209
|
-
else
|
|
210
|
-
source << ".*"
|
|
211
|
-
i += 1
|
|
212
|
-
end
|
|
213
|
-
elsif char == "*" then source << "[^/]*"
|
|
214
|
-
elsif char == "?" then source << "[^/]"
|
|
215
|
-
elsif char == "["
|
|
216
|
-
close = line.index("]", i + 1)
|
|
217
|
-
if close.nil?
|
|
218
|
-
source << "\\["
|
|
219
|
-
else
|
|
220
|
-
set = line[(i + 1)...close]
|
|
221
|
-
set = "^#{set[1..]}" if set.start_with?("!")
|
|
222
|
-
source << "[#{set.gsub("\\", "\\\\\\\\")}]"
|
|
223
|
-
i = close
|
|
224
|
-
end
|
|
225
|
-
elsif char == "\\" && i + 1 < line.length
|
|
226
|
-
i += 1
|
|
227
|
-
source << Regexp.escape(line[i])
|
|
228
|
-
else
|
|
229
|
-
source << Regexp.escape(char)
|
|
230
|
-
end
|
|
231
|
-
i += 1
|
|
232
|
-
end
|
|
233
|
-
source
|
|
234
|
-
end
|
|
235
|
-
|
|
236
|
-
def self.normalize(path)
|
|
237
|
-
out = []
|
|
238
|
-
path.split("/").each do |part|
|
|
239
|
-
next if part.empty? || part == "."
|
|
240
|
-
|
|
241
|
-
part == ".." ? out.pop : out.push(part)
|
|
242
|
-
end
|
|
243
|
-
out.empty? ? "." : out.join("/")
|
|
244
|
-
end
|
|
245
|
-
|
|
246
|
-
private
|
|
247
|
-
|
|
248
|
-
def id_of(ref) = UUID.match?(ref) ? ref : resolve(ref).id
|
|
249
|
-
|
|
250
|
-
def prepare(context_dir, fields)
|
|
251
|
-
body = Fields.body(fields)
|
|
252
|
-
return body unless context_dir
|
|
253
|
-
|
|
254
|
-
uploaded = upload_context(context_dir, dockerignore: body["dockerignore"])
|
|
255
|
-
body.merge("context" => uploaded["context"], "dockerignore" => uploaded["dockerignore"]).compact
|
|
256
|
-
end
|
|
257
|
-
end
|
|
258
|
-
end
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module WithRuntime
|
|
4
|
-
class Domains < Product
|
|
5
|
-
def add(hostname:, sandbox_id:, port:, idempotency_key: nil)
|
|
6
|
-
Record.new(@t.json("POST", "/v1/domains", body: Fields.body(hostname: hostname, sandbox_id: sandbox_id, port: port), key: idempotency_key))
|
|
7
|
-
end
|
|
8
|
-
def get(hostname) = Record.new(@t.json("GET", "/v1/domains/#{seg(hostname)}"))
|
|
9
|
-
def verify(hostname) = Record.new(@t.json("POST", "/v1/domains/#{seg(hostname)}:verify", body: {}))
|
|
10
|
-
def list = @t.json("GET", "/v1/domains")["data"].map { |row| Record.new(row) }
|
|
11
|
-
def remove(hostname) = @t.json("DELETE", "/v1/domains/#{seg(hostname)}") && nil
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
class Ports < Product
|
|
15
|
-
def open(sandbox_id:, port:, idempotency_key: nil)
|
|
16
|
-
Record.new(@t.json("POST", "/v1/ports", body: Fields.body(sandbox_id: sandbox_id, port: port), key: idempotency_key))
|
|
17
|
-
end
|
|
18
|
-
def list(sandbox_id: nil) = @t.json("GET", "/v1/ports", query: { "sandboxId" => sandbox_id })["data"].map { |row| Record.new(row) }
|
|
19
|
-
def close(id) = @t.json("DELETE", "/v1/ports/#{seg(id)}") && nil
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
class Addresses < Product
|
|
23
|
-
def reserve(family: 4) = Record.new(@t.json("POST", "/v1/ips", body: { "family" => family }))
|
|
24
|
-
def list = @t.json("GET", "/v1/ips")["data"].map { |row| Record.new(row) }
|
|
25
|
-
def release(id) = @t.json("DELETE", "/v1/ips/#{seg(id)}") && nil
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# Account WireGuard networking, distinct from Sandbox#open_tunnel.
|
|
29
|
-
class PrivateTunnel < Product
|
|
30
|
-
def get = Record.new(@t.json("GET", "/v1/networks/wireguard"))
|
|
31
|
-
def create(subnet: nil) = Record.new(@t.json("POST", "/v1/networks/wireguard", body: { "subnet" => subnet }.compact))
|
|
32
|
-
def delete = @t.json("DELETE", "/v1/networks/wireguard") && nil
|
|
33
|
-
# Save config even when config_ready is false: its private key is shown once.
|
|
34
|
-
def add_peer(name:, public_key: nil, routes: nil, idempotency_key: nil)
|
|
35
|
-
Record.new(@t.json("POST", "/v1/networks/wireguard/peers", body: Fields.body(name: name, public_key: public_key, routes: routes), key: idempotency_key, no_retry: public_key.to_s.empty?))
|
|
36
|
-
end
|
|
37
|
-
def rotate_peer(id, public_key: nil)
|
|
38
|
-
Record.new(@t.json("POST", "/v1/networks/wireguard/peers/#{seg(id)}:rotate", body: { "publicKey" => public_key }.compact, no_retry: public_key.to_s.empty?))
|
|
39
|
-
end
|
|
40
|
-
def remove_peer(id) = Record.new(@t.json("DELETE", "/v1/networks/wireguard/peers/#{seg(id)}"))
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
class SSO < Product
|
|
44
|
-
def get = Record.new(@t.json("GET", "/v1/sso"))
|
|
45
|
-
end
|
|
46
|
-
class MCP < Product
|
|
47
|
-
def catalog = @t.json("GET", "/v1/mcp/catalog")["data"].map { |row| Record.new(row) }
|
|
48
|
-
end
|
|
49
|
-
end
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module WithRuntime
|
|
4
|
-
class SandboxProduct
|
|
5
|
-
def initialize(sandbox)
|
|
6
|
-
@s = sandbox
|
|
7
|
-
@t = sandbox.transport
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
class Mounts < SandboxProduct
|
|
12
|
-
def add(idempotency_key: nil, **options) = Record.new(@t.json("POST", @s.path("/mounts"), body: Fields.body(options), key: idempotency_key))
|
|
13
|
-
def list = @t.json("GET", @s.path("/mounts"))["data"].map { |row| Record.new(row) }
|
|
14
|
-
def remove(path) = @t.json("POST", @s.path("/mounts:unmount"), body: { "path" => path }) && nil
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
class SandboxMCP < SandboxProduct
|
|
18
|
-
def start(servers:, port: nil, replace: nil, idempotency_key: nil)
|
|
19
|
-
# Secrets/env/options are customer dictionary keys, not SDK field names.
|
|
20
|
-
servers = servers.map do |entry|
|
|
21
|
-
entry.to_h.each_with_object({}) do |(key, value), out|
|
|
22
|
-
name = Fields.camel(key)
|
|
23
|
-
out[name] = %w[secrets env options].include?(name) ? Fields.stringify(value) : Fields.convert(value)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
Record.new(@t.json("POST", @s.path("/mcp"), body: { "servers" => servers, "port" => port, "replace" => replace }.compact, key: idempotency_key))
|
|
27
|
-
end
|
|
28
|
-
def get = Record.new(@t.json("GET", @s.path("/mcp")))
|
|
29
|
-
def stop = @t.json("DELETE", @s.path("/mcp")) && nil
|
|
30
|
-
def ready(timeout: 600)
|
|
31
|
-
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
32
|
-
loop do
|
|
33
|
-
left = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
34
|
-
raise @t.connection_error(true, false, nil) unless left.positive?
|
|
35
|
-
state = Record.new(@t.json("GET", @s.path("/mcp"), timeout: left))
|
|
36
|
-
return state unless state.running && Array(state.servers).any? { |server| server.status == "installing" }
|
|
37
|
-
sleep([2, left].min)
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
class Recordings < SandboxProduct
|
|
43
|
-
def path(id = nil) = @s.path("/desktop/recordings") + (id ? "/#{Transport.segment(id)}" : "")
|
|
44
|
-
def start(idempotency_key: nil, **options) = Record.new(@t.json("POST", path, body: Fields.body(options), key: idempotency_key))
|
|
45
|
-
def get(id) = Record.new(@t.json("GET", path(id)))
|
|
46
|
-
def list = @t.json("GET", path)["data"].map { |row| Record.new(row) }
|
|
47
|
-
def stop(id) = Record.new(@t.json("POST", "#{path(id)}:stop", body: {}))
|
|
48
|
-
def download(id) = @t.file_bytes("#{path(id)}/video", accept: "video/mp4")
|
|
49
|
-
def delete(id) = @t.json("DELETE", path(id)) && nil
|
|
50
|
-
end
|
|
51
|
-
end
|