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


import os
import sys
sys.path.append('../')
import json
import argparse
from switch_lb import LBSwitch
import re
import ConfigParser
from vars import vars_maker
from db import db_handler
from cobar import cobar_handler
from clb import clb_handler
from dns import dns_handler



class switch_handler():
    def __init__(self,config_file='switch.ini'):
        config = ConfigParser.ConfigParser()
        config.readfp(open(config_file))
        
        #Load Base params
        self.project_home = config.get('base','project_home').rstrip('/')
        self.key_file = config.get('base','key_file')
        self.zone = config.get('base','zone')

        #Load DB params
        self.db_playbook = '{0}/playbooks/yoho.switch-db.yml'.format(self.project_home)

        #Load Cobar params
        self.passwd = config.get('cobar','passwd')
        self.cobar_playbook = '{0}/playbooks/yoho.reload-cobar.yml'.format(self.project_home)

        #Load CLB params
        self.secretId = config.get('qcloud','secretId')
        self.secretKey = config.get('qcloud','secretKey')
        self.az1_lb_id = config.get('qcloud','az1_lb_id')
        self.az2_lb_id = config.get('qcloud','az2_lb_id')
        self.az3_lb_id = config.get('qcloud','az3_lb_id')
        self.az1_innerlb_id = config.get('qcloud','az1_innerlb_id')

        #Load DNS params
        self.zone1_yml = '{0}/inventories/az1/group_vars/all.yml'.format(self.project_home)
        self.zone2_yml = '{0}/inventories/az2/group_vars/all.yml'.format(self.project_home)
        self.zone3_yml = '{0}/inventories/az3/group_vars/all.yml'.format(self.project_home)
        self.zone1_tmp_yml = '{0}/inventories/az1/group_vars/.switch.yml.tmp'.format(self.project_home)
        self.zone2_tmp_yml = '{0}/inventories/az2/group_vars/.switch.yml.tmp'.format(self.project_home)
        self.zone3_tmp_yml = '{0}/inventories/az3/group_vars/.switch.yml.tmp'.format(self.project_home)
        self.az1_vars = open(self.zone1_yml).read().strip('\n').split('\n')
        self.az2_vars = open(self.zone2_yml).read().strip('\n').split('\n')
        self.az3_vars = open(self.zone3_yml).read().strip('\n').split('\n')
        self.dns_playbook = '{0}/playbooks/yoho.dns.yml'.format(self.project_home)


    def run(self):
        os.chdir(self.project_home)
        varsMarker = vars_maker(self.az1_vars,self.az2_vars,self.az3_vars,self.zone1_tmp_yml,self.zone2_tmp_yml,self.zone3_tmp_yml)
        dbHandler = db_handler(self.zone,self.project_home,self.db_playbook,self.key_file)
        dnsHandler = dns_handler(self.zone,self.project_home,self.dns_playbook,self.key_file)
        cobarHandler = cobar_handler(self.zone,self.project_home,self.cobar_playbook,self.key_file,self.passwd)
        clbHandler = clb_handler(self.secretId,self.secretKey,self.az1_lb_id,self.az2_lb_id,self.az3_lb_id,self.az1_innerlb_id,self.zone)
        
        #Switch order: vars, db, dns, cobar, clb
        #if varsMarker.start() and dbHandler.start() and dnsHandler.start() and cobarHandler.start() and clbHandler.start():
        if varsMarker.start() and dbHandler.start() and dnsHandler.start():
            print '\033[1;32m双中心切换任务全部正常结束\033[0m' 

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--config',type=str,help='Center switch configuration file path')
    args = parser.parse_args()
    switchHandler = switch_handler(config_file=args.config)
    switchHandler.run()