From 60d1a8658f83535cf74d3c2550dea2a5ed3893b1 Mon Sep 17 00:00:00 2001 From: Joseph Riddle Date: Wed, 31 Jan 2024 20:25:19 -0800 Subject: [PATCH 1/5] Add Fly.io config --- .dockerignore | 3 +++ Dockerfile | 21 +++++++++++++++++++++ fly.toml | 30 ++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 55 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 fly.toml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b7c39d7c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +fly.toml +.git/ +*.sqlite3 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..bf3caa40 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +ARG PYTHON_VERSION=3.11-slim-bullseye + +FROM python:${PYTHON_VERSION} + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +RUN mkdir -p /code + +WORKDIR /code + +COPY requirements.txt /tmp/requirements.txt +RUN set -ex && \ + pip install --upgrade pip && \ + pip install -r /tmp/requirements.txt && \ + rm -rf /root/.cache/ +COPY . /code + +EXPOSE 8000 + +CMD ["gunicorn", "--chdir", "./src", "--bind", ":8000", "--workers", "2", "spokanetech.wsgi"] diff --git a/fly.toml b/fly.toml new file mode 100644 index 00000000..d40f3348 --- /dev/null +++ b/fly.toml @@ -0,0 +1,30 @@ +# fly.toml app configuration file generated for spokanetech-py on 2024-01-31T20:22:30-08:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = 'spokanetech-py' +primary_region = 'sea' +console_command = '/code/manage.py shell' + +[build] + +[env] + PORT = '8000' + +[http_service] + internal_port = 8000 + force_https = true + auto_stop_machines = true + auto_start_machines = true + min_machines_running = 0 + processes = ['app'] + +[[vm]] + cpu_kind = 'shared' + cpus = 1 + memory_mb = 1024 + +[[statics]] + guest_path = '/code/static' + url_prefix = '/static/' diff --git a/requirements.txt b/requirements.txt index 9b152461..036354cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ asgiref==3.7.2 +gunicorn==21.2.0 Django==5.0.1 sqlparse==0.4.4 From 138fc496c470170a74f787ac88fb89cd18c6f566 Mon Sep 17 00:00:00 2001 From: Joseph Riddle Date: Wed, 31 Jan 2024 20:36:23 -0800 Subject: [PATCH 2/5] Add dj_database_url --- requirements.txt | 3 ++- src/spokanetech/settings.py | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 036354cb..bc3592c9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ asgiref==3.7.2 -gunicorn==21.2.0 +dj-database-url==2.1.0 Django==5.0.1 +gunicorn==21.2.0 sqlparse==0.4.4 diff --git a/src/spokanetech/settings.py b/src/spokanetech/settings.py index 1e678d8a..5314d026 100644 --- a/src/spokanetech/settings.py +++ b/src/spokanetech/settings.py @@ -9,7 +9,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/5.0/ref/settings/ """ - +import dj_database_url from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -75,10 +75,11 @@ # https://docs.djangoproject.com/en/5.0/ref/settings/#databases DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', - } + "default": dj_database_url.config( + default=f"sqlite:////{BASE_DIR}/db.sqlite3", + conn_max_age=600, + conn_health_checks=True, + ), } From f4aae86c708ea427294857f0474bff698b880b65 Mon Sep 17 00:00:00 2001 From: Joseph Riddle Date: Wed, 31 Jan 2024 20:48:53 -0800 Subject: [PATCH 3/5] Add psycopg[binary] --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index bc3592c9..1b47c31c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ asgiref==3.7.2 dj-database-url==2.1.0 Django==5.0.1 gunicorn==21.2.0 +psycopg[binary]==3.1.17 sqlparse==0.4.4 From 51ced0898ac7a2200ae3bde32217f73aa7cde8a5 Mon Sep 17 00:00:00 2001 From: Joseph Riddle Date: Wed, 31 Jan 2024 20:51:42 -0800 Subject: [PATCH 4/5] Add fly.io host to ALLOWED_HOSTS --- src/spokanetech/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spokanetech/settings.py b/src/spokanetech/settings.py index 0bc6301a..52ef50f0 100644 --- a/src/spokanetech/settings.py +++ b/src/spokanetech/settings.py @@ -39,7 +39,7 @@ ) from e DEBUG = False - ALLOWED_HOSTS = ["spokanetech.org"] + ALLOWED_HOSTS = ["spokanetech.org", "spokanetech-py.fly.dev"] # SSL Options # TODO: These will have to change depending on how infra-platform handles SSL termination From 4e96423e2b1a4e4ceae9947d972a734b77ebd1ec Mon Sep 17 00:00:00 2001 From: Joseph Riddle Date: Wed, 31 Jan 2024 21:01:43 -0800 Subject: [PATCH 5/5] Add basic Fly.io deploy workflow --- .github/workflows/deploy.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..7a01a745 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,20 @@ +name: Fly Deploy +on: + workflow_dispatch: + push: + branches: + - 'main' + +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + +jobs: + deploy: + name: Deploy app + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Deploy to fly.io + run: flyctl deploy --remote-only