heaven-sdk 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 +202 -0
- data/NOTICE +2 -0
- data/README.md +280 -0
- data/examples/quickstart.rb +56 -0
- data/heaven-sdk.gemspec +20 -0
- data/lib/heaven/client.rb +574 -0
- data/lib/heaven/errors.rb +159 -0
- data/lib/heaven/files.rb +300 -0
- data/lib/heaven/models.rb +131 -0
- data/lib/heaven/stream.rb +180 -0
- data/lib/heaven/version.rb +5 -0
- data/lib/heaven.rb +15 -0
- metadata +58 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Heaven
|
|
4
|
+
# A streamed command began, but its exit status never arrived: whether
|
|
5
|
+
# it finished is unknown, and it is never run again for you. Check the
|
|
6
|
+
# Computer before running it again. #from_heaven? is true when the lobby
|
|
7
|
+
# ended the stream with its own error line (the message is its own), and
|
|
8
|
+
# false when the stream was cut, unreadable or past its timeout.
|
|
9
|
+
class ExecStreamError < UnknownOutcomeError
|
|
10
|
+
def initialize(message, from_heaven:, **kw)
|
|
11
|
+
@from_heaven = from_heaven
|
|
12
|
+
super(message, status: 200, **kw)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def from_heaven? = @from_heaven
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# A command's output as it is written: /v1/computer/exec with
|
|
19
|
+
# Accept: application/x-ndjson (docs/API.md, "Running commands in a
|
|
20
|
+
# Computer") answers one JSON line per piece of output,
|
|
21
|
+
# {"stdout":BASE64} or {"stderr":BASE64}, then {"exit_code":N}, or
|
|
22
|
+
# {"error":MESSAGE} when no exit status arrived.
|
|
23
|
+
class Client
|
|
24
|
+
# How long a streamed command is waited for, in seconds, from sending
|
|
25
|
+
# it to its exit status: the lobby stops one after an hour, and the
|
|
26
|
+
# minute more lets its last line arrive.
|
|
27
|
+
STREAM_TIMEOUT = 3660
|
|
28
|
+
|
|
29
|
+
# The longest line read; the lobby's are at most about 44 KiB.
|
|
30
|
+
MAX_STREAM_LINE = 16 << 20
|
|
31
|
+
|
|
32
|
+
# Runs one shell command in a Computer and gives its output as it is
|
|
33
|
+
# written, standard output and standard error apart, for up to the
|
|
34
|
+
# lobby's hour. Answers its exit status, an Integer, non-zero included.
|
|
35
|
+
#
|
|
36
|
+
# status = client.exec_stream("blog", "make test") do |stream, data|
|
|
37
|
+
# (stream == :stderr ? $stderr : $stdout).write(data)
|
|
38
|
+
# end
|
|
39
|
+
#
|
|
40
|
+
# The block gets +:stdout+ or +:stderr+ and the bytes as written (a
|
|
41
|
+
# binary String, not necessarily whole lines). Without a block they
|
|
42
|
+
# are written to +out+ and +err+ (any object with +write+; $stdout and
|
|
43
|
+
# $stderr unless given, nil to drop), as +system+ does.
|
|
44
|
+
#
|
|
45
|
+
# A refusal before the command starts raises its Error as #exec does.
|
|
46
|
+
# Once the stream has begun, one that ends without an exit status
|
|
47
|
+
# raises ExecStreamError. It is sent once and never again. +break+ or
|
|
48
|
+
# an exception in the block ends the request, which stops the command.
|
|
49
|
+
# +timeout+ bounds the whole stream, apart from the client's +timeout+.
|
|
50
|
+
def exec_stream(computer, command, stdin: nil, out: $stdout, err: $stderr, timeout: STREAM_TIMEOUT, &block)
|
|
51
|
+
raise ArgumentError, "command is one shell command line, a String." unless command.is_a?(String)
|
|
52
|
+
|
|
53
|
+
block ||= lambda do |stream, data|
|
|
54
|
+
io = stream == :stderr ? err : out
|
|
55
|
+
next unless io
|
|
56
|
+
|
|
57
|
+
io.write(data)
|
|
58
|
+
io.flush if io.respond_to?(:flush)
|
|
59
|
+
end
|
|
60
|
+
target, payload, type = exec_payload(computer, command, stdin)
|
|
61
|
+
headers = default_headers.merge("Accept" => "application/x-ndjson", "Content-Type" => type)
|
|
62
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
63
|
+
begin
|
|
64
|
+
http = connect(timeout)
|
|
65
|
+
rescue NotSent => e
|
|
66
|
+
raise UnavailableError, "Could not connect to #{@base_url}: #{e.message}. Nothing was sent."
|
|
67
|
+
end
|
|
68
|
+
heaven_id = nil
|
|
69
|
+
began = false
|
|
70
|
+
block_failed = nil
|
|
71
|
+
begin
|
|
72
|
+
req = Net::HTTP::Post.new(target, headers)
|
|
73
|
+
req.body = payload
|
|
74
|
+
http.request(req) do |res|
|
|
75
|
+
heaven_id = res["Heaven-Request-Id"]
|
|
76
|
+
unless res.code == "200" && res["Content-Type"].to_s.split(";").first.to_s.strip.casecmp?("application/x-ndjson")
|
|
77
|
+
answer("POST", target, res, res.body || "")
|
|
78
|
+
raise Lost, "HTTP #{res.code} from #{@base_url} without a stream."
|
|
79
|
+
end
|
|
80
|
+
began = true
|
|
81
|
+
pending = String.new(encoding: Encoding::BINARY)
|
|
82
|
+
res.read_body do |chunk|
|
|
83
|
+
pending << chunk.b
|
|
84
|
+
while (i = pending.index("\n"))
|
|
85
|
+
item = stream_line(pending.slice!(0, i + 1), heaven_id)
|
|
86
|
+
return item if item.is_a?(Integer)
|
|
87
|
+
next unless item
|
|
88
|
+
|
|
89
|
+
begin
|
|
90
|
+
block.call(*item)
|
|
91
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
92
|
+
block_failed = e
|
|
93
|
+
raise
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
raise stream_lost("A line of the stream was too long to read.", heaven_id) if pending.bytesize > MAX_STREAM_LINE
|
|
97
|
+
|
|
98
|
+
left = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
99
|
+
raise stream_lost("No exit status within the stream's timeout (#{timeout} s).", heaven_id) if left <= 0
|
|
100
|
+
|
|
101
|
+
http.read_timeout = left
|
|
102
|
+
end
|
|
103
|
+
what = pending.empty? ? "The stream ended without the command's exit status." : "The stream ended in the middle of a line."
|
|
104
|
+
raise stream_lost(what, heaven_id)
|
|
105
|
+
end
|
|
106
|
+
rescue Net::ReadTimeout, Net::WriteTimeout => e
|
|
107
|
+
raise block_failed if block_failed
|
|
108
|
+
raise stream_lost("No exit status within the stream's timeout (#{timeout} s).", heaven_id) if began
|
|
109
|
+
|
|
110
|
+
raise may_have_run("No answer within #{timeout} seconds (#{e.class.name}).", heaven_id)
|
|
111
|
+
rescue *CONNECTION_ERRORS => e
|
|
112
|
+
raise block_failed if block_failed
|
|
113
|
+
raise stream_lost("The stream was cut: #{e.message}.", heaven_id) if began
|
|
114
|
+
|
|
115
|
+
raise may_have_run("The connection failed: #{e.message}.", heaven_id)
|
|
116
|
+
rescue Lost => e
|
|
117
|
+
raise may_have_run(e.message, heaven_id)
|
|
118
|
+
ensure
|
|
119
|
+
# Before the exit status (a break, an error) this ends the request,
|
|
120
|
+
# which stops the command.
|
|
121
|
+
http.finish if http.started?
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
private
|
|
126
|
+
|
|
127
|
+
# exec's request: up to 1 MiB of input in the JSON body, more as the
|
|
128
|
+
# request body with the fields in the query.
|
|
129
|
+
def exec_payload(computer, command, stdin)
|
|
130
|
+
if stdin && stdin.bytesize > EXEC_JSON_STDIN_LIMIT
|
|
131
|
+
query = encode_query(computer: computer.to_s, command: command)
|
|
132
|
+
["/v1/computer/exec?#{query}", stdin.b, "application/octet-stream"]
|
|
133
|
+
else
|
|
134
|
+
body = { computer: computer, command: command }
|
|
135
|
+
body[:stdin] = [stdin.b].pack("m0") if stdin
|
|
136
|
+
["/v1/computer/exec", JSON.generate(body), "application/json"]
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A line of the stream: [stream, bytes], an exit status, or nil to skip.
|
|
141
|
+
# Raises ExecStreamError for the lobby's error line or a line that
|
|
142
|
+
# cannot be read.
|
|
143
|
+
def stream_line(line, heaven_id)
|
|
144
|
+
text = line.force_encoding(Encoding::UTF_8).strip
|
|
145
|
+
return nil if text.empty?
|
|
146
|
+
|
|
147
|
+
item = begin
|
|
148
|
+
JSON.parse(text)
|
|
149
|
+
rescue JSON::ParserError, EncodingError
|
|
150
|
+
nil
|
|
151
|
+
end
|
|
152
|
+
raise stream_lost("A line of the stream could not be read.", heaven_id) unless item.is_a?(Hash)
|
|
153
|
+
|
|
154
|
+
%w[stdout stderr].each do |name|
|
|
155
|
+
next unless item.key?(name)
|
|
156
|
+
|
|
157
|
+
data = item[name]
|
|
158
|
+
unless data.is_a?(String) && data.match?(%r{\A[A-Za-z0-9+/]*={0,2}\z}) && (data.length % 4).zero?
|
|
159
|
+
raise stream_lost("A line of the stream could not be read.", heaven_id)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
return [name.to_sym, data.unpack1("m0")]
|
|
163
|
+
end
|
|
164
|
+
return item["exit_code"] if item["exit_code"].is_a?(Integer)
|
|
165
|
+
raise ExecStreamError.new(item["error"], from_heaven: true, heaven_request_id: heaven_id) if item["error"].is_a?(String)
|
|
166
|
+
|
|
167
|
+
nil # a line this SDK does not know yet
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def stream_lost(reason, heaven_id)
|
|
171
|
+
ExecStreamError.new("#{reason} Whether the command finished is unknown: check the Computer before running it again.",
|
|
172
|
+
from_heaven: false, heaven_request_id: heaven_id)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def may_have_run(reason, heaven_id)
|
|
176
|
+
UnknownOutcomeError.new("#{reason} The command may have run: check inside the Computer before running it again.",
|
|
177
|
+
heaven_request_id: heaven_id)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
data/lib/heaven.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "heaven/version"
|
|
4
|
+
require_relative "heaven/errors"
|
|
5
|
+
require_relative "heaven/models"
|
|
6
|
+
require_relative "heaven/client"
|
|
7
|
+
require_relative "heaven/files"
|
|
8
|
+
require_relative "heaven/stream"
|
|
9
|
+
|
|
10
|
+
# Heaven's Ruby SDK: a client for the /v1 HTTPS API. Start with
|
|
11
|
+
# Heaven::Client.new; see README.md.
|
|
12
|
+
module Heaven
|
|
13
|
+
# A fresh request ID (Client.new_request_id).
|
|
14
|
+
def self.new_request_id = Client.new_request_id
|
|
15
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: heaven-sdk
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Heaven
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Lists, reads, creates and deletes Heaven Computers, waits for operations,
|
|
14
|
+
runs bounded commands in a Computer and sends any command in Heaven's catalog. Standard
|
|
15
|
+
library only.
|
|
16
|
+
email: support@heaven.dev
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- NOTICE
|
|
23
|
+
- README.md
|
|
24
|
+
- examples/quickstart.rb
|
|
25
|
+
- heaven-sdk.gemspec
|
|
26
|
+
- lib/heaven.rb
|
|
27
|
+
- lib/heaven/client.rb
|
|
28
|
+
- lib/heaven/errors.rb
|
|
29
|
+
- lib/heaven/files.rb
|
|
30
|
+
- lib/heaven/models.rb
|
|
31
|
+
- lib/heaven/stream.rb
|
|
32
|
+
- lib/heaven/version.rb
|
|
33
|
+
homepage: https://heaven.dev/docs
|
|
34
|
+
licenses:
|
|
35
|
+
- Apache-2.0
|
|
36
|
+
metadata:
|
|
37
|
+
allowed_push_host: https://rubygems.org
|
|
38
|
+
rubygems_mfa_required: 'true'
|
|
39
|
+
post_install_message:
|
|
40
|
+
rdoc_options: []
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.2'
|
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
requirements: []
|
|
54
|
+
rubygems_version: 3.5.22
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: 'A client for Heaven''s /v1 HTTPS API: Computers, operations and exec.'
|
|
58
|
+
test_files: []
|