Skip to content

Home Information - Your home, your data, organized.

Python

Added (untested) scaffolding for running in docker container.

Anthony Cassandra26682c21c36a
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 00000000..95245dac
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,31 @@
+FROM python:3.11
+
+RUN apt-get update \
+ && apt-get install -y --no-install-recommends supervisor nginx redis-server \
+ && mkdir -p /var/log/supervisor \
+ && mkdir -p /etc/supervisor/conf.d \
+ && rm -rf /var/lib/apt/lists/*
+
+WORKDIR /src
+
+ENV PYTHONDONTWRITEBYTECODE 1
+ENV PYTHONUNBUFFERED 1
+ENV PYTHONPATH /src
+
+RUN pip install --upgrade pip
+
+# Assumes base.txt is all that is needed (ignores dev-specific dependencies)
+COPY src/hi/requirements/base.txt /src/requirements.txt
+RUN pip install --no-cache-dir --root-user-action=ignore -r requirements.txt
+
+COPY packaging/supervisor-interface.conf /etc/supervisor/conf.d/waa-interface.conf
+COPY packaging/nginx-interface.conf /etc/nginx/sites-available/default
+
+COPY src /src
+
+EXPOSE 8000
+
+VOLUME /data
+RUN mkdir -p /data
+
+CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf" ]
diff --git a/HI_VERSION b/HI_VERSION
new file mode 100644
index 00000000..8acdd82b
--- /dev/null
+++ b/HI_VERSION
@@ -0,0 +1 @@
+0.0.1
diff --git a/Makefile b/Makefile
new file mode 100644
index 00000000..1f51ede7
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,18 @@
+export HI_VERSION = $(shell cat HI_VERSION)
+
+NOW_DATE := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
+
+.SECONDARY:
+
+docker-build: Dockerfile
+ docker build \
+ --label "name=hi" \
+ --label "version=$(HI_VERSION)" \
+ --label "build-date=$(NOW_DATE)" \
+ --tag hi:$(HI_VERSION) .
+
+docker-run: Dockerfile flutter-webapp-build
+ ./run_container.sh
+
+docker-stop:
+ docker stop hi
diff --git a/packaging/nginx-interface.conf b/packaging/nginx-interface.conf
new file mode 100644
index 00000000..a5a4580d
--- /dev/null
+++ b/packaging/nginx-interface.conf
@@ -0,0 +1,72 @@
+server_tokens off;
+add_header X-XSS-Protection "1; mode=block";
+add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains;';
+add_header Permissions-Policy "geolocation=(self),autoplay=(self),camera=(),accelerometer=(),magnetometer=(self),ambient-light-sensor=(),gyroscope=(self),speaker=(self),vibrate=(self),fullscreen=(self),midi=(),encrypted-media=(),microphone=(),usb=(),picture-in-picture=(),vr=()";
+
+upstream wsgi_server {
+ server unix:/var/run/gunicorn.sock fail_timeout=0;
+}
+
+upstream asgi_server {
+ server unix:/var/run/daphne.sock fail_timeout=0;
+}
+
+server {
+
+ listen 0.0.0.0:8000;
+ listen [::]:8000;
+ server_name wsgi_server;
+
+ client_max_body_size 20M;
+
+ location /favicon.ico {
+ alias /src/static/favicon.ico;
+ access_log off;
+ log_not_found off;
+ }
+
+ location /static/ {
+ add_header Referrer-Policy "strict-origin";
+ add_header Content-Security-Policy "default-src 'self'; base-uri 'self'; connect-src 'self'; img-src 'self'; form-action 'self'; frame-ancestors 'self'; frame-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; manifest-src 'self'; font-src 'self'; style-src 'self' 'unsafe-inline' 'unsafe-eval';";
+ add_header X-Content-Type-Options "nosniff";
+ alias /src/static/;
+ autoindex off;
+ }
+
+ # Use this is need to troubleshoot inside Fargate when healthcheck is failing.
+ #
+ # location /healthcheck {
+ # default_type text/plain;
+ # return 200 "healthy\n";
+ # }
+
+ location /ws/ {
+ proxy_set_header Upgrade $http_upgrade;
+ proxy_set_header Connection "upgrade";
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Host $server_name;
+
+ proxy_read_timeout 86400;
+ proxy_redirect off;
+ proxy_http_version 1.1;
+
+ if (!-f $request_filename) {
+ proxy_pass http://asgi_server;
+ break;
+ }
+ }
+
+ location / {
+ index index.html;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header Host $http_host;
+ proxy_set_header X-Real-IP $remote_addr;
+
+ proxy_redirect off;
+
+ proxy_pass http://wsgi_server;
+ }
+
+}
diff --git a/packaging/supervisor-interface.conf b/packaging/supervisor-interface.conf
new file mode 100644
index 00000000..d48b1712
--- /dev/null
+++ b/packaging/supervisor-interface.conf
@@ -0,0 +1,46 @@
+[supervisord]
+user=root
+nodaemon=true
+
+[program:redis]
+command=/usr/bin/redis-server
+autostart=true
+autorestart=true
+stdout_logfile=/var/log/supervisor/redis.log
+stderr_logfile=/var/log/supervisor/redis_err.log
+
+
+[program:wsgi_gunicorn]
+user=root
+command=/src/bin/docker-start-gunicorn.sh
+directory=/src
+stdout_logfile=/dev/fd/1
+stdout_logfile_maxbytes=0
+redirect_stderr=true
+stopsignal=KILL
+stopasgroup=true
+autostart=true
+autorestart=true
+
+[program:asgi_daphne]
+user=root
+command=/src/bin/docker-start-daphne.sh
+directory=/src
+stdout_logfile=/dev/fd/1
+stdout_logfile_maxbytes=0
+redirect_stderr=true
+stopsignal=KILL
+stopasgroup=true
+autostart=true
+autorestart=true
+
+[program:nginx]
+user=root
+username=root
+command=/usr/sbin/nginx -g "daemon off;"
+priority=900
+stdout_logfile=/dev/fd/1
+stdout_logfile_maxbytes=0
+redirect_stderr=true
+autostart=true
+autorestart=true
diff --git a/run_container.sh b/run_container.sh
new file mode 100755
index 00000000..eb66baec
--- /dev/null
+++ b/run_container.sh
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+ENV_VAR_FILE=".private/env/stage.sh"
+HI_DATA_DIR="/var/hi/data"
+EXTERNAL_PORT=8000
+BACKGROUND_FLAGS=""
+
+usage() {
+ echo "Usage: $0 [-env ENV_VAR_FILE] [-dir HI_DATA_DIR] [-port EXTERNAL_PORT] [-bg]"
+ exit 1
+}
+
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ -env)
+ ENV_VAR_FILE="$2"
+ shift 2
+ ;;
+ -dir)
+ HI_DATA_DIR="$2"
+ shift 2
+ ;;
+ -port)
+ EXTERNAL_PORT="$2"
+ shift 2
+ ;;
+ -bg)
+ BACKGROUND_FLAGS="-d"
+ shift 1
+ ;;
+ *)
+ usage
+ ;;
+ esac
+done
+
+if [[ ! -f "$ENV_VAR_FILE" ]]; then
+ echo "Error: Environment file '$ENV_VAR_FILE' does not exist."
+ exit 1
+fi
+
+if [[ -d "$HI_DATA_DIR" ]]; then
+ if [[ ! -w "$HI_DATA_DIR" ]]; then
+ echo "Error: Directory '$HI_DATA_DIR' exists but is not writable."
+ exit 1
+ fi
+else
+ echo "Creating directory '$HI_DATA_DIR'..."
+ mkdir -p "$HI_DATA_DIR"
+ sudo chown -R "$USER:$USER" "$HI_DATA_DIR"
+ chmod -R 775 "$HI_DATA_DIR"
+fi
+
+if ! docker image inspect hi > /dev/null 2>&1; then
+ echo "Error: Docker image 'hi' does not exist. Please build it first."
+ exit 1
+fi
+
+docker run $BACKGROUND_FLAGS \
+ --name hi \
+ --env-file "$ENV_VAR_FILE" \
+ -v "$HI_DATA_DIR:/data" \
+ -p "$EXTERNAL_PORT:8000" hi
diff --git a/src/bin/asgi_worker_start.sh b/src/bin/asgi_worker_start.sh
new file mode 100755
index 00000000..fc0b825d
--- /dev/null
+++ b/src/bin/asgi_worker_start.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+if [ $# -lt 1 ] ; then
+ echo "Usage: $0 { dev, stage, prod } [bin bind_arg]"
+ exit 1
+fi
+
+HIENV=$1
+shift
+NAME="asgi_worker"
+HOMEDIR=/home/hiuser
+PROJECTDIR=$HOMEDIR/hi
+VIRTUALENVDIR=$HOMEDIR/.virtualenvs/hi_$HIENV
+DJANGODIR=$PROJECTDIR/src
+
+if [ ! -d $VIRTUALENVDIR ] ; then
+ echo "Did not find virtualenv directory: $VIRTUALENVDIR"
+ exit 2
+fi
+
+echo "Starting $NAME as `whoami`"
+
+# Activate the virtual environment
+cd $DJANGODIR
+. ${VIRTUALENVDIR}/bin/activate
+. ${VIRTUALENVDIR}/bin/postactivate
+
+export PYTHONPATH=$DJANGODIR:$PYTHONPATH
+
+# Start your Django ASGI worker
+# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
+exec ${DJANGODIR}/manage.py runworker channels websocket
diff --git a/src/bin/daphne_start.sh b/src/bin/daphne_start.sh
new file mode 100755
index 00000000..ab239605
--- /dev/null
+++ b/src/bin/daphne_start.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+if [ $# -lt 1 ] ; then
+ echo "Usage: $0 { dev, stage, prod } [bin bind_arg]"
+ exit 1
+fi
+
+HIENV=$1
+shift
+NAME="daphne"
+HOMEDIR=/home/hiuser
+PROJECTDIR=$HOMEDIR/hi
+VIRTUALENVDIR=$HOMEDIR/.virtualenvs/hi_$HIENV
+DJANGODIR=$PROJECTDIR/src
+SOCKFILE=$PROJECTDIR/run/daphne.sock
+DJANGO_ASGI_MODULE=hi.asgi
+
+if [ ! -d $VIRTUALENVDIR ] ; then
+ echo "Did not find virtualenv directory: $VIRTUALENVDIR"
+ exit 2
+fi
+
+echo "Starting $NAME as `whoami`"
+
+# Activate the virtual environment
+cd $DJANGODIR
+. ${VIRTUALENVDIR}/bin/activate
+. ${VIRTUALENVDIR}/bin/postactivate
+
+export PYTHONPATH=$DJANGODIR:$PYTHONPATH
+
+# Create the run directory if it doesn't exist
+RUNDIR=$(dirname $SOCKFILE)
+test -d $RUNDIR || mkdir -p $RUNDIR
+
+# For testing so can connect
+if [ "x$1" = 'xbind' ] ; then
+ BINDARG=$2
+else
+ BINDARG=unix:$SOCKFILE
+fi
+
+# Start your Daphne
+# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
+
+exec ${VIRTUALENVDIR}/bin/daphne \
+ -u ${SOCKFILE} \
+ --root-path=${VIRTUALENVDIR} \
+ --access-log=- \
+ ${DJANGO_ASGI_MODULE}:application
diff --git a/src/bin/docker-start-daphne.sh b/src/bin/docker-start-daphne.sh
new file mode 100755
index 00000000..6cda958b
--- /dev/null
+++ b/src/bin/docker-start-daphne.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+SOCKFILE=/var/run/daphne.sock
+
+exec daphne \
+ -u ${SOCKFILE} \
+ --access-log=- \
+ hi.asgi:application
diff --git a/src/bin/docker-start-gunicorn.sh b/src/bin/docker-start-gunicorn.sh
new file mode 100755
index 00000000..92c7e279
--- /dev/null
+++ b/src/bin/docker-start-gunicorn.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+NUM_WORKERS=3
+NUM_THREADS=3
+BINDARG=unix:/var/run/gunicorn.sock
+
+# Make sure static files in the proper place
+./manage.py collectstatic --noinput
+
+exec gunicorn hi.wsgi:application \
+ --name gunicorn \
+ --workers $NUM_WORKERS \
+ --threads $NUM_THREADS \
+ --bind=$BINDARG \
+ --log-file=-
diff --git a/src/bin/gunicorn_start.sh b/src/bin/gunicorn_start.sh
new file mode 100755
index 00000000..aa39a86b
--- /dev/null
+++ b/src/bin/gunicorn_start.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+if [ $# -lt 1 ] ; then
+ echo "Usage: $0 { dev, stage, prod } [bin bind_arg]"
+ exit 1
+fi
+
+HIENV=$1
+shift
+NAME="gunicorn"
+HOMEDIR=/home/hiuser
+PROJECTDIR=$HOMEDIR/hi
+VIRTUALENVDIR=$HOMEDIR/.virtualenvs/hi_$HIENV
+DJANGODIR=$PROJECTDIR/src
+SOCKFILE=$PROJECTDIR/run/gunicorn.sock
+USER=hiuser
+GROUP=hiusers
+NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
+DJANGO_WSGI_MODULE=hi.wsgi
+
+if [ ! -d $VIRTUALENVDIR ] ; then
+ echo "Did not find virtualenv directory: $VIRTUALENVDIR"
+ exit 2
+fi
+
+echo "Starting $NAME as `whoami`"
+
+# Activate the virtual environment
+cd $DJANGODIR
+. ${VIRTUALENVDIR}/bin/activate
+. ${VIRTUALENVDIR}/bin/postactivate
+
+export PYTHONPATH=$DJANGODIR:$PYTHONPATH
+
+# Make sure static files in the proper place
+./manage.py collectstatic --noinput
+
+# Create the run directory if it doesn't exist
+RUNDIR=$(dirname $SOCKFILE)
+test -d $RUNDIR || mkdir -p $RUNDIR
+
+# For testing so can connect
+if [ "x$1" = 'xbind' ] ; then
+ BINDARG=$2
+else
+ BINDARG=unix:$SOCKFILE
+fi
+
+# Start your Django Unicorn
+# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
+exec ${VIRTUALENVDIR}/bin/gunicorn \
+ -c gunicorn.conf.py \
+ ${DJANGO_WSGI_MODULE}:application \
+ --name $NAME \
+ --workers $NUM_WORKERS \
+ --user=$USER --group=$GROUP \
+ --bind=$BINDARG \
+ --log-level=debug \
+ --log-file=-