2020-09-02 22:04:12 +00:00
|
|
|
"""Outpost websocket handler"""
|
|
|
|
from dataclasses import asdict, dataclass, field
|
2020-10-14 08:44:17 +00:00
|
|
|
from datetime import datetime
|
2020-09-02 22:04:12 +00:00
|
|
|
from enum import IntEnum
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from dacite import from_dict
|
|
|
|
from dacite.data import Data
|
2020-09-26 18:11:04 +00:00
|
|
|
from guardian.shortcuts import get_objects_for_user
|
2020-09-02 22:04:12 +00:00
|
|
|
from structlog import get_logger
|
|
|
|
|
2020-09-26 18:11:04 +00:00
|
|
|
from passbook.core.channels import AuthJsonConsumer
|
2020-10-14 08:44:17 +00:00
|
|
|
from passbook.outposts.models import OUTPOST_HELLO_INTERVAL, Outpost, OutpostState
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
LOGGER = get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
class WebsocketMessageInstruction(IntEnum):
|
|
|
|
"""Commands which can be triggered over Websocket"""
|
|
|
|
|
|
|
|
# Simple message used by either side when a message is acknowledged
|
|
|
|
ACK = 0
|
|
|
|
|
|
|
|
# Message used by outposts to report their alive status
|
|
|
|
HELLO = 1
|
|
|
|
|
|
|
|
# Message sent by us to trigger an Update
|
|
|
|
TRIGGER_UPDATE = 2
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class WebsocketMessage:
|
|
|
|
"""Complete Websocket Message that is being sent"""
|
|
|
|
|
|
|
|
instruction: int
|
|
|
|
args: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
|
|
|
2020-09-26 18:11:04 +00:00
|
|
|
class OutpostConsumer(AuthJsonConsumer):
|
2020-09-02 22:04:12 +00:00
|
|
|
"""Handler for Outposts that connect over websockets for health checks and live updates"""
|
|
|
|
|
|
|
|
outpost: Outpost
|
|
|
|
|
|
|
|
def connect(self):
|
2020-09-26 18:11:04 +00:00
|
|
|
if not super().connect():
|
|
|
|
return
|
2020-09-02 22:04:12 +00:00
|
|
|
uuid = self.scope["url_route"]["kwargs"]["pk"]
|
2020-09-26 18:11:04 +00:00
|
|
|
outpost = get_objects_for_user(
|
|
|
|
self.user, "passbook_outposts.view_outpost"
|
|
|
|
).filter(pk=uuid)
|
2020-09-02 22:04:12 +00:00
|
|
|
if not outpost.exists():
|
|
|
|
self.close()
|
|
|
|
return
|
|
|
|
self.accept()
|
|
|
|
self.outpost = outpost.first()
|
2020-10-14 08:44:17 +00:00
|
|
|
OutpostState(
|
|
|
|
uid=self.channel_name, last_seen=datetime.now(), _outpost=self.outpost
|
2020-10-14 10:15:40 +00:00
|
|
|
).save(timeout=OUTPOST_HELLO_INTERVAL * 1.5)
|
2020-10-14 08:44:17 +00:00
|
|
|
LOGGER.debug("added channel to cache", channel_name=self.channel_name)
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def disconnect(self, close_code):
|
2020-10-14 08:44:17 +00:00
|
|
|
OutpostState.for_channel(self.outpost, self.channel_name).delete()
|
|
|
|
LOGGER.debug("removed channel from cache", channel_name=self.channel_name)
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
def receive_json(self, content: Data):
|
|
|
|
msg = from_dict(WebsocketMessage, content)
|
2020-10-14 08:44:17 +00:00
|
|
|
state = OutpostState(
|
|
|
|
uid=self.channel_name,
|
|
|
|
last_seen=datetime.now(),
|
|
|
|
_outpost=self.outpost,
|
|
|
|
)
|
2020-09-02 22:04:12 +00:00
|
|
|
if msg.instruction == WebsocketMessageInstruction.HELLO:
|
2020-10-14 08:44:17 +00:00
|
|
|
state.version = msg.args.get("version", None)
|
2020-09-02 22:04:12 +00:00
|
|
|
elif msg.instruction == WebsocketMessageInstruction.ACK:
|
|
|
|
return
|
2020-10-14 10:15:40 +00:00
|
|
|
state.save(timeout=OUTPOST_HELLO_INTERVAL * 1.5)
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
response = WebsocketMessage(instruction=WebsocketMessageInstruction.ACK)
|
|
|
|
self.send_json(asdict(response))
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def event_update(self, event):
|
2020-10-14 08:44:17 +00:00
|
|
|
"""Event handler which is called by post_save signals, Send update instruction"""
|
2020-09-02 22:04:12 +00:00
|
|
|
self.send_json(
|
|
|
|
asdict(
|
|
|
|
WebsocketMessage(instruction=WebsocketMessageInstruction.TRIGGER_UPDATE)
|
|
|
|
)
|
|
|
|
)
|