From 4ab091d6104c214614ab22d62dea5ec4888eebb1 Mon Sep 17 00:00:00 2001 From: Trey t Date: Mon, 26 Jun 2023 10:24:35 -0500 Subject: [PATCH] dokku shit --- docker-compose.yml | 57 ++++++++++++++++++++++++ dockerfile | 21 +++++++++ dokku_notes | 61 ++++++++++++++++++++++++++ misc/CHECKS | 0 misc/DOKKU_SCALE | 3 ++ misc/Procfile | 3 ++ misc/app.json | 7 +++ misc/uwsgi.ini | 10 +++++ requirements.txt | 34 +++++++++++++++ werkout_api/__init__.py | 3 ++ werkout_api/celery.py | 8 ++++ werkout_api/settings.py | 97 +++++++++++++++++++++++++++++++++++++---- 12 files changed, 296 insertions(+), 8 deletions(-) create mode 100644 docker-compose.yml create mode 100644 dockerfile create mode 100644 dokku_notes create mode 100644 misc/CHECKS create mode 100644 misc/DOKKU_SCALE create mode 100644 misc/Procfile create mode 100644 misc/app.json create mode 100644 misc/uwsgi.ini create mode 100644 requirements.txt create mode 100644 werkout_api/celery.py diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..97c63c2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,57 @@ +version: "3.9" + +services: + db: + image: postgres + volumes: + - database:/var/lib/postgresql/data + environment: + - POSTGRES_DB=werkout + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 + + web: + build: . + command: > + sh -c "python manage.py collectstatic --noinput && python manage.py migrate && python manage.py runserver 0.0.0.0:8000" + volumes: + - .:/code + ports: + - "8000:8000" + environment: + - POSTGRES_NAME=werkout + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + depends_on: + db: + condition: service_healthy + links: + - db + + redis: + image: redis:alpine + + celery: + restart: always + build: + context: . + command: celery -A werkout worker -l info + volumes: + - .:/code + environment: + - DB_HOST=db + - DB_NAME=werkout + - DB_USER=postgres + - DB_PASS=postgres + depends_on: + - db + - redis + - web + +volumes: + database: \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..d6d1c0d --- /dev/null +++ b/dockerfile @@ -0,0 +1,21 @@ +# syntax=docker/dockerfile:1 +FROM python:3.9.13 +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +RUN apt-get update + +RUN apt-get install -y swig libssl-dev dpkg-dev netcat +RUN pip install -U pip + +# Add the Dokku-specific files to their locations. +ADD misc/dokku/CHECKS /app/ +ADD misc/dokku/* /code/ + +WORKDIR /code +COPY requirements.txt /code/ +RUN pip install -r requirements.txt + +COPY . /code/ + +RUN /code/manage.py collectstatic --noinput \ No newline at end of file diff --git a/dokku_notes b/dokku_notes new file mode 100644 index 0000000..9721271 --- /dev/null +++ b/dokku_notes @@ -0,0 +1,61 @@ +docker ps +docker-compose exec web python manage.py makemigrations +---------------------------------------- +wget https://raw.githubusercontent.com/dokku/dokku/v0.28.1/bootstrap.sh +sudo DOKKU_TAG=v0.28.1 bash bootstrap.sh + +cat ~/.ssh/authorized_keys | dokku ssh-keys:add admin + +# server ip +dokku domains:set-global ###severip + +sudo dokku apps:create werkout +sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git +sudo dokku postgres:create werkout +sudo dokku plugin:install https://github.com/dokku/dokku-redis.git redis +sudo dokku redis:create werkout + +sudo dokku postgres:link werkout werkout +sudo dokku redis:link werkout werkout + +sudo dokku config:set --no-restart werkout SECRET_KEY=wz%C*@^$QLaFDc$u*wKZK9bV3CBMt7cj3wk2y^0vdu@2*pz7yco+p=7@sa% + +#if dev env +sudo dokku config:set --no-restart werkout IS_DEV=True + +#push code +# add werkoutDev and werkout to /etc/hosts +''' +Host brodkast + HostName 45.33.120.174 + User root + PreferredAuthentications publickey + IdentityFile /Users/treyt/.ssh/bk_linode + IdentitiesOnly yes + AddKeysToAgent yes +# ---------------------------- +Host brodkastDev + HostName 45.79.26.38 + User root + PreferredAuthentications publickey + IdentityFile /Users/treyt/.ssh/bk_linode + IdentitiesOnly yes + AddKeysToAgent yes +''' +git push dokku@werkoutDev:werkout dokku +git push dokku@werkout:werkout master + +dokku run werkout python manage.py createsuperuser + +#check if debug +dokku run werkout python manage.py shell +from django.conf import settings +print(settings.DEBUG) +exit() +---------------------------------------- +sudo dokku domains:add werkout dev.werkout.io +sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git +sudo dokku config:set --no-restart --global DOKKU_LETSENCRYPT_EMAIL=werkoutdev@treymail.com +sudo dokku letsencrypt:enable werkout +sudo dokku letsencrypt:cron-job --add +sudo dokku plugin:install https://github.com/dokku/dokku-redirect.git diff --git a/misc/CHECKS b/misc/CHECKS new file mode 100644 index 0000000..e69de29 diff --git a/misc/DOKKU_SCALE b/misc/DOKKU_SCALE new file mode 100644 index 0000000..da73f41 --- /dev/null +++ b/misc/DOKKU_SCALE @@ -0,0 +1,3 @@ +web=1 +worker=3 +beat=1 \ No newline at end of file diff --git a/misc/Procfile b/misc/Procfile new file mode 100644 index 0000000..6a35008 --- /dev/null +++ b/misc/Procfile @@ -0,0 +1,3 @@ +web: daphne werkout_api.asgi:application --port 5000 --bind 0.0.0.0 -v2 +beat: /usr/local/bin/celery -A werkout beat -linfo +worker: python3 manage.py runworker channel_layer -v2 \ No newline at end of file diff --git a/misc/app.json b/misc/app.json new file mode 100644 index 0000000..d790b9f --- /dev/null +++ b/misc/app.json @@ -0,0 +1,7 @@ +{ + "scripts": { + "dokku": { + "predeploy": "/code/manage.py migrate --noinput" + } + } +} \ No newline at end of file diff --git a/misc/uwsgi.ini b/misc/uwsgi.ini new file mode 100644 index 0000000..43c03a2 --- /dev/null +++ b/misc/uwsgi.ini @@ -0,0 +1,10 @@ +[uwsgi] +module=werkout.wsgi:application +master=True +vacuum=True +max-requests=5000 +http-socket=:5000 +processes=3 +harakiri=120 +single-interpreter=True +enable-threads=True \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b57a858 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,34 @@ +amqp==5.1.1 +asgiref==3.7.2 +billiard==4.1.0 +celery==5.3.1 +click==8.1.3 +click-didyoumean==0.3.0 +click-plugins==1.1.1 +click-repl==0.3.0 +defusedxml==0.7.1 +diff-match-patch==20230430 +Django==4.2.2 +django-debug-toolbar==4.1.0 +django-import-export==3.2.0 +djangorestframework==3.14.0 +et-xmlfile==1.1.0 +kombu==5.3.1 +MarkupPy==1.14 +odfpy==1.4.1 +openpyxl==3.1.2 +prompt-toolkit==3.0.38 +psycopg2==2.9.6 +python-dateutil==2.8.2 +pytz==2023.3 +PyYAML==6.0 +six==1.16.0 +sqlparse==0.4.4 +tablib==3.5.0 +typing_extensions==4.6.3 +tzdata==2023.3 +vine==5.0.0 +wcwidth==0.2.6 +whitenoise==6.4.0 +xlrd==2.0.1 +xlwt==1.3.0 diff --git a/werkout_api/__init__.py b/werkout_api/__init__.py index e69de29..857d1b3 100644 --- a/werkout_api/__init__.py +++ b/werkout_api/__init__.py @@ -0,0 +1,3 @@ +from .celery import app as celery_app + +__all__ = ['celery_app'] \ No newline at end of file diff --git a/werkout_api/celery.py b/werkout_api/celery.py new file mode 100644 index 0000000..be85e4a --- /dev/null +++ b/werkout_api/celery.py @@ -0,0 +1,8 @@ +import os +from celery import Celery + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'werkout.settings') + +app = Celery('werkout') +app.config_from_object('django.conf:settings', namespace='CELERY') +app.autodiscover_tasks() \ No newline at end of file diff --git a/werkout_api/settings.py b/werkout_api/settings.py index 2eec5b4..b9028ac 100644 --- a/werkout_api/settings.py +++ b/werkout_api/settings.py @@ -9,7 +9,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-o_0sbr3lxcy#_r#imo4tl0cw*%@*__2a48dcd6hbp&u9b5dx=1' + # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -76,13 +76,6 @@ WSGI_APPLICATION = 'werkout_api.wsgi.application' # Database # https://docs.djangoproject.com/en/4.1/ref/settings/#databases -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', - } -} - # Password validation # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators @@ -123,3 +116,91 @@ STATIC_URL = 'static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, "media") + + +if os.environ.get("DATABASE_URL"): + ALLOWED_HOSTS = ['*'] + + # if os.environ.get("IS_DEV"): + # DEBUG = True + # PUSH_NOTIFICATIONS_SETTINGS = { + # "APNS_CERTIFICATE": "certs/dev/prod_aps.pem", + # "APNS_TOPIC": "io.brodkast.ios-Dev", + # "APNS_TEAM_ID": "JCU65VV9D9", + # "APNS_USE_SANDBOX": False + # } + # else: + # DEBUG = False + # PUSH_NOTIFICATIONS_SETTINGS = { + # "APNS_CERTIFICATE": "certs/prod/prod_aps.pem", + # "APNS_TOPIC": "io.brodkast.ios", + # "APNS_TEAM_ID": "JCU65VV9D9", + # "APNS_USE_SANDBOX": False + # } + + CSRF_TRUSTED_ORIGINS = ['https://*.werkout.io'] + SECRET_KEY = os.environ.get("SECRET_KEY", 'secret') + + # Parse the DATABASE_URL env var. + USER, PASSWORD, HOST, PORT, NAME = re.match("^postgres://(?P.*?)\:(?P.*?)\@(?P.*?)\:(?P\d+)\/(?P.*?)$", os.environ.get("DATABASE_URL", "")).groups() + + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': NAME, + 'USER': USER, + 'PASSWORD': PASSWORD, + 'HOST': HOST, + 'PORT': int(PORT), + } + } + + CELERY_BROKER_URL = os.environ.get("REDIS_URL", "") + "/1" + CELERY_RESULT_BACKEND = os.environ.get("REDIS_URL", "") + "/1" + + CHANNEL_LAYERS = { + 'default': { + 'BACKEND': 'channels_redis.core.RedisChannelLayer', + "CONFIG": { + "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')], + } + } + } + + INTERNAL_IPS = [ + "127.0.0.1", + ] +else: + DEBUG = True + ALLOWED_HOSTS = ['*'] + SECRET_KEY = 'django-insecure-o_0sbr3lxcy#_r#imo4tl0cw*%@*__2a48dcd6hbp&u9b5dx=1' + + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } + } + + CELERY_BROKER_URL = "redis://redis:6379" + CELERY_RESULT_BACKEND = "redis://redis:6379" + + CHANNEL_LAYERS = { + 'default': { + 'BACKEND': 'channels_redis.core.RedisChannelLayer', + 'CONFIG': { + 'hosts': [('redis', 6379)], + } + } + } + + INTERNAL_IPS = [ + "127.0.0.1", + ] + + # PUSH_NOTIFICATIONS_SETTINGS = { + # "APNS_CERTIFICATE": "certs/dev/dev_aps.pem", + # "APNS_TOPIC": "io.brodkast.ios-Dev", + # "APNS_TEAM_ID": "JCU65VV9D9", + # "APNS_USE_SANDBOX": True + # } \ No newline at end of file