Skip to content

Commit

Permalink
simpilify and QOL imporve
Browse files Browse the repository at this point in the history
  • Loading branch information
Max 🍉 committed Feb 11, 2024
1 parent 9641a61 commit 8051924
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 58 deletions.
16 changes: 8 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,17 @@ RUN apk del build-dependencies
COPY src/ ${CODE_DIR}/
RUN ln -s ${CODE_DIR}/chaosctl /usr/bin/chaosctl && chmod +x /usr/bin/chaosctl

#----------------------------------------------------------------------------------------------------------------------
# Set launch config
#----------------------------------------------------------------------------------------------------------------------
# Define Static files volume
VOLUME ${CODE_DIR}/static
VOLUME ${CODE_DIR}/media

#----------------------------------------------------------------------------------------------------------------------
# Set for Sekurity
#----------------------------------------------------------------------------------------------------------------------
RUN chown -R ${APP_USER}:${APP_GROUP} ${CODE_DIR}/* && \
RUN chown -R ${APP_USER}:${APP_GROUP} ${CODE_DIR}/ && \
chmod -R 540 ${CODE_DIR}/* && \
chmod -R 774 ${CODE_DIR}/static && \
chmod -R 774 ${CODE_DIR}/staticfiles && \
Expand All @@ -84,13 +91,6 @@ RUN chown -R ${APP_USER}:${APP_GROUP} ${CODE_DIR}/* && \
# Change to a non-root user - Beacuse we dont want anybody being naughty if they ever manage to get in ;P
USER ${APP_USER}:${APP_GROUP}

#----------------------------------------------------------------------------------------------------------------------
# Set launch config
#----------------------------------------------------------------------------------------------------------------------
# Define Static files volume
VOLUME ${CODE_DIR}/static
VOLUME ${CODE_DIR}/media

# Expose the HTTP TCP socket - this way Nginx can do all the hard work
EXPOSE 40269

Expand Down
53 changes: 13 additions & 40 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
depends_on:
- gunicorn
volumes:
- ${STATIC_VOLUME_PATH}:/code/static
- static:/code/static
- ${MEDIA_VOLUME_PATH}:/code/mediafiles

trunk-player-frontend:
Expand All @@ -27,24 +27,17 @@ services:
image: trunkplayer/trunkplayer-ng-api:latest
build: .
command:
- /bin/sh
- '-c'
- |
sleep 5; \
python -m gevent.monkey manage.py migrate && \
python -m gevent.monkey manage.py createsuperuser --noinput; \
python -m gevent.monkey manage.py collectstatic --no-input; \
gunicorn -c trunkplayer_ng/gunicorn.conf.py
- chaosctl
- web
# - --init
env_file:
- .env
environment:
DB_HOST: "database"
PROMETHEUS_MULTIPROC_DIR: /opt/chaos.corp/tpng/metrics
CELERY_BROKER_URL: "amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq:5672"
volumes:
- ${STATIC_VOLUME_PATH}:/opt/chaos.corp/tpng/static
- static:/opt/chaos.corp/tpng/static
- ${MEDIA_VOLUME_PATH}:/opt/chaos.corp/tpng/mediafiles
- metrics:/opt/chaos.corp/tpng/metrics
depends_on:
- database
- rabbitmq
Expand All @@ -56,30 +49,16 @@ services:
deploy:
replicas: ${WORKER_REPLICAS}
command:
- celery
- '-A'
- 'trunkplayer_ng'
- 'worker'
- '-l'
- 'info'
- '--pool'
- 'gevent'
- '--concurrency'
- '100'
- '-E'
- '-Q'
- |-
default,transmission_forwarding,radio_refrence,radio_alerts,transmission_ingest,tranmission_push
- chaosctl
- worker
env_file:
- .env
environment:
DB_HOST: "database"
PROMETHEUS_MULTIPROC_DIR: /opt/chaos.corp/tpng/metrics
CELERY_BROKER_URL: "amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq:5672"
volumes:
- ${STATIC_VOLUME_PATH}:/opt/chaos.corp/tpng/static
- static:/opt/chaos.corp/tpng/static
- ${MEDIA_VOLUME_PATH}:/opt/chaos.corp/tpng/mediafiles
- metrics:/opt/chaos.corp/tpng/metrics
depends_on:
- database
- rabbitmq
Expand All @@ -88,23 +67,17 @@ services:
restart: always
image: trunkplayer/trunkplayer-ng-api:latest
build: .
command:
- celery
- '-A'
- 'trunkplayer_ng'
- 'beat'
- '-l'
- 'info'
command:
- chaosctl
- beat
env_file:
- .env
environment:
DB_HOST: "database"
PROMETHEUS_MULTIPROC_DIR: /opt/chaos.corp/tpng/metrics
CELERY_BROKER_URL: "amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq:5672"
volumes:
- ${STATIC_VOLUME_PATH}:/opt/chaos.corp/tpng/static
- static:/opt/chaos.corp/tpng/static
- ${MEDIA_VOLUME_PATH}:/opt/chaos.corp/tpng/mediafiles
- metrics:/opt/chaos.corp/tpng/metrics
depends_on:
- database
- rabbitmq
Expand All @@ -129,4 +102,4 @@ services:
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}

volumes:
metrics:
static:
2 changes: 1 addition & 1 deletion nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ARG APP_GID=1000
#----------------------------------------------------------------------------------------------------------------------
RUN addgroup -g ${APP_GID} ${APP_GROUP} && \
adduser --ingroup ${APP_GROUP} -D -u ${APP_UID} --no-create-home -s /bin/bash ${APP_USER} && \
adduser nginx ${AUX_GROUP}
addgroup nginx ${APP_GROUP}

# --------------------------------------------------------
# NGINX CONFIG SETUP
Expand Down
88 changes: 88 additions & 0 deletions src/chaosctl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,90 @@
#!/bin/bash

#!/bin/bash

THREADS=${THREADS:-100}
LOG_LEVEL=${LOG_LEVEL:-info}

# Function to display help message
show_help() {
echo "Chaos Control"
echo "Usage: chaosctl [command]"
echo "Commands:"
echo " migrate Run database migrations"
echo " createsuperuser Create a superuser"
echo " staticfiles Collect static files"
echo " web [--init] Launch the web application, add --init to migrate"
echo " worker Launch the bacground worker"
echo " beat Launch the beat timer"
}

# Check if at least one argument is provided
if [ $# -lt 1 ]; then
echo "Error: No command provided."
show_help
exit 1
fi

createsuperuser() {
echo -e "\n[+]Creating a superuser for environment: $3..."
python /opt/chaos.corp/tpng/manage.py createsuperuser --noinput;
}

migrate() {
echo -e "\n[+]Running database migrations..."
python /opt/chaos.corp/tpng/manage.py migrate
}

collect_static() {
echo -e "\n[+]Collecting static files..."
python /opt/chaos.corp/tpng/manage.py collectstatic --noinput
}

start_gunicorn() {
echo -e "\n[+]Starting gunicorn..."
gunicorn -c trunkplayer_ng/gunicorn.conf.py
}

start_beat() {
echo -e "\n[+]Starting Celery beat..."
celery -A trunkplayer_ng beat -l $LOG_LEVEL
}

start_worker() {
echo -e "\n[+]Starting celerry worker $THREADS:THREADS..."
celery -A trunkplayer_ng worker -l $LOG_LEVEL --pool gevent --concurrency $THREADS -E -Q default,transmission_forwarding,radio_refrence,radio_alerts,transmission_ingest,tranmission_push
}

# Process commands
case "$1" in
web)
if [[ "$2" == "--init" ]]; then
migrate
createsuperuser
collect_static
start_gunicorn
else
start_gunicorn
fi
;;

beat)
start_beat
;;

worker)
start_worker
;;

help)
show_help
exit 0
;;

*)
echo "Error: Unknown command '$1'."
show_help
exit 1
;;
esac

5 changes: 2 additions & 3 deletions src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ python-json-logger
django-storages
boto3
splunk_handler
zeep
django-prometheus
prometheus-client
zeep # RR Client
paho-mqtt # MQTT
6 changes: 4 additions & 2 deletions src/trunkplayer_ng/gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import gevent.monkey

gevent.monkey.patch_all()
from trunkplayer_ng.mqtt import GeventMqttExample, launch


import multiprocessing
from psycogreen.gevent import patch_psycopg
Expand Down Expand Up @@ -29,12 +32,11 @@

wsgi_app='trunkplayer_ng.wsgi'


def post_fork(server, worker):
patch_psycopg()
worker.log.info("Monkey Patched Thread 🙊")



@close_connection
def foo_func():
#logger.debug("[-] DB CONN CLOSED")
Expand Down
3 changes: 0 additions & 3 deletions src/trunkplayer_ng/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@
'django_filters',
"storages",
# 'dj_rest_auth'
# 'django_prometheus',

]

Expand All @@ -244,7 +243,6 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#std:setting-MIDDLEWARE
######################################################################
MIDDLEWARE = [
# 'django_prometheus.middleware.PrometheusBeforeMiddleware',
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand All @@ -254,7 +252,6 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
# 'django_prometheus.middleware.PrometheusAfterMiddleware',
]

######################################################################
Expand Down
1 change: 0 additions & 1 deletion src/trunkplayer_ng/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@
),
path("swagger.json", schema_view.without_ui(cache_timeout=0), name="schema-json"),
path("redoc/", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc"),
path('', include('django_prometheus.urls')),
]

0 comments on commit 8051924

Please sign in to comment.