Skip to content

Commit

Permalink
Fix Github Actions Docker build and local Docker web service
Browse files Browse the repository at this point in the history
  • Loading branch information
philipbelesky committed Apr 17, 2022
1 parent 6280ade commit 3a526c3
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ FROM python:3.9

# Just needed for all things python (note this is setting an env variable)
ENV PYTHONUNBUFFERED 1
# Needed for correct settings input
ENV IN_DOCKER 1

# Setup Node/NPM
RUN apt-get update
Expand Down
2 changes: 1 addition & 1 deletion deploy_heroku.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def get_git_push_spec():
run_heroku_command(["buildpacks:add", "heroku/python"])

# Set config variables
command = ["config:set", "DISABLE_COLLECTSTATIC=1", "ON_HEROKU=1"]
command = ["config:set", "DISABLE_COLLECTSTATIC=1", "ON_HEROKU=1", "USING_NGINX=1"]
secret_key = get_random_secret_key()

if platform.system() == "Windows": # Windows shell needs escaping
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ services:
- DEBUG=0
- DOCKER_REDIS=1
- IN_DOCKER=1
- USING_NGINX=1

worker:
environment:
- DEBUG=0
- DOCKER_REDIS=1
- IN_DOCKER=1
- USING_NGINX=1
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ services:
- IN_DOCKER=1
- DISABLE_SENTRY=1
- DOCKER_REDIS=1
- USING_NGINX=1
ports:
- "127.0.0.1:8000:8000"
volumes:
Expand All @@ -57,6 +58,7 @@ services:
- IN_DOCKER=1
- DISABLE_SENTRY=1
- DOCKER_REDIS=1
- USING_NGINX=1
volumes:
- ./tabbycat/settings:/tcd/tabbycat/settings
working_dir: /tcd
Expand Down
54 changes: 37 additions & 17 deletions tabbycat/run-asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# ==============================================================================

import logging
import os
import sys

root = logging.getLogger()
Expand Down Expand Up @@ -33,20 +34,39 @@
from daphne.endpoints import build_endpoint_description_strings # noqa: E402
from daphne.server import Server # noqa: E402

root.info('TC_DEPLOY: Initialising daphne')
Server(
application=asgi.application,
endpoints=build_endpoint_description_strings(
host="0.0.0.0",
port="8000",
),
ping_interval=15,
ping_timeout=30,
websocket_timeout=10800, # 3 hours maximum length
websocket_connect_timeout=10,
application_close_timeout=10,
verbosity=2,
proxy_forwarded_address_header="X-Forwarded-For",
proxy_forwarded_port_header="X-Forwarded-Port",
proxy_forwarded_proto_header="X-Forwarded-Proto",
).run()
# Docker/Heroku environments use NGINX and must bind to a socket; others bind to address
if bool(int(os.environ['USING_NGINX'])):
root.info('TC_DEPLOY: Initialising Daphne with NGINX')
Server(
application=asgi.application,
endpoints=build_endpoint_description_strings(
unix_socket="/tmp/asgi.socket",
),
ping_interval=15,
ping_timeout=30,
websocket_timeout=10800, # 3 hours maximum length
websocket_connect_timeout=10,
application_close_timeout=10,
verbosity=2,
proxy_forwarded_address_header="X-Forwarded-For",
proxy_forwarded_port_header="X-Forwarded-Port",
proxy_forwarded_proto_header="X-Forwarded-Proto",
).run()
else:
root.info('TC_DEPLOY: Initialising Daphne with Host/Port')
Server(
application=asgi.application,
endpoints=build_endpoint_description_strings(
host="0.0.0.0",
port="8000",
),
ping_interval=15,
ping_timeout=30,
websocket_timeout=10800, # 3 hours maximum length
websocket_connect_timeout=10,
application_close_timeout=10,
verbosity=2,
proxy_forwarded_address_header="X-Forwarded-For",
proxy_forwarded_port_header="X-Forwarded-Port",
proxy_forwarded_proto_header="X-Forwarded-Proto",
).run()
19 changes: 18 additions & 1 deletion tabbycat/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import os
import logging
import sys

from split_settings.tools import optional, include


root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)

# ==============================================================================
# Environments
# ==============================================================================
Expand All @@ -11,17 +21,24 @@
'core.py',
]

if os.environ.get('GITHUB_CI', ''):
if os.environ.get('GITHUB_CI', '') and bool(os.environ['GITHUB_CI']):
base_settings.append('github.py')
root.info('SPLIT_SETTINGS: imported github.py')
elif os.environ.get('IN_DOCKER', '') and bool(int(os.environ['IN_DOCKER'])):
base_settings.append('docker.py')
root.info('SPLIT_SETTINGS: imported github.py')
elif os.environ.get('ON_HEROKU', ''):
base_settings.append('heroku.py')
root.info('SPLIT_SETTINGS: imported heroku.py')
elif os.environ.get('ON_RENDER', ''):
base_settings.append('render.py')
root.info('SPLIT_SETTINGS: imported render.py')
else:
base_settings.append('local.py')
if os.environ.get('LOCAL_DEVELOPMENT', ''):
base_settings.append('development.py')
root.info('SPLIT_SETTINGS: imported local.py & development.py')
else:
root.info('SPLIT_SETTINGS: imported local.py')

include(*base_settings)
2 changes: 0 additions & 2 deletions tabbycat/settings/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import os

print("Imported docker settings")

ALLOWED_HOSTS = ["*"]

DATABASES = {
Expand Down
2 changes: 0 additions & 2 deletions tabbycat/settings/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Github CI
# ==============================================================================

print("Imported CI settings")

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
Expand Down
2 changes: 0 additions & 2 deletions tabbycat/settings/heroku.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

from .core import TABBYCAT_VERSION

print("Imported heroku settings")

# ==============================================================================
# Heroku
# ==============================================================================
Expand Down
2 changes: 0 additions & 2 deletions tabbycat/settings/local.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
ON_LOCAL = True

print("Imported local settings")

# ==============================================================================
# Settings that you should specify
# ==============================================================================
Expand Down
6 changes: 0 additions & 6 deletions tabbycat/settings/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

from .core import TABBYCAT_VERSION

print("Imported render settings")

# ==============================================================================
# Render per https://render.com/docs/deploy-django
# ==============================================================================
Expand Down Expand Up @@ -49,10 +47,6 @@
# Redis
# ==============================================================================

REDIS_HOST = os.environ.get('REDIS_HOST')
REDIS_PORT = os.environ.get('REDIS_PORT')
print("render_debug_redis", "redis://" + REDIS_HOST + ":" + REDIS_PORT)

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
Expand Down

0 comments on commit 3a526c3

Please sign in to comment.