diff --git a/Makefile b/Makefile deleted file mode 100644 index b852cd6..0000000 --- a/Makefile +++ /dev/null @@ -1,17 +0,0 @@ -PYTHON = python3 -FLASK = flask -SQLITE = sqlite3 - -default: venv - -test: venv - test/all.sh - -venv: - $(PYTHON) -m venv $@ - . ./venv/bin/activate && pip3 install -r requirements.txt - -forum.db: - $(SQLITE) $@ < schema.txt - -.PHONY: test diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2230332 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: "3.7" + +services: + forum: + build: + context: ./forum/ + dockerfile: ./Dockerfile + args: + UID: ${UID} + GID: ${GID} + TZ: ${TZ} + volumes: + - "./forum/:/app" + ports: + - "${EXPOSE}:5000" + environment: + ADMINU: ${ADMINU} + ADMINP: ${ADMINP} diff --git a/example.env b/example.env new file mode 100644 index 0000000..7d6239a --- /dev/null +++ b/example.env @@ -0,0 +1,7 @@ + +UID=1000 +GID=1000 +TZ=Europe/Helsinki +ADMINU=admin +ADMINP=admin +EXPOSE=5000 diff --git a/forum/Dockerfile b/forum/Dockerfile new file mode 100644 index 0000000..531f94b --- /dev/null +++ b/forum/Dockerfile @@ -0,0 +1,36 @@ +FROM ubuntu:22.04 + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update -yqq \ + && apt-get install -y --no-install-recommends \ + sqlite3 \ + tzdata \ + git \ + make \ + python3-venv \ + python3-pip \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +ARG UID +ARG GID +ARG TZ +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +RUN groupadd -g $GID user && \ + useradd -u $UID -g $GID -ms /bin/bash user && \ + mkdir -p /opt/venv && chown $UID:$GID /opt/venv +COPY ./requirements.txt /requirements.txt +COPY docker-builder.sh / +USER user + +RUN bash /docker-builder.sh +#COPY ./ /app +#USER root +#RUN chown -R $UID:$GID /app +USER user + +WORKDIR /app + +COPY docker-entrypoint.sh / +CMD bash /docker-entrypoint.sh diff --git a/captcha.py b/forum/captcha.py similarity index 100% rename from captcha.py rename to forum/captcha.py diff --git a/db/sqlite.py b/forum/db/sqlite.py similarity index 100% rename from db/sqlite.py rename to forum/db/sqlite.py diff --git a/forum/docker-builder.sh b/forum/docker-builder.sh new file mode 100755 index 0000000..dd54adc --- /dev/null +++ b/forum/docker-builder.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -eux + +python3 -m venv /opt/venv +. /opt/venv/bin/activate +pip3 install -r requirements.txt diff --git a/forum/docker-entrypoint.sh b/forum/docker-entrypoint.sh new file mode 100755 index 0000000..67e8a8a --- /dev/null +++ b/forum/docker-entrypoint.sh @@ -0,0 +1,50 @@ +#!/bin/sh +PYTHON=python3 +SQLITE=sqlite3 + +export DB="/app/forum.db" +export SERVER=gunicorn +export PID="forum.pid" +export WORKERS=4 + +if [[ $( stat -c %u /app ) -ne $( id -u ) ]]; then + echo User id and /app folder owner do not match + printf 'UID: %s\nFolder: %s\n' $( id -u ) $( stat -c %u /app ) + exit 1 +fi + +set -eu +. /opt/venv/bin/activate +if [[ -e "$DB" ]]; then + $SQLITE -header "$DB" "SELECT version,name,description,registration_enabled,login_required FROM config" + echo Database already exists +else + password=$($PYTHON tool.py password "$ADMINP") + time=$($PYTHON -c 'import time; print(time.time_ns())') + version=$($PYTHON tool.py version) + + $SQLITE "$DB" -init schema.txt "insert into config ( + version, + name, + description, + secret_key, + captcha_key, + registration_enabled, + login_required +) +values ( + '$version', + 'Forum', + '', + '$(head -c 30 /dev/urandom | base64)', + '$(head -c 30 /dev/urandom | base64)', + 0, + 1 + )" + $SQLITE "$DB" " + insert into users (name, password, role, join_time) + values (lower('$ADMINU'), '$password', 2, $time) + " +fi + +exec "$SERVER" -w $WORKERS 'main:app' --pid="$PID" -b 0.0.0.0:5000 diff --git a/init_sqlite.sh b/forum/init_sqlite.sh similarity index 100% rename from init_sqlite.sh rename to forum/init_sqlite.sh diff --git a/main.py b/forum/main.py similarity index 100% rename from main.py rename to forum/main.py diff --git a/minimd.py b/forum/minimd.py similarity index 100% rename from minimd.py rename to forum/minimd.py diff --git a/password.py b/forum/password.py similarity index 100% rename from password.py rename to forum/password.py diff --git a/requirements.txt b/forum/requirements.txt similarity index 100% rename from requirements.txt rename to forum/requirements.txt diff --git a/forum/restart.sh b/forum/restart.sh new file mode 100755 index 0000000..a1997c7 --- /dev/null +++ b/forum/restart.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +set -e +SERVER="$1" +if [ -z "$SERVER" ] +then + echo "SERVER is not set" >&2 + exit 1 +fi + +case "$SERVER" in + dev) + touch main.py + ;; + gunicorn) + if [ -z "$PID" ] + then + echo "PID is not set" >&2 + exit 1 + fi + kill -hup $(cat "$PID") + ;; + *) + echo "Unsupported $SERVER" >&2 + exit 1 + ;; +esac diff --git a/run_sqlite.sh b/forum/run_sqlite.sh similarity index 100% rename from run_sqlite.sh rename to forum/run_sqlite.sh diff --git a/schema.txt b/forum/schema.txt similarity index 100% rename from schema.txt rename to forum/schema.txt diff --git a/forum/static/button.png b/forum/static/button.png new file mode 100644 index 0000000..4d49088 Binary files /dev/null and b/forum/static/button.png differ diff --git a/static/theme.css b/forum/static/theme.css similarity index 100% rename from static/theme.css rename to forum/static/theme.css diff --git a/templates/admin/base.html b/forum/templates/admin/base.html similarity index 100% rename from templates/admin/base.html rename to forum/templates/admin/base.html diff --git a/templates/admin/index.html b/forum/templates/admin/index.html similarity index 100% rename from templates/admin/index.html rename to forum/templates/admin/index.html diff --git a/templates/admin/query.html b/forum/templates/admin/query.html similarity index 100% rename from templates/admin/query.html rename to forum/templates/admin/query.html diff --git a/templates/base.html b/forum/templates/base.html similarity index 100% rename from templates/base.html rename to forum/templates/base.html diff --git a/templates/comment.html b/forum/templates/comment.html similarity index 100% rename from templates/comment.html rename to forum/templates/comment.html diff --git a/templates/comments.html b/forum/templates/comments.html similarity index 100% rename from templates/comments.html rename to forum/templates/comments.html diff --git a/templates/confirm_delete_comment.html b/forum/templates/confirm_delete_comment.html similarity index 100% rename from templates/confirm_delete_comment.html rename to forum/templates/confirm_delete_comment.html diff --git a/templates/confirm_delete_thread.html b/forum/templates/confirm_delete_thread.html similarity index 100% rename from templates/confirm_delete_thread.html rename to forum/templates/confirm_delete_thread.html diff --git a/templates/edit_comment.html b/forum/templates/edit_comment.html similarity index 100% rename from templates/edit_comment.html rename to forum/templates/edit_comment.html diff --git a/templates/edit_thread.html b/forum/templates/edit_thread.html similarity index 100% rename from templates/edit_thread.html rename to forum/templates/edit_thread.html diff --git a/templates/forum.html b/forum/templates/forum.html similarity index 100% rename from templates/forum.html rename to forum/templates/forum.html diff --git a/templates/help.html b/forum/templates/help.html similarity index 100% rename from templates/help.html rename to forum/templates/help.html diff --git a/templates/index.html b/forum/templates/index.html similarity index 100% rename from templates/index.html rename to forum/templates/index.html diff --git a/templates/login.html b/forum/templates/login.html similarity index 100% rename from templates/login.html rename to forum/templates/login.html diff --git a/templates/moderator.html b/forum/templates/moderator.html similarity index 100% rename from templates/moderator.html rename to forum/templates/moderator.html diff --git a/templates/new_thread.html b/forum/templates/new_thread.html similarity index 100% rename from templates/new_thread.html rename to forum/templates/new_thread.html diff --git a/templates/register.html b/forum/templates/register.html similarity index 100% rename from templates/register.html rename to forum/templates/register.html diff --git a/templates/thread.html b/forum/templates/thread.html similarity index 100% rename from templates/thread.html rename to forum/templates/thread.html diff --git a/templates/user_edit.html b/forum/templates/user_edit.html similarity index 100% rename from templates/user_edit.html rename to forum/templates/user_edit.html diff --git a/templates/user_info.html b/forum/templates/user_info.html similarity index 100% rename from templates/user_info.html rename to forum/templates/user_info.html diff --git a/test/all.sh b/forum/test/all.sh similarity index 100% rename from test/all.sh rename to forum/test/all.sh diff --git a/test/init_db.txt b/forum/test/init_db.txt similarity index 100% rename from test/init_db.txt rename to forum/test/init_db.txt diff --git a/tool.py b/forum/tool.py similarity index 100% rename from tool.py rename to forum/tool.py diff --git a/upgrade/sqlite/v0.1.sh b/forum/upgrade/sqlite/v0.1.sh similarity index 100% rename from upgrade/sqlite/v0.1.sh rename to forum/upgrade/sqlite/v0.1.sh diff --git a/upgrade_sqlite.sh b/forum/upgrade_sqlite.sh similarity index 100% rename from upgrade_sqlite.sh rename to forum/upgrade_sqlite.sh diff --git a/forum/version.py b/forum/version.py new file mode 100755 index 0000000..c87fc89 --- /dev/null +++ b/forum/version.py @@ -0,0 +1,3 @@ + +VERSION = "agreper-v0.1.1q1" + diff --git a/stop.sh b/stop.sh new file mode 100755 index 0000000..bdb6029 --- /dev/null +++ b/stop.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +set -e +docker-compose down forum