ehmatthes / django-simple-deploy

A reusable Django app that configures your project for deployment
BSD 3-Clause "New" or "Revised" License
307 stars 26 forks source link

Support Pipenv across all existing platforms #198

Closed ehmatthes closed 1 year ago

ehmatthes commented 1 year ago

Similar to #197, make sure Pipenv is supported across the three currently-supported platforms.

ehmatthes commented 1 year ago

Running integration tests

ehmatthes commented 1 year ago

Support Pipenv on Fly.io

RUN set -ex && \ pip install --upgrade pip && \ pip install pipenv && \ pipenv install --skip-lock && \ rm -rf /root/.cache/

Pipfile:

% cat tmp_django_simple_deploy_test/Pipfile [[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi"

[packages] whitenoise = "" # Added by simple_deploy command. dj-database-url = "" # Added by simple_deploy command. psycopg2-binary = "" # Added by simple_deploy command. gunicorn = "" # Added by simple_deploy command. django = "" django-bootstrap5 = "" requests = "" django-simple-deploy = ""

[dev-packages]

[requires] python_version = "3.10"

Relevant output:

=> [4/7] COPY Pipfile /tmp/Pipfile 0.0s => [5/7] RUN set -ex && pip install --upgrade pip && pip install pipenv && pipenv install --skip-lock && rm 10.1s => [6/7] COPY . /code/ 0.0s => ERROR [7/7] RUN ON_FLYIO_SETUP="1" python manage.py collectstatic --noinput 0.3s


[7/7] RUN ON_FLYIO_SETUP="1" python manage.py collectstatic --noinput:

10 0.310 Traceback (most recent call last):

10 0.310 File "/code/manage.py", line 11, in main

10 0.310 from django.core.management import execute_from_command_line

10 0.311 ModuleNotFoundError: No module named 'django'

[packages] whitenoise = "" # Added by simple_deploy command. dj-database-url = "" # Added by simple_deploy command. psycopg2 = "<2.9" # Added by simple_deploy command. gunicorn = "" # Added by simple_deploy command. django = "" django-bootstrap5 = "" requests = "" django-simple-deploy = "*"

[dev-packages]

[requires] python_version = "3.10"



- [x] Pass integration test.
- [ ] Unit test this approach.
ehmatthes commented 1 year ago

More working notes - fly.io support

https://user-images.githubusercontent.com/1886842/207141186-c51fdaac-ffa2-4ea6-81eb-51ec09404797.mov

FROM python:${PYTHON_VERSION}

ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 ENV PIPENV_PIPFILE /tmp/Pipfile

RUN mkdir -p /code

WORKDIR /code

COPY Pipfile /tmp/Pipfile

RUN set -ex && \ pip install --upgrade pip && \ pip install pipenv && \ pipenv install && \ rm -rf /root/.cache/

COPY . /code/

RUN ON_FLYIO_SETUP="1" pipenv run python manage.py collectstatic --noinput

EXPOSE 8000

CMD ["gunicorn", "--bind", ":8000", "--workers", "2", "blog.wsgi"]


- [x] Use pipenv in fly.toml as well, for migrate command. This works.
- [x] May need to tweak final `CMD` in dockerfile?
- [x] Note: Fly may be incompatible with Pipenv, and maybe we should just generate requirements.txt. (nope)
ehmatthes commented 1 year ago

Run gunicorn after pipenv install

ehmatthes commented 1 year ago

Works!

I've been tweaking a project that was initially deployed through the integration test. Process: modify Dockerfile and fly.toml, then run fly deploy --verbose. It finally works, and I can make an account, and the site and admin home page have proper css. I do get this message at the release phase:

--> This release will not be available until the release command succeeds.
     Starting instance
     Configuring virtual machine
     Pulling container image
     Unpacking image
     Preparing kernel init
     Configuring firecracker
     Starting virtual machine
     Starting init (commit: f447594)...
     Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
     no label, UUID=bb945d0a-7089-4bf6-ac44-a55bba78ab63
     Preparing to run: `pipenv run python manage.py migrate` as root
     2022/12/12 20:19:54 listening on [fdaa:0:94e0:a7b:bbfb:8717:d0fb:2]:22 (DNS: [fdaa::3]:53)
     System check identified some issues:
     WARNINGS:
     ?: (staticfiles.W004) The directory '/code/static' in the STATICFILES_DIRS setting does not exist.
     Operations to perform:
       Apply all migrations: admin, auth, blogs, contenttypes, sessions
     Running migrations:
       No migrations to apply.
     Starting clean up.

Dockerfile:

ARG PYTHON_VERSION=3.10-slim-buster

FROM python:${PYTHON_VERSION}

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PIPENV_PIPFILE /tmp/Pipfile

RUN mkdir -p /code

WORKDIR /code

COPY Pipfile /tmp/Pipfile

RUN set -ex && \
    pip install --upgrade pip && \
    pip install pipenv && \
    pipenv install && \
    rm -rf /root/.cache/

COPY . /code/

RUN ON_FLYIO_SETUP="1" pipenv run python manage.py collectstatic --noinput

EXPOSE 8000

CMD ["pipenv", "run", "gunicorn", "--bind", ":8000", "--workers", "2", "blog.wsgi"]

fly.toml:

app = "ancient-bird-3980"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]
  PORT = "8000"

[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8000
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

[[statics]]
  guest_path = "/app/public"
  url_prefix = "/static/"

[deploy]
  release_command = "pipenv run python manage.py migrate"
ehmatthes commented 1 year ago

Final testing for Fly.io

ehmatthes commented 1 year ago

Cleaning up Fly.io