support postgres, mysql and sqlite #7

This commit is contained in:
Maxi Quoß
2022-01-16 17:46:22 +01:00
parent 3f0487ef6a
commit 60704ae333
8 changed files with 153 additions and 27 deletions

View File

@@ -5,7 +5,7 @@ FROM base as builder
ENV PYTHONUNBUFFERED 1
RUN apk update &&\
apk add python3-dev musl-dev build-base gcc libffi-dev libressl-dev postgresql-dev cargo &&\
apk add python3-dev musl-dev build-base gcc libffi-dev libressl-dev postgresql-dev mariadb-dev cargo &&\
rm -rf /var/cache/apk/* &&\
mkdir /install
WORKDIR /install
@@ -19,7 +19,7 @@ COPY --from=builder /install /usr/local
COPY app /app
WORKDIR /app
RUN apk update &&\
apk add iputils nmap curl bash &&\
apk add iputils nmap curl bash mariadb-dev &&\
rm -rf /var/cache/apk/*
HEALTHCHECK --interval=10s \

View File

@@ -30,10 +30,14 @@
## 🐳 Run your own instance
You can use the example [docker-compose.yml](docker-compose.yml) file and just run `docker-compose up -d`.
There are 3 example docker-compose files to choose from. The simplest is [docker-compose-sqlite.yml](docker-compose-sqlite.yml).
The website will be available at [localhost:8000](http://localhost:8000). If you run it on a different pc, it will be `http://<your-ip>:8000`. You can change the port in the docker-compose file.
### Databases
Upsnap supports 3 different databases. Postgres, MySQL and SQLite. If you already have an existing database you want to use, delete the database container from the compose file. Always make sure to set the correct database type environment variable, e.g. DB_TYPE=mysql
## 🔧 Available Env Vars
| env var | type | info |
@@ -45,9 +49,14 @@ The website will be available at [localhost:8000](http://localhost:8000). If you
| DJANGO_LANGUAGE_CODE | Str | Language code in RFC 3066 (e.g. "en-us" or "de") |
| DJANGO_TIME_ZONE | Str | e.g. Europe/Berlin |
| DJANGO_PORT | Int | Web port |
| POSTGRES_USER | Str | Database user |
| POSTGRES_PASSWORD | Str | Database password |
| POSTGRES_DB | Str | Database name |
| REDIS_HOST | Str | The ip redis runs on |
| REDIS_PORT | Int | The port redis runs on |
| DB_TYPE | Str | Database type. Can be "postgres", "mysql" or "sqlite" |
| DB_HOST | Str | The ip the database runs on |
| DB_PORT | Str | The port the database runs on|
| DB_NAME | Str | Database name |
| DB_USER | Str | Database user |
| DB_PASSWORD | Str | Database password |
| PING_INTERVAL | Int | Time between pings |
| ENABLE_NOTIFICATIONS | Bool | Show notifications in the bottom right corner |

View File

@@ -79,17 +79,41 @@ CELERY_BROKER_URL = f"redis://{os.getenv('REDIS_HOST', '127.0.0.1')}:{os.getenv(
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": os.getenv("POSTGRES_DB", "upsnap"),
"USER": os.getenv("POSTGRES_USER", "upsnap"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD", "upsnap"),
"HOST": os.getenv("POSTGRES_HOST", "127.0.0.1"),
"PORT": os.getenv("POSTGRES_PORT", 5432),
"OPTIONS": {"connect_timeout": 5},
if os.getenv("DB_TYPE") == "postgres":
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": os.getenv("DB_NAME", "upsnap"),
"USER": os.getenv("DB_USER", "upsnap"),
"PASSWORD": os.getenv("DB_PASSWORD", "upsnap"),
"HOST": os.getenv("DB_HOST", "127.0.0.1"),
"PORT": os.getenv("DB_PORT", 5432),
"OPTIONS": {
"connect_timeout": 30
}
}
}
elif os.getenv("DB_TYPE") == "mysql":
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": os.getenv("DB_NAME", "upsnap"),
"USER": os.getenv("DB_USER", "upsnap"),
"PASSWORD": os.getenv("DB_PASSWORD", "upsnap"),
"HOST": os.getenv("DB_HOST", "127.0.0.1"),
"PORT": os.getenv("DB_PORT", 3306),
"OPTIONS": {
"connect_timeout": 30
}
}
}
elif os.getenv("DB_TYPE") == "sqlite":
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME" : "db.sqlite3",
}
}
}
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

View File

@@ -1,7 +1,10 @@
#!/bin/sh
# wait for db and redis
/usr/bin/env bash ./wait-for-it.sh "${POSTGRES_HOST}":"${POSTGRES_PORT}" -t 300 -s
if [ "${DB_TYPE}" != "sqlite" ]; then
/usr/bin/env bash ./wait-for-it.sh "${DB_HOST}":"${DB_PORT}" -t 300 -s
sleep 20
fi
/usr/bin/env bash ./wait-for-it.sh "${REDIS_HOST}":"${REDIS_PORT}" -t 300 -s
python manage.py makemigrations

57
docker-compose-mysql.yml Normal file
View File

@@ -0,0 +1,57 @@
version: "3"
services:
upsnap_django:
container_name: upsnap_django
image: seriousm4x/upsnap:latest
network_mode: host
restart: unless-stopped
environment:
- DJANGO_SUPERUSER_USER=admin
- DJANGO_SUPERUSER_PASSWORD=admin
- DJANGO_SECRET_KEY=secret
- DJANGO_DEBUG=False
- DJANGO_LANGUAGE_CODE=de
- DJANGO_TIME_ZONE=Europe/Berlin
- DJANGO_PORT=8000
- REDIS_HOST=127.0.0.1
- REDIS_PORT=6379
- DB_TYPE=mysql
- DB_HOST=127.0.0.1
- DB_PORT=3306
- DB_NAME=upsnap
- DB_USER=upsnap
- DB_PASSWORD=upsnap
- PING_INTERVAL=5
- ENABLE_NOTIFICATIONS=True
depends_on:
- upsnap_redis
- upsnap_mysql
upsnap_redis:
container_name: upsnap_redis
image: redis:alpine
ports:
- "6379:6379"
restart: unless-stopped
healthcheck:
test: redis-cli ping
interval: 10s
upsnap_mysql:
container_name: upsnap_mysql
image: mysql
restart: unless-stopped
ports:
- "3306:3306"
environment:
- MYSQL_DATABASE=upsnap
- MYSQL_USER=upsnap
- MYSQL_PASSWORD=upsnap
- MYSQL_ROOT_PASSWORD=upsnap
cap_add:
- SYS_NICE
volumes:
- upsnap_db:/var/lib/mysql
healthcheck:
test: mysqladmin ping -h localhost -u $$MYSQL_USER -p$$MYSQL_PASSWORD
interval: 10s
volumes:
upsnap_db:

View File

@@ -15,11 +15,12 @@ services:
- DJANGO_PORT=8000
- REDIS_HOST=127.0.0.1
- REDIS_PORT=6379
- POSTGRES_HOST=127.0.0.1
- POSTGRES_PORT=5432
- POSTGRES_DB=upsnap
- POSTGRES_USER=upsnap
- POSTGRES_PASSWORD=upsnap
- DB_TYPE=postgres
- DB_HOST=127.0.0.1
- DB_PORT=5432
- DB_NAME=upsnap
- DB_USER=upsnap
- DB_PASSWORD=upsnap
- PING_INTERVAL=5
- ENABLE_NOTIFICATIONS=True
depends_on:

31
docker-compose-sqlite.yml Normal file
View File

@@ -0,0 +1,31 @@
version: "3"
services:
upsnap_django:
container_name: upsnap_django
image: seriousm4x/upsnap:latest
network_mode: host
restart: unless-stopped
environment:
- DJANGO_SUPERUSER_USER=admin
- DJANGO_SUPERUSER_PASSWORD=admin
- DJANGO_SECRET_KEY=secret
- DJANGO_DEBUG=False
- DJANGO_LANGUAGE_CODE=de
- DJANGO_TIME_ZONE=Europe/Berlin
- DJANGO_PORT=8000
- REDIS_HOST=127.0.0.1
- REDIS_PORT=6379
- DB_TYPE=sqlite
- PING_INTERVAL=5
- ENABLE_NOTIFICATIONS=True
depends_on:
- upsnap_redis
upsnap_redis:
container_name: upsnap_redis
image: redis:alpine
ports:
- "6379:6379"
restart: unless-stopped
healthcheck:
test: redis-cli ping
interval: 10s

View File

@@ -1,9 +1,10 @@
django
wakeonlan
celery[redis]
channels
channels-redis
celery[redis]
psycopg2-binary
whitenoise
django
gunicorn
mysqlclient
psycopg2-binary
uvicorn[standard]
wakeonlan
whitenoise