octaspace 0.2.0 → 0.3.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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +93 -30
  3. data/LICENSE +21 -0
  4. data/README.md +104 -289
  5. data/lib/octaspace/client.rb +40 -23
  6. data/lib/octaspace/configuration.rb +73 -14
  7. data/lib/octaspace/errors.rb +70 -39
  8. data/lib/octaspace/json_null.rb +56 -0
  9. data/lib/octaspace/middleware/url_rotator.rb +3 -3
  10. data/lib/octaspace/model.rb +115 -0
  11. data/lib/octaspace/money.rb +69 -0
  12. data/lib/octaspace/railtie.rb +1 -2
  13. data/lib/octaspace/resources/accounts.rb +8 -20
  14. data/lib/octaspace/resources/base.rb +49 -2
  15. data/lib/octaspace/resources/compute.rb +123 -0
  16. data/lib/octaspace/resources/hosting.rb +107 -0
  17. data/lib/octaspace/resources/network.rb +24 -5
  18. data/lib/octaspace/resources/render.rb +46 -0
  19. data/lib/octaspace/resources/sessions.rb +125 -10
  20. data/lib/octaspace/resources/stats.rb +57 -0
  21. data/lib/octaspace/resources/vpn.rb +48 -0
  22. data/lib/octaspace/response.rb +62 -12
  23. data/lib/octaspace/transport/base.rb +30 -30
  24. data/lib/octaspace/transport/faraday_transport.rb +157 -91
  25. data/lib/octaspace/transport/persistent_transport.rb +23 -49
  26. data/lib/octaspace/types/account.rb +17 -4
  27. data/lib/octaspace/types/hosting.rb +116 -0
  28. data/lib/octaspace/types/machines.rb +158 -0
  29. data/lib/octaspace/types/network.rb +87 -0
  30. data/lib/octaspace/types/session.rb +173 -4
  31. data/lib/octaspace/types/stats.rb +143 -0
  32. data/lib/octaspace/version.rb +1 -1
  33. data/lib/octaspace.rb +21 -19
  34. metadata +30 -35
  35. data/MIT-LICENSE +0 -22
  36. data/lib/octaspace/payload_helpers.rb +0 -53
  37. data/lib/octaspace/resources/apps.rb +0 -19
  38. data/lib/octaspace/resources/idle_jobs.rb +0 -32
  39. data/lib/octaspace/resources/nodes.rb +0 -63
  40. data/lib/octaspace/resources/services/machine_rental.rb +0 -78
  41. data/lib/octaspace/resources/services/render.rb +0 -30
  42. data/lib/octaspace/resources/services/session_proxy.rb +0 -54
  43. data/lib/octaspace/resources/services/vpn.rb +0 -30
  44. data/lib/octaspace/resources/services.rb +0 -39
  45. data/lib/octaspace/types/balance.rb +0 -11
  46. data/lib/octaspace/types/node.rb +0 -12
@@ -8,80 +8,54 @@ module OctaSpace
8
8
  # gem "faraday-net_http_persistent"
9
9
  # gem "connection_pool"
10
10
  #
11
- # Inherits all retry/failover/hook logic from FaradayTransport.
12
- # Overrides adapter configuration to use net_http_persistent with pooling.
11
+ # Inherits all retry/failover/hook logic from FaradayTransport; each base
12
+ # URL gets its own pool of persistent connections.
13
13
  #
14
14
  # @example
15
15
  # client = OctaSpace::Client.new(
16
- # api_key: "key",
16
+ # api_key: "octa_ak_...",
17
17
  # keep_alive: true,
18
18
  # pool_size: ENV.fetch("RAILS_MAX_THREADS", 5).to_i
19
19
  # )
20
20
  class PersistentTransport < FaradayTransport
21
- def initialize(config)
22
- @pools = {}
23
- @pools_mutex = Mutex.new
24
- super
25
- end
26
-
27
21
  # @return [Hash] connection pool diagnostics per URL
28
22
  def pool_stats
29
- @pools_mutex.synchronize do
30
- @pools.transform_values do |pool|
23
+ @mutex.synchronize do
24
+ @connections.transform_values do |pool|
31
25
  {size: pool.size, available: pool.available}
32
26
  end
33
27
  end
34
28
  end
35
29
 
30
+ # @return [Hash] transport diagnostics
31
+ def transport_stats
32
+ {mode: :persistent, urls: @urls, pools: pool_stats}
33
+ end
34
+
36
35
  # Shut down all connection pools gracefully
37
36
  def shutdown
38
- @pools_mutex.synchronize do
39
- @pools.each_value { |pool| pool.shutdown { |_conn| nil } }
40
- @pools.clear
37
+ @mutex.synchronize do
38
+ @connections.each_value { |pool| pool.shutdown { |conn| conn.close if conn.respond_to?(:close) } }
39
+ @connections.clear
41
40
  end
42
41
  end
43
42
 
44
43
  private
45
44
 
46
- # Override: return a ConnectionPool instead of a bare Faraday connection
45
+ # Override: a ConnectionPool of persistent Faraday connections
47
46
  def build_connection(base_url)
48
- pool_for(base_url)
49
- end
50
-
51
- # Execute request via pool — `pool.with` yields a Faraday connection
52
- def execute(conn_or_pool, method, path, params: {}, body: nil, headers: {})
53
- if conn_or_pool.is_a?(::ConnectionPool)
54
- conn_or_pool.with { |conn| super(conn, method, path, params: params, body: body, headers: headers) }
55
- else
56
- super
57
- end
58
- end
59
-
60
- def pool_for(base_url)
61
- @pools_mutex.synchronize do
62
- @pools[base_url] ||= build_pool(base_url)
63
- end
64
- end
65
-
66
- def build_pool(base_url)
67
- pool_size = config.pool_size
68
- pool_timeout = config.pool_timeout
69
-
70
- ::ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
71
- build_persistent_connection(base_url)
47
+ ::ConnectionPool.new(size: config.pool_size, timeout: config.pool_timeout) do
48
+ Faraday.new(url: base_url, request: request_options, ssl: ssl_options) do |f|
49
+ f.adapter :net_http_persistent, pool_size: config.pool_size do |http|
50
+ http.idle_timeout = config.idle_timeout
51
+ end
52
+ end
72
53
  end
73
54
  end
74
55
 
75
- def build_persistent_connection(base_url)
76
- Faraday.new(url: base_url, request: request_options, ssl: ssl_options) do |f|
77
- f.request :json
78
- f.request :retry, retry_options
79
- f.response :json, content_type: /\bjson/
80
- f.response :logger, config.logger, {headers: true, bodies: false} if config.logger
81
- f.adapter :net_http_persistent, pool_size: config.pool_size do |http|
82
- http.idle_timeout = config.idle_timeout
83
- end
84
- end
56
+ # Run the request on a connection checked out of the pool
57
+ def execute(pool, method, path, query:, payload:, headers:)
58
+ pool.with { |conn| super(conn, method, path, query: query, payload: payload, headers: headers) }
85
59
  end
86
60
  end
87
61
  end
@@ -2,10 +2,23 @@
2
2
 
3
3
  module OctaSpace
4
4
  module Types
5
- # Value object representing an OctaSpace account
6
- Account = Data.define(:account_uuid, :email, :avatar, :balance) do
7
- # @return [String]
8
- def to_s = "OctaSpace Account <#{email}>"
5
+ # Account balance (+GET /accounts/balance+). It can be negative.
6
+ class Balance < Model
7
+ # @!attribute [r] balance
8
+ # @return [Integer] amount in units of 10^-decimals of {#currency}
9
+ # @!attribute [r] currency
10
+ # @return [String] "USD"
11
+ # @!attribute [r] decimals
12
+ # @return [Integer] decimal places of {#balance} (6: micro-USD)
13
+ attribute :balance, :currency, :decimals
14
+
15
+ # @return [Rational] the balance in currency units, exactly
16
+ def to_usd = Money.to_usd(balance, decimals: decimals)
17
+
18
+ # @return [String] e.g. "$12.500000"
19
+ def formatted = Money.format(balance, decimals: decimals)
20
+
21
+ def negative? = balance.negative?
9
22
  end
10
23
  end
11
24
  end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module OctaSpace
6
+ module Types
7
+ # Prices of a hosted node, integer micro-USD
8
+ class NodePrices < Model
9
+ # @!attribute [r] base
10
+ # @return [Integer] per hour
11
+ # @!attribute [r] storage
12
+ # @return [Integer] per GB of disk per hour
13
+ # @!attribute [r] traffic
14
+ # @return [Integer] per GB of traffic
15
+ # @!attribute [r] min_rent_minutes
16
+ # @return [Integer] minimum rent, 0..60 (0 = none)
17
+ attribute :base, :storage, :traffic, :min_rent_minutes
18
+ end
19
+
20
+ # Whether a node is offered for a service or a VPN protocol, and why not
21
+ class ServiceOffer < Model
22
+ # @!attribute [r] reason
23
+ # @return [String, nil] e.g. "ports_closed" or "vpn_probe_failed"
24
+ # @!attribute [r] details
25
+ # @return [String, nil] what the check found
26
+ attribute :offered, :reason, :details
27
+
28
+ def offered? = offered == true
29
+
30
+ # @return [Hash{String=>ServiceOffer}] VPN only: the offer of each protocol
31
+ def protocols
32
+ @protocols ||= self["protocols"].to_h.transform_values { |offer| ServiceOffer.wrap(offer) }
33
+ end
34
+ end
35
+
36
+ # A verification or benchmark that is running on a node
37
+ class NodeActivity < Model
38
+ # @!attribute [r] kind
39
+ # @return [String] "verification" or "benchmark"
40
+ # @!attribute [r] started_at
41
+ # @return [Integer, nil] Unix seconds
42
+ attribute :kind, :step, :started_at
43
+
44
+ # @return [Time, nil]
45
+ def started_time = started_at.is_a?(Numeric) ? Time.at(started_at) : nil
46
+ end
47
+
48
+ # Size and use of memory or disk, bytes
49
+ class ResourceUsage < Model
50
+ attribute :size, :free, :used, :used_percent
51
+ end
52
+
53
+ # A GPU as the node reports it; fields beyond these via #[]
54
+ class HostingGpu < Gpu
55
+ attribute :mem_free_mb, :fan_speed, :gpu_temperature, :gpu_utilization, :power_draw_watt, :pstate
56
+ end
57
+
58
+ # Hardware and OS of a hosted node
59
+ class HostingSystem < Model
60
+ attribute :cpu_cores, :cpu_model_name, :cpu_load_percent, :os_version
61
+ attribute :memory, :disk, model: ResourceUsage
62
+ attribute :gpus, model: HostingGpu, many: true
63
+ end
64
+
65
+ # Location of a node (GeoIP)
66
+ class Location < Model
67
+ attribute :city, :country, :latitude, :longitude
68
+ end
69
+
70
+ # An online node of the account (+GET /hosting/nodes+, +GET /hosting/nodes/{number}+)
71
+ class HostingNode < Model
72
+ # @!attribute [r] number
73
+ # @return [String] node number, e.g. "7KQ4-M2XD-4" (the one renters see)
74
+ # @!attribute [r] status
75
+ # @return [String] "idle", "rented", "verification", "benchmark" or "unknown"
76
+ # @!attribute [r] maintenance
77
+ # @return [Boolean] in maintenance: not offered for new rentals
78
+ # @!attribute [r] verified
79
+ # @return [Boolean, nil] passed verification; nil while unknown
80
+ # @!attribute [r] osn
81
+ # @return [String] node software version
82
+ # @!attribute [r] online_at
83
+ # @return [String] ISO 8601; see {#online_time}
84
+ # @!attribute [r] uptime
85
+ # @return [Integer] seconds since the machine booted
86
+ # @!attribute [r] uptime_percent
87
+ # @return [Float] online over the last 30 days (97 and above show as 100)
88
+ attribute :number, :status, :maintenance, :verified, :ip, :osn, :online_at, :uptime, :uptime_percent
89
+ attribute :activity, model: NodeActivity
90
+ attribute :prices, model: NodePrices
91
+ attribute :system, model: HostingSystem
92
+ attribute :location, model: Location
93
+
94
+ def maintenance? = maintenance == true
95
+ def verified? = verified == true
96
+ def idle? = status == "idle"
97
+ def rented? = status == "rented"
98
+
99
+ # @return [Hash{String=>ServiceOffer}] by service ("compute", "render", "vpn", …)
100
+ def services
101
+ @services ||= self["services"].to_h.transform_values { |offer| ServiceOffer.wrap(offer) }
102
+ end
103
+
104
+ # @param service [String, Symbol]
105
+ # @return [Boolean] whether the node is offered for this service
106
+ def offered?(service) = services[service.to_s]&.offered? || false
107
+
108
+ # @return [Time, nil]
109
+ def online_time
110
+ online_at.is_a?(String) ? Time.iso8601(online_at) : nil
111
+ rescue ArgumentError
112
+ nil
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OctaSpace
4
+ module Types
5
+ # A GPU of a machine listing
6
+ class Gpu < Model
7
+ # @!attribute [r] model
8
+ # @return [String] GPU model name
9
+ # @!attribute [r] mem_total_mb
10
+ # @return [Integer] total memory, MB
11
+ # @!attribute [r] core_clock
12
+ # @return [Integer, nil] core clock ceiling, MHz
13
+ # @!attribute [r] mem_clock
14
+ # @return [Integer, nil] memory clock ceiling, MHz
15
+ # @!attribute [r] power_limit
16
+ # @return [Integer, nil] power limit, W; nil if the card reports none
17
+ # @!attribute [r] tuned
18
+ # @return [Boolean] the node owner changed the card's power limit or clocks
19
+ attribute :model, :mem_total_mb, :pcie_link_gen, :pcie_link_width,
20
+ :core_clock, :mem_clock, :power_limit, :tuned
21
+
22
+ def tuned? = tuned == true
23
+ end
24
+
25
+ # Node reliability. {#uptime} is the percent of time online over the last
26
+ # 30 days; other keys via {#[]}.
27
+ class Reliability < Model
28
+ attribute :uptime, :stability, :s_normal, :s_failed, :s_avg_d, :s_max_d
29
+ end
30
+
31
+ # Fields shared by compute and render listings
32
+ class Machine < Model
33
+ # @!attribute [r] node_number
34
+ # @return [String] node number, e.g. "7KQ4-M2XD-4"
35
+ # @!attribute [r] base_price
36
+ # @return [Integer] price per hour, micro-USD
37
+ # @!attribute [r] min_rent_minutes
38
+ # @return [Integer] minimum rent, minutes (0 = none); a session stopped
39
+ # sooner is charged for that long
40
+ # @!attribute [r] memory
41
+ # @return [Integer] free memory, bytes
42
+ # @!attribute [r] disk
43
+ # @return [Integer] free disk, bytes
44
+ # @!attribute [r] net_down_mbps
45
+ # @return [Integer] download speed, Mbit/s; 0 if not measured
46
+ # @!attribute [r] net_up_mbps
47
+ # @return [Integer] upload speed, Mbit/s; 0 if not measured
48
+ attribute :node_number, :base_price, :min_rent_minutes,
49
+ :blender_score, :city, :country, :cpu_cores, :cpu_model_name, :cuda_version,
50
+ :disk, :memory, :gpu_vendor, :net_down_mbps, :net_up_mbps
51
+ attribute :gpus, model: Gpu, many: true
52
+ attribute :reliability, model: Reliability
53
+ end
54
+
55
+ # A machine available for rent (+GET /services/compute+)
56
+ class ComputeMachine < Machine
57
+ # @!attribute [r] storage_price
58
+ # @return [Integer] price per GB of disk per hour, micro-USD
59
+ # @!attribute [r] traffic_price
60
+ # @return [Integer] price per GB of traffic, micro-USD
61
+ # @!attribute [r] disk_type
62
+ # @return [String, nil] "nvme", "ssd", "hdd" or nil if unknown
63
+ # @!attribute [r] country_iso
64
+ # @return [String, nil] ISO 3166-1 alpha-2 country code, e.g. "DE"
65
+ # @!attribute [r] os_version
66
+ # @return [String, nil] operating system of the node, e.g. "Ubuntu 24.04"
67
+ # @!attribute [r] virt
68
+ # @return [String, nil] virtualization the node runs in; "none" on bare metal
69
+ attribute :storage_price, :traffic_price, :arch, :ai_score,
70
+ :disk_type, :disk_speed_mbs, :memory_speed, :memory_type, :free_ports,
71
+ :country_iso, :os_version, :virt, :is_wsl, :is_hive_os, :gpu_pt
72
+
73
+ # @return [Boolean] the node runs in WSL
74
+ def wsl? = is_wsl == true
75
+
76
+ # @return [Boolean] the node runs HiveOS
77
+ def hive_os? = is_hive_os == true
78
+
79
+ # @return [Boolean] GPU passthrough is available
80
+ def gpu_passthrough? = gpu_pt == true
81
+ end
82
+
83
+ # A node available for render (+GET /services/render+); billed by the hour only
84
+ class RenderMachine < Machine
85
+ end
86
+
87
+ # A node offered for VPN (+GET /services/vpn+); billed by traffic only
88
+ class VpnMachine < Model
89
+ # @!attribute [r] node_number
90
+ # @return [String] node number, e.g. "7KQ4-M2XD-4"
91
+ # @!attribute [r] traffic_price
92
+ # @return [Integer] price per GB of traffic, micro-USD
93
+ # @!attribute [r] protocols
94
+ # @return [Array<String>] protocols that passed the connection check on
95
+ # this node (at least one): "wg", "awg", "reality", "http_proxy"
96
+ # @!attribute [r] residential
97
+ # @return [Boolean] the node's address is believed to belong to a residence
98
+ # @!attribute [r] net_down_mbps
99
+ # @return [Integer] download speed, Mbit/s; 0 if not measured
100
+ attribute :node_number, :traffic_price, :protocols, :city, :country, :country_iso,
101
+ :latitude, :longitude, :residential, :net_down_mbps, :net_up_mbps
102
+
103
+ def residential? = residential == true
104
+
105
+ # @param protocol [String, Symbol] e.g. :wg
106
+ # @return [Boolean] whether a session with this +subkind+ can be started here
107
+ def supports?(protocol) = Array(protocols).include?(protocol.to_s)
108
+ end
109
+
110
+ # An app to start a compute session from (+GET /compute/apps+)
111
+ class ComputeApp < Model
112
+ # Logo and disk recommendation of an app
113
+ class Extra < Model
114
+ # @!attribute [r] min_disk_size
115
+ # @return [Integer, nil] disk the app needs, GB; a recommendation only
116
+ attribute :logo, :min_disk_size
117
+ end
118
+
119
+ # @!attribute [r] uuid
120
+ # @return [String] pass as +app:+ to +compute.start+
121
+ # @!attribute [r] image
122
+ # @return [String] container image, e.g. "ubuntu:24.04"; pass as +image:+
123
+ # @!attribute [r] personal
124
+ # @return [Boolean] one of your own apps, not an official one
125
+ # @!attribute [r] startup_script
126
+ # @return [String, nil] your own apps only
127
+ # @!attribute [r] envs
128
+ # @return [Hash{String=>String}, nil] your own apps only
129
+ attribute :uuid, :name, :image, :personal, :category, :description,
130
+ :entrypoint, :ports, :http_ports, :startup_script, :envs
131
+ attribute :extra, model: Extra
132
+
133
+ def personal? = personal == true
134
+
135
+ # @return [Integer, nil] see {Extra#min_disk_size}
136
+ def min_disk_size = extra&.min_disk_size
137
+
138
+ # @return [String, nil] logo URL
139
+ def logo = extra&.logo
140
+ end
141
+
142
+ # Outcome of one item of a batch start
143
+ class BatchResult < Model
144
+ # @!attribute [r] id
145
+ # @return [Integer, String] the client-side id of the request item
146
+ # @!attribute [r] status
147
+ # @return [Integer] 0 = started, 1 = failed
148
+ # @!attribute [r] uuid
149
+ # @return [String, nil] session uuid when started
150
+ # @!attribute [r] reason
151
+ # @return [String, nil] why the item failed
152
+ attribute :id, :status, :uuid, :reason
153
+
154
+ def started? = status == 0
155
+ def failed? = !started?
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OctaSpace
4
+ module Types
5
+ # Platform statistics snapshot (+GET /network+), refreshed every few minutes
6
+ class Network < Model
7
+ # Node counts
8
+ class Nodes < Model
9
+ attribute :online, :available, :rented, :countries
10
+ end
11
+
12
+ # Total hardware of online nodes; +memory+ and +disk+ in bytes
13
+ class Power < Model
14
+ # @!attribute [r] gpus_rented
15
+ # @return [Integer, nil] GPUs in running sessions
16
+ attribute :cpu_cores, :disk, :gpus, :gpus_rented, :memory, :tflops
17
+ end
18
+
19
+ # Rendered frames of one week
20
+ class WeekFrames < Model
21
+ attribute :week, :frames
22
+ end
23
+
24
+ # Render farm counters
25
+ class Render < Model
26
+ attribute :frames_24h, :frames_total
27
+ attribute :frames_by_week, model: WeekFrames, many: true
28
+ end
29
+
30
+ # Session counters
31
+ class Sessions < Model
32
+ attribute :running, :total, :last_24h
33
+ end
34
+
35
+ # User counters
36
+ class Users < Model
37
+ attribute :total, :last_24h
38
+ end
39
+
40
+ # VPN node counters
41
+ class Vpn < Model
42
+ attribute :nodes, :countries
43
+ end
44
+
45
+ # @!attribute [r] at
46
+ # @return [Integer] snapshot time, Unix seconds; see {#time}
47
+ attribute :at
48
+ attribute :nodes, model: Nodes
49
+ attribute :power, model: Power
50
+ attribute :render, model: Render
51
+ attribute :sessions, model: Sessions
52
+ attribute :users, model: Users
53
+ attribute :vpn, model: Vpn
54
+
55
+ # @return [Time, nil]
56
+ def time = at.is_a?(Numeric) ? Time.at(at) : nil
57
+ end
58
+
59
+ # Fields of {NetworkHistory} and {NetworkHistoryPoint}, besides the timestamp
60
+ NETWORK_HISTORY_FIELDS = %i[nodes_online nodes_available nodes_rented countries gpus gpus_rented
61
+ tflops vpn_nodes users sessions_running sessions_24h].freeze
62
+
63
+ # One point of the network history, see {NetworkHistory#points}. Values
64
+ # are averages over the step (so they can be fractional), nil without data.
65
+ NetworkHistoryPoint = Data.define(:ts, *NETWORK_HISTORY_FIELDS) do
66
+ # @return [Time]
67
+ def time = Time.at(ts)
68
+ end
69
+
70
+ # Platform statistics over time (+GET /network/history+): parallel arrays,
71
+ # one value per entry of {#timestamps}
72
+ class NetworkHistory < Model
73
+ # @!attribute [r] timestamps
74
+ # @return [Array<Integer>] start of each step, Unix seconds; steps
75
+ # without data are left out
76
+ attribute :timestamps, *NETWORK_HISTORY_FIELDS
77
+
78
+ # Turns the parallel arrays into one point per timestamp, e.g. for a chart.
79
+ # @return [Array<NetworkHistoryPoint>]
80
+ def points
81
+ Array(timestamps).each_with_index.map do |ts, i|
82
+ NetworkHistoryPoint.new(ts: ts, **NETWORK_HISTORY_FIELDS.to_h { |field| [field, Array(self[field])[i]] })
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -2,10 +2,179 @@
2
2
 
3
3
  module OctaSpace
4
4
  module Types
5
- # Value object representing a service session
6
- Session = Data.define(:uuid, :service, :app_name, :node_id, :urls, :prices, :node_hw) do
7
- # @return [String]
8
- def to_s = "Session <#{uuid}> #{service}"
5
+ # Converts Unix milliseconds to Time
6
+ # @api private
7
+ def self.time_ms(value) = value.is_a?(Numeric) ? Time.at(value / 1000.0) : nil
8
+
9
+ # An SSH endpoint of a session
10
+ #
11
+ # +user+ comes from the image's USER, so the image author controls it.
12
+ # Before running +ssh+ with these values, check them and pass them as
13
+ # separate arguments after "--" (+ssh -p PORT -l USER -- HOST+), so a
14
+ # value like "-oProxyCommand=…" cannot run a local command.
15
+ class SshEndpoint < Model
16
+ attribute :host, :port, :user
17
+ end
18
+
19
+ # SSH access of a compute session
20
+ class SessionSsh < Model
21
+ # @!attribute [r] direct
22
+ # @return [SshEndpoint, nil] direct access; only when the account has an SSH key
23
+ # @!attribute [r] proxy
24
+ # @return [SshEndpoint, nil] access through the platform's SSH proxy
25
+ # @!attribute [r] web
26
+ # @return [String, nil] web terminal URL. It contains credentials:
27
+ # do not log it.
28
+ attribute :direct, :proxy, model: SshEndpoint
29
+ attribute :web
30
+ redact :web
31
+
32
+ # @return [SshEndpoint, nil] direct access when available, otherwise the proxy
33
+ def endpoint = direct || proxy
34
+ end
35
+
36
+ # Hardware of the node a session runs on, as recorded at its start
37
+ class NodeHw < Model
38
+ # @!attribute [r] total_memory
39
+ # @return [Integer] bytes
40
+ # @!attribute [r] disk
41
+ # @return [Integer] rented disk, GB
42
+ # @!attribute [r] net_down_mbps
43
+ # @return [Integer, nil] Mbit/s; nil if unknown
44
+ attribute :cpu, :cpu_cores, :total_memory, :memory_speed, :memory_type,
45
+ :disk, :disk_type, :disk_speed_mbs, :gpu_vendor, :net_down_mbps, :net_up_mbps
46
+ attribute :gpus, model: Gpu, many: true
47
+ end
48
+
49
+ # A running session (+GET /sessions+, +GET /services/{uuid}/info+).
50
+ #
51
+ # {.build} returns a {ComputeSession}, {RenderSession} or {VpnSession} by
52
+ # the +service+ field, and a plain Session for any other service.
53
+ # Many fields are nil while the session is starting.
54
+ class Session < Model
55
+ # @!attribute [r] service
56
+ # @return [String] "compute", "render" or "vpn"
57
+ # @!attribute [r] status
58
+ # @return [String] "starting" or "ready"
59
+ # @!attribute [r] progress
60
+ # @return [String, nil] current configuration step
61
+ # @!attribute [r] pull_percent
62
+ # @return [Integer, nil] image pull progress, 0..100 (an estimate)
63
+ # @!attribute [r] duration
64
+ # @return [Integer] seconds since the start
65
+ # @!attribute [r] started_at
66
+ # @return [Integer, nil] Unix milliseconds; see {#started_time}
67
+ # @!attribute [r] base_price
68
+ # @return [Integer, nil] price per hour fixed at the start, micro-USD
69
+ # @!attribute [r] charge_amount
70
+ # @return [Integer, nil] charged so far, micro-USD
71
+ attribute :service, :uuid, :status, :progress, :pull_percent, :duration, :started_at,
72
+ :node_number, :base_price, :charge_amount, :app_name, :app_logo
73
+ attribute :node_hw, model: NodeHw
74
+
75
+ # @param data [Hash] a session JSON object
76
+ # @return [Session] the subclass matching +service+
77
+ def self.build(data)
78
+ return data unless data.is_a?(Hash)
79
+
80
+ klass = case data["service"]
81
+ when "compute" then ComputeSession
82
+ when "render" then RenderSession
83
+ when "vpn" then VpnSession
84
+ else Session
85
+ end
86
+ klass.new(data)
87
+ end
88
+
89
+ def ready? = status == "ready"
90
+ def starting? = status == "starting"
91
+ def compute? = service == "compute"
92
+ def render? = service == "render"
93
+ def vpn? = service == "vpn"
94
+
95
+ # @return [Time, nil] {#started_at} as a Time
96
+ def started_time = Types.time_ms(started_at)
97
+ end
98
+
99
+ # A running compute session
100
+ class ComputeSession < Session
101
+ # @!attribute [r] ports
102
+ # @return [Hash{String=>Integer}] container port => public port
103
+ # @!attribute [r] urls
104
+ # @return [Hash{String=>String}] HTTP port => URL
105
+ # @!attribute [r] fm
106
+ # @return [String, nil] web file manager URL; it needs no login, so treat it as a secret
107
+ # @!attribute [r] envs
108
+ # @return [Hash{String=>String}] environment variables of the container
109
+ # @!attribute [r] rx
110
+ # @return [Integer] bytes received
111
+ # @!attribute [r] tx
112
+ # @return [Integer] bytes sent
113
+ attribute :public_ip, :city, :country, :country_iso, :ports, :urls, :fm, :envs,
114
+ :rx, :tx, :storage_price, :traffic_price, :min_rent_minutes
115
+ attribute :ssh, model: SessionSsh
116
+ redact :fm
117
+ end
118
+
119
+ # A running render session; billed by the hour until stopped
120
+ class RenderSession < Session
121
+ end
122
+
123
+ # Address and login of an +http_proxy+ VPN session (HTTP Basic). The
124
+ # login is valid for this session only.
125
+ class HttpProxyConfig < Model
126
+ # @!attribute [r] password
127
+ # @return [String, nil] a credential: do not log it
128
+ attribute :ip, :port, :username, :password
129
+ redact :password
130
+ end
131
+
132
+ # A running VPN session, billed by traffic until stopped. VPN sessions
133
+ # have no logs.
134
+ class VpnSession < Session
135
+ # @!attribute [r] subkind
136
+ # @return [String] protocol: "wg", "awg", "reality" or "http_proxy"
137
+ # @!attribute [r] config
138
+ # @return [String, HttpProxyConfig, nil] what the client connects with,
139
+ # nil until ready: a config file with the private key ("wg", "awg"), a
140
+ # link ("reality") or the proxy address and login ("http_proxy"). It
141
+ # is a credential: do not log it.
142
+ # @!attribute [r] public_ip
143
+ # @return [String, nil] exit IP address
144
+ # @!attribute [r] rx
145
+ # @return [Integer] bytes received
146
+ # @!attribute [r] tx
147
+ # @return [Integer] bytes sent
148
+ attribute :subkind, :public_ip, :city, :country, :country_iso, :rx, :tx
149
+ attribute :config, model: HttpProxyConfig
150
+ redact :config
151
+ end
152
+
153
+ # A platform event of a session's system log
154
+ class LogEntry < Model
155
+ # @!attribute [r] msg
156
+ # @return [String] may span several lines
157
+ # @!attribute [r] ts
158
+ # @return [Integer, nil] Unix milliseconds; see {#time}
159
+ attribute :msg, :ts
160
+
161
+ # @return [Time, nil]
162
+ def time = Types.time_ms(ts)
163
+ end
164
+
165
+ # Logs of a running compute session; render and VPN sessions have none (+GET /services/{uuid}/logs+)
166
+ class SessionLogs < Model
167
+ # @!attribute [r] system
168
+ # @return [Array<LogEntry>] platform events, oldest first
169
+ # @!attribute [r] container
170
+ # @return [String, nil] the latest container output (up to 1 MiB, may
171
+ # contain "\r\n"); nil while the node does not report it
172
+ # @!attribute [r] container_truncated
173
+ # @return [Boolean] the output was cut to its last 1 MiB
174
+ attribute :system, model: LogEntry, many: true
175
+ attribute :container, :container_truncated
176
+
177
+ def container_truncated? = container_truncated == true
9
178
  end
10
179
  end
11
180
  end