diff options
author | Matthew Lemon <y@yulqen.org> | 2024-05-28 15:15:44 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-05-28 15:15:44 +0100 |
commit | 2037c6b87564772c7ac3f3ece3e4b2df5bc44cdc (patch) | |
tree | 222bb552655033468af47818a072096999cd03d9 | |
parent | 2b4181aecadbbad988cd80f97ef71b42d47a3cb1 (diff) |
Should be a buildable Django container
-rw-r--r-- | .dockerignore | 1 | ||||
-rw-r--r-- | Makefile | 32 | ||||
-rw-r--r-- | compose/production/django/Dockerfile | 40 | ||||
-rw-r--r-- | config/settings/base.py | 60 | ||||
-rw-r--r-- | config/settings/local-bak.py (renamed from config/settings/local.py) | 1 | ||||
-rw-r--r-- | config/settings/production.py | 15 | ||||
-rw-r--r-- | docker-entrypoint.sh | 5 | ||||
-rw-r--r-- | requirements/base.txt | 1 | ||||
-rwxr-xr-x | scripts/deploy.sh | 24 |
9 files changed, 117 insertions, 62 deletions
diff --git a/.dockerignore b/.dockerignore index a602416..0e6fc1e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,5 +8,6 @@ .readthedocs.yml .travis.yml venv +.venv .git .envs/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4744c12 --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +all: build run migrate collectstatic + +start: build run migrate collectstatic + +build: + @docker build -f compose/production/django/Dockerfile -t pyblackbird_cc:latest . + +push: + @docker build -f compose/production/django/Dockerfile -t pyblackbird_cc:latest . && docker tag pyblackbird_cc registry.digitalocean.com/twentyfour-registry/pyblackbird && docker push registry.digitalocean.com/twentyfour-registry/pyblackbird + +clean-registry: + @doctl registry garbage-collection start --include-untagged-manifests twentyfour-registry + +run: + @docker run -d --rm --name pyblackbird_cc_django --env-file .env -p 8080:8080 pyblackbird_cc + +collectstatic: + @docker run --rm --env-file .env -v .:/app pyblackbird sh -c "python manage.py collectstatic --noinput" + +migrate: + @docker run --rm --env-file .env -v .:/app pyblackbird sh -c "python manage.py makemigrations && python manage.py migrate" + +clean: + @docker stop pyblackbird + @sleep 2 + @docker rmi pyblackbird + +test-all: + @pytest -q -s . + +test: + @pytest -q -s -m "not slow" diff --git a/compose/production/django/Dockerfile b/compose/production/django/Dockerfile index 6e816f1..e81a03c 100644 --- a/compose/production/django/Dockerfile +++ b/compose/production/django/Dockerfile @@ -57,28 +57,26 @@ COPY --from=python-build-stage /usr/src/app/wheels /wheels/ RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \ && rm -rf /wheels/ +# COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint +# RUN sed -i 's/\r$//g' /entrypoint +# RUN chmod +x /entrypoint -COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint -RUN sed -i 's/\r$//g' /entrypoint -RUN chmod +x /entrypoint +# COPY --chown=django:django ./compose/production/django/start /start +# RUN sed -i 's/\r$//g' /start +# RUN chmod +x /start +# COPY --chown=django:django ./compose/production/django/celery/worker/start /start-celeryworker +# RUN sed -i 's/\r$//g' /start-celeryworker +# RUN chmod +x /start-celeryworker -COPY --chown=django:django ./compose/production/django/start /start -RUN sed -i 's/\r$//g' /start -RUN chmod +x /start -COPY --chown=django:django ./compose/production/django/celery/worker/start /start-celeryworker -RUN sed -i 's/\r$//g' /start-celeryworker -RUN chmod +x /start-celeryworker +# COPY --chown=django:django ./compose/production/django/celery/beat/start /start-celerybeat +# RUN sed -i 's/\r$//g' /start-celerybeat +# RUN chmod +x /start-celerybeat -COPY --chown=django:django ./compose/production/django/celery/beat/start /start-celerybeat -RUN sed -i 's/\r$//g' /start-celerybeat -RUN chmod +x /start-celerybeat - - -COPY --chown=django:django ./compose/production/django/celery/flower/start /start-flower -RUN sed -i 's/\r$//g' /start-flower -RUN chmod +x /start-flower +# COPY --chown=django:django ./compose/production/django/celery/flower/start /start-flower +# RUN sed -i 's/\r$//g' /start-flower +# RUN chmod +x /start-flower # copy application code to WORKDIR @@ -89,9 +87,5 @@ RUN chown -R django:django ${APP_HOME} USER django -RUN DATABASE_URL="" \ - CELERY_BROKER_URL="" \ - DJANGO_SETTINGS_MODULE="config.settings.test" \ - python manage.py compilemessages - -ENTRYPOINT ["/entrypoint"] +RUN chmod +x docker-entrypoint.sh +ENTRYPOINT ["/app/docker-entrypoint.sh"] diff --git a/config/settings/base.py b/config/settings/base.py index 9414953..09a748f 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -317,36 +317,36 @@ SOCIALACCOUNT_FORMS = {"signup": "pyblackbird_cc.users.forms.UserSocialSignupFor # STORAGES # ------------------------------------------------------------------------------ -# # https://django-storages.readthedocs.io/en/latest/#installation -# INSTALLED_APPS += ["storages"] -# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings -# # AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID") -# AWS_ACCESS_KEY_ID = env("SPACES_KEY") -# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings -# # AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY") -# AWS_SECRET_ACCESS_KEY = env("SPACES_SECRET") -# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings -# # AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME") -# AWS_STORAGE_BUCKET_NAME = env("SPACES_BUCKET_NAME") -# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings -# AWS_QUERYSTRING_AUTH = False -# # DO NOT change these unless you know what you're doing. -# _AWS_EXPIRY = 60 * 60 * 24 * 7 -# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings -# AWS_S3_OBJECT_PARAMETERS = { -# "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate", -# } -# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings -# AWS_S3_MAX_MEMORY_SIZE = env.int( -# "DJANGO_AWS_S3_MAX_MEMORY_SIZE", -# default=100_000_000, # 100MB -# ) -# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings -# AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None) -# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#cloudfront -# AWS_S3_CUSTOM_DOMAIN = env("DJANGO_AWS_S3_CUSTOM_DOMAIN", default=None) -# aws_s3_domain = AWS_S3_CUSTOM_DOMAIN or f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com" -# AWS_S3_ENDPOINT_URL = env("SPACES_ENDPOINT_URL") +# https://django-storages.readthedocs.io/en/latest/#installation +INSTALLED_APPS += ["storages"] +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings +# AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID") +AWS_ACCESS_KEY_ID = env("SPACES_KEY") +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings +# AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY") +AWS_SECRET_ACCESS_KEY = env("SPACES_SECRET") +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings +# AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME") +AWS_STORAGE_BUCKET_NAME = env("SPACES_BUCKET_NAME") +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings +AWS_QUERYSTRING_AUTH = False +# DO NOT change these unless you know what you're doing. +_AWS_EXPIRY = 60 * 60 * 24 * 7 +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings +AWS_S3_OBJECT_PARAMETERS = { + "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate", +} +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings +AWS_S3_MAX_MEMORY_SIZE = env.int( + "DJANGO_AWS_S3_MAX_MEMORY_SIZE", + default=100_000_000, # 100MB +) +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings +AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None) +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#cloudfront +AWS_S3_CUSTOM_DOMAIN = env("DJANGO_AWS_S3_CUSTOM_DOMAIN", default=None) +aws_s3_domain = AWS_S3_CUSTOM_DOMAIN or f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com" +AWS_S3_ENDPOINT_URL = env("STATIC_ENDPOINT_URL") # Your stuff... # ------------------------------------------------------------------------------ diff --git a/config/settings/local.py b/config/settings/local-bak.py index 8abae6f..afcdb3f 100644 --- a/config/settings/local.py +++ b/config/settings/local-bak.py @@ -1,6 +1,5 @@ # ruff: noqa: E501 from .base import * # noqa: F403 -from .base import INSTALLED_APPS from .base import env # GENERAL diff --git a/config/settings/production.py b/config/settings/production.py index 63afa6d..1c04152 100644 --- a/config/settings/production.py +++ b/config/settings/production.py @@ -3,7 +3,6 @@ from .base import * # noqa: F403 from .base import DATABASES -from .base import INSTALLED_APPS from .base import env # GENERAL @@ -124,16 +123,16 @@ ADMIN_URL = env("DJANGO_ADMIN_URL") # Anymail # ------------------------------------------------------------------------------ # https://anymail.readthedocs.io/en/stable/installation/#installing-anymail -INSTALLED_APPS += ["anymail"] +#INSTALLED_APPS += ["anymail"] # https://docs.djangoproject.com/en/dev/ref/settings/#email-backend # https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference # https://anymail.readthedocs.io/en/stable/esps/mailgun/ -EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend" -ANYMAIL = { - "MAILGUN_API_KEY": env("MAILGUN_API_KEY"), - "MAILGUN_SENDER_DOMAIN": env("MAILGUN_DOMAIN"), - "MAILGUN_API_URL": env("MAILGUN_API_URL", default="https://api.mailgun.net/v3"), -} +# EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend" +# ANYMAIL = { +# "MAILGUN_API_KEY": env("MAILGUN_API_KEY"), +# "MAILGUN_SENDER_DOMAIN": env("MAILGUN_DOMAIN"), +# "MAILGUN_API_URL": env("MAILGUN_API_URL", default="https://api.mailgun.net/v3"), +#} # LOGGING diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..5e8164c --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/sh +cd /app +python manage.py migrate +python manage.py collectstatic --noinput +exec gunicorn --bind ':8080' --worker-tmp-dir /dev/shm --workers 3 config.wsgi:application diff --git a/requirements/base.txt b/requirements/base.txt index f13eeff..fdf7dd1 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -20,6 +20,7 @@ markdown==3.6.0 # Django # ------------------------------------------------------------------------------ +django-storages==1.14.3 Django==5.0.4 # pyup: < 5.0 # https://www.djangoproject.com/ django-environ==0.11.2 # https://github.com/joke2k/django-environ django-model-utils==4.5.1 # https://github.com/jazzband/django-model-utils diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 0000000..b7ecf74 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Assign arguments to variables +IMAGE_NAME="pyblackbird" +TAG="latest" # Adjust tagging strategy as needed +DOCKER_REGISTRY="registry.gitlab.com/yulqen" # Docker registry URL + +SERVER_HOST="substracker-web" + +# SSH into server to pull the image, restart the container, and configure NGINX and Certbot +#ssh -i $SSH_KEY_PATH $SSH_USER@$SERVER_HOST << EOF +ssh $SERVER_HOST << EOF + +# Stop and remove the existing container if it exists +docker stop $IMAGE_NAME || true +docker rm $IMAGE_NAME || true +docker rmi $DOCKER_REGISTRY/$IMAGE_NAME:$TAG || true + +# run using the .env file for config settings +cd code/pyblackbird && docker run -d --rm --name pyblackbird --mount type=bind,src=/home/surge/code/pyblackbird/data,dst=/app/data --env-file .env -p 8080:8080 $DOCKER_REGISTRY/$IMAGE_NAME:$TAG + +EOF + +echo "Deployment complete." |