def start_replica_set(members, auth=False, fresh=True):
    global cur_port, key_file

    if fresh:
        if os.path.exists(dbpath):
            try:
                shutil.rmtree(dbpath)
            except OSError:
                pass

        try:
            os.makedirs(dbpath)
        except __HOLE__ as e:
            print(e)
            print('\tWhile creating %s' % dbpath)

    if auth:
        key_file = os.path.join(dbpath, 'key.txt')
        if not os.path.exists(key_file):
            f = open(key_file, 'wb')
            try:
                f.write(b'my super secret system password')
            finally:
                f.close()
            os.chmod(key_file, S_IRUSR)

    for i in range(len(members)):
        host = '%s:%d' % (hostname, cur_port)
        members[i].update({'_id': i, 'host': host})
        path = os.path.join(dbpath, 'db' + str(i))
        if not os.path.exists(path):
            os.makedirs(path)
        member_logpath = os.path.join(logpath, 'db' + str(i) + '.log')
        if not os.path.exists(os.path.dirname(member_logpath)):
            os.makedirs(os.path.dirname(member_logpath))
        cmd = [mongod,
               '--dbpath', path,
               '--port', str(cur_port),
               '--replSet', set_name,
               '--nojournal', '--oplogSize', '64',
               '--logappend', '--logpath', member_logpath]
        if auth:
            cmd += ['--keyFile', key_file]

        if ha_tools_debug:
            print('starting %s' % ' '.join(cmd))

        proc = start_subprocess(cmd)
        nodes[host] = {'proc': proc, 'cmd': cmd}
        assert wait_for(proc, cur_port)

        cur_port += 1

    config = {'_id': set_name, 'members': members}
    primary = members[0]['host']
    c = pymongo.MongoClient(primary, use_greenlets=use_greenlets)
    try:
        if ha_tools_debug:
            pprint.pprint({'replSetInitiate': config})

        c.admin.command('replSetInitiate', config)
    except pymongo.errors.OperationFailure as e:
        # Already initialized from a previous run?
        if ha_tools_debug:
            print(e)

    expected_arbiters = 0
    for member in members:
        if member.get('arbiterOnly'):
            expected_arbiters += 1
    expected_secondaries = len(members) - expected_arbiters - 1

    # Wait for 4 minutes for replica set to come up.
    patience = 4
    for i in range(int(patience * 60 / 1)):
        time.sleep(1)
        try:
            if (get_primary() and
                    len(get_secondaries()) == expected_secondaries and
                    len(get_arbiters()) == expected_arbiters):
                break
        except pymongo.errors.ConnectionFailure:
            # Keep waiting
            pass

        if ha_tools_debug:
            print('waiting for RS %s' % i)
    else:
        kill_all_members()
        raise Exception(
            "Replica set still not initalized after %s minutes" % patience)
    return primary, set_name
dataset/ETHPy150Open mongodb/motor/test/high_availability/ha_tools.py/start_replica_set