zonesync 0.4.0 → 0.5.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: 00751af08d69afada768a779c3e2344b3e728e0b3ee9162436f82f936737bcab
4
- data.tar.gz: 772a439ddfd4f9e9cd96bffeade456ba7023732e221a81ef48c39595f0e2328d
3
+ metadata.gz: 45302044aaba7f575bcd49cd55780d710fdac60f87ff22917d2b58e4ec8cc0c6
4
+ data.tar.gz: 5caa809cd4c2987a10a485990a477520aabe3fd619270b59610a6e34ce80fcf6
5
5
  SHA512:
6
- metadata.gz: 896d74bfd34d5f4609c5e4ab9d79bcad73b812b694941ab24f3807f6d3a08f9d25c28b13dc73201a6e6ed318ae475e43f641f7c9376c8b4af612ea0ba0c3d78d
7
- data.tar.gz: a6568612418b311a6c3c89bf8a8e680214336d0075e05373aad7ca7ad32938e000175ed72c198d230dc3d9c7ecfbfcc6ce37b4125c14cf3376d83834ba9e9b4b
6
+ metadata.gz: 86da22286d6c1f32d1a0fb052deb9733db90c6a05802a10cef63c72e5b7da20fbb2fa012502d1a2d95d31c2567153f94a883bf92790b8ddfcf24b05558791a8a
7
+ data.tar.gz: 1da0b2120b182a26eb1274b4e9200ffcee306c31a27714abf74fcc35fd4fd71a8b13c650eb6b540c6ee931d8aa1bed6b19cacb64b9166ada7d733801c7497d8e
data/README.md CHANGED
@@ -80,7 +80,9 @@ $ bundle exec zonesync
80
80
  ```
81
81
  $ bundle exec zonesync --dry-run # log to STDOUT but don't actually perform the sync
82
82
  ```
83
-
83
+ ```
84
+ $ bundle exec zonesync generate # generate a Zonefile from the configured provider
85
+ ```
84
86
  #### Ruby
85
87
 
86
88
  Assuming your zone file lives in `hostfile.txt` and your DNS provider credentials are configured in `provider.yml`:
data/lib/zonesync/cli.rb CHANGED
@@ -9,6 +9,11 @@ module Zonesync
9
9
  Zonesync.call dry_run: options[:dry_run]
10
10
  end
11
11
 
12
+ desc "generate", "generates a Zonefile from the DNS server configured in Rails.application.credentials.zonesync"
13
+ def generate
14
+ Zonesync.generate
15
+ end
16
+
12
17
  def self.exit_on_failure? = true
13
18
  end
14
19
  end
@@ -35,18 +35,31 @@ module Zonesync
35
35
  @records ||= begin
36
36
  response = http.get(nil)
37
37
  response["result"].reduce({}) do |map, attrs|
38
- map.merge attrs["id"] => Record.new(
39
- attrs["name"] + ".", # normalize to trailing period
40
- attrs["type"],
41
- attrs["ttl"].to_i,
42
- attrs["content"],
43
- ).to_h
44
- end.invert
38
+ map.merge to_record(attrs) => attrs["id"]
39
+ end
45
40
  end
46
41
  end
47
42
 
48
43
  private
49
44
 
45
+ def to_record attrs
46
+ rdata = attrs["content"]
47
+ if %w[CNAME MX].include?(attrs["type"])
48
+ rdata = normalize_trailing_period(rdata)
49
+ end
50
+
51
+ Record.new(
52
+ normalize_trailing_period(attrs["name"]),
53
+ attrs["type"],
54
+ attrs["ttl"].to_i,
55
+ rdata,
56
+ ).to_h
57
+ end
58
+
59
+ def normalize_trailing_period value
60
+ value =~ /\.$/ ? value : value + "."
61
+ end
62
+
50
63
  def http
51
64
  return @http if @http
52
65
  @http = HTTP.new("https://api.cloudflare.com/client/v4/zones/#{credentials[:zone_id]}/dns_records")
@@ -4,6 +4,7 @@ require "zonesync/record"
4
4
  module Zonesync
5
5
  class Provider < Struct.new(:credentials)
6
6
  def self.from credentials
7
+ return credentials if credentials.is_a?(Provider)
7
8
  Zonesync.const_get(credentials[:provider]).new(credentials)
8
9
  end
9
10
 
@@ -27,6 +28,10 @@ module Zonesync
27
28
  raise NotImplementedError
28
29
  end
29
30
 
31
+ def write text
32
+ raise NotImplementedError
33
+ end
34
+
30
35
  def remove record
31
36
  raise NotImplementedError
32
37
  end
@@ -46,12 +51,20 @@ module Zonesync
46
51
  def read
47
52
  credentials[:string]
48
53
  end
54
+
55
+ def write string
56
+ credentials[:string] = string
57
+ end
49
58
  end
50
59
 
51
60
  class Filesystem < Provider
52
61
  def read
53
62
  File.read(credentials[:path])
54
63
  end
64
+
65
+ def write string
66
+ File.write(credentials[:path], string)
67
+ end
55
68
  end
56
69
  end
57
70
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zonesync
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/zonesync.rb CHANGED
@@ -9,6 +9,10 @@ module Zonesync
9
9
  Sync.new({ provider: "Filesystem", path: zonefile }, credentials).call(dry_run: dry_run)
10
10
  end
11
11
 
12
+ def self.generate zonefile: "Zonefile", credentials: default_credentials
13
+ Generate.new({ provider: "Filesystem", path: zonefile }, credentials).call
14
+ end
15
+
12
16
  def self.default_credentials
13
17
  require "active_support/encrypted_configuration"
14
18
  require "active_support/core_ext/hash/keys"
@@ -20,6 +24,10 @@ module Zonesync
20
24
  ).zonesync
21
25
  end
22
26
 
27
+ def self.default_provider
28
+ Provider.from(default_credentials)
29
+ end
30
+
23
31
  class Sync < Struct.new(:source, :destination)
24
32
  def call dry_run: false
25
33
  source = Provider.from(self.source)
@@ -31,5 +39,13 @@ module Zonesync
31
39
  end
32
40
  end
33
41
  end
42
+
43
+ class Generate < Struct.new(:source, :destination)
44
+ def call
45
+ source = Provider.from(self.source)
46
+ destination = Provider.from(self.destination)
47
+ source.write(destination.read)
48
+ end
49
+ end
34
50
  end
35
51
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zonesync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-01-31 00:00:00.000000000 Z
12
+ date: 2024-02-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dns-zonefile