2020-09-02 22:04:12 +00:00
|
|
|
"""outpost tasks"""
|
2020-09-13 12:29:40 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from asgiref.sync import async_to_sync
|
|
|
|
from channels.layers import get_channel_layer
|
|
|
|
from structlog import get_logger
|
|
|
|
|
|
|
|
from passbook.lib.utils.reflection import path_to_class
|
|
|
|
from passbook.outposts.models import (
|
|
|
|
Outpost,
|
|
|
|
OutpostDeploymentType,
|
|
|
|
OutpostModel,
|
2020-10-14 08:44:17 +00:00
|
|
|
OutpostState,
|
2020-09-13 12:29:40 +00:00
|
|
|
OutpostType,
|
|
|
|
)
|
2020-10-03 22:36:12 +00:00
|
|
|
from passbook.providers.proxy.controllers.docker import ProxyDockerController
|
2020-09-02 22:04:12 +00:00
|
|
|
from passbook.providers.proxy.controllers.kubernetes import ProxyKubernetesController
|
|
|
|
from passbook.root.celery import CELERY_APP
|
|
|
|
|
2020-09-13 12:29:40 +00:00
|
|
|
LOGGER = get_logger()
|
|
|
|
|
2020-09-02 22:04:12 +00:00
|
|
|
|
2020-10-14 08:44:17 +00:00
|
|
|
@CELERY_APP.task()
|
|
|
|
def outpost_controller():
|
2020-10-03 22:36:12 +00:00
|
|
|
"""Launch Controller for all Outposts which support it"""
|
|
|
|
for outpost in Outpost.objects.exclude(
|
|
|
|
deployment_type=OutpostDeploymentType.CUSTOM
|
2020-09-02 22:04:12 +00:00
|
|
|
):
|
2020-10-03 22:36:12 +00:00
|
|
|
outpost_controller_single.delay(
|
|
|
|
outpost.pk.hex, outpost.deployment_type, outpost.type
|
|
|
|
)
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
|
2020-10-14 08:44:17 +00:00
|
|
|
@CELERY_APP.task()
|
|
|
|
def outpost_controller_single(outpost_pk: str, deployment_type: str, outpost_type: str):
|
2020-10-03 22:36:12 +00:00
|
|
|
"""Launch controller and reconcile deployment/service/etc"""
|
2020-09-02 22:04:12 +00:00
|
|
|
if outpost_type == OutpostType.PROXY:
|
2020-10-03 22:36:12 +00:00
|
|
|
if deployment_type == OutpostDeploymentType.KUBERNETES:
|
2020-10-14 08:44:17 +00:00
|
|
|
ProxyKubernetesController(outpost_pk).run()
|
2020-10-03 22:36:12 +00:00
|
|
|
if deployment_type == OutpostDeploymentType.DOCKER:
|
2020-10-14 08:44:17 +00:00
|
|
|
ProxyDockerController(outpost_pk).run()
|
2020-09-13 12:29:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@CELERY_APP.task()
|
|
|
|
def outpost_send_update(model_class: str, model_pk: Any):
|
|
|
|
"""Send outpost update to all registered outposts, irregardless to which passbook
|
|
|
|
instance they are connected"""
|
|
|
|
model = path_to_class(model_class)
|
2020-10-14 08:44:17 +00:00
|
|
|
model_instace = model.objects.get(pk=model_pk)
|
|
|
|
channel_layer = get_channel_layer()
|
|
|
|
if isinstance(model_instace, OutpostModel):
|
|
|
|
for outpost in model_instace.outpost_set.all():
|
|
|
|
_outpost_single_update(outpost, channel_layer)
|
|
|
|
elif isinstance(model_instace, Outpost):
|
|
|
|
_outpost_single_update(model_instace, channel_layer)
|
|
|
|
|
|
|
|
|
|
|
|
def _outpost_single_update(outpost: Outpost, layer=None):
|
|
|
|
"""Update outpost instances connected to a single outpost"""
|
|
|
|
if not layer: # pragma: no cover
|
|
|
|
layer = get_channel_layer()
|
|
|
|
for state in OutpostState.for_outpost(outpost):
|
|
|
|
LOGGER.debug("sending update", channel=state.uid, outpost=outpost)
|
|
|
|
async_to_sync(layer.send)(state.uid, {"type": "event.update"})
|