mysql_failover.py 1.08 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import yaml
import sys
import os
 


def get_db(key):
    return key[:key.rfind('_')]  

def get_extra_vars( orig_master_ip, new_master_ip, az = 'aws',):

  extra_vars = {}
  group_var_file = "%s/inventories/%s/group_vars/all.yml" %(os.getcwd(),az)

  with open(group_var_file, 'r') as stream:
        g_vars = yaml.safe_load(stream)
         # find db key
        dbs = []
        for k, v in g_vars.items():
             if k.startswith('db') and v == orig_master_ip:
                dbs.append(get_db(k))              
        
        # find all key need to replace
        for k, v in g_vars.items():
            if get_db(k) in dbs:
                extra_vars[k] = new_master_ip
        
        return extra_vars


if __name__ == '__main__':

    # read arg
    orig_master_ip = sys.argv[1]
    new_master_ip = sys.argv[2]
    az = sys.argv[3]

    msg = ''
    extra_vars = get_extra_vars(orig_master_ip, new_master_ip, az)
    for key, value in extra_vars.items():
        msg = msg + " "  + key + "=" + value
    print(msg.strip())
    sys.exit(0)