Rework so that dockerized deployments can self-configure

This commit is contained in:
Reid 'arrdem' McKenzie 2022-11-26 18:13:56 -07:00
parent 5c69288d48
commit 0db988a754
4 changed files with 51 additions and 46 deletions

View file

@ -8,15 +8,18 @@ RUN mkdir -p /app
RUN chown -R app:app /app RUN chown -R app:app /app
USER app USER app
WORKDIR /app WORKDIR /app
ENV PATH="/app/.local/bin:${PATH}" VOLUME /data
ENV PYTHONPATH="/app:${PYTHONPATH}"
# Trivialize detecting dockerization
ENV DOCKER_RUNNING=true ENV DOCKER_RUNNING=true
ENV PYTHONPATH="/app:${PYTHONPATH}"
ENV PATH="/app/.local/bin:${PATH}"
### App specific crap ### App specific crap
# Deps vary least so do them first # Deps vary least so do them first
RUN pip3 install --user install aiohttp aiohttp_basicauth async_lru cachetools click pycryptodome pyyaml retry RUN pip3 install --user install aiohttp aiohttp_basicauth async_lru cachetools click pycryptodome pyyaml retry
COPY --chown=app:app src/python relay.yaml relay.jsonld /app/ COPY --chown=app:app docker_relay.sh /app/relay.sh
COPY --chown=app:app src/python /app/
CMD ["python3", "relay/__main__.py", "-c", "relay.yaml", "run"] EXPOSE 8080
ENTRYPOINT ["/bin/sh", "/app/relay.sh"]

View file

@ -0,0 +1,11 @@
#!/usr/bin/env sh
# A launcher script for the dockerized relay
# First do config init if needed
if [ ! -f "/data/config.yml" ]; then
python3 -m "relay" setup
fi
# Then run the blame thing
exec python3 -m "relay" "${@:-run}"

View file

@ -9,7 +9,6 @@ from relay.application import (
Application, Application,
request_id_middleware, request_id_middleware,
) )
from relay.config import relay_software_names
@click.group( @click.group(
@ -238,13 +237,6 @@ def cli_software_list(obj: Application):
def cli_software_ban(obj: Application, name, fetch_nodeinfo): def cli_software_ban(obj: Application, name, fetch_nodeinfo):
"Ban software. Use RELAYS for NAME to ban relays" "Ban software. Use RELAYS for NAME to ban relays"
if name == "RELAYS":
for name in relay_software_names:
obj.config.ban_software(name)
obj.config.save()
return click.echo("Banned all relay software")
if fetch_nodeinfo: if fetch_nodeinfo:
software = asyncio.run(misc.fetch_nodeinfo(name)) software = asyncio.run(misc.fetch_nodeinfo(name))
@ -273,13 +265,6 @@ def cli_software_ban(obj: Application, name, fetch_nodeinfo):
def cli_software_unban(obj: Application, name, fetch_nodeinfo): def cli_software_unban(obj: Application, name, fetch_nodeinfo):
"Ban software. Use RELAYS for NAME to unban relays" "Ban software. Use RELAYS for NAME to unban relays"
if name == "RELAYS":
for name in relay_software_names:
obj.config.unban_software(name)
config.save()
return click.echo("Unbanned all relay software")
if fetch_nodeinfo: if fetch_nodeinfo:
software = asyncio.run(misc.fetch_nodeinfo(name)) software = asyncio.run(misc.fetch_nodeinfo(name))
@ -345,8 +330,9 @@ def cli_whitelist_remove(obj: Application, instance):
def relay_setup(obj: Application): def relay_setup(obj: Application):
"Generate a new config" "Generate a new config"
if not obj.config.is_docker:
while True: while True:
obj.config.host = click.prompt( obj.config.host = os.getenv("RELAY_HOSTNAME") or click.prompt(
"What domain will the relay be hosted on?", default=obj.config.host "What domain will the relay be hosted on?", default=obj.config.host
) )
@ -355,7 +341,7 @@ def relay_setup(obj: Application):
click.echo("The domain must not be example.com") click.echo("The domain must not be example.com")
obj.config.listen = click.prompt( obj.config.listen = os.getenv("LISTEN_ADDRESS") or click.prompt(
"Which address should the relay listen on?", default=obj.config.listen "Which address should the relay listen on?", default=obj.config.listen
) )
@ -367,9 +353,17 @@ def relay_setup(obj: Application):
) )
break break
else:
obj.config.listen = os.getenv("LISTEN_ADDRESS", obj.config.listen)
obj.config.port = int(os.getenv("LISTEN_PORT", obj.config.port))
obj.config.host = os.getenv("RELAY_HOSTNAME")
if not obj.config.host:
click.echo("Error: No relay host configured! Set $RELAY_HOSTNAME")
exit(1)
obj.config.save() obj.config.save()
if not obj["is_docker"] and click.confirm( if not obj.config.is_docker and click.confirm(
"Relay all setup! Would you like to run it now?" "Relay all setup! Would you like to run it now?"
): ):
relay_run.callback() relay_run.callback()

View file

@ -1,3 +1,4 @@
import os
from pathlib import Path from pathlib import Path
from urllib.parse import urlparse from urllib.parse import urlparse
@ -5,14 +6,6 @@ from relay.misc import DotDict
import yaml import yaml
relay_software_names = [
"activityrelay",
"aoderelay",
"social.seattle.wa.us-relay",
"unciarelay",
]
class RelayConfig(DotDict): class RelayConfig(DotDict):
apkeys = { apkeys = {
"host", "host",
@ -44,6 +37,10 @@ class RelayConfig(DotDict):
super().__setitem__(key, value) super().__setitem__(key, value)
@property
def is_docker(self):
return bool(os.getenv("DOCKER_RUNNING"))
@property @property
def db(self): def db(self):
return Path(self["db"]).expanduser().resolve() return Path(self["db"]).expanduser().resolve()