Skip to content

Commit

Permalink
Simplify old configs on settings and tasks.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Administrator committed Mar 12, 2024
1 parent 0ebdead commit dd33388
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 100 deletions.
21 changes: 12 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,25 @@ ENV PATH="$PATH:/root/.local/bin" \
RUN curl -sSL https://install.python-poetry.org | python3 - \
&& poetry --version

RUN pip install --upgrade pip
RUN poetry install

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 /src
WORKDIR /src
COPY ./src/ /src/
COPY ./docs/ /src/docs/
COPY ./locale/ /src/locale/
COPY ./tasks.py /src/tasks.py
COPY ./run.sh /src/run.sh
COPY ./etc/ /src/etc/

RUN pip install --upgrade pip
RUN mkdir -p /jandig/src /jandig/locale /jandig/docs /jandig/static /jandig/build

WORKDIR /jandig

COPY ./src/ /jandig/src/
COPY ./docs/ /jandig/docs/
COPY ./locale/ /jandig/locale/
COPY ./tasks.py /jandig/tasks.py
COPY ./run.sh /jandig/run.sh
COPY ./etc/ /jandig/etc/


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

Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
- postgres
- storage
- createbuckets
command: ./run.sh
command: /jandig/run.sh

postgres:
image: postgres:15.2
Expand All @@ -38,7 +38,7 @@ services:
- 9000:9000
- 9001:9001
volumes:
- storage:/storage
- media_data:/storage
env_file:
- .envs/.env
command: server /storage --console-address ":9001"
Expand All @@ -62,4 +62,4 @@ services:
volumes:
postgres_data:
storage:
media_data:
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
poetry show
poetry run inv collect db -p i18n --compile docs run -p -g
poetry run inv collect db i18n --compile docs run -g
47 changes: 19 additions & 28 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

from .wait_db import start_services

ROOT_DIR = environ.Path(__file__) - 2 # (src/config/settings.py - 2 = src/)

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_DIR = environ.Path("/jandig/")
BASE_DIR = "/jandig/src"

env = environ.Env()

Expand Down Expand Up @@ -140,27 +138,18 @@ def debug(request):

WSGI_APPLICATION = "config.wsgi.application"

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"HOST": env("POSTGRES_HOST"),
"NAME": env("POSTGRES_DB"),
"USER": env("POSTGRES_USER"),
"PASSWORD": env("POSTGRES_PASSWORD"),
},
}

if env.bool("DEV_DB", True):
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}
else:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"HOST": env("POSTGRES_HOST"),
"NAME": env("POSTGRES_DB"),
"USER": env("POSTGRES_USER"),
"PASSWORD": env("POSTGRES_PASSWORD"),
},
}

# STARTS SERVICES THAT DJANGO DEPENDS E.G. postgres
start_services()
# STARTS SERVICES THAT DJANGO DEPENDS E.G. postgres
start_services()


AUTH_PASSWORD_VALIDATORS = [
Expand Down Expand Up @@ -192,6 +181,8 @@ def debug(request):
LANGUAGES = (
("en-us", _("English")),
("pt-br", _("Brazilian Portuguese")),
("es-ES", _("Spain Spanish")),
("fr-FR", _("France French")),
)

TIME_ZONE = "UTC"
Expand Down Expand Up @@ -232,11 +223,11 @@ def debug(request):
os.path.join(BASE_DIR, "core", "static"),
os.path.join(BASE_DIR, "users", "static"),
]
COLLECT_DIR = os.path.dirname(os.path.dirname(BASE_DIR))
STATIC_ROOT = os.path.join(COLLECT_DIR, "collect")

# STATIC_ROOT = "/jandig/static"
STATICFILES_STORAGE = "config.storage_backends.StaticStorage"

MEDIA_ROOT = os.path.join(BASE_DIR, "users", "media")
# MEDIA_ROOT = os.path.join(BASE_DIR, "users", "media")

AWS_PUBLIC_MEDIA_LOCATION = "media/public"
DEFAULT_FILE_STORAGE = "config.storage_backends.PublicMediaStorage"
Expand All @@ -254,7 +245,7 @@ def debug(request):
LOGOUT_REDIRECT_URL = "home"

# Sphinx docs
DOCS_ROOT = os.path.join(BASE_DIR, "../../build/")
DOCS_ROOT = "/jandig/build/"

SMTP_SERVER = env("SMTP_SERVER", default="smtp.gmail.com")
SMTP_PORT = env("SMTP_PORT", default=587)
Expand Down
71 changes: 12 additions & 59 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,48 @@

python = sys.executable
directory = os.path.dirname(__file__)
sys.path.append('src')
sys.path.append('jandig')

#
# Call python manage.py in a more robust way
#
def robust_manage(ctx, cmd, env=None, **kwargs):
kwargs = {k.replace('_', '-'): v for k, v in kwargs.items() if v is not False}
opts = ' '.join(f'--{k} {"" if v is True else v}' for k, v in kwargs.items())
cmd = f'{python} /src/manage.py {cmd} {opts}'
cmd = f'{python} /jandig/src/manage.py {cmd} {opts}'
env = {**os.environ, **(env or {})}
path = env.get("PYTHONPATH", ":".join(sys.path))
env.setdefault('PYTHONPATH', f'src:{path}')
print(cmd)
ctx.run(cmd, pty=True, env=env)


def manage(ctx, cmd, postgres=False):
cmd = f'python3 /src/manage.py {cmd}'
ctx.run(cmd, pty=True, env=default_env(postgres))


def default_env(postgres):
os.environ['DEV_DB'] = 'True' if not postgres else 'False'
e = os.environ
return e
def manage(ctx, cmd):
cmd = f'python3 /jandig/src/manage.py {cmd}'
ctx.run(cmd, pty=True, env=os.environ)


@task
def run(ctx, ssl=False, gunicorn=False, postgres=False):
def run(ctx, ssl=False, gunicorn=False):
"""
Run development server
"""
if gunicorn:
ctx.run('gunicorn --reload --worker-connections=10000 --workers=4 --log-level debug --bind 0.0.0.0:8000 config.wsgi', env={"DEV_DB":"False"})
ctx.run('cd src && gunicorn --reload --worker-connections=10000 --workers=4 --log-level debug --bind 0.0.0.0:8000 config.wsgi')
else:
manage(ctx, "runserver 0.0.0.0:8000", postgres)

manage(ctx, "runserver 0.0.0.0:8000")


@task
def db(ctx, make=False, postgres=False):
def db(ctx, make=False):
"""
Run migrations
"""
if make:
manage(ctx, "makemigrations", postgres)
manage(ctx, "migrate", postgres)
manage(ctx, "makemigrations")
manage(ctx, "migrate")
else:
manage(ctx, "migrate", postgres)
manage(ctx, "migrate")


@task
Expand All @@ -63,46 +56,6 @@ def collect(ctx):
manage(ctx, "collectstatic --no-input --clear")


@task
def install_deps(ctx):
"""
Install all dependencies
"""
ctx.run('pip3 install -r src/requirements.txt')


@task
def docker(ctx, build=False):
command = 'sudo docker-compose -f docker/docker-compose.yml up'

if build:
command += ' --build'

ctx.run(command)


@task
def build_base(ctx, publish=False):
"""
Build base docker images
"""
command = './src/etc/scripts/build-base.sh'

if publish:
command += ' publish'

ctx.run(command)


@task
def init_production(ctx):
"""
Init production environment
"""
command = './src/etc/scripts/init-production.sh'
ctx.run(command)


#
# Translations
#
Expand Down

0 comments on commit dd33388

Please sign in to comment.