From 7146cdebaf68255658f7dfbabf0b79057a876b3a Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Mon, 21 Nov 2022 11:44:07 +1100 Subject: [PATCH] rax-dns-backup: fix parsing It looks like at some point the RAX bind output changed format slightly, which messed up our backup script. Rework it to parse the current output. This parsing is obviously a little fragile ... it is nice to have the output sorted and lined up nicely (like our manually maintained opendev.org bind files...). If the format changes again and this becomes a problem, maybe we switch to dumping the RAX output directly and forget about formatting it nicely. Change-Id: I742dd6ef9ffdb377274b384b847625c98dd5ff16 --- .../roles/rax-dns-backup/files/rax-dns-backup | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/playbooks/roles/rax-dns-backup/files/rax-dns-backup b/playbooks/roles/rax-dns-backup/files/rax-dns-backup index 3feb937278..3e20d6755e 100755 --- a/playbooks/roles/rax-dns-backup/files/rax-dns-backup +++ b/playbooks/roles/rax-dns-backup/files/rax-dns-backup @@ -124,16 +124,20 @@ def do_bind_export(session, token, domain_id, outfile): query = {'showDetails': 'true'} final_response = session.get(callback_url, params=query, headers=headers) - bind_output = final_response.json()['response']['contents'] + bind_output = final_response.json()['response']['contents'].split('\n') output = [] - for line in bind_output.split('\n'): + + # Read and parse the record lines for sorting; the skip is because + # the first 3 lines are comments and the rest are SOA records + # (written separately below). + for line in bind_output[10:]: if line == '': continue fields = line.split(' ') output.append(fields) - # find padding space for the first column + # find padding space for the first column so everything lines up nice max_first = max([len(x[0]) for x in output]) # create a dict keyed by domain with each record @@ -143,10 +147,10 @@ def do_bind_export(session, token, domain_id, outfile): outstr = '' - # first output SOA then get rid of it - outstr += ("%-*s\t%s\n\n" % - (max_first+1, '@', '\t'.join(out_dict['@'][0]) )) - del(out_dict['@']) + # first output comments and SOA from original + for line in bind_output[:10]: + outstr += "%s\n" % line + outstr += '\n' # print out the rest of the entries, with individual records # sorted and grouped