Skip to content

Commit

Permalink
Merge pull request #583 from memeLab/create-tools-container
Browse files Browse the repository at this point in the history
Update Docker default user and Storage settings
  • Loading branch information
pablodiegoss authored Nov 21, 2024
2 parents e35cd38 + 4bc4e77 commit 162c6c0
Show file tree
Hide file tree
Showing 35 changed files with 202 additions and 112 deletions.
10 changes: 9 additions & 1 deletion .envs/.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
DJANGO_DEBUG=True
DEBUG_TOOLBAR=True
DJANGO_SECRET_KEY=change_me
USE_MINIO=True
DJANGO_ADMIN_URL=jandig-admin/
USE_MINIO=True
USE_GUNICORN=True

# Sentry Variables
ENABLE_SENTRY=False
Expand Down Expand Up @@ -47,3 +48,10 @@ RECAPTCHA_GCLOUD_API_KEY=
RECAPTCHA_SITE_KEY=
RECAPTCHA_SECRET_KEY=




RECAPTCHA_PROJECT_ID=jandig-1730288777649
RECAPTCHA_GCLOUD_API_KEY=AIzaSyDlXg-ey8TF7jWWHyZysiCxTNmOKdqlLk4
RECAPTCHA_SITE_KEY=6LebeHAqAAAAALfIfbLm0znmZcUxG8U1VDy7iUDB
RECAPTCHA_SECRET_KEY=6LebeHAqAAAAACQUN1laIgne5-rcTy4q5-4Br7Po
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ src/ARte/config/__pycache__/
*.sqlite3

build/
static/
media/
src/data/certbot/
docker/node_modules/
node_modules/
Expand Down
2 changes: 2 additions & 0 deletions .venv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
46 changes: 29 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@

FROM python:3.13.0-slim-bookworm as base-image
FROM python:3.13.0-slim-bookworm

RUN apt-get update && \
apt-get install -y --no-install-recommends \
gettext \
docutils-common \
curl \
wget


ENV PATH="$PATH:/root/.local/bin" \
ENV PATH="$PATH:/home/jandig/.local/bin:/jandig/.venv/bin" \
TINI_VERSION=v0.19.0 \
# poetry:
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=true \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_VIRTUALENVS_OPTIONS_ALWAYS_COPY=true \
POETRY_VIRTUALENV_PATH="/jandig/.venv" \
POETRY_CACHE_DIR='/home/jandig/cache/pypoetry' \
POETRY_VERSION=1.8.4


# Installing `poetry` package manager:
# https://github.com/python-poetry/poetry
RUN curl -sSL https://install.python-poetry.org | python3 -
RUN apt-get update && \
apt-get install -y --no-install-recommends \
gettext \
docutils-common \
curl \
wget

RUN dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \
&& wget "https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-${dpkgArch}" -O /usr/local/bin/tini \
&& chmod +x /usr/local/bin/tini && tini --version


RUN mkdir -p /jandig/src /jandig/locale /jandig/docs /jandig/static /jandig/build
RUN mkdir -p /jandig/src /jandig/locale /jandig/docs /jandig/.venv /jandig/static /jandig/build /home/jandig/cache/pypoetry

WORKDIR /jandig

Expand All @@ -42,7 +39,22 @@ COPY ./run.sh /jandig/run.sh
COPY ./etc/ /jandig/etc/


# Create group and user
RUN groupadd -g 1000 jandig && useradd -u 1000 -g 1000 -r -m -d /home/jandig jandig

# Change ownership of the directories to the new user and group
RUN chown -R jandig:jandig /jandig /home/jandig
RUN chmod 2775 /jandig /home/jandig

# Switch to the new user
USER jandig

# Installing `poetry` package manager:
# https://github.com/python-poetry/poetry
RUN curl -sSL https://install.python-poetry.org | python3 -

RUN find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

ENTRYPOINT ["tini", "--"]

ENTRYPOINT ["tini", "--"]
CMD [ "/jandig/run.sh" ]
9 changes: 6 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ services:
build:
dockerfile: Dockerfile
context: .
user: jandig
ports:
- 8000:8000
- 80:8000
volumes:
- ./:/jandig
- poetry_cache:/var/cache/pypoetry
- poetry_cache:/home/jandig/cache/pypoetry
env_file:
- .envs/.example
environment:
- USE_GUNICORN=True
- INSTALL_DEV=True
depends_on:
storage:
condition: service_started
Expand All @@ -19,7 +23,6 @@ services:
condition: service_started
postgres:
condition: service_healthy
command: /jandig/run.sh

postgres:
image: postgres:15.4
Expand Down
9 changes: 7 additions & 2 deletions locustfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from locust import HttpLocust, Set, between


def index(load):
load.client.get("/")


def load_gifs(load):
load.client.get("/collection/")


def exhibit(load):
load.client.get("/longavida/")


class UserBehavior(Set):
s = {exhibit:1}
s = {exhibit: 1}


class WebsiteUser(HttpLocust):
_set = UserBehavior
wait_time = between(3,5)
wait_time = between(3, 5)
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ exclude = '''
/(
\.git
| migrations
| .venv
)/
'''
[tool.isort]
profile = "black"
src_paths = ["src"]
skip_glob = "*/migrations/*.py"
skip_glob = "*/migrations/*.py,.venv/*"

[tool.flake8]
max-line-length = 200
exclude="**/migrations/"
exclude="**/migrations/, .venv/*"
27 changes: 23 additions & 4 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
#!/bin/bash
poetry install

# Set default values for environment variables if not provided
INSTALL_DEV=${INSTALL_DEV:-false}
USE_GUNICORN=${USE_GUNICORN:-true}

# Convert variables to lowercase for case-insensitive comparison
INSTALL_DEV=$(echo "$INSTALL_DEV" | tr '[:upper:]' '[:lower:]')
USE_GUNICORN=$(echo "$USE_GUNICORN" | tr '[:upper:]' '[:lower:]')

poetry install --without dev

if [ "$INSTALL_DEV" = "true" ]; then
poetry install --only dev
fi

poetry show
# poetry run python src/manage.py collectstatic --no-input
poetry run python src/manage.py collectstatic --no-input
poetry run python src/manage.py migrate
poetry run sphinx-build docs/ build/
poetry run python etc/scripts/compilemessages.py

bash -c "cd src && poetry run gunicorn --reload --worker-connections=10000 --workers=4 --log-level debug --bind 0.0.0.0:8000 config.wsgi"
# poetry run python src/manage.py runserver 0.0.0.0:8000
if [ "$USE_GUNICORN" = "true" ]; then
echo "Running Gunicorn Server"
bash -c "cd src && poetry run gunicorn --reload --worker-connections=10000 --workers=4 --log-level debug --bind 0.0.0.0:8000 config.wsgi"
else
echo "Running Django development server"
poetry run python src/manage.py runserver 0.0.0.0:8000
fi
3 changes: 2 additions & 1 deletion src/blog/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from blog.models import Category, Clipping, Post, PostImage
from django.contrib import admin

from blog.models import Category, Clipping, Post, PostImage

admin.site.register(Category)
admin.site.register(PostImage)
admin.site.register(Clipping)
Expand Down
1 change: 1 addition & 0 deletions src/blog/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.files.storage import default_storage
from django.db import models

from users.models import Profile

IMAGE_BASE_PATH = "post_images/"
Expand Down
3 changes: 2 additions & 1 deletion src/blog/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# blog/urls.py

from blog import views
from django.urls import path

from blog import views

urlpatterns = [
path("", views.blog_index, name="blog_index"),
path("post/<int:pk>/", views.blog_detail, name="blog_detail"),
Expand Down
3 changes: 2 additions & 1 deletion src/blog/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from blog.models import Category, Clipping, Post, PostStatus
from django.shortcuts import render

from blog.models import Category, Clipping, Post, PostStatus

PREVIEW_SIZE = 300
PAGE_SIZE = 4

Expand Down
59 changes: 8 additions & 51 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from django.utils.translation import gettext_lazy as _
from sentry_sdk.integrations.django import DjangoIntegration

from .storage_settings import * # noqa F403 F401

ROOT_DIR = environ.Path("/jandig/")
BASE_DIR = "/jandig/src"

Expand Down Expand Up @@ -43,7 +45,7 @@
HEALTH_CHECK_URL = env("HEALTH_CHECK_URL", default="api/v1/status/")
SENTRY_TRACES_SAMPLE_RATE = env("SENTRY_TRACES_SAMPLE_RATE", default=0.1)
SENTRY_ENVIRONMENT = env("SENTRY_ENVIRONMENT", default="")
SENTRY_RELEASE = env("SENTRY_RELEASE", default="1.4.0")
SENTRY_RELEASE = env("SENTRY_RELEASE", default="1.4.1")


def traces_sampler(sampling_context):
Expand Down Expand Up @@ -184,53 +186,6 @@ def debug(request):

USE_TZ = True


# AWS credentials
AWS_S3_OBJECT_PARAMETERS = {
"CacheControl": "max-age=86400",
}
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "")
AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STORAGE_BUCKET_NAME", "")
AWS_S3_REGION_NAME = os.getenv("AWS_S3_REGION_NAME", "us-east-2")
AWS_DEFAULT_ACL = os.getenv("AWS_DEFAULT_ACL", None)
AWS_STATIC_LOCATION = os.getenv("AWS_STATIC_LOCATION", "static")
AWS_MEDIA_LOCATION = os.getenv("AWS_MEDIA_LOCATION", "media")
USE_MINIO = os.getenv("USE_MINIO", "false").lower() in ("true", "True", "1")
if USE_MINIO:
AWS_S3_ENDPOINT_URL = os.getenv("MINIO_S3_ENDPOINT_URL", "http://storage:9000")
AWS_S3_CUSTOM_DOMAIN = f"localhost:9000/{AWS_STORAGE_BUCKET_NAME}"
AWS_S3_USE_SSL = False
AWS_S3_SECURE_URLS = False
AWS_S3_URL_PROTOCOL = "http:"

else:
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
AWS_S3_URL_PROTOCOL = "https:"

# Static configuration
# Add your own apps statics in this list
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "core", "static"),
os.path.join(BASE_DIR, "users", "static"),
os.path.join(BASE_DIR, "blog", "static"),
]

STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

AWS_PUBLIC_MEDIA_LOCATION = "media/public"

# Storages
STORAGES = {
"default": {
"BACKEND": "config.storage_backends.PublicMediaStorage",
},
"staticfiles": {
"BACKEND": "config.storage_backends.StaticStorage",
},
}
# LOGIN / LOGOUT
LOGIN_URL = "login"
LOGIN_REDIRECT_URL = "home"
Expand All @@ -245,10 +200,12 @@ def debug(request):
SMTP_PASSWORD = env("SMTP_PASSWORD", default="password")
SMTP_SENDER_MAIL = env("SMTP_SENDER_MAIL", default="[email protected]")


# RECAPTCHA
# Recaptcha
RECAPTCHA_ENABLED = env("RECAPTCHA_ENABLED", default=False)
RECAPTCHA_SITE_KEY = env("RECAPTCHA_SITE_KEY", default="fake-key")
RECAPTCHA_SITE_KEY = env("RECAPTCHA_SITE_KEY", default="")
RECAPTCHA_SECRET_KEY = env("RECAPTCHA_SECRET_KEY", default="")
RECAPTCHA_PROJECT_ID = env("RECAPTCHA_PROJECT_ID", default="")
RECAPTCHA_GCLOUD_API_KEY = env("RECAPTCHA_GCLOUD_API_KEY", default="")

if len(sys.argv) > 1 and sys.argv[1] == "test":
logging.disable(logging.CRITICAL)
56 changes: 56 additions & 0 deletions src/config/storage_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os

AWS_S3_OBJECT_PARAMETERS = {
"CacheControl": "max-age=86400",
}
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "")
AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STORAGE_BUCKET_NAME", "")
AWS_S3_REGION_NAME = os.getenv("AWS_S3_REGION_NAME", "us-east-2")
AWS_DEFAULT_ACL = os.getenv("AWS_DEFAULT_ACL", None)
AWS_STATIC_LOCATION = os.getenv("AWS_STATIC_LOCATION", "static")
AWS_MEDIA_LOCATION = os.getenv("AWS_MEDIA_LOCATION", "media")

if os.getenv("USE_GUNICORN", "true").lower() in ("false", "0"):
USE_MINIO = False
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
STATIC_ROOT = os.path.join("/jandig", "static")
MEDIA_ROOT = os.path.join("/jandig", "media")
else:
USE_MINIO = os.getenv("USE_MINIO", False)

if USE_MINIO:
AWS_S3_ENDPOINT_URL = os.getenv("MINIO_S3_ENDPOINT_URL", "http://storage:9000")
AWS_S3_CUSTOM_DOMAIN = f"localhost:9000/{AWS_STORAGE_BUCKET_NAME}"
AWS_S3_USE_SSL = False
AWS_S3_SECURE_URLS = False
AWS_S3_URL_PROTOCOL = "http:"

else:
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
AWS_S3_URL_PROTOCOL = "https:"

# Static configuration
# Add your own apps statics in this list
STATICFILES_DIRS = [
os.path.join("/jandig/src", "core", "static"),
os.path.join("/jandig/src", "users", "static"),
os.path.join("/jandig/src", "blog", "static"),
]

STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

AWS_PUBLIC_MEDIA_LOCATION = "media/public"

if os.getenv("USE_GUNICORN", "true").lower() in ("true", "1"):
STORAGES = {
"default": {
"BACKEND": "config.storage_backends.PublicMediaStorage",
},
"staticfiles": {
"BACKEND": "config.storage_backends.StaticStorage",
},
}
Loading

0 comments on commit 162c6c0

Please sign in to comment.