Exemplo n.º 1
0
def change_master(cluster_name, pg_id, host):
    print magenta("\n[%s] Change master, PG:%d" % (host ,pg_id))

    # Get candidates for master
    candidates = []

    pgs_list = cm.get_pgs_list(cluster_name, pg_id)
    for id, data in pgs_list.items():
        if data['smr_role'] == 'S' and data['hb'] == 'Y':
            active_role = get_role_of_smr(data['ip'], data['mgmt_port'], verbose=False)
            if active_role != data['smr_role']:
                warn(red("[%s] Change master fail, PGS '%d' has invalid state. %s(%s)" % 
                          (host, id, data['smr_role'], active_role )))
                return False

            candidates.append(data)

    # Check candidates
    if len(candidates) == 0:
        warn(red("[%s] Change master fail, PG '%d' doesn't have any slave." % (host, pg_id)))
        return False

    pgs_id = random.choice(candidates)['pgs_id']

    master_id = cm.role_change(cluster_name, pgs_id, host)
    if master_id == -1:
        warn(red("[%s] Change master fail." % host))
        return False

    print green("[%s] Change master success, PG:%d, MASTER:%d" % (host, pg_id, master_id))
    return True
Exemplo n.º 2
0
def show_pgs_list(cluster_name, pg_id, print_header, cluster_json=None):
    # Check cluster
    if cluster_json == None:
        cluster_json = cm.cluster_info(cluster_name)
        if cluster_json == None:
            return False

    exist = [k for loop_pg_data in cluster_json['data']['pg_list'] 
                    for k, v in loop_pg_data.iteritems() if k == 'pg_id' and int(v) == pg_id]
    if len(exist) == 0:
        warn(red("PG '%d' doesn't exist." % pg_id))
        return True

    # get specific pg info
    pg = cm.pg_info(cluster_name, pg_id, cluster_json)

    if print_header:
        print yellow(PG_PARTITION[:-1] + PGS_PARTITION)
        print yellow(PG_COLUMN[:-1] + PGS_COLUMN)
        print yellow(PG_PARTITION[:-1] + PGS_PARTITION)

    # get all pgs info
    pgs_list = cm.get_pgs_list(cluster_name, pg_id, cluster_json)  
    if None == pgs_list:
        print yellow('| %-57s |' % ("There is no PGS in PG '%d'." % pg_id))
        print yellow(PG_PARTITION[:-1] + PGS_PARTITION)
        return True

    for id, data in pgs_list.items():
        data['active_role'] = util.get_role_of_smr(data['ip'], data['mgmt_port'], verbose=False)
        if data['active_role'] == 'M':
            data['quorum'] = remote.get_quorum(data)
        else:
            data['quorum'] = ''

    pgs_state = True
    begin = True
    for pgs_id, pgs in sorted(pgs_list.items(), key=lambda x: int(x[0])):
        if pgs['active_role'] == 'M':
            bold = True
        else:
            bold = False

        if begin:
            prefix_pg = PG_FORMAT[:-1] % pg
            begin = False
        else:
            prefix_pg = PG_PREFIX[:-1]

        if pgs['active_role'] != pgs['smr_role']:
            print yellow(prefix_pg[:-1]), magenta(PGS_FORMAT % pgs)
            pgs_state = False
        elif pgs['active_role'] == 'M' or pgs['active_role'] == 'S':
            print yellow(prefix_pg[:-1]), yellow(PGS_FORMAT % pgs, bold)
        else:
            print yellow(prefix_pg[:-1]), red(PGS_FORMAT % pgs)
            pgs_state = False
            
    print yellow(PG_PARTITION[:-1] + PGS_PARTITION)
    return pgs_state
Exemplo n.º 3
0
def change_master(cluster_name, pg_id, host):
    print magenta("\n[%s] Change master, PG:%d" % (host ,pg_id))

    # Get candidates for master
    candidates = []

    pgs_list = cm.get_pgs_list(cluster_name, pg_id)
    for id, data in pgs_list.items():
        if data['smr_role'] == 'S' and data['hb'] == 'Y':
            active_role = get_role_of_smr(data['ip'], data['mgmt_port'], verbose=False)
            if active_role != data['smr_role']:
                warn(red("[%s] Change master fail, PGS '%d' has invalid state. %s(%s)" % 
                          (host, id, data['smr_role'], active_role )))
                return False

            candidates.append(data)

    # Check candidates
    if len(candidates) == 0:
        warn(red("[%s] Change master fail, PG '%d' doesn't have any slave." % (host, pg_id)))
        return False

    pgs_id = random.choice(candidates)['pgs_id']

    master_id = cm.role_change(cluster_name, pgs_id, host)
    if master_id == -1:
        warn(red("[%s] Change master fail." % host))
        return False

    print green("[%s] Change master success, PG:%d, MASTER:%d" % (host, pg_id, master_id))
    return True
Exemplo n.º 4
0
def get_worker_pgs(cluster_name, pg_id, job_id):
    out_pgs_list = cm.get_pgs_list(cluster_name, pg_id) 
    if out_pgs_list.cm == False:
        log_error("%s mgmt-cc error." % BACKUP, job_id)
        return None

    # First, check a slave
    slaves = find_in_map_of_dicts(out_pgs_list.json, "smr_role", "S")
    if len(slaves) > 0:
        return slaves[0]

    # If any slave isn't available, it checks the master
    master = find_in_map_of_dicts(out_pgs_list.json, "smr_role", "M")
    if len(master) > 0:
        return master[0]

    log_error('%s there is no available PGS. JOB_ID:%s, PG_ID:%d' % (BACKUP, job_id, pg_id), job_id)
    return None 
Exemplo n.º 5
0
def get_worker_pgs(cluster_name, pg_id, job_id):
    out_pgs_list = cm.get_pgs_list(cluster_name, pg_id)
    if out_pgs_list.cm == False:
        log_error("%s mgmt-cc error." % BACKUP, job_id)
        return None

    # First, check a slave
    slaves = find_in_map_of_dicts(out_pgs_list.json, "smr_role", "S")
    if len(slaves) > 0:
        return slaves[0]

    # If any slave isn't available, it checks the master
    master = find_in_map_of_dicts(out_pgs_list.json, "smr_role", "M")
    if len(master) > 0:
        return master[0]

    log_error(
        '%s there is no available PGS. JOB_ID:%s, PG_ID:%d' %
        (BACKUP, job_id, pg_id), job_id)
    return None
Exemplo n.º 6
0
def show_pgs_list(cluster_name, pg_id, print_header, cluster_json=None):
    # Check cluster
    if cluster_json == None:
        cluster_json = cm.cluster_info(cluster_name)
        if cluster_json == None:
            return False

    exist = [
        k for loop_pg_data in cluster_json['data']['pg_list']
        for k, v in loop_pg_data.iteritems()
        if k == 'pg_id' and int(v) == pg_id
    ]
    if len(exist) == 0:
        warn(red("PG '%d' doesn't exist." % pg_id))
        return True

    # get specific pg info
    pg = cm.pg_info(cluster_name, pg_id, cluster_json)

    if print_header:
        print yellow(PG_PARTITION[:-1] + PGS_PARTITION)
        print yellow(PG_COLUMN[:-1] + PGS_COLUMN)
        print yellow(PG_PARTITION[:-1] + PGS_PARTITION)

    # get all pgs info
    pgs_list = cm.get_pgs_list(cluster_name, pg_id, cluster_json)
    if None == pgs_list:
        print yellow('| %-57s |' % ("There is no PGS in PG '%d'." % pg_id))
        print yellow(PG_PARTITION[:-1] + PGS_PARTITION)
        return True

    for id, data in pgs_list.items():
        # Get active role
        data['active_role'] = util.get_role_of_smr(data['ip'],
                                                   data['mgmt_port'],
                                                   verbose=False)
        if data['active_role'] == 'M':
            data['quorum'] = remote.get_quorum(data)
        else:
            data['quorum'] = ''

        # Get memlog
        host = config.USERNAME + '@' + data['ip'].encode('ascii')
        with settings(hide('warnings', 'running', 'stdout', 'user'),
                      hosts=[host]):
            data['memlog'] = execute(remote.exist_smr_memlog,
                                     data['smr_base_port'])[host]

    pgs_state = True
    begin = True
    for pgs_id, pgs in sorted(pgs_list.items(), key=lambda x: int(x[0])):
        if pgs['active_role'] == 'M':
            bold = True
        else:
            bold = False

        if begin:
            prefix_pg = PG_FORMAT[:-1] % pg
            begin = False
        else:
            prefix_pg = PG_PREFIX[:-1]

        if pgs['active_role'] != pgs['smr_role']:
            print yellow(prefix_pg[:-1]), magenta(PGS_FORMAT % pgs)
            pgs_state = False
        elif pgs['active_role'] == 'M' or pgs['active_role'] == 'S':
            print yellow(prefix_pg[:-1]), yellow(PGS_FORMAT % pgs, bold)
        else:
            print yellow(prefix_pg[:-1]), red(PGS_FORMAT % pgs)
            pgs_state = False

    print yellow(PG_PARTITION[:-1] + PGS_PARTITION)
    return pgs_state