knot-ruby 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/knot-ruby.gemspec +1 -1
- data/lib/knot/interface.rb +40 -11
- data/lib/knot/protocol.rb +29 -8
- data/lib/knot/version.rb +1 -1
- data/lib/knot.rb +10 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 102f6dc54dc3b034d4b71e890c921cdd3067ef32999cf92e829b56e6cf661266
|
4
|
+
data.tar.gz: 847a1e73410dd0dbe17793c7c2819e22879dfbe47f728ec0625e02b88dcb7c2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e87bfff66910c223c3046dbc0ea63929d7c60d62434e06f1dccac5f2bca3ffc8b9e0652b32c1f4137ff6912cca50f26855dd989a64ec8f2a1c2f741f34307637
|
7
|
+
data.tar.gz: bcd68e1fa196ec799970697180b2dd375c6816da752c721dab9b0bf4bb82405d5909f090f5f10185713ee4c28166af0a64f103ff926ede60a519fc41a13a2e6a
|
data/Gemfile
CHANGED
data/knot-ruby.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.summary = %q{Provides interface to knot-server.}
|
11
11
|
spec.description = %q{Implements knot-protocol to provide an interface to knot-DNS-server}
|
12
12
|
spec.homepage = 'https://git.denkn.at/deac/knot'
|
13
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
14
14
|
|
15
15
|
#spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
16
16
|
|
data/lib/knot/interface.rb
CHANGED
@@ -11,22 +11,35 @@ class Knot::Zone
|
|
11
11
|
@zone, @transaction_opened = zone, 0
|
12
12
|
end
|
13
13
|
|
14
|
+
# If no transaction opened, yet, it opens a new transaction.
|
15
|
+
# Counts calling begin.
|
16
|
+
# Knot allowes only one global transaction, so if two clients tries to open a transaction,
|
17
|
+
# the second will fail. But the second client can change items in the same
|
18
|
+
# transaction like the first client.
|
19
|
+
# The first client, which calls commit or abort, wins and the transaction will be closed.
|
20
|
+
# So, the transaction-handling is only safe, if one client connects to knot.
|
14
21
|
def begin
|
15
22
|
@transaction_opened += 1
|
16
23
|
@protocol.call command: 'zone-begin', zone: @zone if 1 == @transaction_opened
|
17
24
|
end
|
18
25
|
|
26
|
+
# Decreases opened transactions.
|
27
|
+
# If opened transactions is zero, it stores items and closes transaction successfully.
|
19
28
|
def commit
|
20
29
|
@protocol.call command: 'zone-commit', zone: @zone if 1 == @transaction_opened
|
21
30
|
@transaction_opened -= 1 if 0 < @transaction_opened
|
22
31
|
end
|
23
32
|
|
33
|
+
# Closes transaction without saving immidiatly.
|
34
|
+
# Counter of opened transaction will be reset to 0.
|
24
35
|
def abort
|
25
36
|
@protocol.call command: 'zone-abort', zone: @zone
|
26
37
|
@transaction_opened = 0
|
27
38
|
end
|
28
39
|
|
29
|
-
|
40
|
+
# Opens transaction, calls yielded Proc and closes transaction after return.
|
41
|
+
# If exception was raised, the transaction will be aborted.
|
42
|
+
def transaction # :yield:
|
30
43
|
self.begin
|
31
44
|
yield self
|
32
45
|
rescue Object
|
@@ -56,7 +69,7 @@ class Knot::Zone
|
|
56
69
|
|
57
70
|
def read() @protocol.call command: 'zone-read', zone: @zone end
|
58
71
|
def diff() @protocol.call command: 'zone-diff', zone: @zone end
|
59
|
-
|
72
|
+
|
60
73
|
# setting record
|
61
74
|
# if data is nil, it will be unset.
|
62
75
|
def set owner, ttl = nil, type, data
|
@@ -72,6 +85,7 @@ class Knot::Zone
|
|
72
85
|
rescue Knot::Errors::ENONODE, Knot::Errors::ENOENT
|
73
86
|
end
|
74
87
|
alias delete unset
|
88
|
+
alias del unset
|
75
89
|
|
76
90
|
def get owner = nil, type = nil
|
77
91
|
@protocol.call command: 'zone-get',
|
@@ -115,10 +129,12 @@ class Knot::Conf
|
|
115
129
|
|
116
130
|
def parse_item k
|
117
131
|
case k
|
118
|
-
when
|
132
|
+
when Hash
|
119
133
|
case k.keys.sort
|
120
|
-
when %w[section], %w[id section], %w[item section], %w[id item section]
|
121
|
-
|
134
|
+
when %w[section], %w[id section], %w[item section], %w[id item section]
|
135
|
+
k
|
136
|
+
else
|
137
|
+
raise ArgumentError, "Invalid Item-format"
|
122
138
|
end
|
123
139
|
|
124
140
|
when Array
|
@@ -134,26 +150,39 @@ class Knot::Conf
|
|
134
150
|
(?: \[ (?<id> [a-z0-9_.-]+) \] )?
|
135
151
|
(?: \. (?<item>[a-z0-9_-]+) )?
|
136
152
|
\z/xi
|
153
|
+
|
137
154
|
$~.named_captures.delete_if {|_,v| v.nil? }
|
138
|
-
|
155
|
+
|
156
|
+
when nil
|
157
|
+
{}
|
158
|
+
|
159
|
+
else
|
160
|
+
raise ArgumentError, "Invalid Item-format"
|
139
161
|
end
|
140
162
|
end
|
141
163
|
|
164
|
+
def get item = nil
|
165
|
+
@protocol.call **parse_item( item).update( command: 'conf-get')
|
166
|
+
end
|
167
|
+
|
168
|
+
# Sets or adds a new value to item.
|
169
|
+
# knot knows single-value items like `server.rundir` and multi-value items like `server.listen`.
|
170
|
+
# If you set a single-value, it will replace the old value. On a multi-value, it will add it.
|
142
171
|
def set item, value
|
143
|
-
@protocol.call parse_item( item).update( command: 'conf-set', data: value)
|
172
|
+
@protocol.call **parse_item( item).update( command: 'conf-set', data: value)
|
144
173
|
end
|
145
|
-
alias [] set
|
146
174
|
|
175
|
+
# Removes value from item. If you provide a value, this value will be removed.
|
147
176
|
def unset item, value = nil
|
148
|
-
@protocol.call parse_item( item).update( command: 'conf-unset', data: value)
|
177
|
+
@protocol.call **parse_item( item).update( command: 'conf-unset', data: value)
|
149
178
|
end
|
150
179
|
alias delete unset
|
151
180
|
|
152
181
|
def list item = nil
|
153
|
-
@protocol.call
|
182
|
+
@protocol.call **parse_item( item).update( command: 'conf-list')
|
154
183
|
end
|
155
184
|
|
156
185
|
def read item = nil
|
157
|
-
@protocol.call
|
186
|
+
@protocol.call **parse_item( item).update( command: 'conf-read')
|
158
187
|
end
|
159
188
|
end
|
data/lib/knot/protocol.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'iounpack'
|
2
|
+
require 'stringio'
|
2
3
|
require_relative 'errors'
|
3
4
|
|
4
5
|
module Knot
|
@@ -51,6 +52,26 @@ module Knot::Protocol::Type
|
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
55
|
+
#class Knot::KnotC
|
56
|
+
# attr_accessor :debug, :binary
|
57
|
+
#
|
58
|
+
# def initialize path = nil, binary: nil
|
59
|
+
# @path = path
|
60
|
+
# @binary = binary || 'knotc'
|
61
|
+
# @conf = Knot::Conf.new self
|
62
|
+
# @zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self }
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# def call command:, flags: nil, section: nil, item: nil, id: nil, zone: nil, owner: nil, ttl: nil, type: nil, data: nil, filter: nil
|
66
|
+
# cs =
|
67
|
+
# case command.to_s
|
68
|
+
# when 'conf-begin', 'conf-commit', 'conf-abort', 'status', 'stop', 'reload'
|
69
|
+
# [@binary, command, ]
|
70
|
+
# else raise ArgumentError, "Unknown Command: #{command}"
|
71
|
+
# end
|
72
|
+
# end
|
73
|
+
#end
|
74
|
+
|
54
75
|
module Knot::Protocol::Idx
|
55
76
|
Idx = [
|
56
77
|
:command, # 10, :CMD, # Control command name.
|
@@ -117,7 +138,7 @@ class Knot::Protocol
|
|
117
138
|
STDERR.puts( {data: data, _: s}.inspect) if @debug
|
118
139
|
rsock.write s
|
119
140
|
rsock.flush
|
120
|
-
end
|
141
|
+
end
|
121
142
|
|
122
143
|
class RecordIO
|
123
144
|
attr_reader :str
|
@@ -153,7 +174,7 @@ class Knot::Protocol
|
|
153
174
|
when 1, 2
|
154
175
|
type = t
|
155
176
|
ret.push( r = {})
|
156
|
-
else
|
177
|
+
else
|
157
178
|
raise Knot::Errors::EINVAL, "Missing Type before: #{t}" if ret.empty?
|
158
179
|
i = Idx::Idx[t - 0x10] or raise Knot::Errors::EINVAL, "Unknown index: #{t-0x10}"
|
159
180
|
l = sock.unpack1 'n'
|
@@ -167,7 +188,7 @@ class Knot::Protocol
|
|
167
188
|
|
168
189
|
def call sock: nil, **data
|
169
190
|
snd sock: sock, **data
|
170
|
-
rcv( sock: sock).each do |r|
|
191
|
+
rcv( sock: sock).each do |r|
|
171
192
|
if r[:error]
|
172
193
|
if e = Knot::Errors.err2exc[r[:error]]
|
173
194
|
raise e, r[:error]
|
@@ -179,10 +200,10 @@ class Knot::Protocol
|
|
179
200
|
|
180
201
|
def zone( zone) @zones[zone.to_s.to_sym] end
|
181
202
|
|
182
|
-
def conf_set( **opts) call opts.update( command: 'conf-set')
|
183
|
-
def conf_unset( **opts) call opts.update( command: 'conf-
|
203
|
+
def conf_set( **opts) call **opts.update( command: 'conf-set') end
|
204
|
+
def conf_unset( **opts) call **opts.update( command: 'conf-unset') end
|
184
205
|
|
185
|
-
def zone_set( **opts) call opts.update( command: 'zone-set')
|
186
|
-
def zone_unset( **opts) call opts.update( command: 'zone-unset') end
|
187
|
-
def zone_get( **opts) call opts.update( command: 'zone-get')
|
206
|
+
def zone_set( **opts) call **opts.update( command: 'zone-set') end
|
207
|
+
def zone_unset( **opts) call **opts.update( command: 'zone-unset') end
|
208
|
+
def zone_get( **opts) call **opts.update( command: 'zone-get') end
|
188
209
|
end
|
data/lib/knot/version.rb
CHANGED
data/lib/knot.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: knot-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Knauf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: iounpack
|
@@ -57,7 +57,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
57
|
requirements:
|
58
58
|
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: 2.
|
60
|
+
version: 2.7.0
|
61
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
62
|
requirements:
|
63
63
|
- - ">="
|