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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04ae67be521fbc10dc42972c8b3c7bfc9c0edc2822617a0ef565564e7e93bd6f
4
- data.tar.gz: 89396bbc12e9aa181dcbf4af502262d4e8a4fb3ea982c28034c54d374078bd99
3
+ metadata.gz: 102f6dc54dc3b034d4b71e890c921cdd3067ef32999cf92e829b56e6cf661266
4
+ data.tar.gz: 847a1e73410dd0dbe17793c7c2819e22879dfbe47f728ec0625e02b88dcb7c2a
5
5
  SHA512:
6
- metadata.gz: 8cf55c28b347d9f5103451ad60d0da577c1febf8909b10bd30449985057c7d8640777953de99ff5509736ead73b8a8f5a9329150948f1a2dea9c30e55728c738
7
- data.tar.gz: 826ffdff42cbf5a3dd7fc0a54707796c283d13eb0e99478c293ac620cf4b179c24393f0c2db2638717904d7aec0ef097dda2c1cb3d566ad81ebfe43273e8b53c
6
+ metadata.gz: e87bfff66910c223c3046dbc0ea63929d7c60d62434e06f1dccac5f2bca3ffc8b9e0652b32c1f4137ff6912cca50f26855dd989a64ec8f2a1c2f741f34307637
7
+ data.tar.gz: bcd68e1fa196ec799970697180b2dd375c6816da752c721dab9b0bf4bb82405d5909f090f5f10185713ee4c28166af0a64f103ff926ede60a519fc41a13a2e6a
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source "https://rubygems.org"
2
+ ruby '>=2.7'
2
3
 
3
4
  # Specify your gem's dependencies in knot.gemspec
4
5
  gemspec
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.3.0")
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
 
@@ -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
- def transaction
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 k
132
+ when Hash
119
133
  case k.keys.sort
120
- when %w[section], %w[id section], %w[item section], %w[id item section] then k
121
- else raise ArgumentError, "Invalid Item-format"
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
- else raise ArgumentError, "Invalid Item-format"
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 (item ? parse_item( item) : {}).update( command: 'conf-list')
182
+ @protocol.call **parse_item( item).update( command: 'conf-list')
154
183
  end
155
184
 
156
185
  def read item = nil
157
- @protocol.call (item ? parse_item( item) : {}).update( command: 'conf-read')
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') end
183
- def conf_unset( **opts) call opts.update( command: 'conf-set') end
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') end
186
- def zone_unset( **opts) call opts.update( command: 'zone-unset') end
187
- def zone_get( **opts) call opts.update( command: 'zone-get') end
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
@@ -1,3 +1,3 @@
1
1
  module Knot
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/knot.rb CHANGED
@@ -2,3 +2,13 @@ require 'knot/version'
2
2
  require 'knot/errors'
3
3
  require 'knot/protocol'
4
4
  require 'knot/interface'
5
+
6
+ module Knot
7
+ class <<self
8
+ def new *as, **os
9
+ Protocol.new *as, **os
10
+ end
11
+ alias connect new
12
+ alias open new
13
+ end
14
+ end
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.1.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: 2020-06-20 00:00:00.000000000 Z
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.3.0
60
+ version: 2.7.0
61
61
  required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  requirements:
63
63
  - - ">="