unikraft-cloud 0.1.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 +7 -0
- data/LICENSE.md +28 -0
- data/README.md +587 -0
- data/lib/unikraft/cloud/api/controlplane/auth.gen.rb +83 -0
- data/lib/unikraft/cloud/api/controlplane/images.gen.rb +51 -0
- data/lib/unikraft/cloud/api/controlplane/metros.gen.rb +45 -0
- data/lib/unikraft/cloud/api/controlplane/models.gen.rb +865 -0
- data/lib/unikraft/cloud/api/controlplane/node_activation_service.gen.rb +79 -0
- data/lib/unikraft/cloud/api/controlplane/node_service.gen.rb +276 -0
- data/lib/unikraft/cloud/api/controlplane/resources.gen.rb +12 -0
- data/lib/unikraft/cloud/api/controlplane.rb +49 -0
- data/lib/unikraft/cloud/api/platform/audit.gen.rb +55 -0
- data/lib/unikraft/cloud/api/platform/autoscale.gen.rb +231 -0
- data/lib/unikraft/cloud/api/platform/certificates.gen.rb +168 -0
- data/lib/unikraft/cloud/api/platform/images.gen.rb +109 -0
- data/lib/unikraft/cloud/api/platform/instances.gen.rb +873 -0
- data/lib/unikraft/cloud/api/platform/models.gen.rb +6456 -0
- data/lib/unikraft/cloud/api/platform/node.gen.rb +45 -0
- data/lib/unikraft/cloud/api/platform/resources.gen.rb +16 -0
- data/lib/unikraft/cloud/api/platform/services.gen.rb +172 -0
- data/lib/unikraft/cloud/api/platform/users.gen.rb +134 -0
- data/lib/unikraft/cloud/api/platform/volumes.gen.rb +430 -0
- data/lib/unikraft/cloud/api/platform.rb +64 -0
- data/lib/unikraft/cloud/api.rb +45 -0
- data/lib/unikraft/cloud/client.rb +186 -0
- data/lib/unikraft/cloud/core/base64.rb +36 -0
- data/lib/unikraft/cloud/core/errors.rb +96 -0
- data/lib/unikraft/cloud/core/fanout.rb +150 -0
- data/lib/unikraft/cloud/core/handle.rb +214 -0
- data/lib/unikraft/cloud/core/handle_set.rb +105 -0
- data/lib/unikraft/cloud/core/http.rb +361 -0
- data/lib/unikraft/cloud/core/metro.rb +75 -0
- data/lib/unikraft/cloud/core/pagination.rb +43 -0
- data/lib/unikraft/cloud/core/patch.rb +113 -0
- data/lib/unikraft/cloud/core/pool.rb +97 -0
- data/lib/unikraft/cloud/core/ready.rb +145 -0
- data/lib/unikraft/cloud/core/ref.rb +95 -0
- data/lib/unikraft/cloud/core/resource.rb +399 -0
- data/lib/unikraft/cloud/core/response.rb +96 -0
- data/lib/unikraft/cloud/core/session.rb +182 -0
- data/lib/unikraft/cloud/core/url.rb +34 -0
- data/lib/unikraft/cloud/model.rb +194 -0
- data/lib/unikraft/cloud/resources/certificates.rb +57 -0
- data/lib/unikraft/cloud/resources/instances.rb +172 -0
- data/lib/unikraft/cloud/resources/service_groups.rb +36 -0
- data/lib/unikraft/cloud/resources/users.rb +39 -0
- data/lib/unikraft/cloud/resources/volumes.rb +93 -0
- data/lib/unikraft/cloud/version.rb +11 -0
- data/lib/unikraft/cloud.rb +29 -0
- metadata +89 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
# Copyright (c) 2026, Unikraft GmbH.
|
|
5
|
+
|
|
6
|
+
require_relative "core/http"
|
|
7
|
+
require_relative "core/metro"
|
|
8
|
+
require_relative "core/session"
|
|
9
|
+
|
|
10
|
+
module Unikraft
|
|
11
|
+
# The official Ruby SDK for Unikraft Cloud.
|
|
12
|
+
module Cloud
|
|
13
|
+
# The idiomatic resource clients for one metro scope. Reads fan out across
|
|
14
|
+
# the scope and are merged; each result carries the `metro` it came from.
|
|
15
|
+
class Scope
|
|
16
|
+
# @return [Core::Session] the credentials, transports and metro
|
|
17
|
+
# discovery these clients share
|
|
18
|
+
attr_reader :session
|
|
19
|
+
# @return [:all, Array<String>] the metros these clients cover
|
|
20
|
+
attr_reader :scope
|
|
21
|
+
# @return [Resources::Instances] instances (microVMs)
|
|
22
|
+
attr_reader :instances
|
|
23
|
+
# @return [Resources::Volumes] persistent volumes
|
|
24
|
+
attr_reader :volumes
|
|
25
|
+
# @return [Resources::ServiceGroups] service groups (load-balanced
|
|
26
|
+
# networking)
|
|
27
|
+
attr_reader :services
|
|
28
|
+
# @return [Resources::Certificates] TLS certificates
|
|
29
|
+
attr_reader :certificates
|
|
30
|
+
# @return [Resources::Users] users and quotas
|
|
31
|
+
attr_reader :users
|
|
32
|
+
|
|
33
|
+
# @api private
|
|
34
|
+
def initialize(session, scope, transport = session.transport)
|
|
35
|
+
@session = session
|
|
36
|
+
@scope = scope
|
|
37
|
+
@transport = transport
|
|
38
|
+
@instances = Resources::Instances.new(session, scope, transport)
|
|
39
|
+
@volumes = Resources::Volumes.new(session, scope, transport)
|
|
40
|
+
@services = Resources::ServiceGroups.new(session, scope, transport)
|
|
41
|
+
@certificates = Resources::Certificates.new(session, scope, transport)
|
|
42
|
+
@users = Resources::Users.new(session, scope, transport)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def inspect = "#<#{self.class.name} scope=#{@scope.inspect}>"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The resource clients for exactly one metro, from {Client#metro}. The
|
|
49
|
+
# metro is known, so `get(name: ...).suspend` is one request.
|
|
50
|
+
#
|
|
51
|
+
# A sandbox lives in exactly one metro, so `sandboxes` belongs here rather
|
|
52
|
+
# than on {Scope}, built in {#initialize} from `session` and `endpoint`.
|
|
53
|
+
class MetroScope < Scope
|
|
54
|
+
# @return [Core::Endpoint] the metro these clients are pinned to
|
|
55
|
+
attr_reader :endpoint
|
|
56
|
+
# @return [API] the raw API surfaces, the platform one pinned to this
|
|
57
|
+
# metro
|
|
58
|
+
attr_reader :api
|
|
59
|
+
|
|
60
|
+
# @api private
|
|
61
|
+
def initialize(session, endpoint)
|
|
62
|
+
super(session, [endpoint.metro].freeze, session.transport(endpoint.base_url))
|
|
63
|
+
@endpoint = endpoint
|
|
64
|
+
@api = session.api(endpoint.base_url)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The Unikraft Cloud SDK entry point, built by {Unikraft::Cloud.new}.
|
|
69
|
+
#
|
|
70
|
+
# By default the client is account-wide: reads cover every metro the
|
|
71
|
+
# account can reach and are merged, each result tagged with its metro.
|
|
72
|
+
# Narrow that with {#metro}, {#metros}, `metros:` on a call, or `metro:`
|
|
73
|
+
# / `metros:` here, which also skips metro discovery.
|
|
74
|
+
#
|
|
75
|
+
# A client is thread-safe and meant to be long-lived: build one and share
|
|
76
|
+
# it. {#close} releases its connections.
|
|
77
|
+
#
|
|
78
|
+
# @example
|
|
79
|
+
# ukc = Unikraft::Cloud.new(token: ENV["UKC_TOKEN"])
|
|
80
|
+
# ukc.instances.each { |inst| puts "#{inst.metro} #{inst.name}" }
|
|
81
|
+
# ukc.metro(:fra).instances.get(name: "web").suspend
|
|
82
|
+
class Client < Scope
|
|
83
|
+
# @return [API] the raw ("plumbing") API surfaces: `api.platform`
|
|
84
|
+
# (pointing at the default metro unless a call passes `base_url:`)
|
|
85
|
+
# and `api.controlplane`
|
|
86
|
+
attr_reader :api
|
|
87
|
+
|
|
88
|
+
# @param token [String, nil] bearer token; defaults to `UKC_TOKEN`
|
|
89
|
+
# @param metro [String, Symbol, nil] the metro of operations that must
|
|
90
|
+
# pick one (and the scope, unless `metros:` is given), or a full
|
|
91
|
+
# `http(s)://` base URL for a self-hosted deployment; defaults to
|
|
92
|
+
# `UKC_METRO`
|
|
93
|
+
# @param metros [:all, String, Symbol, Array, nil] the default scope;
|
|
94
|
+
# `:all` (every metro the account reaches) unless `metro:` is set
|
|
95
|
+
# @param base_url [String, nil] an explicit platform base URL;
|
|
96
|
+
# overrides `metro:`
|
|
97
|
+
# @param control_plane_url [String, nil]
|
|
98
|
+
# @param headers [Hash] sent with every request
|
|
99
|
+
# @param timeout [Numeric, nil] seconds per connect and read
|
|
100
|
+
# @param proxy_from_env [Boolean] honour `http_proxy`/`https_proxy`
|
|
101
|
+
# @param user_agent [String, nil]
|
|
102
|
+
# @param transport [#call, nil] builds the transport from the
|
|
103
|
+
# {Core::ApiClient} keywords (`base_url:`, `token:`, ...); for tests
|
|
104
|
+
# or a custom HTTP stack
|
|
105
|
+
def initialize(token: nil, metro: nil, metros: nil, base_url: nil, control_plane_url: nil,
|
|
106
|
+
headers: {}, timeout: Core::ApiClient::DEFAULT_TIMEOUT, proxy_from_env: true,
|
|
107
|
+
user_agent: nil, transport: nil)
|
|
108
|
+
metro ||= env("UKC_METRO")
|
|
109
|
+
explicit = base_url || (metro if Core::Metro.url?(metro))
|
|
110
|
+
config = { token: token || env("UKC_TOKEN"), headers:, timeout:, proxy_from_env:,
|
|
111
|
+
user_agent: }
|
|
112
|
+
session = Core::Session.new(
|
|
113
|
+
config:, transport:, pinned: !explicit.nil?,
|
|
114
|
+
platform_url: Core::Metro.base_url(explicit || metro || DEFAULT_METRO),
|
|
115
|
+
controlplane_url: control_plane_url || CONTROLPLANE_BASE_URL,
|
|
116
|
+
default_metro: (metro || DEFAULT_METRO).to_s
|
|
117
|
+
)
|
|
118
|
+
scope = Core::Metro.scope(metros || metro || :all)
|
|
119
|
+
super(session, scope)
|
|
120
|
+
@api = session.api
|
|
121
|
+
@scopes = {}
|
|
122
|
+
@mutex = Mutex.new
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The resource clients for one metro, cached.
|
|
126
|
+
#
|
|
127
|
+
# @param metro [String, Symbol]
|
|
128
|
+
# @return [MetroScope]
|
|
129
|
+
def metro(metro)
|
|
130
|
+
endpoint = session.endpoint_for(metro)
|
|
131
|
+
@mutex.synchronize { @scopes[endpoint.base_url] ||= MetroScope.new(session, endpoint) }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# The resource clients for several metros, or every one.
|
|
135
|
+
#
|
|
136
|
+
# @example
|
|
137
|
+
# ukc.metros(:fra, :dal).instances.to_a
|
|
138
|
+
# ukc.metros(:all)
|
|
139
|
+
# @return [Scope]
|
|
140
|
+
def metros(*metros) = Scope.new(session, Core::Metro.scope(metros))
|
|
141
|
+
|
|
142
|
+
# Every metro the account can reach, as the control plane reports it,
|
|
143
|
+
# cached for the client's lifetime.
|
|
144
|
+
#
|
|
145
|
+
# @return [Array<Core::Endpoint>] each with `metro` and `base_url`
|
|
146
|
+
def available_metros = session.discover
|
|
147
|
+
|
|
148
|
+
# Close every connection the client opened. Idempotent; a later call
|
|
149
|
+
# reopens what it needs.
|
|
150
|
+
#
|
|
151
|
+
# @return [nil]
|
|
152
|
+
def close = session.close
|
|
153
|
+
|
|
154
|
+
private
|
|
155
|
+
|
|
156
|
+
def env(name)
|
|
157
|
+
value = ENV.fetch(name, nil)
|
|
158
|
+
value unless value.nil? || value.empty?
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
class << self
|
|
163
|
+
# Build a client.
|
|
164
|
+
#
|
|
165
|
+
# @example
|
|
166
|
+
# ukc = Unikraft::Cloud.new(token: ENV["UKC_TOKEN"])
|
|
167
|
+
# @param (see Client#initialize)
|
|
168
|
+
# @return [Client]
|
|
169
|
+
def new(**) = Client.new(**)
|
|
170
|
+
|
|
171
|
+
# Build a client, yield it, and close it when the block returns.
|
|
172
|
+
#
|
|
173
|
+
# @example
|
|
174
|
+
# Unikraft::Cloud.open(metro: :fra) { |ukc| ukc.instances.count }
|
|
175
|
+
# @param (see Client#initialize)
|
|
176
|
+
# @yieldparam ukc [Client]
|
|
177
|
+
# @return [Object] the block's value
|
|
178
|
+
def open(**)
|
|
179
|
+
client = Client.new(**)
|
|
180
|
+
yield client
|
|
181
|
+
ensure
|
|
182
|
+
client&.close
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
# Copyright (c) 2026, Unikraft GmbH.
|
|
5
|
+
|
|
6
|
+
module Unikraft
|
|
7
|
+
module Cloud
|
|
8
|
+
module Core
|
|
9
|
+
# Strict base64, the way the plugins carry binary payloads. Built on
|
|
10
|
+
# `pack("m0")` because the `base64` library left the default gems in
|
|
11
|
+
# Ruby 3.4, and the SDK takes no runtime dependencies.
|
|
12
|
+
module Base64
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# @param bytes [String] any encoding; its bytes are encoded
|
|
16
|
+
# @return [String] strict (unwrapped) base64
|
|
17
|
+
def encode(bytes)
|
|
18
|
+
[bytes.to_s.b].pack("m0")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# @param text [String] strict base64
|
|
22
|
+
# @return [String] the decoded bytes, as binary
|
|
23
|
+
# @raise [ArgumentError] when the input is not strict base64
|
|
24
|
+
def decode(text)
|
|
25
|
+
text.to_s.unpack1("m0")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @param text [String] strict base64 of UTF-8 text
|
|
29
|
+
# @return [String] the decoded text, as UTF-8
|
|
30
|
+
def decode_text(text)
|
|
31
|
+
decode(text).force_encoding(Encoding::UTF_8)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
# Copyright (c) 2026, Unikraft GmbH.
|
|
5
|
+
|
|
6
|
+
module Unikraft
|
|
7
|
+
module Cloud
|
|
8
|
+
# Raised for every failure the SDK reports. A single class (with
|
|
9
|
+
# subclasses for the multi-metro cases) so one `rescue` covers both APIs.
|
|
10
|
+
#
|
|
11
|
+
# `kind` says what went wrong:
|
|
12
|
+
# - `:http`: the server answered with a non-2xx status;
|
|
13
|
+
# - `:network`: the request never got an answer (refused, reset, DNS, TLS,
|
|
14
|
+
# read timeout);
|
|
15
|
+
# - `:parse`: a 2xx answer whose body is not the JSON it should be;
|
|
16
|
+
# - `:fanout`: a multi-metro operation failed in some metros, or a ref is
|
|
17
|
+
# ambiguous across metros;
|
|
18
|
+
# - `:config`: a call that could never be sent as configured (no token,
|
|
19
|
+
# contradicting options, a body on a GET);
|
|
20
|
+
# - `:timeout`: a wait ran out of time. It carries no `status`, because no
|
|
21
|
+
# single request failed; the last failure is the `cause`.
|
|
22
|
+
class Error < StandardError
|
|
23
|
+
KINDS = %i[http network parse fanout config timeout].freeze
|
|
24
|
+
|
|
25
|
+
# @return [Symbol] one of {KINDS}
|
|
26
|
+
attr_reader :kind
|
|
27
|
+
# @return [Integer, nil] the HTTP status code, when there was one
|
|
28
|
+
attr_reader :status
|
|
29
|
+
# @return [Array<Hash>, nil] the `errors` of the response envelope
|
|
30
|
+
attr_reader :errors
|
|
31
|
+
# @return [Object, nil] the parsed response body, or the raw text
|
|
32
|
+
attr_reader :body
|
|
33
|
+
|
|
34
|
+
# @param message [String]
|
|
35
|
+
# @param kind [Symbol] one of {KINDS}
|
|
36
|
+
# @param status [Integer, nil]
|
|
37
|
+
# @param errors [Array<Hash>, nil]
|
|
38
|
+
# @param body [Object, nil]
|
|
39
|
+
# @param cause [Exception, nil] overrides Ruby's implicit `cause`
|
|
40
|
+
def initialize(message = nil, kind:, status: nil, errors: nil, body: nil, cause: nil)
|
|
41
|
+
raise ArgumentError, "unknown error kind #{kind.inspect}" unless KINDS.include?(kind)
|
|
42
|
+
|
|
43
|
+
super(message)
|
|
44
|
+
@kind = kind
|
|
45
|
+
@status = status
|
|
46
|
+
@errors = errors
|
|
47
|
+
@body = body
|
|
48
|
+
@cause = cause
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The underlying failure: the one given explicitly, or else the
|
|
52
|
+
# exception being handled when this one was raised.
|
|
53
|
+
def cause
|
|
54
|
+
@cause || super
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Raised when a name matches in more than one metro and the operation can
|
|
59
|
+
# act on only one. Names are unique within a metro, not across them, so
|
|
60
|
+
# this is routine: qualify the ref with a metro, narrow the scope, or act
|
|
61
|
+
# on every match. The matches are attached, so recovering costs nothing.
|
|
62
|
+
class AmbiguousRefError < Error
|
|
63
|
+
# @return [Array] the matching resources, each tagged with its `metro`
|
|
64
|
+
attr_reader :matches
|
|
65
|
+
|
|
66
|
+
def initialize(message, matches:)
|
|
67
|
+
super(message, kind: :fanout, body: matches)
|
|
68
|
+
@matches = matches
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# @return [Array<String>] the metros the name was found in
|
|
72
|
+
def metros
|
|
73
|
+
matches.map(&:metro)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Raised when an operation fanned out over several metros failed in some
|
|
78
|
+
# of them. `failures` holds `{metro:, error:}` entries in the order they
|
|
79
|
+
# occurred; `results` holds whatever the other metros returned.
|
|
80
|
+
class MetroFanoutError < Error
|
|
81
|
+
# @return [Array<Hash>] `{metro:, error:}` per failed metro
|
|
82
|
+
attr_reader :failures
|
|
83
|
+
# @return [Array] the results of the metros that succeeded
|
|
84
|
+
attr_reader :results
|
|
85
|
+
|
|
86
|
+
def initialize(message, failures:, results: [])
|
|
87
|
+
statuses = failures.map { |f| f[:error].respond_to?(:status) ? f[:error].status : nil }.uniq
|
|
88
|
+
# A single underlying status is surfaced, so `status == 403` still matches.
|
|
89
|
+
super(message, kind: :fanout, status: statuses.size == 1 ? statuses.first : nil,
|
|
90
|
+
body: failures, cause: failures.first&.dig(:error))
|
|
91
|
+
@failures = failures
|
|
92
|
+
@results = results
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
# Copyright (c) 2026, Unikraft GmbH.
|
|
5
|
+
#
|
|
6
|
+
# Running one operation across several metros and sewing the results back
|
|
7
|
+
# together. The platform API is metro-scoped, so an account-wide view means
|
|
8
|
+
# asking every metro and merging what comes back.
|
|
9
|
+
|
|
10
|
+
require_relative "errors"
|
|
11
|
+
|
|
12
|
+
module Unikraft
|
|
13
|
+
module Cloud
|
|
14
|
+
module Core
|
|
15
|
+
# Concurrent per-metro work on threads.
|
|
16
|
+
module Fanout
|
|
17
|
+
# One item's outcome from {settled}.
|
|
18
|
+
Outcome = Data.define(:item, :value, :error) do
|
|
19
|
+
def ok? = error.nil?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
# Run `producer` for every endpoint concurrently and push each item it
|
|
25
|
+
# emits onto `out` as soon as it arrives, so a slow metro never holds
|
|
26
|
+
# up a fast one. Every healthy metro is drained first, then one
|
|
27
|
+
# {MetroFanoutError} names the failures. A single endpoint runs
|
|
28
|
+
# inline, and its failure propagates as itself. Only an {Error} is a
|
|
29
|
+
# metro failure: anything else (a bug, an interrupt) is re-raised.
|
|
30
|
+
#
|
|
31
|
+
# The queue holds about one item per metro, so a producer fetches
|
|
32
|
+
# its next page only once the consumer has nearly used up the last.
|
|
33
|
+
# Leaving early (`break`, `first`) kills the metros still in flight.
|
|
34
|
+
# An external enumerator (`Enumerator#next`) abandoned midway never
|
|
35
|
+
# leaves, so its producers stay parked on the full queue, sending
|
|
36
|
+
# nothing, until the process exits: prefer `each`, `first` or `lazy`.
|
|
37
|
+
#
|
|
38
|
+
# @param endpoints [Array<Endpoint>]
|
|
39
|
+
# @param out [#<<] e.g. an Enumerator::Yielder
|
|
40
|
+
# @yieldparam endpoint [Endpoint]
|
|
41
|
+
# @yieldparam emit [Proc] call with each item
|
|
42
|
+
def merge(endpoints, out)
|
|
43
|
+
return yield(endpoints.first, ->(item) { out << item }) if endpoints.size == 1
|
|
44
|
+
|
|
45
|
+
queue = SizedQueue.new(endpoints.size)
|
|
46
|
+
threads = endpoints.map do |endpoint|
|
|
47
|
+
Thread.new do
|
|
48
|
+
error = nil
|
|
49
|
+
yield endpoint, ->(item) { queue << [:item, item] }
|
|
50
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
51
|
+
error = e # handed to the consumer, which re-raises what is not an Error
|
|
52
|
+
ensure
|
|
53
|
+
# Always report, or the consumer waits forever. A killed
|
|
54
|
+
# producer (the consumer left, or the process is exiting) has
|
|
55
|
+
# no one to report to, and could block on the full queue.
|
|
56
|
+
begin
|
|
57
|
+
queue << [:finished, endpoint, error] unless Thread.current.status == "aborting"
|
|
58
|
+
rescue ClosedQueueError
|
|
59
|
+
nil
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
drain(queue, out, endpoints.size)
|
|
64
|
+
ensure
|
|
65
|
+
queue&.close # wakes producers blocked on a full queue
|
|
66
|
+
threads&.each(&:kill)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def drain(queue, out, total)
|
|
70
|
+
failures = []
|
|
71
|
+
pending = total
|
|
72
|
+
while pending.positive?
|
|
73
|
+
kind, value, error = queue.pop
|
|
74
|
+
next out << value if kind == :item
|
|
75
|
+
|
|
76
|
+
pending -= 1
|
|
77
|
+
next if error.nil?
|
|
78
|
+
raise error unless error.is_a?(Error)
|
|
79
|
+
|
|
80
|
+
failures << { metro: value.metro, error: }
|
|
81
|
+
end
|
|
82
|
+
raise error(total, failures) unless failures.empty?
|
|
83
|
+
end
|
|
84
|
+
private_class_method :drain
|
|
85
|
+
|
|
86
|
+
# Run the block for every item (anything with a `metro`) concurrently
|
|
87
|
+
# and report every outcome, successes and failures alike. A failure
|
|
88
|
+
# is an {Error}; any other exception propagates.
|
|
89
|
+
#
|
|
90
|
+
# @return [Array<Outcome>] in the order of `items`
|
|
91
|
+
def settled(items)
|
|
92
|
+
run = lambda do |item|
|
|
93
|
+
Outcome.new(item:, value: yield(item), error: nil)
|
|
94
|
+
rescue Error => e
|
|
95
|
+
Outcome.new(item:, value: nil, error: e)
|
|
96
|
+
end
|
|
97
|
+
return items.map(&run) if items.size < 2
|
|
98
|
+
|
|
99
|
+
# Collected as they finish, so a bug (anything but an Error) in one
|
|
100
|
+
# surfaces at once, even while another is still blocked.
|
|
101
|
+
done = Queue.new
|
|
102
|
+
threads = items.each_with_index.map do |item, i|
|
|
103
|
+
Thread.new do
|
|
104
|
+
done << [i, run.call(item), nil]
|
|
105
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
106
|
+
done << [i, nil, e]
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
items.size.times.with_object(Array.new(items.size)) do |_, outcomes|
|
|
110
|
+
i, outcome, error = done.pop
|
|
111
|
+
raise error if error
|
|
112
|
+
|
|
113
|
+
outcomes[i] = outcome
|
|
114
|
+
end
|
|
115
|
+
ensure
|
|
116
|
+
# One raised: stop the rest, or they go on sending after the caller left.
|
|
117
|
+
threads&.each(&:kill)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Run the block for every item concurrently and return the values.
|
|
121
|
+
# When some fail, raise a {MetroFanoutError} carrying the successes as
|
|
122
|
+
# `results`.
|
|
123
|
+
#
|
|
124
|
+
# @param flat [Boolean] each value is an Array to concatenate
|
|
125
|
+
# @return [Array]
|
|
126
|
+
def all(items, flat: false, &)
|
|
127
|
+
outcomes = settled(items, &)
|
|
128
|
+
results = outcomes.select(&:ok?).map(&:value)
|
|
129
|
+
results = results.flatten(1) if flat
|
|
130
|
+
failures = outcomes.reject(&:ok?).map { |o| { metro: o.item.metro, error: o.error } }
|
|
131
|
+
raise error(items.size, failures, results:) unless failures.empty?
|
|
132
|
+
|
|
133
|
+
results
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The aggregate error, e.g. `1 of 3 metros failed: sin (503)`.
|
|
137
|
+
#
|
|
138
|
+
# @return [MetroFanoutError]
|
|
139
|
+
def error(attempted, failures, results: [])
|
|
140
|
+
detail = failures.map do |f|
|
|
141
|
+
status = f[:error].respond_to?(:status) ? f[:error].status : nil
|
|
142
|
+
status ? "#{f[:metro]} (#{status})" : f[:metro]
|
|
143
|
+
end
|
|
144
|
+
message = "#{failures.size} of #{attempted} metros failed: #{detail.join(", ")}"
|
|
145
|
+
MetroFanoutError.new(message, failures:, results:)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
# Copyright (c) 2026, Unikraft GmbH.
|
|
5
|
+
|
|
6
|
+
require_relative "patch"
|
|
7
|
+
require_relative "ref"
|
|
8
|
+
require_relative "response"
|
|
9
|
+
require_relative "../model"
|
|
10
|
+
|
|
11
|
+
module Unikraft
|
|
12
|
+
module Cloud
|
|
13
|
+
module Core
|
|
14
|
+
# Where a resource was found, and the resource itself when finding it
|
|
15
|
+
# already read it.
|
|
16
|
+
Located = Data.define(:location, :value)
|
|
17
|
+
|
|
18
|
+
# A reference to one resource in one metro.
|
|
19
|
+
#
|
|
20
|
+
# `get` returns a handle without sending anything. Reading an attribute
|
|
21
|
+
# (`handle.state`) resolves it once: it locates the resource if the
|
|
22
|
+
# scope spans several metros, reads it, and memoizes the result.
|
|
23
|
+
#
|
|
24
|
+
# Ruby has no `await`, and a lazy mutation that nobody forces would
|
|
25
|
+
# silently never run, so mutations (`delete`, `update`, `apply`, and
|
|
26
|
+
# each resource's own verbs) run immediately. Each returns a new handle
|
|
27
|
+
# carrying what the endpoint reported, at the same location, so calls
|
|
28
|
+
# still chain:
|
|
29
|
+
#
|
|
30
|
+
# ukc.instances.get(name: "web").suspend.state # => "standby"
|
|
31
|
+
#
|
|
32
|
+
# A handle is meant for one thread. The client behind it is shared.
|
|
33
|
+
class Handle
|
|
34
|
+
class << self
|
|
35
|
+
# Define a reader per attribute of the resource's model, resolving
|
|
36
|
+
# the handle on first use. Names the handle already answers keep
|
|
37
|
+
# their own meaning.
|
|
38
|
+
#
|
|
39
|
+
# @param klass [Class<Model>]
|
|
40
|
+
def attributes_of(klass)
|
|
41
|
+
klass.attributes.each_key do |name|
|
|
42
|
+
next if method_defined?(name) || private_method_defined?(name)
|
|
43
|
+
|
|
44
|
+
define_method(name) { attribute(name) }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @api private
|
|
50
|
+
# @param partial [Boolean] `located.value` is a mutation's reply,
|
|
51
|
+
# which may leave out attributes the resource has
|
|
52
|
+
def initialize(resource, ref, opts = {}, located: nil, partial: false, &locator)
|
|
53
|
+
@resource = resource
|
|
54
|
+
@ref = ref
|
|
55
|
+
@opts = opts
|
|
56
|
+
@located = located
|
|
57
|
+
@locator = locator
|
|
58
|
+
@model = located&.value
|
|
59
|
+
@partial = partial
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Where the resource lives, locating it first if the scope spans
|
|
63
|
+
# several metros. Raises {AmbiguousRefError} when a name matches in
|
|
64
|
+
# more than one. An operation's own `headers:`/`timeout:` apply to
|
|
65
|
+
# that lookup too, over those given to `get`.
|
|
66
|
+
#
|
|
67
|
+
# @param call [Hash] `headers:`, `timeout:`
|
|
68
|
+
# @return [Location]
|
|
69
|
+
def location(**call)
|
|
70
|
+
(@located ||= @locator.call(call_opts(call))).location
|
|
71
|
+
end
|
|
72
|
+
alias resolve location
|
|
73
|
+
|
|
74
|
+
# @return [String] the metro holding the resource
|
|
75
|
+
def metro = location.metro
|
|
76
|
+
|
|
77
|
+
# The resource: what the endpoint reported for a handle a mutation
|
|
78
|
+
# returned, or else the resource read once and memoized.
|
|
79
|
+
#
|
|
80
|
+
# @return [Model]
|
|
81
|
+
def model
|
|
82
|
+
@model ||= (@located ||= @locator.call).value || @resource.fetch(location,
|
|
83
|
+
**call_opts(@opts))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Read the resource again (in place, like ActiveRecord's reload).
|
|
87
|
+
#
|
|
88
|
+
# @return [self]
|
|
89
|
+
def reload(**call)
|
|
90
|
+
@model = @resource.fetch(location(**call), **call_opts(@opts), **call)
|
|
91
|
+
@partial = false
|
|
92
|
+
self
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# @return [Object] a field by its wire key, declared or not
|
|
96
|
+
def [](key) = model[key]
|
|
97
|
+
|
|
98
|
+
# @return [Hash]
|
|
99
|
+
def to_h = model.to_h
|
|
100
|
+
|
|
101
|
+
def deconstruct_keys(keys) = model.deconstruct_keys(keys)
|
|
102
|
+
|
|
103
|
+
# Delete the resource.
|
|
104
|
+
#
|
|
105
|
+
# @param call [Hash] `headers:`, `timeout:`
|
|
106
|
+
# @return [Handle] carrying what the endpoint reported
|
|
107
|
+
def delete(**call) = mutate(@resource.spec.delete, [wire(call)], **call)
|
|
108
|
+
|
|
109
|
+
# Change the resource's properties. A value sets a property, nil
|
|
110
|
+
# removes it, an omitted one is left alone. `headers:` and
|
|
111
|
+
# `timeout:` are always call options, never properties. Raw
|
|
112
|
+
# `{prop:, op:, value:}` triples (or a Hash) go positionally.
|
|
113
|
+
#
|
|
114
|
+
# @example
|
|
115
|
+
# inst.update(memory_mb: 512, autokill: nil, timeout: 10)
|
|
116
|
+
# inst.update([{ prop: "memory_mb", op: "set", value: 512 }])
|
|
117
|
+
# @return [Handle] carrying the updated resource
|
|
118
|
+
def update(changes = nil, **kwargs)
|
|
119
|
+
call = kwargs.slice(:headers, :timeout)
|
|
120
|
+
props = kwargs.except(*call.keys)
|
|
121
|
+
if changes && props.any?
|
|
122
|
+
raise ArgumentError, "pass the changes positionally or as keywords, not both"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
patch(Patch.changes(changes || props), **call)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Stage several operations and send them as one update, at the end of
|
|
129
|
+
# the block or on {Patch::Editor#apply}.
|
|
130
|
+
#
|
|
131
|
+
# @example
|
|
132
|
+
# inst.edit.set(memory_mb: 512).add(tags: ["prod"]).apply
|
|
133
|
+
# inst.edit { |e| e.remove(env: ["OLD"]) }
|
|
134
|
+
# @yieldparam editor [Patch::Editor]
|
|
135
|
+
# @return [Patch::Editor, Handle] the editor, or the applied result
|
|
136
|
+
def edit(**call)
|
|
137
|
+
editor = Patch::Editor.new(@resource.spec.noun) { |items| patch(items, **call) }
|
|
138
|
+
return editor unless block_given?
|
|
139
|
+
|
|
140
|
+
yield editor
|
|
141
|
+
editor.apply
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def method_missing(name, *args, &)
|
|
145
|
+
return super unless args.empty? && loaded_attribute?(name)
|
|
146
|
+
|
|
147
|
+
@model[name]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def respond_to_missing?(name, include_private = false)
|
|
151
|
+
loaded_attribute?(name) || super
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def inspect
|
|
155
|
+
return "#<#{self.class.name} #{@ref} (not loaded)>" unless @model
|
|
156
|
+
|
|
157
|
+
"#<#{self.class.name} #{@model.inspect_fields}>"
|
|
158
|
+
end
|
|
159
|
+
alias to_s inspect
|
|
160
|
+
|
|
161
|
+
protected
|
|
162
|
+
|
|
163
|
+
# The ref as the request body carries it.
|
|
164
|
+
def wire(call = {}) = location(**call).ref.wire
|
|
165
|
+
|
|
166
|
+
# Send update triples to where the resource lives.
|
|
167
|
+
def patch(items, **call)
|
|
168
|
+
mutate(@resource.spec.update, items.map { |item| wire(call).merge(item) }, **call)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Send one operation with `body` to the resource's metro, and return
|
|
172
|
+
# a handle on what it reported.
|
|
173
|
+
def mutate(operation, body, **call)
|
|
174
|
+
call = effective(call)
|
|
175
|
+
loc = location(**call)
|
|
176
|
+
res = @resource.api.public_send(operation, body:, **@resource.call_options(loc, call))
|
|
177
|
+
following(loc, one(res), call)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Unwrap a single-resource response, tagged with the metro.
|
|
181
|
+
def one(res, key = @resource.spec.key)
|
|
182
|
+
Response.first(res, key, "#{@resource.spec.noun} #{location.ref}").with_metro(metro)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# The handle a mutation returns keeps the options it ran with, so a
|
|
186
|
+
# later read uses the same credentials.
|
|
187
|
+
def following(loc, value, call = {})
|
|
188
|
+
self.class.new(@resource, loc.ref, @opts.merge(call_opts(call)),
|
|
189
|
+
located: Located.new(location: loc, value:), partial: true)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# A declared attribute. A mutation's reply may leave it out; then
|
|
193
|
+
# the resource is read once to fill it in.
|
|
194
|
+
def attribute(name)
|
|
195
|
+
value = model[name]
|
|
196
|
+
return value unless value.nil? && @partial
|
|
197
|
+
|
|
198
|
+
reload(**call_opts(@opts))[name]
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def call_opts(opts) = opts.slice(:headers, :timeout)
|
|
202
|
+
|
|
203
|
+
# An operation's options over the `headers:`/`timeout:` given to `get`.
|
|
204
|
+
def effective(call) = call_opts(@opts).merge(call)
|
|
205
|
+
|
|
206
|
+
private
|
|
207
|
+
|
|
208
|
+
def loaded_attribute?(name)
|
|
209
|
+
@model.is_a?(Model) && @model.class.attributes.key?(name)
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|