コード例 #1
0
def clear_brokers(customer_id):
    customer_id = global_id.latest_glob_id(customer_id)
    service_dir = settings.ServiceDir('service_private_groups')
    brokers_dir = os.path.join(service_dir, 'brokers')
    customer_dir = os.path.join(brokers_dir, customer_id)
    known_brokers(customer_id, erase_brokers=True)
    if os.path.isdir(customer_dir):
        bpio.rmdir_recursive(customer_dir, ignore_errors=True)
コード例 #2
0
def known_brokers(customer_id=None, erase_brokers=False):
    global _KnownBrokers
    if not customer_id:
        return _KnownBrokers
    customer_id = global_id.latest_glob_id(customer_id)
    if erase_brokers:
        return _KnownBrokers.pop(customer_id, None)
    if customer_id not in _KnownBrokers:
        _KnownBrokers[customer_id] = [None, ] * REQUIRED_BROKERS_COUNT
    if len(_KnownBrokers[customer_id]) < REQUIRED_BROKERS_COUNT:
        _KnownBrokers[customer_id] += [None, ] * (REQUIRED_BROKERS_COUNT - len(_KnownBrokers[customer_id]))
    return _KnownBrokers[customer_id]
コード例 #3
0
def set_broker(customer_id, broker_id, position=0):
    customer_id = global_id.latest_glob_id(customer_id)
    broker_id = global_id.latest_glob_id(broker_id)
    service_dir = settings.ServiceDir('service_private_groups')
    brokers_dir = os.path.join(service_dir, 'brokers')
    customer_dir = os.path.join(brokers_dir, customer_id)
    broker_path = os.path.join(customer_dir, broker_id)
    if not os.path.isdir(customer_dir):
        bpio._dirs_make(customer_dir)
    if os.path.isfile(broker_path):
        if _Debug:
            lg.dbg(_DebugLevel, 'broker %r already exist for customer %r, overwriting' % (broker_id, customer_id, ))
    broker_info = {
        'position': position,
    }
    prev_borker_id = known_brokers(customer_id)[position]
    if prev_borker_id:
        if prev_borker_id == broker_id:
            if _Debug:
                lg.args(_DebugLevel, customer_id=customer_id, position=position, broker_id=broker_id, prev_borker_id=prev_borker_id)
            return True
        prev_broker_path = os.path.join(customer_dir, prev_borker_id)
        if os.path.isfile(prev_broker_path):
            lg.info('replacing existing broker for customer %r at position %d : %r -> %r' % (
                customer_id, position, prev_borker_id, broker_id, ))
            try:
                os.remove(prev_broker_path)
            except:
                lg.exc()
                return False
    if not local_fs.WriteTextFile(broker_path, jsn.dumps(broker_info)):
        lg.err('failed to set broker %r at position %d for customer %r' % (broker_id, position, customer_id, ))
        return False
    known_brokers(customer_id)[position] = broker_id
    if _Debug:
        lg.args(_DebugLevel, customer_id=customer_id, broker_id=broker_id, broker_info=broker_info)
    return True
コード例 #4
0
def clear_broker(customer_id, position):
    customer_id = global_id.latest_glob_id(customer_id)
    service_dir = settings.ServiceDir('service_private_groups')
    brokers_dir = os.path.join(service_dir, 'brokers')
    customer_dir = os.path.join(brokers_dir, customer_id)
    if not os.path.isdir(customer_dir):
        if _Debug:
            lg.args(_DebugLevel, customer_id=customer_id, position=position)
        return False
    to_be_erased = []
    for broker_id in os.listdir(customer_dir):
        broker_path = os.path.join(customer_dir, broker_id)
        broker_info = jsn.loads_text(local_fs.ReadTextFile(broker_path))
        if not broker_info:
            to_be_erased.append(broker_id)
            lg.warn('found empty broker info for customer %r : %r' % (customer_id, broker_id, ))
            continue
        if broker_info.get('position') != position:
            continue
        to_be_erased.append(broker_id)
    if not to_be_erased:
        if _Debug:
            lg.args(_DebugLevel, customer_id=customer_id, position=position, to_be_erased=to_be_erased)
        return False
    removed = []
    for broker_id in to_be_erased:
        broker_path = os.path.join(customer_dir, broker_id)
        if os.path.isfile(broker_path):
            try:
                os.remove(broker_path)
            except:
                lg.exc()
                continue
        removed.append(broker_path)
    if _Debug:
        lg.args(_DebugLevel, customer_id=customer_id, position=position, removed=removed)
    return True
コード例 #5
0
def load_groups():
    loaded_brokers = 0
    loaded_groups = 0
    service_dir = settings.ServiceDir('service_private_groups')
    groups_dir = os.path.join(service_dir, 'groups')
    if not os.path.isdir(groups_dir):
        bpio._dirs_make(groups_dir)
    brokers_dir = os.path.join(service_dir, 'brokers')
    if not os.path.isdir(brokers_dir):
        bpio._dirs_make(brokers_dir)
    for group_key_id in os.listdir(groups_dir):
        latest_group_key_id = my_keys.latest_key_id(group_key_id)
        latest_group_path = os.path.join(groups_dir, latest_group_key_id)
        if latest_group_key_id != group_key_id:
            lg.info('going to rename rotated group key: %r -> %r' % (group_key_id, latest_group_key_id, ))
            old_group_path = os.path.join(groups_dir, group_key_id)
            try:
                os.rename(old_group_path, latest_group_path)
            except:
                lg.exc()
                continue
        latest_group_info = jsn.loads_text(local_fs.ReadTextFile(latest_group_path))
        if not latest_group_info:
            lg.err('was not able to load group info from %r' % latest_group_path)
            continue
        active_groups()[latest_group_key_id] = latest_group_info
        loaded_groups += 1
    for customer_id in os.listdir(brokers_dir):
        latest_customer_id = global_id.latest_glob_id(customer_id)
        latest_customer_dir = os.path.join(brokers_dir, latest_customer_id)
        if latest_customer_id != customer_id:
            lg.info('going to rename rotated customer id: %r -> %r' % (customer_id, latest_customer_id, ))
            old_customer_dir = os.path.join(brokers_dir, customer_id)
            try:
                bpio.move_dir_recursive(old_customer_dir, latest_customer_dir)
                bpio.rmdir_recursive(old_customer_dir)
            except:
                lg.exc()
                continue
        for broker_id in os.listdir(latest_customer_dir):
            if latest_customer_id not in known_brokers():
                known_brokers(latest_customer_id)
            latest_broker_id = global_id.latest_glob_id(broker_id)
            latest_broker_path = os.path.join(latest_customer_dir, latest_broker_id)
            if latest_broker_id != broker_id:
                lg.info('going to rename rotated broker id: %r -> %r' % (broker_id, latest_broker_id, ))
                old_broker_path = os.path.join(latest_customer_dir, broker_id)
                try:
                    os.rename(old_broker_path, latest_broker_path)
                except:
                    lg.exc()
                    continue
            latest_broker_info = jsn.loads_text(local_fs.ReadTextFile(latest_broker_path))
            if not latest_broker_info:
                lg.err('was not able to load broker info from %r' % latest_broker_path)
                continue
            existing_broker_id = known_brokers(latest_customer_id)[int(latest_broker_info['position'])]
            if existing_broker_id:
                if os.path.isfile(latest_broker_path):
                    lg.err('found duplicated broker for customer %r on position %d, erasing file %r' % (
                        latest_customer_id, int(latest_broker_info['position']), latest_broker_path, ))
                    try:
                        os.remove(latest_broker_path)
                    except:
                        lg.exc()
                continue
            known_brokers()[latest_customer_id][int(latest_broker_info['position'])] = latest_broker_id
            loaded_brokers += 1
    if _Debug:
        lg.args(_DebugLevel, loaded_groups=loaded_groups, loaded_brokers=loaded_brokers)