-
Notifications
You must be signed in to change notification settings - Fork 0
/
zonediff
executable file
·182 lines (147 loc) · 6.92 KB
/
zonediff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# PowerDNS Auth Zone Differ (depends on PowerDNS API)
# Copyright 2015 Deduktiva GmbH <http://deduktiva.com/>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import print_function, unicode_literals
import requests # python-requests
import difflib # stdlib
import argparse # stdlib
import sys
from requests.exceptions import RequestException
def fetch(api_key, server, url):
req = requests.get(server + url, headers={u'X-API-Key': api_key, u'Accept': u'application/json'})
req.raise_for_status()
return req.json()
def fetch_server_name(api_key, server):
return next((x[u'value'] for x in fetch(api_key, server, u'/servers/localhost/config')
if x[u'name'] == u'server-id'), None)
def fetch_basedata(api_key, server):
name = fetch_server_name(api_key, server)
zones = fetch(api_key, server, u'/servers/localhost/zones')
zonenames = {zone[u'name'] for zone in zones}
return name, zones, zonenames
def comment_format(c):
return u'; [%s @ %s] %s' % (c[u'account'], c[u'modified_at'], c[u'content'])
def record_format(r):
return u'%s%s\t%s\t%s\t%s' % ((u'; ' if r[u'disabled'] else u''),
r[u'name'],
r[u'type'],
r[u'ttl'],
r[u'content'])
def type_weight(typename):
return {u'SOA': 0, u'NS': 1, u'DS': 2}.get(typename, 1000 + sum(ord(x) for x in typename))
def comparator(x, y):
x_revname = x[u'name'][::-1]
y_revname = y[u'name'][::-1]
if x_revname != y_revname:
return cmp(x_revname, y_revname)
if x[u'type'] != y[u'type']:
return cmp(type_weight(x[u'type']), type_weight(y[u'type']))
x_is_comment = u'modified_at' in x
y_is_comment = u'modified_at' in y
if x_is_comment != y_is_comment:
return 1 if x_is_comment else -1
if x_is_comment: # both comments
if x[u'modified_at'] != y[u'modified_at']:
return cmp(x[u'modified_at'], y[u'modified_at'])
if x[u'account'] != y[u'account']:
return cmp(x[u'account'], y[u'account'])
if x[u'content'] != y[u'content']:
return cmp(x[u'content'], y[u'content'])
else: # both records
if x[u'content'] != y[u'content']:
return cmp(x[u'content'], y[u'content'])
if x[u'ttl'] != y[u'ttl']:
return cmp(x[u'ttl'], y[u'ttl'])
return 0
def zone_format(zone):
# Note: does NOT produce an industry standard format representation.
keys = sorted(set(zone.keys()) - {u'comments', u'records', u'id', u'url', u'name'})
combined = sorted([(c, comment_format(c)) for c in zone[u'comments']] +
[(r, record_format(r)) for r in zone[u'records']],
cmp=comparator, key=lambda thing: thing[0])
return ([u';;; %s' % zone[u'name']] +
[u'; %s = %s' % (k, zone[k]) for k in keys] +
[thing[1] for thing in combined])
def run(api_key, server1, server2):
try:
name_server1, zones_server1, zonenames_server1 = fetch_basedata(api_key, server1)
except RequestException as except_inst:
print(u"E: Failed fetching config or zones from %s: %s" % (server1, except_inst))
return 3
try:
name_server2, zones_server2, zonenames_server2 = fetch_basedata(api_key, server2)
except RequestException as except_inst:
print(u"E: Failed fetching config or zones from %s: %s" % (server2, except_inst))
return 4
print(u"I: server1: URL: %s, Name: %s" % (server1, name_server1))
print(u"I: server2: URL: %s, Name: %s" % (server2, name_server2))
missing_server1 = zonenames_server2 - zonenames_server1
missing_server2 = zonenames_server1 - zonenames_server2
if missing_server1:
print(u"W: Zones missing from %s:" % name_server1, u' '.join(missing_server1))
if missing_server2:
print(u"W: Zones missing from %s:" % name_server2, u' '.join(missing_server2))
errors, diffs, identical = 0, 0, 0
common_zones = [zone for zone in zones_server1 if zone[u'name'] in zonenames_server2]
for zone in common_zones:
zone_url = zone[u'url']
try:
zone1 = fetch(api_key, server1, zone_url)
except RequestException as except_inst:
print(u"E: Error while fetching %s from %s: %s" % (zone_url, server1, except_inst))
errors += 1
continue
try:
zone2 = fetch(api_key, server2, zone_url)
except RequestException as except_inst:
print(u"E: Error while fetching %s from %s: %s" % (zone_url, server2, except_inst))
errors += 1
continue
zonetxt1 = zone_format(zone1)
zonetxt2 = zone_format(zone2)
if zonetxt1 == zonetxt2:
identical += 1
else:
diffs += 1
print(u'diff zone %s on server %s to server %s' % (zone1[u'name'], name_server1, name_server2))
for line in difflib.unified_diff(zonetxt1, zonetxt2,
u'%s/%s' % (name_server1, zone1[u'name']),
u'%s/%s' % (name_server2, zone2[u'name']), lineterm=u""):
print(line)
if diffs:
print(u"---")
print(u"I: %s zones, %s different, %s identical, plus %s errors" % (identical + diffs, diffs, identical, errors))
if diffs:
return 1
if errors:
return 2
return 0
def parse_args():
parser = argparse.ArgumentParser(description='Diff PowerDNS data')
parser.add_argument(u'apikey', metavar=u'KEY', help=u'API Key', default=u'changeme')
parser.add_argument(u'server1', metavar=u'URL1', help=u'API URL for server1')
parser.add_argument(u'server2', metavar=u'URL2', help=u'API URL for server2')
return parser.parse_args()
if __name__ == u'__main__':
args = parse_args()
sys.exit(run(args.apikey, args.server1, args.server2))