network_inventory/dev.sh

163 lines
4.4 KiB
Bash
Raw Permalink Normal View History

2022-12-12 21:42:00 +01:00
#!/usr/bin/env bash
2023-07-22 18:27:33 +02:00
# Helper functions not exposed to the user {
# Load example data
2023-08-28 12:57:32 +02:00
_init() {
2023-07-22 18:27:33 +02:00
python ./src/manage.py loaddata src/network_inventory.yaml
2023-07-22 17:24:15 +02:00
}
2023-07-22 18:27:33 +02:00
# Setup the database
2023-08-28 12:57:32 +02:00
_setup() {
2023-07-13 23:48:43 +02:00
overmind start -l db -D
2023-07-18 20:40:58 +02:00
if [ -f .direnv/first_run ]; then
2022-12-12 21:42:00 +01:00
sleep 2
python ./src/manage.py collectstatic --noinput
python ./src/manage.py makemigrations
python ./src/manage.py migrate
else
python ./src/manage.py collectstatic --noinput
python ./src/manage.py makemigrations backups
python ./src/manage.py makemigrations computers
python ./src/manage.py makemigrations core
python ./src/manage.py makemigrations customers
python ./src/manage.py makemigrations devices
python ./src/manage.py makemigrations licenses
python ./src/manage.py makemigrations nets
python ./src/manage.py makemigrations softwares
python ./src/manage.py makemigrations users
python ./src/manage.py makemigrations
python ./src/manage.py migrate
python ./src/manage.py loaddata backups
python ./src/manage.py loaddata computers
python ./src/manage.py loaddata core
python ./src/manage.py loaddata devices
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')"
2023-07-22 18:27:33 +02:00
_init
2023-07-18 20:40:58 +02:00
touch .direnv/first_run
2022-12-12 21:42:00 +01:00
fi
overmind quit
sleep 2
2022-12-12 21:42:00 +01:00
}
2023-08-28 12:57:32 +02:00
_open_url() {
if [[ ! -z "${DEFAULT_BROWSER}" ]]; then
$DEFAULT_BROWSER $url
2023-08-28 12:57:32 +02:00
elif type explorer.exe &>/dev/null; then
explorer.exe $url
fi
}
2023-08-28 12:57:32 +02:00
_create_url() {
if [ -f /etc/wsl.conf ]; then
echo "http://localhost:$WEBPORT"
else
echo "http://$(hostname -f):$WEBPORT"
fi
}
2023-07-22 18:27:33 +02:00
#}
# Main tasks start
declare -A tasks
declare -A descriptions
2023-08-28 12:57:32 +02:00
run() {
2023-07-22 18:27:33 +02:00
_setup
find . -name __pycache__ -o -name "*.pyc" -delete
url=$(_create_url)
2023-07-22 18:27:33 +02:00
sudo iptables -I INPUT -p tcp --dport $WEBPORT -j ACCEPT
overmind start -D
printf "\n---\n webserver: $url\n---\n"
_open_url $url
2023-07-22 18:27:33 +02:00
}
descriptions["run"]="Start the webserver."
tasks["run"]=run
descriptions["start"]="Alias for run."
tasks["start"]=run
2023-08-28 12:57:32 +02:00
stop() {
2023-07-22 18:27:33 +02:00
overmind quit
}
descriptions["stop"]="Stop the webserver and DB."
tasks["stop"]=stop
2022-12-12 21:42:00 +01:00
2023-08-28 12:57:32 +02:00
venv() {
2023-07-13 22:22:03 +02:00
nix build .#venv -o .venv
2022-12-12 21:42:00 +01:00
}
2023-07-22 18:24:48 +02:00
descriptions["venv"]="Build a pseudo venv that editors like VS Code can use."
2023-07-22 16:29:34 +02:00
tasks["venv"]=venv
2022-12-12 21:42:00 +01:00
2023-08-28 12:57:32 +02:00
build-container() {
nix build && docker load <result
2022-12-12 21:42:00 +01:00
}
2023-07-22 18:24:48 +02:00
descriptions["build-container"]="Build and load OCI container."
2023-07-22 16:29:34 +02:00
tasks["build-container"]=build-container
2022-12-12 21:42:00 +01:00
2023-08-28 12:57:32 +02:00
clean() {
2022-12-12 21:42:00 +01:00
find . \( -name __pycache__ -o -name "*.pyc" \) -delete
rm -rf htmlcov/
rm -f .direnv/first_run
rm -f src/*/migrations/0*.py
rm -rf .direnv/postgres/
2022-12-12 21:42:00 +01:00
}
2023-07-22 18:24:48 +02:00
descriptions["clean"]="Reset the project to a fresh state including the database."
2023-07-22 16:29:34 +02:00
tasks["clean"]=clean
2022-12-12 21:42:00 +01:00
2023-08-28 12:57:32 +02:00
cleanall() {
2023-07-18 20:44:01 +02:00
git clean -xdf
2022-12-12 21:42:00 +01:00
}
2023-07-22 18:24:48 +02:00
descriptions["cleanall"]="Completly remove any files which are not checked into git."
2023-07-22 16:29:34 +02:00
tasks["cleanall"]=cleanall
2022-12-12 21:42:00 +01:00
2023-08-28 12:57:32 +02:00
debug() {
2022-12-12 21:42:00 +01:00
pytest --pdb --nomigrations --cov=. --cov-report=html ./src/
}
2023-07-22 18:24:48 +02:00
descriptions["debug"]="Run the tests and drop into the debugger on failure."
2023-07-22 16:29:34 +02:00
tasks["debug"]=debug
2022-12-12 21:42:00 +01:00
2023-08-28 12:57:32 +02:00
lint() {
echo "Running pylint"
pylint \
2023-08-28 12:57:32 +02:00
--rc-file="$PROJECT_DIR/pyproject.toml" \
-j 0 \
-E "$PROJECT_DIR/src"
echo "Running mypy"
mypy --config-file="$PROJECT_DIR/pyproject.toml" "$PROJECT_DIR/src"
2022-12-12 21:42:00 +01:00
}
2023-08-28 12:57:32 +02:00
descriptions["lint"]="Run the linters against the src directory."
tasks["lint"]=lint
test() {
DJANGO_SETTINGS_MODULE=network_inventory.settings.ram_test pytest \
2023-08-28 12:57:32 +02:00
-nauto \
--nomigrations \
--cov-config="$PROJECT_DIR/.coveragerc" \
--cov-report=html \
"$PROJECT_DIR/src"
2023-03-07 21:53:03 +01:00
}
2023-07-22 18:24:48 +02:00
descriptions["test"]="Run the tests in the RAM DB and write a coverage report."
2023-07-22 16:29:34 +02:00
tasks["test"]=test
2023-03-07 21:53:03 +01:00
2023-08-28 12:57:32 +02:00
update() {
poetry update --lock
2023-03-07 21:30:27 +01:00
}
2023-07-22 18:24:48 +02:00
descriptions["update"]="Update the dependencies."
2023-07-22 16:29:34 +02:00
tasks["update"]=update
2022-12-12 21:42:00 +01:00
# only one task at a time
if [ $# != 1 ]; then
2023-07-22 18:24:48 +02:00
printf "usage: dev <task_name>\n\n"
2023-08-28 12:57:32 +02:00
for task in "${!tasks[@]}"; do
2023-07-22 18:24:48 +02:00
echo "$task - ${descriptions[$task]}"
done
2023-07-22 16:29:34 +02:00
else
# Check if task is available
2023-08-28 12:57:32 +02:00
if [[ -v "tasks[$1]" ]]; then
2023-07-22 16:29:34 +02:00
${tasks["$1"]}
else
echo "Task not found."
fi
2022-12-12 21:42:00 +01:00
fi