Merge pull request #148 from Nebucatnetzer/overmind

Overmind
This commit is contained in:
Andreas Zweili 2023-07-18 21:00:38 +02:00 committed by GitHub
commit 3734f26947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 93 additions and 49 deletions

View File

@ -1,4 +1,9 @@
[paths]
source = ${PROJECT_DIR-default .}/src/
[html]
directory = ${PROJECT_DIR-default .}/htmlcov
[run]
data_file = ${PROJECT_DIR-default .}/.coverage
omit =
*apps.py,
*migrations/*,

12
.envrc
View File

@ -1,2 +1,14 @@
use flake
eval "$shellHook"
layout_postgres() {
export PGDATA="$(direnv_layout_dir)/postgres"
export PGHOST="$PGDATA"
if [[ ! -d "$PGDATA" ]]; then
initdb
echo -e "listen_addresses = 'localhost'\nunix_socket_directories = '$PGHOST'" >> "$PGDATA/postgresql.conf"
echo "CREATE DATABASE django;" | postgres --single -E postgres
fi
}
layout postgres
export PROJECT_DIR=$(pwd)
watch_file "$PGDATA/postgresql.conf"

2
.gitignore vendored
View File

@ -173,7 +173,7 @@ migrations/
.vscode/
.pytest_cache/
htmlcov/
.second_run
.first_run
/src/static
.idea/

2
Procfile Normal file
View File

@ -0,0 +1,2 @@
web: python ./src/manage.py runserver 0.0.0.0:8000
db: postgres

View File

@ -14,26 +14,62 @@ inventory over my various servers and other network equipment.
## Development Setup
There are two ways to work on this project.
For the first one you will need to install the Nix package manager[^1].
Afterwards you can enter the development environment with `nix develop`.
There is currently only one supported way to work with this repository. You
will need a Linux system (WSL might work) onto wich you install the Nix package
manager with Flakes enabled[^1] and direnv[^3]. Afterwards you can enter the
development environment with `direnv allow`.
For the other way you have to install poetry[^2] and then run `poetry shell` to
enter the virtual environment.
[^1]: https://nixos.org/download.html
[^3]: https://direnv.net/
After you've entered the development environment with either method you can
start the development server with `dev run`. This will start a PostgreSQL
database running and start the Django development server.
_It will prompt you for your sudo password because it opens port 8000 in your
firewall. This is because I sometimes develope from my iPad on my notebook and
with this tweak I can access the dev server running on my notebook._
You can then access the project in the browser under the FQDN of your
computer. E.g. `http://mypc.domain.local:8000`.
In case you want a fresh start or remove the project you can just remove the
`.direnv` directory at the root of the project. All the data of the PostgreSQL
database is stored there together with the symlinks to the Nix store.
In case you want to tweak something these are the applications use do build the
development environment:
- Nix package manager
- direnv
- overmind[^4]
The `dev` command is a simple BASH script called `dev.sh` at the root of the
project.
[^4]: https://github.com/DarthSim/overmind
Run the `dev` command without an argument to see all options.
> Why aren't you using Docker/containers for development.
_I think containers have their uses but developing with them is in my opinion a
pain in the ass. You just can't easily interact with the tools inside the
container and you have to hack around to get your editor working with it.
In addition they aren't fully reproducable. Nix solves all of these
problems. Overmind then comes into play to orchestrate the few tasks that are
required to get a development environment up an running._
**Manual way**
The manual way you have to install poetry[^2] and then run `poetry shell` to
enter the virtual environment. You will then need a local PostgreSQL server or
modify the settings so that you can use your prefered database.
Please note that I will only use and test the first method.
[^1]: https://nixos.org/download.html
[^2]: https://python-poetry.org
After you've entered the development environment with either method you can
start the server. With the nix version you can start it with `dev run`. With
poetry `./dev.sh run`. This will start a PostgreSQL database running inside a
docker container and start the Django development server. You can then access
it in the browser under the FQDN of your computer. E.g.
`http://mypc.domain.local:8000`. Run the `dev` command without an argument to
see all options.
## Environment Variables
To customise the application in the Docker container you can use environment

28
dev.sh
View File

@ -4,12 +4,12 @@ run () {
setup
find . -name __pycache__ -o -name "*.pyc" -delete
sudo iptables -I INPUT -p tcp --dport 8000 -j ACCEPT
python ./src/manage.py runserver 0.0.0.0:8000
overmind start
}
setup () {
docker-compose -f docker-compose-development.yml up -d
if [ -f .second_run ]; then
overmind start -l db -D
if [ -f .direnv/first_run ]; then
sleep 2
python ./src/manage.py collectstatic --noinput
python ./src/manage.py makemigrations
@ -34,12 +34,16 @@ setup () {
python ./src/manage.py loaddata nets
python ./src/manage.py loaddata softwares
python ./src/manage.py shell -c "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin@example.com', 'password')"
touch .second_run
init
touch .direnv/first_run
fi
overmind quit
sleep 2
}
venv () {
nix build .#venv -o venv
nix build .#venv -o .venv
}
docker (){
@ -47,21 +51,19 @@ docker (){
}
clean () {
docker-compose -f docker-compose-development.yml down -v
find . \( -name __pycache__ -o -name "*.pyc" \) -delete
rm -rf htmlcov/
rm -f .direnv/first_run
rm -f src/*/migrations/0*.py
rm -f .second_run
rm -rf .direnv/postgres/
}
cleanall () {
clean
docker-compose -f docker-compose-development.yml down -v --rmi local
rm -r .venv
git clean -xdf
}
init () {
python ./src/manage.py loaddata network_inventory.yaml
python ./src/manage.py loaddata src/network_inventory.yaml
}
debug () {
@ -73,8 +75,8 @@ check (){
}
test (){
export DJANGO_SETTINGS_MODULE=network_inventory.settings.local
pytest -nauto --nomigrations --cov-report=html --cov=./src ./src
export DJANGO_SETTINGS_MODULE=network_inventory.settings.ram_test
pytest -nauto --nomigrations --cov-config="$PROJECT_DIR/.coveragerc" --cov-report=html "$PROJECT_DIR/src"
}
update (){

View File

@ -1,15 +0,0 @@
version: '3'
volumes:
db_data:
services:
db:
image: postgres
environment:
- POSTGRES_DB=network_inventory
- POSTGRES_PASSWORD=password
volumes:
- db_data:/var/lib/postgresql/data/
ports:
- "5432:5432"

View File

@ -45,10 +45,11 @@
rec {
devShells.default = pkgs.mkShell {
buildInputs = [
pkgs.gnumake
pkgs.inventoryDevEnv
pkgs.poetry
pkgs.python310Packages.pip
pkgs.overmind
pkgs.postgresql_15
(pkgs.writeScriptBin "dev" "${builtins.readFile ./dev.sh}")
];
PYTHON_KEYRING_BACKEND = "keyring.backends.fail.Keyring";
@ -105,7 +106,7 @@
pkgs.coreutils
inventory
(pkgs.writeShellScriptBin "start-inventory" ''
if [ -f .second_run ]; then
if [ -f .first_run ]; then
sleep 2
${pkgs.inventoryEnv}/bin/django-admin collectstatic --noinput
${pkgs.inventoryEnv}/bin/django-admin makemigrations
@ -130,7 +131,7 @@
${pkgs.inventoryEnv}/bin/django-admin loaddata nets
${pkgs.inventoryEnv}/bin/django-admin loaddata softwares
${pkgs.inventoryEnv}/bin/django-admin shell -c "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin@example.com', 'password')"
touch .second_run
touch .first_run
fi
${pkgs.inventoryEnv}/bin/gunicorn network_inventory.wsgi:application --reload --bind 0.0.0.0:8000 --workers 3
'')

View File

@ -2,6 +2,8 @@ from socket import gethostname
from socket import gethostbyname
from socket import getfqdn
import os
from .base import *
@ -26,10 +28,9 @@ CRISPY_FAIL_SILENTLY = not DEBUG
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres",
"USER": "postgres",
"NAME": "django",
"USER": os.environ.get("USER"),
"HOST": "localhost",
"PORT": 5432,
"PASSWORD": "password",
}
}