dns.py
1.93 KB
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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn
import os
import sys
import json
import argparse
import re
class dns_handler():
def __init__(self,zone,project_home,dns_playbook,key_file):
self.zone = zone
self.rex_failed_count = re.compile(r'failed=(\d+)')
self.rex_unreachable_count = re.compile(r'unreachable=(\d+)')
self.project_home = project_home
self.dns_playbook = dns_playbook
self.key_file = key_file
def start(self):
print '\033[1;32m开始执行DNS双中心切换...\033[0m'
if self.zone == 'zone1':
for zoneNum in [1,2]:
cmd = 'ansible-playbook -i {0}/inventories/az{1}/hosts {2} -e confirmation=DNS --key-file={3}'.format(self.project_home,zoneNum,self.dns_playbook,self.key_file)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0:
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
return False
elif self.zone == 'zone2':
for zoneNum in [1,2]:
extra_vars = '{0}/inventories/az{1}/group_vars/.switch.yml.tmp'.format(self.project_home,zoneNum)
cmd = 'ansible-playbook -i {0}/inventories/az{1}/hosts {2} -e "@{3}" -e confirmation=DNS --key-file={4}'.format(self.project_home,zoneNum,self.dns_playbook,extra_vars,self.key_file)
print '\033[1;32m{0}\033[0m'.format(cmd)
rsp = os.popen(cmd).read()
print rsp
if int(max(self.rex_failed_count.findall(rsp)))>0 or int(max(self.rex_unreachable_count.findall(rsp)))>0:
print '\033[1;31mPlaybook执行出现错误,后续操作已中断\033[0m'
return False
return True