Пример #1
0
def get_com_select_persed():
    all_cluster = Cluster.gets_all()
    cluster_status = {}
    for cluster in all_cluster:
        cursor = cluster.get_node(cluster.name + '_m').get_cursor()
        sql = ("select variable_value "
                "from information_schema.GLOBAL_STATUS "
                "where variable_name in (%s)")
        cursor.execute(sql,('Com_select',))
        cluster_status[cluster.name] = cursor.fetchone()[0]
    return cluster_status
def main():
    logging.basicConfig()
    logger = logging.getLogger(__name__)

    parser = ArgumentParser()
    parser.add_argument('--vip', required=True)
    args = parser.parse_args()


#     all_cluster = Cluster.gets_all()
#     failover_farm = []
#     for cluster in all_cluster:
#         if [i.roles for i in cluster.nodes if len(i.roles)>=2]:
#             failover_farm.append[cluster.name]
#     if failover_farm:
#         logger.error('Farm: %s have different roles on the same physical host!',
#                      ','.join(failover_farm))
#         prompt = 'Push dmkit config changes to zk: %s ? NO(default)/YES? ' % node

    distribution = yaml.load(open(DIS_CONF_FILE))
    if args.vip not in distribution:
        logger.error('The specified vip: %s is not right.', args.vip)
        return

    # outfile = 'outfile_%s_%s' % (args.vip, datetime.now().strftime('%Y%m%d%H%M%S'))
    outfile = 'outfile_%s' % (args.vip,)
    vip_dict = distribution[args.vip]
    vip = args.vip
    vip_tag = '%s_%s' % (vip.split('.')[-2], vip.split('.')[-1])
    vip_route_id = vip.split('.')[-1]

    f = open(outfile, 'w')
    f.write(header_t.substitute(vip=vip, vip_tag=vip_tag))
    f.write(vrrp_instance_t.substitute(vip=vip, vip_tag=vip_tag, vip_route_id=vip_route_id))

    for role in ['m', 's', 'b', 'h']:
        for farm in vip_dict[role]:
            cluster = Cluster.get_by_name(farm)
            if not cluster:
                logger.error('The farm: %s is not exists!', farm)
                return
            port = cluster.port
            f.write(virtual_server_begin_t.substitute(vip=vip, port=port))
            for node in cluster.nodes:
                if role in ['m', 's']:
                    if 'm' not in node.roles and 's' in node.roles:
                        continue
                elif role == 'b':
                    if 'h' in node.roles:
                        continue
                elif role == 'h':
                    if 'h' not in node.roles:
                        continue

                f.write(real_server_t.substitute(hostname=node.host,
                                                 farm=farm,
                                                 role=role,
                                                 port=port,
                                                 rs_ip=node.ip
                                                 )
                        )
            f.write(terminator)

    f.close()
    logger.info('Keepalived config of vip: %s has generated to %s', vip, outfile)