db.py 1.18 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn


import os
import sys
import json
import argparse
import re


class db_handler():
    def __init__(self,zone,project_home,db_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.db_playbook = db_playbook
        self.key_file = key_file


    def start(self):
        print '\033[1;32m开始执行数据库双中心切换...\033[0m'
        db_mgmt = 'db-mgmt-zone1' if self.zone == 'zone1' else 'db-mgmt-zone2'
        cmd = 'ansible-playbook -i {0}/inventories/az1/hosts {1} -e center={2} -e db_mgmt={3} -e confirmation=DB --key-file={4}'.format(self.project_home,self.db_playbook,self.zone,db_mgmt,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