2020-09-02 22:04:12 +00:00
|
|
|
"""passbook outposts app config"""
|
|
|
|
from importlib import import_module
|
2020-11-04 09:41:18 +00:00
|
|
|
from os import R_OK, access
|
|
|
|
from os.path import expanduser
|
|
|
|
from pathlib import Path
|
|
|
|
from socket import gethostname
|
|
|
|
from urllib.parse import urlparse
|
2020-09-02 22:04:12 +00:00
|
|
|
|
2020-11-04 12:01:38 +00:00
|
|
|
import yaml
|
2020-09-02 22:04:12 +00:00
|
|
|
from django.apps import AppConfig
|
2020-11-04 09:41:18 +00:00
|
|
|
from django.db import ProgrammingError
|
|
|
|
from docker.constants import DEFAULT_UNIX_SOCKET
|
|
|
|
from kubernetes.config.incluster_config import SERVICE_TOKEN_FILENAME
|
|
|
|
from kubernetes.config.kube_config import KUBE_CONFIG_DEFAULT_LOCATION
|
|
|
|
from structlog import get_logger
|
|
|
|
|
|
|
|
LOGGER = get_logger()
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PassbookOutpostConfig(AppConfig):
|
|
|
|
"""passbook outposts app config"""
|
|
|
|
|
|
|
|
name = "passbook.outposts"
|
|
|
|
label = "passbook_outposts"
|
|
|
|
mountpoint = "outposts/"
|
|
|
|
verbose_name = "passbook Outpost"
|
|
|
|
|
|
|
|
def ready(self):
|
|
|
|
import_module("passbook.outposts.signals")
|
2020-11-04 09:41:18 +00:00
|
|
|
try:
|
|
|
|
self.init_local_connection()
|
2020-11-04 09:54:44 +00:00
|
|
|
except ProgrammingError:
|
2020-11-04 09:41:18 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def init_local_connection(self):
|
2020-11-04 09:54:44 +00:00
|
|
|
"""Check if local kubernetes or docker connections should be created"""
|
2020-11-04 09:41:18 +00:00
|
|
|
from passbook.outposts.models import (
|
|
|
|
KubernetesServiceConnection,
|
|
|
|
DockerServiceConnection,
|
|
|
|
)
|
|
|
|
|
|
|
|
if Path(SERVICE_TOKEN_FILENAME).exists():
|
|
|
|
LOGGER.debug("Detected in-cluster Kubernetes Config")
|
|
|
|
if not KubernetesServiceConnection.objects.filter(local=True).exists():
|
|
|
|
LOGGER.debug("Created Service Connection for in-cluster")
|
|
|
|
KubernetesServiceConnection.objects.create(
|
2020-11-04 12:01:38 +00:00
|
|
|
name="Local Kubernetes Cluster", local=True, kubeconfig={}
|
2020-11-04 09:41:18 +00:00
|
|
|
)
|
|
|
|
# For development, check for the existence of a kubeconfig file
|
|
|
|
kubeconfig_path = expanduser(KUBE_CONFIG_DEFAULT_LOCATION)
|
|
|
|
if Path(kubeconfig_path).exists():
|
|
|
|
LOGGER.debug("Detected kubeconfig")
|
2020-11-04 12:01:38 +00:00
|
|
|
kubeconfig_local_name = f"k8s-{gethostname()}"
|
2020-11-04 09:41:18 +00:00
|
|
|
if not KubernetesServiceConnection.objects.filter(
|
2020-11-04 12:01:38 +00:00
|
|
|
name=kubeconfig_local_name
|
2020-11-04 09:41:18 +00:00
|
|
|
).exists():
|
|
|
|
LOGGER.debug("Creating kubeconfig Service Connection")
|
|
|
|
with open(kubeconfig_path, "r") as _kubeconfig:
|
|
|
|
KubernetesServiceConnection.objects.create(
|
2020-11-04 12:01:38 +00:00
|
|
|
name=kubeconfig_local_name,
|
|
|
|
kubeconfig=yaml.safe_load(_kubeconfig),
|
2020-11-04 09:41:18 +00:00
|
|
|
)
|
|
|
|
unix_socket_path = urlparse(DEFAULT_UNIX_SOCKET).path
|
|
|
|
socket = Path(unix_socket_path)
|
|
|
|
if socket.exists() and access(socket, R_OK):
|
|
|
|
LOGGER.debug("Detected local docker socket")
|
|
|
|
if not DockerServiceConnection.objects.filter(local=True).exists():
|
|
|
|
LOGGER.debug("Created Service Connection for docker")
|
|
|
|
DockerServiceConnection.objects.create(
|
|
|
|
name="Local Docker connection",
|
|
|
|
local=True,
|
|
|
|
url=unix_socket_path,
|
|
|
|
)
|