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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +28 -0
  3. data/README.md +587 -0
  4. data/lib/unikraft/cloud/api/controlplane/auth.gen.rb +83 -0
  5. data/lib/unikraft/cloud/api/controlplane/images.gen.rb +51 -0
  6. data/lib/unikraft/cloud/api/controlplane/metros.gen.rb +45 -0
  7. data/lib/unikraft/cloud/api/controlplane/models.gen.rb +865 -0
  8. data/lib/unikraft/cloud/api/controlplane/node_activation_service.gen.rb +79 -0
  9. data/lib/unikraft/cloud/api/controlplane/node_service.gen.rb +276 -0
  10. data/lib/unikraft/cloud/api/controlplane/resources.gen.rb +12 -0
  11. data/lib/unikraft/cloud/api/controlplane.rb +49 -0
  12. data/lib/unikraft/cloud/api/platform/audit.gen.rb +55 -0
  13. data/lib/unikraft/cloud/api/platform/autoscale.gen.rb +231 -0
  14. data/lib/unikraft/cloud/api/platform/certificates.gen.rb +168 -0
  15. data/lib/unikraft/cloud/api/platform/images.gen.rb +109 -0
  16. data/lib/unikraft/cloud/api/platform/instances.gen.rb +873 -0
  17. data/lib/unikraft/cloud/api/platform/models.gen.rb +6456 -0
  18. data/lib/unikraft/cloud/api/platform/node.gen.rb +45 -0
  19. data/lib/unikraft/cloud/api/platform/resources.gen.rb +16 -0
  20. data/lib/unikraft/cloud/api/platform/services.gen.rb +172 -0
  21. data/lib/unikraft/cloud/api/platform/users.gen.rb +134 -0
  22. data/lib/unikraft/cloud/api/platform/volumes.gen.rb +430 -0
  23. data/lib/unikraft/cloud/api/platform.rb +64 -0
  24. data/lib/unikraft/cloud/api.rb +45 -0
  25. data/lib/unikraft/cloud/client.rb +186 -0
  26. data/lib/unikraft/cloud/core/base64.rb +36 -0
  27. data/lib/unikraft/cloud/core/errors.rb +96 -0
  28. data/lib/unikraft/cloud/core/fanout.rb +150 -0
  29. data/lib/unikraft/cloud/core/handle.rb +214 -0
  30. data/lib/unikraft/cloud/core/handle_set.rb +105 -0
  31. data/lib/unikraft/cloud/core/http.rb +361 -0
  32. data/lib/unikraft/cloud/core/metro.rb +75 -0
  33. data/lib/unikraft/cloud/core/pagination.rb +43 -0
  34. data/lib/unikraft/cloud/core/patch.rb +113 -0
  35. data/lib/unikraft/cloud/core/pool.rb +97 -0
  36. data/lib/unikraft/cloud/core/ready.rb +145 -0
  37. data/lib/unikraft/cloud/core/ref.rb +95 -0
  38. data/lib/unikraft/cloud/core/resource.rb +399 -0
  39. data/lib/unikraft/cloud/core/response.rb +96 -0
  40. data/lib/unikraft/cloud/core/session.rb +182 -0
  41. data/lib/unikraft/cloud/core/url.rb +34 -0
  42. data/lib/unikraft/cloud/model.rb +194 -0
  43. data/lib/unikraft/cloud/resources/certificates.rb +57 -0
  44. data/lib/unikraft/cloud/resources/instances.rb +172 -0
  45. data/lib/unikraft/cloud/resources/service_groups.rb +36 -0
  46. data/lib/unikraft/cloud/resources/users.rb +39 -0
  47. data/lib/unikraft/cloud/resources/volumes.rb +93 -0
  48. data/lib/unikraft/cloud/version.rb +11 -0
  49. data/lib/unikraft/cloud.rb +29 -0
  50. metadata +89 -0
@@ -0,0 +1,97 @@
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
+ # The idle, started Net::HTTP connections of one {ApiClient}, by
10
+ # destination. A connection is either checked out by exactly one
11
+ # thread or idle here, never both, so no two threads share one.
12
+ class Pool
13
+ # Idle connections kept per destination; more are closed on checkin.
14
+ MAX_IDLE = 8
15
+
16
+ # Seconds a connection may sit idle before it is discarded. Below
17
+ # the idle timeout of typical servers and load balancers, so the
18
+ # server rarely closes a connection first.
19
+ KEEP_ALIVE = 5
20
+
21
+ # @return [Numeric] the keep-alive window in seconds
22
+ attr_reader :keep_alive
23
+
24
+ # @param max_idle [Integer] idle connections kept per key
25
+ # @param keep_alive [Numeric] seconds an idle connection is kept
26
+ def initialize(max_idle: MAX_IDLE, keep_alive: KEEP_ALIVE)
27
+ @max_idle = max_idle
28
+ @keep_alive = keep_alive
29
+ @mutex = Mutex.new
30
+ @idle = Hash.new { |h, k| h[k] = [] }
31
+ @pid = Process.pid
32
+ end
33
+
34
+ # Take the most recently used idle connection for key, closing the
35
+ # expired ones on the way.
36
+ #
37
+ # @return [Net::HTTP, nil] nil when none is idle
38
+ def checkout(key)
39
+ expired = []
40
+ http = @mutex.synchronize do
41
+ forget_if_forked
42
+ idle = @idle[key]
43
+ while (entry = idle.pop)
44
+ break entry[0] if now - entry[1] < @keep_alive
45
+
46
+ expired << entry[0]
47
+ end
48
+ end
49
+ expired.each { |c| discard(c) }
50
+ http
51
+ end
52
+
53
+ # Return a connection that finished its exchange cleanly.
54
+ def checkin(key, http)
55
+ kept = @mutex.synchronize do
56
+ forget_if_forked
57
+ idle = @idle[key]
58
+ idle << [http, now] if idle.size < @max_idle
59
+ end
60
+ discard(http) unless kept
61
+ end
62
+
63
+ # Close a connection; never raises.
64
+ def discard(http)
65
+ http.finish
66
+ rescue StandardError
67
+ nil
68
+ end
69
+
70
+ # Close every idle connection. Idempotent; the pool stays usable.
71
+ def close
72
+ conns = @mutex.synchronize do
73
+ forget_if_forked
74
+ all = @idle.values.flatten(1).map(&:first)
75
+ @idle.clear
76
+ all
77
+ end
78
+ conns.each { |c| discard(c) }
79
+ nil
80
+ end
81
+
82
+ private
83
+
84
+ # A forked child shares its parent's sockets: closing them would
85
+ # cut the parent's connections, so the child only forgets them.
86
+ def forget_if_forked
87
+ return if @pid == Process.pid
88
+
89
+ @idle = Hash.new { |h, k| h[k] = [] }
90
+ @pid = Process.pid
91
+ end
92
+
93
+ def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+ #
6
+ # Asking a service repeatedly whether it is ready yet.
7
+ #
8
+ # This module owns the mechanism only: the backoff, the deadline and, most
9
+ # importantly, which failures are worth asking again about. What to ask, and
10
+ # what to say when the answer never comes, belong to the caller.
11
+
12
+ require_relative "errors"
13
+
14
+ module Unikraft
15
+ module Cloud
16
+ module Core
17
+ # Polling with capped, jittered exponential backoff and a deadline.
18
+ module Ready
19
+ # The values {wait_until_ready} uses for anything left out, in seconds.
20
+ DEFAULTS = { initial_delay: 0.1, max_delay: 2.0, timeout: 60.0 }.freeze
21
+
22
+ # The HTTP statuses worth probing again for. 502/503/504 are a proxy
23
+ # with nothing to forward to yet; 404 is a plugin route not registered
24
+ # yet while the instance boots. Everything else is final, and 401/403
25
+ # especially so: retrying a rejected token would turn "unauthorized"
26
+ # into a timeout and hide the one error the caller can act on.
27
+ RETRYABLE_STATUSES = [404, 502, 503, 504].freeze
28
+
29
+ module_function
30
+
31
+ # Whether a failed probe says "not yet" rather than "no".
32
+ #
33
+ # @param err [Exception]
34
+ # @return [Boolean]
35
+ def retryable?(err)
36
+ return false unless err.is_a?(Unikraft::Cloud::Error)
37
+ # A refused connection, a reset, a DNS miss: still coming up.
38
+ return true if err.kind == :network
39
+
40
+ err.kind == :http && RETRYABLE_STATUSES.include?(err.status)
41
+ end
42
+
43
+ # Probe until it succeeds, and return once it does.
44
+ #
45
+ # The block is the probe. It should be cheap, idempotent, and answer as
46
+ # soon as the service is usable. It is given the seconds left until the
47
+ # deadline, and should pass them on as the request's `timeout:`, since
48
+ # a probe in flight is not interrupted.
49
+ #
50
+ # @param initial_delay [Numeric] first delay between probes (0.1 s)
51
+ # @param max_delay [Numeric] cap on the delay between probes (2 s)
52
+ # @param timeout [Numeric] give up after this long (60 s)
53
+ # @param what [String] the subject of the timeout message
54
+ # @param diagnose [#call, nil] asked once, only after the deadline, with
55
+ # the last error; returns a phrase naming the real cause, appended to
56
+ # the timeout message
57
+ # @yieldparam remaining [Float] seconds left until the deadline
58
+ # @return [nil]
59
+ # @raise [Unikraft::Cloud::Error] kind `:timeout` when the deadline
60
+ # passes, with the last probe failure as `cause`; or the probe's own
61
+ # error when it is not retryable
62
+ # @raise [ArgumentError] when a duration is not a usable number
63
+ #
64
+ # @example
65
+ # Ready.wait_until_ready(timeout: 10, what: "sandbox #{uuid}") do |left|
66
+ # api.list_commands(timeout: left)
67
+ # end
68
+ def wait_until_ready(initial_delay: DEFAULTS[:initial_delay],
69
+ max_delay: DEFAULTS[:max_delay], timeout: DEFAULTS[:timeout],
70
+ what: "the service", diagnose: nil)
71
+ delay = duration(:initial_delay, initial_delay, zero: false)
72
+ max_delay = duration(:max_delay, max_delay, zero: false)
73
+ timeout = duration(:timeout, timeout, zero: true)
74
+ deadline = now + timeout
75
+ attempts = 0
76
+ last_error = nil
77
+
78
+ loop do
79
+ attempts += 1
80
+ begin
81
+ yield [deadline - now, 0.0].max
82
+ return nil
83
+ rescue Unikraft::Cloud::Error => e
84
+ raise unless retryable?(e)
85
+
86
+ last_error = e
87
+ end
88
+
89
+ delay = back_off(delay, max_delay, deadline - now)
90
+ next if now < deadline
91
+
92
+ raise timed_out(what, timeout, attempts, last_error, diagnose)
93
+ end
94
+ end
95
+
96
+ # Sleep before the next probe, never past the deadline, and return the
97
+ # next delay. Jitter covers the lower half of the window, so a fleet
98
+ # created together does not probe in lockstep.
99
+ def back_off(delay, max_delay, remaining)
100
+ return delay unless remaining.positive?
101
+
102
+ window = [delay, max_delay].min
103
+ sleep([(window / 2.0) + (rand * window / 2.0), remaining].min)
104
+ [delay * 2, max_delay].min
105
+ end
106
+
107
+ def timed_out(what, timeout, attempts, last_error, diagnose)
108
+ why = begin
109
+ diagnose&.call(last_error)
110
+ rescue StandardError
111
+ nil # A failed diagnosis must not replace the timeout it explains.
112
+ end
113
+ last = if last_error
114
+ "The last attempt failed with: #{last_error.message}"
115
+ else
116
+ "The last probe never answered."
117
+ end
118
+ # No `status`: the deadline failed, not one request. Carrying the last
119
+ # probe's 404 would make this look like a missing resource.
120
+ Unikraft::Cloud::Error.new(
121
+ "#{what} was not ready within #{timeout} s (#{attempts} " \
122
+ "#{attempts == 1 ? "probe" : "probes"})#{": #{why}" if why}. #{last}",
123
+ kind: :timeout, cause: last_error
124
+ )
125
+ end
126
+
127
+ # A value the loop cannot use (NaN, negative) would poll flat out
128
+ # instead of waiting, so it is refused rather than clamped.
129
+ def duration(name, value, zero:)
130
+ ok = value.is_a?(Numeric) && value.to_f.finite? && (zero ? value >= 0 : value.positive?)
131
+ return value.to_f if ok
132
+
133
+ raise ArgumentError, "`#{name}` must be a number of seconds, " \
134
+ "#{zero ? "0 or more" : "above 0"}. Received #{value.inspect}."
135
+ end
136
+
137
+ def now
138
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
139
+ end
140
+
141
+ private_class_method :back_off, :timed_out, :duration, :now
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,95 @@
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
+ # A reference to one resource: a `name` or a `uuid`, never both.
10
+ #
11
+ # The API validates every identifier it is given, so a name sent in the
12
+ # `uuid` filter fails with `Invalid uuid '<name>'`: the ref states which
13
+ # kind it holds, and only that field is sent. A name is unique only
14
+ # within a metro, so it can be qualified with `metro`, which also saves
15
+ # a lookup. A uuid identifies one resource wherever it lives.
16
+ #
17
+ # @example
18
+ # Ref.new(name: "web")
19
+ # Ref.new(name: "web", metro: :fra)
20
+ # Ref.coerce({ uuid: "550e8400-e29b-41d4-a716-446655440000" })
21
+ # case ref
22
+ # in { name: String => name, metro: nil } then ...
23
+ # end
24
+ Ref = Data.define(:name, :uuid, :metro) do
25
+ # @raise [ArgumentError] when given both or neither of name and uuid
26
+ def initialize(name: nil, uuid: nil, metro: nil)
27
+ raise ArgumentError, "a ref takes `name:` or `uuid:`, not both" if name && uuid
28
+ raise ArgumentError, "a ref needs `name:` or `uuid:`" if name.nil? && uuid.nil?
29
+
30
+ super(name: name&.to_s, uuid: uuid&.to_s, metro: metro&.to_s)
31
+ end
32
+
33
+ # @param value [Ref, Hash] a ref, or a Hash with `:name` or `:uuid`
34
+ # and optionally `:metro` (Symbol or String keys)
35
+ # @return [Ref]
36
+ def self.coerce(value)
37
+ return value if value.is_a?(Ref)
38
+ unless value.is_a?(Hash)
39
+ raise ArgumentError,
40
+ "a ref is a Hash with `name:` or `uuid:`, got #{value.inspect}"
41
+ end
42
+
43
+ fields = value.transform_keys(&:to_sym)
44
+ unknown = fields.keys - members
45
+ raise ArgumentError, "unknown ref key(s): #{unknown.join(", ")}" unless unknown.empty?
46
+
47
+ new(**fields)
48
+ end
49
+
50
+ # One ref or an Array of them, as an Array.
51
+ #
52
+ # @param value [Ref, Hash, Array<Ref, Hash>]
53
+ # @return [Array<Ref>]
54
+ def self.list(value)
55
+ refs = value.is_a?(Array) ? value.map { |v| coerce(v) } : [coerce(value)]
56
+ raise ArgumentError, "no refs given" if refs.empty?
57
+
58
+ refs
59
+ end
60
+
61
+ # The ref as a request body carries it: the identifier alone. `metro`
62
+ # says where to send the request and must not reach the wire.
63
+ #
64
+ # @return [Hash]
65
+ def wire
66
+ uuid ? { uuid: } : { name: }
67
+ end
68
+
69
+ # The ref as a query filter.
70
+ #
71
+ # @return [Hash]
72
+ def query
73
+ uuid ? { uuid: [uuid] } : { name: [name] }
74
+ end
75
+
76
+ # @return [String] e.g. `name "web" in fra`
77
+ def to_s
78
+ id = uuid ? %(uuid "#{uuid}") : %(name "#{name}")
79
+ metro ? "#{id} in #{metro}" : id
80
+ end
81
+ end
82
+
83
+ # Where a handle's resource lives: the ref, and the metro and base URL
84
+ # serving it.
85
+ #
86
+ # @!attribute [r] ref
87
+ # @return [Ref] qualified with the metro
88
+ # @!attribute [r] metro
89
+ # @return [String]
90
+ # @!attribute [r] base_url
91
+ # @return [String]
92
+ Location = Data.define(:ref, :metro, :base_url)
93
+ end
94
+ end
95
+ end