This commit is contained in:
j3d1 2026-08-17 01:45:14 +02:00
parent 0f51f6e33f
commit 419893d93d
11 changed files with 925 additions and 72 deletions

View file

@ -1,13 +0,0 @@
FROM python:alpine
WORKDIR /app
RUN apk add --no-cache gcc musl-dev python3-dev
COPY requirements.txt /app
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . /app
RUN python configure.py
RUN python manage.py collectstatic --noinput
CMD python manage.py migrate && python manage.py runserver 0.0.0.0:8000 --insecure
# TODO serve static files with nginx and remove --insecure
EXPOSE 8000

View file

@ -32,25 +32,41 @@ def yesno(prompt, default=False):
def configure():
if not os.path.exists('.env'):
if not yesno("the .env file does not exist, do you want to create it?", default=True):
print('Aborting')
exit(0)
if not os.path.exists('.env.dist'):
print('No .env.dist file found')
exit(1)
else:
from shutil import copyfile
copyfile('.env.dist', '.env')
# Keys this function may generate/update, tracked so that if .env turns
# out to be unwritable (e.g. a prod container running as an unprivileged
# user, whose real config comes from --env-file instead) we can still
# print the resulting configuration for the operator to apply manually,
# rather than silently discarding it or crashing.
tracked_keys = ['SECRET_KEY', 'ALLOWED_HOSTS']
unwritable = False
env = dotenv.load_dotenv('.env')
if not env or not os.getenv('SECRET_KEY'):
if not os.path.exists('.env'):
if yesno("the .env file does not exist, do you want to create it?", default=True):
if not os.path.exists('.env.dist'):
print('No .env.dist file found')
else:
for key in dotenv.dotenv_values('.env.dist'):
if key not in tracked_keys:
tracked_keys.append(key)
from shutil import copyfile
try:
copyfile('.env.dist', '.env')
except PermissionError:
unwritable = True
dotenv.load_dotenv('.env')
if not os.getenv('SECRET_KEY'):
from django.core.management.utils import get_random_secret_key
print('No SECRET_KEY found in .env file, generating one...')
with open('.env', 'a') as f:
f.write('\nSECRET_KEY=')
f.write(get_random_secret_key())
f.write('\n')
secret_key = get_random_secret_key()
os.environ['SECRET_KEY'] = secret_key
try:
with open('.env', 'a') as f:
f.write('\nSECRET_KEY=')
f.write(secret_key)
f.write('\n')
except PermissionError:
unwritable = True
# TODO rename ALLOWED_HOSTS to something more self-explanatory
current_hosts = os.getenv('ALLOWED_HOSTS')
@ -59,7 +75,16 @@ def configure():
if yesno("Do you want to add ALLOWED_HOSTS?"):
hosts = input("Enter a comma-separated list of allowed hosts: ")
joined_hosts = current_hosts + ',' + hosts if current_hosts else hosts
dotenv.set_key('.env', 'ALLOWED_HOSTS', joined_hosts)
os.environ['ALLOWED_HOSTS'] = joined_hosts
try:
dotenv.set_key('.env', 'ALLOWED_HOSTS', joined_hosts)
except PermissionError:
unwritable = True
if unwritable:
print('Could not write .env (read-only working directory) - resulting configuration:')
for key in tracked_keys:
print('{}={}'.format(key, os.getenv(key, '')))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")
import django