forked from thejollyroger1/Rackspace-DNS-Export-Import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RAX_dns_export_import.py
352 lines (305 loc) · 16.1 KB
/
RAX_dns_export_import.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#/bin/python
import argparse
import json
import os
import requests
import time
from requests.adapters import HTTPAdapter
class Auth:
auth_url = "https://identity.api.rackspacecloud.com/v2.0/tokens"
auth_headers = {'Content-type': 'application/json'}
def __init__(self, user, api_key):
self.user = user
self.api_key = api_key
def auth_call(self):
self.auth_data = json.dumps({"auth": {'RAX-KSKEY:apiKeyCredentials': {'username': self.user, 'apiKey': self.api_key}}})
self.auth_request = s.post(self.auth_url, data=self.auth_data, headers=self.auth_headers)
self.token_raw = self.auth_request.json()['access']['token']['id']
self.token = str(self.token_raw)
return self.token
class RetryHTTPAdapter(HTTPAdapter):
SECONDS_BETWEEN_RETRIES = 10
def __init__(self, retry_time=120, *args, **kwargs):
self.retry_time = retry_time
super(RetryHTTPAdapter, self).__init__(*args, **kwargs)
def send(self, *args, **kwargs):
for _ in range(int(self.retry_time / self.SECONDS_BETWEEN_RETRIES)):
response = super(RetryHTTPAdapter, self).send(*args, **kwargs)
if response.status_code in (200, 201, 202, 203, 204):
break
time.sleep(self.SECONDS_BETWEEN_RETRIES)
return response
s = requests.Session()
s.mount('http://', RetryHTTPAdapter(retry_time=60))
s.mount('https://', RetryHTTPAdapter(retry_time=60))
#EXAMPLE USAGE
#s.get('http://example.com')
def dns_export_import_single(srcddi, srctoken, dstddi, dsttoken, custom_dns_id):
#Export the Domain
export_dns_endpoint = 'https://dns.api.rackspacecloud.com/v1.0/%s/domains/%s/export' % (srcddi, custom_dns_id)
export_dns_headers = {'X-Auth-Token': srctoken}
export_dns_get = s.get(export_dns_endpoint, headers=export_dns_headers)
export_dns_joburlraw = export_dns_get.json()['callbackUrl']
export_job_check = s.get(export_dns_joburlraw, headers=export_dns_headers)
#Check Export Job
while str(export_job_check.json()['status']) == 'RUNNING':
time.sleep(5)
export_job_check = s.get(export_dns_joburlraw, headers=export_dns_headers)
if str(export_job_check.json()['status']) != 'COMPLETED':
print "There was a problem with the export job for domain ID : " + str(custom_dns_id)
print str(export_job_check.text)
quit()
#Now that the job is completed move on to importing the domain
export_dns_joburl = str(export_dns_joburlraw) + '?showDetails=true'
export_dns_bind9_request = s.get(export_dns_joburl, headers=export_dns_headers)
export_dns_bind9_text = ''
for line in export_dns_bind9_request.json()['response']['contents'].splitlines():
if 'ipadmin.stabletransit.com.' in line:
bind_line_list = line.split()
export_dns_bind9_text += bind_line_list[0] + '\t\t' + bind_line_list[1] + '\tIN\tSOA\t' + 'ns.rackspace.com. ' + admin_email + ' ' + bind_line_list[6] + ' ' + bind_line_list[7] + ' ' + bind_line_list[8] + ' ' + bind_line_list[9] + ' ' + bind_line_list[10] + '\n'
if 'dns1.stabletransit.com.' in line:
continue
if 'dns2.stabletransit.com.' in line:
continue
else:
export_dns_bind9_text += line + '\n'
export_dns_bind9_json = json.dumps(export_dns_bind9_text)
if export_dns_bind9_text == "":
print "\nNo records found to import for this domain, quitting..."
quit()
if import_option != True:
print "\nImport option not used, printing Bind9 export below and quitting:\n"
print export_dns_bind9_json
quit()
dns_import_data = '{"domains" : [ {"contentType" : "BIND_9", "contents" : %s} ]}' % export_dns_bind9_json
#Before we import this domain we need to remove it from the origin account
print "\nPreparing to remove domain ID : " + str(custom_dns_id) + " from source account"
print "\nHere is the raw json formated Bind9 output if the import fails and has to be manually retried via the API:\n"
print str(dns_import_data)
remove_domain_url = 'https://dns.api.rackspacecloud.com/v1.0/%s/domains?id=%s' % (srcddi, custom_dns_id)
remove_domain_request = s.delete(remove_domain_url, headers=export_dns_headers)
remove_domain_job = remove_domain_request.json()['callbackUrl']
remove_domain_job_url = str(remove_domain_job) + '?showDetails=true'
remove_domain_check = s.get(remove_domain_job_url, headers=export_dns_headers)
while str(remove_domain_check.json()['status']) == 'RUNNING':
time.sleep(5)
remove_domain_check = s.get(remove_domain_job_url, headers=export_dns_headers)
if str(remove_domain_check.json()['status']) != 'COMPLETED':
print "\nThere was a problem with the delete job for domain ID : " + str(custom_dns_id)
print "\nStatus: " + str(remove_domain_check.json()['status'])
if str(remove_domain_check.json()['status']) == 'ERROR':
print '\nError Message : ' + str(remove_domain_check.json()['error'])
print '\nQuitting...'
quit()
if str(remove_domain_check.json()['status']) == 'COMPLETED':
print "\nDomain removal successful, preparing to import domain to destination account"
import_dns_endpoint = 'https://dns.api.rackspacecloud.com/v1.0/%s/domains/import' % dstddi
import_dns_headers = {'X-Auth-Token': dsttoken, 'Accept': 'application/json', 'Content-Type': 'application/json'}
import_dns_request = s.post(import_dns_endpoint, data=dns_import_data, headers=import_dns_headers)
print "\nImport DNS response : " + str(import_dns_request.text)
import_dns_job = import_dns_request.json()['callbackUrl']
import_dns_job_url = str(import_dns_job) + '?showDetails=true'
print "\nImport dns job URL : " + str(import_dns_job_url)
import_job_headers = {'X-Auth-Token': dsttoken}
import_job_check = s.get(import_dns_job_url, headers=import_job_headers)
#Only 20 POSTs/min so adding 3 second sleep
time.sleep(3)
while str(import_job_check.json()['status']) == 'RUNNING':
time.sleep(5)
import_job_check = s.get(import_dns_job_url, headers=import_job_headers)
if str(import_job_check.json()['status']) not in ('COMPLETED', 'RUNNING'):
print "\nThere was a problem with the import job for domain ID : " + str(custom_dns_id)
print "\nStatus: " + str(import_job_check.json()['status'])
if str(import_job_check.json()['status']) == 'ERROR':
print '\nError Message : ' + str(import_job_check.json()['error'])
if str(import_job_check.json()['status']) == 'COMPLETED':
print "\nImport Job Output: " + str(import_job_check.text)
print "\nImport appears to have been successful for domain ID : " + str(custom_dns_id)
def dns_export_import(srcddi, srctoken, dstddi, dsttoken):
id_list = []
if dns_id_file:
print "\nDomain ID file specified, gathering ID list...\n"
if os.path.isfile(dns_id_file):
with open(dns_id_file) as file:
content = [x.strip('\n') for x in file.readlines()]
for dns_id in content:
id_list.append(dns_id)
print "Script will run against the following IDs: \n"
print id_list
else:
print "\nDNS ID file not found! Quitting..."
quit()
else:
print "\nNo specific domain or DNS file provided, script will run against ALL domains on account"
while True:
answer = raw_input("\nPlease verify you want to continue [y/n]: ").lower()
if answer in ('y', 'yes', 'ye'):
break
if answer in ('n', 'no'):
print "\nQuitting..."
quit()
else:
print "\nPlease provide a valid response"
src_dns_endpoint = 'https://dns.api.rackspacecloud.com/v1.0/%s/domains' % srcddi
dns_headers = {'X-Auth-Token': srctoken}
dns_get = s.get(src_dns_endpoint, headers=dns_headers)
dns_return = dns_get.text
dns_domains = json.loads(dns_return)['domains']
for domain in dns_domains:
id_list.append(domain['id'])
for dns_id in id_list:
#Export the Domain
export_dns_endpoint = 'https://dns.api.rackspacecloud.com/v1.0/%s/domains/%s/export' % (srcddi, dns_id)
export_dns_headers = {'X-Auth-Token': srctoken}
export_dns_get = s.get(export_dns_endpoint, headers=export_dns_headers)
export_dns_joburlraw = export_dns_get.json()['callbackUrl']
export_job_check = s.get(export_dns_joburlraw, headers=export_dns_headers)
#Check Export Job
while str(export_job_check.json()['status']) == 'RUNNING':
time.sleep(5)
export_job_check = s.get(export_dns_joburlraw, headers=export_dns_headers)
if str(export_job_check.json()['status']) != 'COMPLETED':
print "\nThere was a problem with the export job for domain ID : " + str(dns_id)
print str(export_job_check.text)
quit()
#Now that the job is completed move on to importing the domain
export_dns_joburl = str(export_dns_joburlraw) + '?showDetails=true'
export_dns_bind9_request = s.get(export_dns_joburl, headers=export_dns_headers)
export_dns_bind9_text = ''
for line in export_dns_bind9_request.json()['response']['contents'].splitlines():
if 'ipadmin.stabletransit.com.' in line:
bind_line_list = line.split()
export_dns_bind9_text += bind_line_list[0] + '\t\t' + bind_line_list[1] + '\tIN\tSOA\t' + 'ns.rackspace.com. ' + admin_email + ' ' + bind_line_list[6] + ' ' + bind_line_list[7] + ' ' + bind_line_list[8] + ' ' + bind_line_list[9] + ' ' + bind_line_list[10] + '\n'
if 'dns1.stabletransit.com.' in line:
continue
if 'dns2.stabletransit.com.' in line:
continue
else:
export_dns_bind9_text += line + '\n'
export_dns_bind9_json = json.dumps(export_dns_bind9_text)
if export_dns_bind9_text == "":
print "\nNo records found for domain ID : " + str(dns_id)
continue
if import_option != True:
print "\nImport option not used, printing all Bind9 exports below and quitting: \n"
print export_dns_bind9_json
time.sleep(1)
continue
#Only 10 deletes/min allowed by default so addding 6 second sleep
time.sleep(6)
dns_import_data = '{"domains" : [ {"contentType" : "BIND_9", "contents" : %s} ]}' % export_dns_bind9_json
#Before we import this domain we need to remove it from the origin account
print "\nPreparing to remove domain ID : " + str(dns_id) + " from source account"
print "\nHere is the raw json formated Bind9 output if the import fails and has to be manually retried via the API:\n"
print str(dns_import_data)
remove_domain_url = 'https://dns.api.rackspacecloud.com/v1.0/%s/domains?id=%s' % (srcddi, dns_id)
remove_domain_request = s.delete(remove_domain_url, headers=export_dns_headers)
remove_domain_job = remove_domain_request.json()['callbackUrl']
remove_domain_job_url = str(remove_domain_job) + '?showDetails=true'
remove_domain_check = s.get(remove_domain_job_url, headers=export_dns_headers)
while str(remove_domain_check.json()['status']) == 'RUNNING':
time.sleep(5)
remove_domain_check = s.get(remove_domain_job_url, headers=export_dns_headers)
if str(remove_domain_check.json()['status']) != 'COMPLETED':
print "\nThere was a problem with the delete job for domain ID : " + str(dns_id)
print "\nStatus: " + str(remove_domain_check.json()['status'])
if str(remove_domain_check.json()['status']) == 'ERROR':
print '\nError Message : ' + str(remove_domain_check.json()['error'])
print '\nSkipping domain ID : ' + dns_id
continue
if str(remove_domain_check.json()['status']) == 'COMPLETED':
print "\nDomain removal successful, preparing to import domain to destination account"
import_dns_endpoint = 'https://dns.api.rackspacecloud.com/v1.0/%s/domains/import' % dstddi
import_dns_headers = {'X-Auth-Token': dsttoken, 'Accept': 'application/json', 'Content-Type': 'application/json'}
import_dns_request = s.post(import_dns_endpoint, data=dns_import_data, headers=import_dns_headers)
print "\nImport DNS response : " + str(import_dns_request.text)
import_dns_job = import_dns_request.json()['callbackUrl']
import_dns_job_url = str(import_dns_job) + '?showDetails=true'
print "\nImport dns job URL : " + str(import_dns_job_url)
import_job_headers = {'X-Auth-Token': dsttoken}
import_job_check = s.get(import_dns_job_url, headers=import_job_headers)
#Only 20 POSTs/min so adding 3 second sleep
time.sleep(3)
while str(import_job_check.json()['status']) == 'RUNNING':
time.sleep(5)
import_job_check = s.get(import_dns_job_url, headers=import_job_headers)
if str(import_job_check.json()['status']) not in ('COMPLETED', 'RUNNING'):
print "\nThere was a problem with the import job for domain ID : " + str(dns_id)
print "\nStatus: " + str(import_job_check.json()['status'])
if str(import_job_check.json()['status']) == 'ERROR':
print '\nError Message : ' + str(import_job_check.json()['error'])
if str(import_job_check.json()['status']) == 'COMPLETED':
print "\nImport Job Output: " + str(import_job_check.text)
print "\nImport appears to have been successful for domain ID : " + str(dns_id)
#prevent API call limit issues
time.sleep(1)
parser = argparse.ArgumentParser()
parser.add_argument('--srcddi',
required=True,
default=None,
help='The account number or DDI for the source account')
parser.add_argument('--srcuser',
required=True,
default=None,
help='The user for the source account')
parser.add_argument('--srcapikey',
required=True,
default=None,
help='Source account apikey')
parser.add_argument('--dstddi',
required=False,
default=None,
help='The account number or DDI for the destination account')
parser.add_argument('--dstuser',
required=False,
default=None,
help='The user for the destination account')
parser.add_argument('--dstapikey',
required=False,
default=None,
help='Destination account apikey')
parser.add_argument('--domainid',
required=False,
default=None,
help='Choose a specific domain to export and import')
parser.add_argument('--importdomains',
action='store_true',
required=False,
default=None,
help='Option to import the domain(s) after exporting, will delete the source')
parser.add_argument('--dnsidfile',
required=False,
default=None,
help='A file containing DNS IDs that you want to export or transfer')
parser.add_argument('--email',
required=True,
default=None,
help='The administrative email address to be inserted to the SOA record if it has to be generated manually, must be formatted email.domain.com.')
args = parser.parse_args()
srcuser = args.srcuser
dstuser = args.dstuser
user = args.srcuser
api_key = args.srcapikey
srcddi = args.srcddi
srctoken_return = Auth(user,api_key)
srctoken = srctoken_return.auth_call()
custom_dns_id = args.domainid
import_option = args.importdomains
dns_id_file = args.dnsidfile
admin_email = args.email
if import_option != True:
#don't set dst variables since they don't exist
dstddi = srcddi
dsttoken = srctoken
else:
user = args.dstuser
api_key = args.dstapikey
dstddi = args.dstddi
dsttoken_return = Auth(user,api_key)
dsttoken = dsttoken_return.auth_call()
if __name__ == '__main__':
if custom_dns_id:
print "\n Single domain ID detected, running script for just this domain ID : " + str(custom_dns_id)
dns_export_import_single(srcddi, srctoken, dstddi, dsttoken, custom_dns_id)
else:
dns_export_import(srcddi, srctoken, dstddi, dsttoken)