1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 23:04:21 +00:00
sophon/backend/sophon/notebooks/apache.py
Stefano Pigozzi b44f7b4ebf 💥 Dockerize everything! (#74 closes #59)
*  Create Dockerfile for the backend

* 🔧 Limit the allowed slug values

*  Create docker image for the proxy

*  Add serve script (and serve dependency)

*  Add .dockerignore symlinks to .gitignore

*  Create docker image for the frontend

* 🔧 Proxy the frontend

* 🔧 Configure the proxy.dbm directory

* 🚧 WIP

* 💥 Improve settings handling

* 💥 Prepare backend for docker deployment

* 🔧 Reserve `static` notebook slug

*  Make static work

* 🐛 Set a default value for reduce

* 💥 Things

* 💥 Everything works!
2021-10-19 22:41:18 +02:00

74 lines
2.1 KiB
Python

import dbm.gnu
import logging
import os
import pathlib
import socket
import typing as t
from django.conf import settings
log = logging.getLogger(__name__)
class ApacheDB:
def __init__(self, path: t.Union[str, pathlib.Path]):
self.path: pathlib.Path
if isinstance(path, str):
self.path = pathlib.Path(path)
else:
self.path = path
log.debug(f"Initializing directories...")
os.makedirs(self.path.parent, exist_ok=True)
log.debug(f"Initializing database...")
with dbm.open(str(self.path), "c"):
pass
log.debug("Done!")
@staticmethod
def convert_to_bytes(item: t.Union[str, bytes]) -> bytes:
if isinstance(item, str):
log.debug(f"Encoding {item!r} as ASCII...")
item = item.encode("ascii")
return item
def __getitem__(self, key: t.Union[str, bytes]) -> bytes:
key = self.convert_to_bytes(key)
log.debug(f"{self.path}: Getting {key!r}...")
with dbm.open(str(self.path), "r") as adb:
return adb[key]
def __setitem__(self, key: bytes, value: bytes) -> None:
key = self.convert_to_bytes(key)
value = self.convert_to_bytes(value)
log.debug(f"{self.path}: Setting {key!r}{value!r}...")
with dbm.open(str(self.path), "w") as adb:
adb[key] = value
def __delitem__(self, key):
key = self.convert_to_bytes(key)
log.debug(f"{self.path}: Deleting {key!r}...")
with dbm.open(str(self.path), "w") as adb:
del adb[key]
db: ApacheDB = ApacheDB(settings.PROXY_FILE)
def get_ephemeral_port() -> int:
"""
Request a free TCP port from the operating system by opening and immediately closing a TCP socket.
:return: A free port number.
.. warning:: Prone to race conditions, be sure to bind something to the obtained port as soon as it is retrieved!
.. seealso:: https://stackoverflow.com/a/36331860/4334568
"""
sock: socket.socket = socket.socket()
sock.bind(("localhost", 0))
port: int
_, port = sock.getsockname()
return port