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


import os
import sys
import json
import argparse
import re


class cobar_handler():
    def __init__(self,zone,project_home,cobar_playbook,key_file,passwd):
        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.cobar_playbook = cobar_playbook
        self.key_file = key_file
        self.passwd = passwd

    def start(self):
        print '\033[1;32m重载cobar中间件...\033[0m'
        for zoneNum in [1,2]:
            cmd = 'ansible-playbook -i {0}/inventories/az{1}/hosts {2} -e passwd={3} -e confirmation=COBAR --key-file={4} --check'.format(self.project_home,zoneNum,self.cobar_playbook,self.passwd,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