Exemplo n.º 1
0
def edit_host(user_id, host_id, hostname, servs):
    """Edits the given host."""
    user_id = str(user_id)
    host_id = str(host_id)
    if host_id in red.smembers(_KEY_USER_HOSTS.format(user_id)):
        pip = red.pipeline()
        pip.hset(_KEY_HOST.format(host_id), _ATTR_HOST_NAME, hostname)
        saved_servs = red.smembers(_KEY_HOST_SET_SERVS.format(host_id))
        # Remove all
        pip.delete(_KEY_HOST_SET_SERVS.format(host_id))
        for serv in saved_servs:
            if red.scard(_KEY_SERVNAME_HOSTS.format(serv)) == 1:
                pip.delete(_KEY_SERVNAME_HOSTS.format(serv))
                pip.srem(_KEY_SERVS, serv)
            else:
                pip.srem(_KEY_SERVNAME_HOSTS.format(serv), host_id)
        # create new associated servs
        for serv in servs:
            pip.sadd(_KEY_HOST_SET_SERVS.format(host_id), serv)
            pip.sadd(_KEY_SERVNAME_HOSTS.format(serv), host_id)
            pip.sadd(_KEY_SERVS, serv)
        pip.execute()
        return (True, 'Everything went fine!')
    else:
        return (False, 'Unknown host for the given user.')
Exemplo n.º 2
0
def delete_network(user_id, net_id):
    """Deletes a network."""
    pip = red.pipeline()
    user_id = str(user_id)
    net_id = str(net_id)
    pip.delete(_KEY_NET.format(net_id))
    pip.srem(_KEY_NETS_USER.format(user_id), net_id)
    # Delete associated events
    events = get_user_events_network(user_id, net_id)
    for event in events:
        pip.lrem(_KEY_EVENTS_USER_DATE.format(user_id, event['date']), 0,
                 event['id'])
        pip.delete(_KEY_EVENT_USER.format(event['id'], user_id))
    pip.delete(_KEY_EVENTS_USER_NET.format(user_id, net_id)) # user-network
    # Delete associated entries on wb lists
    for entry in get_entries_network(net_id):
        # delete network from the entry, if it has one network delete entry
        if red.scard(_KEY_ENTRY_SET_NETS.format(entry['id'])) == 1:
            pip.delete(_KEY_ENTRY.format(entry['id']))
            # delete from w or b list of entries
            if entry['type'] == 'B':
                pip.srem(_KEY_ENTRY_BLACK_USER.format(user_id), entry['id'])
            else:
                pip.srem(_KEY_ENTRY_WHITE_USER.format(user_id), entry['id'])
        pip.srem(_KEY_ENTRY_SET_NETS.format(entry['id']), 0, net_id)
    pip.execute()
    # Search for associated api keys to this network
    apik_ids = red.smembers(_KEY_APIKS_USER.format(user_id))
    for apik in apik_ids:
        tmp = red.hgetall(_KEY_APIK.format(apik))
        if tmp and tmp['network'] == net_id:
            _delete_api_key(user_id, apik)
Exemplo n.º 3
0
def clean_api_key(key_id):
    """Cleans an api key."""
    key_id = str(key_id)
    tmp = red.hgetall(_KEY_APIK.format(key_id))
    red.delete(_KEY_BUOYACTION.format(key_id))
    red.delete(_KEY_UUID_APIK.format(tmp['key']))
    pip = red.pipeline()
    pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_KEY, '')
    pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_GENERATED, '')
    pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_STATUS, const.BUOY_NOTDEP)
    pip.execute()
Exemplo n.º 4
0
def set_host(user_id, hostname, servs):
    """Saves a host"""
    key = _get_key_host()
    user_id = str(user_id)
    pip = red.pipeline()
    pip.sadd(_KEY_USER_HOSTS.format(user_id), key)
    pip.hset(_KEY_HOST.format(key), _ATTR_HOST_NAME, hostname)
    for serv in servs:
        pip.sadd(_KEY_HOST_SET_SERVS.format(key), serv)
        pip.sadd(_KEY_SERVNAME_HOSTS.format(serv), key)
        pip.sadd(_KEY_SERVS, serv)
    pip.execute()
Exemplo n.º 5
0
def generate_api_key(key_id):
    """Generates an API key, set's staus to stopped."""
    key_id = str(key_id)
    new_key = uuid.uuid4()
    time = datetime.datetime.now()
    pip = red.pipeline()
    pip.set(_KEY_UUID_APIK.format(new_key), key_id)
    pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_KEY, new_key)
    pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_GENERATED,
             time.strftime(const.STRTIME_KEY_GENERATED))
    pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_STATUS, const.BUOY_STOPPED)
    pip.execute()
    set_action_buoy(key_id, const.BUOY_AC_STOP)
Exemplo n.º 6
0
def save_event(user_id, net_id, event_desc, date, day, time, priority):
    """Saves an event."""
    key = _get_key_event()
    pipe = red.pipeline()
    pipe.lpush(_KEY_EVENTS_USER_DATE.format(user_id, date), key)
    pipe.lpush(_KEY_EVENTS_USER_NET.format(user_id, net_id), key)
    pipe.hset(_KEY_EVENT_USER.format(key, user_id), _ATTR_EVENT_DESC,
              event_desc)
    pipe.hset(_KEY_EVENT_USER.format(key, user_id), _ATTR_EVENT_NET, net_id)
    pipe.hset(_KEY_EVENT_USER.format(key, user_id), _ATTR_EVENT_DATE, date)
    pipe.hset(_KEY_EVENT_USER.format(key, user_id), _ATTR_EVENT_DAY, day)
    pipe.hset(_KEY_EVENT_USER.format(key, user_id), _ATTR_EVENT_TIME, time)
    pipe.hset(_KEY_EVENT_USER.format(key, user_id), _ATTR_EVENT_PRIO, priority)
    pipe.execute()
Exemplo n.º 7
0
def set_api_key(userid, netid, apikey, generated_at, status):
    """Saves an api key."""
    userid = str(userid)
    netid = str(netid)
    key = _get_key_api()
    pip = red.pipeline()
    pip.sadd(_KEY_APIKS_USER.format(userid), key)
    pip.hset(_KEY_APIK.format(key), _ATTR_APIK_USER, userid)
    pip.hset(_KEY_APIK.format(key), _ATTR_APIK_KEY, apikey)
    pip.hset(_KEY_APIK.format(key), _ATTR_APIK_NET, netid)
    pip.hset(_KEY_APIK.format(key), _ATTR_APIK_GENERATED, generated_at)
    pip.hset(_KEY_APIK.format(key), _ATTR_APIK_STATUS, status)
    pip.hset(_KEY_APIK.format(key), _ATTR_APIK_LASTHOST, '')
    pip.hset(_KEY_APIK.format(key), _ATTR_APIK_LASTSCAND, '')
    pip.execute()
Exemplo n.º 8
0
def _delete_api_key(user_id, apik_id):
    """Deletes an api key."""
    user_id = str(user_id)
    apik_id = str(apik_id)
    # Delete uuid to key
    tmp = red.hgetall(_KEY_APIK.format(apik_id))
    red.delete(_KEY_UUID_APIK.format(tmp['key']))
    pip = red.pipeline()
    if pip.scard(_KEY_APIKS_USER.format(user_id)) == 1:
        pip.delete(_KEY_APIKS_USER.format(user_id))
    else:
        pip.srem(_KEY_APIKS_USER.format(user_id), apik_id)
    pip.delete(_KEY_APIK.format(apik_id))
    pip.execute()
    # delete also the orders a buoy must perform
    red.delete(_KEY_BUOYACTION.format(apik_id))
Exemplo n.º 9
0
def delete_host(user_id, host_id):
    """Deletes a host"""
    user_id = str(user_id)
    host_id = str(host_id)
    pip = red.pipeline()
    pip.srem(_KEY_USER_HOSTS.format(user_id), host_id)
    pip.delete(_KEY_HOST.format(host_id))
    servs = red.smembers(_KEY_HOST_SET_SERVS.format(host_id))
    for serv in servs:
        if red.scard(_KEY_SERVNAME_HOSTS.format(serv)) == 1:
            pip.delete(_KEY_SERVNAME_HOSTS.format(serv))
            pip.srem(_KEY_SERVS, serv)
        else:
            pip.srem(_KEY_SERVNAME_HOSTS.format(serv), host_id)
    red.delete(_KEY_HOST_SET_SERVS.format(host_id))
    pip.execute()
Exemplo n.º 10
0
def set_network(user, name, iface, haddress, speed, sec, address, submask,
                defroute, dns1, dns2=None):
    """Saves a network."""
    key = _get_key_net()
    pip = red.pipeline()
    pip.sadd(_KEY_NETS_USER.format(str(user)), key)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_NAME, name)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_IFACE, iface)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_HADDR, haddress)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_SPEED, speed)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_SEC, sec)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_ADDR, address)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_MASK, submask)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_DEFR, defroute)
    pip.hset(_KEY_NET.format(key), _ATTR_NET_DNS1, dns1)
    if dns2 != None:
        pip.hset(_KEY_NET.format(key), _ATTR_NET_DNS2, dns2)
    pip.execute()
    set_api_key(user, key, '', '', const.BUOY_NOTDEP)
Exemplo n.º 11
0
def save_entry(user_id, typ, host, mac, addr, nets):
    """Saves and entry."""
    user_id = str(user_id)
    consistent = _is_entry_consistent(user_id, typ, mac, nets)
    if consistent:
        if typ in ['B', 'W']:
            key = _get_key_entry()
            pipe = red.pipeline()
            if typ == 'B':
                pipe.sadd(_KEY_ENTRY_BLACK_USER.format(user_id), key)
            else:
                pipe.sadd(_KEY_ENTRY_WHITE_USER.format(user_id), key)
            pipe.hset(_KEY_ENTRY.format(key), _ATTR_ENTRY_TYPE, typ)
            pipe.hset(_KEY_ENTRY.format(key), _ATTR_ENTRY_HOST, host)
            pipe.hset(_KEY_ENTRY.format(key), _ATTR_ENTRY_MAC, mac)
            pipe.hset(_KEY_ENTRY.format(key), _ATTR_ENTRY_ADDR, addr)
            for net in nets:
                pipe.sadd(_KEY_ENTRY_SET_NETS.format(key), str(net))
                pipe.sadd(_KEY_NET_SET_ENTRIES.format(str(net)), key)
            pipe.execute()
    return consistent