ndb 1.2.1 → 1.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.
- checksums.yaml +4 -4
- data/lib/ndb/client.rb +25 -0
- data/lib/ndb/collection.rb +8 -0
- data/lib/ndb/connection.rb +48 -11
- data/lib/ndb/errors.rb +13 -1
- data/lib/ndb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0bb592a41c30083191c79dd91e060d07f66122db47f246cdda14df0f582ef567
|
|
4
|
+
data.tar.gz: 809b5b3a2432de824422626623e469b45feb938ab8d0df6d5f4733479cc8583e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: df0bbaedb369ef365a9d9419a329ecea317cbc379c2ed20861afd54bfc5e39755e0612dde806491c2c69d0814c91e62e0a7e2ff7df175a7d8c49c15bd3311878
|
|
7
|
+
data.tar.gz: 0a30f9063691e5f908c26c5beb63598abe5fe427b58e45caebde94188d093577ea9a92aa0cce00f7ad63837aaf99931eaf742accb30c3904dbd179001635d0dc
|
data/lib/ndb/client.rb
CHANGED
|
@@ -193,6 +193,31 @@ module Ndb
|
|
|
193
193
|
json(request("DELETE", "#{path(coll)}?confirm=#{confirm(coll)}", nil))
|
|
194
194
|
end
|
|
195
195
|
|
|
196
|
+
# Gives the collection the name +to+: documents, indexes and watch
|
|
197
|
+
# positions move with it. With +replace+, an existing +to+ goes aside
|
|
198
|
+
# first; without it, an existing +to+ raises CollectionExists.
|
|
199
|
+
# {"collection", "renamed_from", "replaced"}
|
|
200
|
+
def rename(coll, to, replace: false)
|
|
201
|
+
json(post(coll, "_rename", { to: to, replace: replace }))
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Makes the collection +to+ hold +coll+ as it was at +at+ (a Time or
|
|
205
|
+
# an RFC 3339 string), from the PITR window of the daemon (1 day by
|
|
206
|
+
# default). +coll+ may be dropped while it is in the window. Raises
|
|
207
|
+
# OutOfWindow, PitrOff or CollectionExists.
|
|
208
|
+
# {"collection", "restored", "at", "duration_ms"}
|
|
209
|
+
def restore(coll, at:, to:)
|
|
210
|
+
at = at.utc.iso8601(9) if at.respond_to?(:utc)
|
|
211
|
+
|
|
212
|
+
json(post(coll, "_restore", { at: at, to: to }))
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# The PITR window of the collection: {"collection", "window", "oldest",
|
|
216
|
+
# "newest", "snapshots", "bytes"}.
|
|
217
|
+
def pitr(coll)
|
|
218
|
+
json(request("GET", "#{path(coll)}/_pitr", nil))
|
|
219
|
+
end
|
|
220
|
+
|
|
196
221
|
# PUT /{coll}: creates the collection or merges the definition into it.
|
|
197
222
|
def ensure_collection(coll, definition = {})
|
|
198
223
|
json(request("PUT", path(coll), definition))
|
data/lib/ndb/collection.rb
CHANGED
|
@@ -146,6 +146,14 @@ module Ndb
|
|
|
146
146
|
client.drop(name)
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
+
# Renames the collection on the daemon; this object keeps the old name.
|
|
150
|
+
def rename(to, replace: false) = client.rename(name, to, replace: replace)
|
|
151
|
+
|
|
152
|
+
# A copy of the collection as it was at +at+, in the collection +to+.
|
|
153
|
+
def restore(at:, to:) = client.restore(name, at: at, to: to)
|
|
154
|
+
|
|
155
|
+
def pitr = client.pitr(name)
|
|
156
|
+
|
|
149
157
|
def stats
|
|
150
158
|
ready!
|
|
151
159
|
client.collection_stats(name)
|
data/lib/ndb/connection.rb
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "openssl"
|
|
3
4
|
require "socket"
|
|
4
5
|
require "uri"
|
|
5
6
|
|
|
6
7
|
module Ndb
|
|
7
8
|
Response = Data.define(:status, :headers, :body)
|
|
8
9
|
|
|
9
|
-
# Where the daemon listens: a unix socket path,
|
|
10
|
-
#
|
|
11
|
-
#
|
|
10
|
+
# Where the daemon listens: a unix socket path, a TCP host and port, or an
|
|
11
|
+
# https:// URL (ndb behind a TLS proxy such as Caddy). The host name is
|
|
12
|
+
# resolved each time a connection opens, so a changed DNS record is
|
|
13
|
+
# followed after a reconnect.
|
|
12
14
|
class Endpoint
|
|
15
|
+
INVALID = "must be unix:///path, tcp://host:port or https://host"
|
|
16
|
+
|
|
13
17
|
attr_reader :path, :host, :port
|
|
14
18
|
|
|
15
|
-
# "unix:///path", "tcp://host:port",
|
|
19
|
+
# "unix:///path", "tcp://host:port", "https://host[:port]",
|
|
20
|
+
# "http://host[:port]", or a bare socket path.
|
|
16
21
|
def self.parse(address)
|
|
17
22
|
address = address.to_s
|
|
18
23
|
|
|
@@ -23,13 +28,14 @@ module Ndb
|
|
|
23
28
|
case uri.scheme
|
|
24
29
|
when "unix" then new(path: uri.path)
|
|
25
30
|
when "tcp" then new(host: uri.host, port: uri.port)
|
|
26
|
-
|
|
31
|
+
when "https", "http" then new(host: uri.host, port: uri.port, tls: uri.scheme == "https")
|
|
32
|
+
else raise ArgumentError, "ndb address #{address.inspect} #{INVALID}"
|
|
27
33
|
end
|
|
28
34
|
rescue URI::InvalidURIError
|
|
29
|
-
raise ArgumentError, "ndb address #{address.inspect}
|
|
35
|
+
raise ArgumentError, "ndb address #{address.inspect} #{INVALID}"
|
|
30
36
|
end
|
|
31
37
|
|
|
32
|
-
def initialize(path: nil, host: nil, port: nil)
|
|
38
|
+
def initialize(path: nil, host: nil, port: nil, tls: false)
|
|
33
39
|
if path.to_s.empty? && (host.to_s.empty? || port.nil?)
|
|
34
40
|
raise ArgumentError, "ndb address needs a socket path or a host and port"
|
|
35
41
|
end
|
|
@@ -37,18 +43,49 @@ module Ndb
|
|
|
37
43
|
@path = path
|
|
38
44
|
@host = host
|
|
39
45
|
@port = port
|
|
46
|
+
@tls = tls
|
|
40
47
|
end
|
|
41
48
|
|
|
42
49
|
def unix? = !@path.to_s.empty?
|
|
43
50
|
|
|
44
|
-
def
|
|
51
|
+
def tls? = @tls
|
|
52
|
+
|
|
53
|
+
def to_s
|
|
54
|
+
return "unix://#{@path}" if unix?
|
|
55
|
+
|
|
56
|
+
tls? ? "https://#{@host}:#{@port}" : "tcp://#{@host}:#{@port}"
|
|
57
|
+
end
|
|
45
58
|
|
|
46
59
|
def host_header = unix? ? "ndb" : "#{@host}:#{@port}"
|
|
47
60
|
|
|
48
61
|
def connect(timeout)
|
|
49
62
|
return UNIXSocket.new(@path) if unix?
|
|
50
63
|
|
|
51
|
-
TCPSocket.new(@host, @port, connect_timeout: timeout)
|
|
64
|
+
sock = TCPSocket.new(@host, @port, connect_timeout: timeout)
|
|
65
|
+
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
66
|
+
sock.timeout = timeout
|
|
67
|
+
|
|
68
|
+
tls? ? secure(sock) : sock
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
# Wraps sock in TLS with the system certificates (SSL_CERT_FILE adds a
|
|
74
|
+
# private CA), the host name for SNI, and a check that the certificate is
|
|
75
|
+
# for that host.
|
|
76
|
+
def secure(sock)
|
|
77
|
+
store = OpenSSL::X509::Store.new
|
|
78
|
+
store.set_default_paths
|
|
79
|
+
|
|
80
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
|
81
|
+
ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER, cert_store: store)
|
|
82
|
+
|
|
83
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
|
84
|
+
ssl.hostname = @host
|
|
85
|
+
ssl.sync_close = true
|
|
86
|
+
ssl.connect
|
|
87
|
+
ssl.post_connection_check(@host)
|
|
88
|
+
ssl
|
|
52
89
|
end
|
|
53
90
|
end
|
|
54
91
|
|
|
@@ -130,8 +167,8 @@ module Ndb
|
|
|
130
167
|
|
|
131
168
|
def open_socket
|
|
132
169
|
@socket = @endpoint.connect(@timeout)
|
|
133
|
-
@socket.timeout = @timeout
|
|
134
|
-
rescue SystemCallError, SocketError => e
|
|
170
|
+
@socket.timeout = @timeout if @socket.respond_to?(:timeout=)
|
|
171
|
+
rescue SystemCallError, SocketError, OpenSSL::SSL::SSLError => e
|
|
135
172
|
raise ConnectError, "cannot reach ndb at #{@endpoint}: #{e.message}"
|
|
136
173
|
end
|
|
137
174
|
|
data/lib/ndb/errors.rb
CHANGED
|
@@ -55,6 +55,15 @@ module Ndb
|
|
|
55
55
|
# truncate or drop without the collection name as confirm.
|
|
56
56
|
class ConfirmRequired < Error; end
|
|
57
57
|
|
|
58
|
+
# rename onto a name that exists, without replace.
|
|
59
|
+
class CollectionExists < Error; end
|
|
60
|
+
|
|
61
|
+
# restore to an instant out of the PITR window of the collection.
|
|
62
|
+
class OutOfWindow < Error; end
|
|
63
|
+
|
|
64
|
+
# restore of a collection with pitr: off.
|
|
65
|
+
class PitrOff < Error; end
|
|
66
|
+
|
|
58
67
|
# The daemon error code -> the Ruby class.
|
|
59
68
|
CODE_TO_ERROR = {
|
|
60
69
|
"unauthorized" => Unauthorized,
|
|
@@ -81,7 +90,10 @@ module Ndb
|
|
|
81
90
|
"invalid_field" => InvalidField,
|
|
82
91
|
"unindexed_many" => UnindexedMany,
|
|
83
92
|
"empty_filter" => EmptyFilter,
|
|
84
|
-
"confirm_required" => ConfirmRequired
|
|
93
|
+
"confirm_required" => ConfirmRequired,
|
|
94
|
+
"collection_exists" => CollectionExists,
|
|
95
|
+
"out_of_window" => OutOfWindow,
|
|
96
|
+
"pitr_off" => PitrOff
|
|
85
97
|
}.freeze
|
|
86
98
|
|
|
87
99
|
def self.error_for(code, message, status)
|
data/lib/ndb/version.rb
CHANGED