From e3ebb925e1746aaa6953bd714df11d352e18237e Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Mon, 7 Nov 2022 22:32:06 +0100 Subject: [PATCH] =?UTF-8?q?Reforma=20Makefil=C5=AF=20WIP=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 156 ++------------------------------- Makefile_old | 128 +++++++++++++++++++++++++++ make/all | 17 ++++ make/deploy | 3 + make/deploy_prod | 3 + make/install | 1 + make/install_venv | 3 + make/install_web | 3 + make/lib.sh | 26 ++++++ make/push_compiled_vue_to_test | 3 + make/run | 3 + make/schema | 3 + make/schema_all.pdf | 3 + make/schema_seminar.pdf | 3 + make/sync_local | 3 + make/sync_local_db | 3 + make/sync_local_media | 3 + make/sync_prod_flatpages | 3 + make/sync_test | 3 + make/sync_test_db_aggressive | 3 + make/sync_test_media | 3 + make/test | 3 + make/venv_check | 3 + 23 files changed, 231 insertions(+), 151 deletions(-) create mode 100644 Makefile_old create mode 100755 make/all create mode 100755 make/deploy create mode 100755 make/deploy_prod create mode 120000 make/install create mode 100755 make/install_venv create mode 100755 make/install_web create mode 100644 make/lib.sh create mode 100755 make/push_compiled_vue_to_test create mode 100755 make/run create mode 100755 make/schema create mode 100755 make/schema_all.pdf create mode 100755 make/schema_seminar.pdf create mode 100755 make/sync_local create mode 100755 make/sync_local_db create mode 100755 make/sync_local_media create mode 100755 make/sync_prod_flatpages create mode 100755 make/sync_test create mode 100755 make/sync_test_db_aggressive create mode 100755 make/sync_test_media create mode 100755 make/test create mode 100755 make/venv_check diff --git a/Makefile b/Makefile index 94a377c4..5e3acddc 100644 --- a/Makefile +++ b/Makefile @@ -1,154 +1,8 @@ -PYTHON ?= python3 -VENV ?= ${PYTHON} -m venv -# Všechny flagy, které se s venvem/virtualenvem/... mají volat patří sem. Volá se "${VENV} cesta" -VENV_PATH ?= env -# Musí být definovaná, i kdyby to měla být "." +%: + @make/$* -.PHONY: all venv_check clean install install_web install_venv clean_venv clean_schema run test deploy_test deploy_prod sync_test_media sync_test_db sync_test sync_local_media sync_local_db sync_local - -# activate by mělo být předpokladem ke všemu, co volá webový python (i.e. python nasazený do ${VENV}u kvůli webu, např. manage.py) all: - @# Just echo: - # Install je trochu magický: - # Spusť následující posloupnost příkazů: - # make install_venv - # . ${VENV_PATH}/bin/activate - # make install_web - # - # Pokud install_web říká Error: pg_config executable not found. nainstaluj si libpq-dev - # Pokud chybová hláška obsahuje #include , nainstaluj si python3-dev - # - # Až skončíš s vývojem webu, spusť "deactivate". Tím zmizí '(${VENV_PATH})' ze začátku promptu. - -venv_check: - @# Pokud org nemá zapnutý venv, poradíme mu, aby si ho zapnul a spadneme. Jinak nic. - @expr $$PATH : ".*:*$(shell pwd)/${VENV_PATH}/bin" > /dev/null && exit 0 || echo 'Není zapnutý venv, spusť ". ${VENV_PATH}/bin/activate".\nPokud není venv nainstalovaný, spusť "make install_venv"' && false - -clean: clean_venv clean_schema - -install: install_web - -install_web: venv_check - @# venv může být příšerně starý, takže nejdříve upgradujeme venvové věci - pip install --upgrade pip - pip install --upgrade setuptools - # Instalace závislostí webu - pip install -r requirements.txt --upgrade - # Pro vygenerování tesdat spusť ./manage.py testdata - # Po vygenerování testdat spusť ./manage.py loaddata data/*, ať máš menu a další modely - # Pro synchronizaci flatpages spusť make sync_prod_flatpages - -install_venv: - ${VENV} ${VENV_PATH} - -clean_venv: - # Možná není 100% foolproof... - @test ! ${VENV_PATH} = . || ! echo "Smaž si prosím venv sám, nebudu mazat celý web" - rm -rfv ${VENV_PATH} - rm -f pip-selfcheck.json -clean_schema: - rm -f schema_seminar.pdf schema_all.pdf - -run: venv_check - ./manage.py runserver - -test: venv_check - ./manage.py test -v2 seminar mamweb - -# DB schemata - -schema: schema_seminar.pdf schema_all.pdf - -schema_seminar.pdf: venv_check - ./manage.py graph_models seminar | dot -Tpdf > schema_seminar.pdf - -schema_all.pdf: venv_check - ./manage.py graph_models -a -g | dot -Tpdf > schema_all.pdf - -# Deploy to current *mamweb-test* directory -deploy_test: venv_check - @if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi - @if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-test" ]; then echo "Only possible in directory mamweb-test"; exit 1; fi - @echo "Installing version from origin/test ..." - git pull origin test - git clean -f - make install - ./manage.py migrate - ./manage.py collectstatic --noinput - (chown -R :mam . || true ) - (chmod -R g+rX,go-w . || true ) - @echo Restarting systemd unit - systemctl --user restart mamweb-test.service - @echo Done. - -# Deploy to current *mamweb-prod* directory -deploy_prod: venv_check - @if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi - @if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-prod" ]; then echo "Only possible in directory mamweb-prod"; exit 1; fi - @echo "Backing up production DB ..." - ( cd -P .. && ./backup_prod_db.sh ) - @echo "Installing version from origin/master ..." - git pull origin master - git clean -f - make install - ./manage.py migrate - ./manage.py collectstatic --noinput - (chown -R :mam . || true ) - (chmod -R g+rX,go-w . || true ) - @echo Restarting systemd user unit for MaM web - systemctl --user restart mamweb-prod.service - @echo Done. - - -sync_prod_flatpages: venv_check - @echo Downloading current version of flatpages from mamweb-prod. - ssh mam-web@gimli.ms.mff.cuni.cz \ - "cd /akce/mam/www/mamweb-prod; . env/bin/activate; ./manage.py dumpdata flatpages --indent=2 > flat.json; ./fix_json.py flat.json flat_fixed.json" - rsync -ave ssh mam-web@gimli.ms.mff.cuni.cz:/akce/mam/www/mamweb-prod/flat_fixed.json data/flat.json - @echo "Applying downloaded flatpages." - ./manage.py loaddata data/flat.json - @echo "Done." - -# Sync test media directory with production -sync_test_media: - @if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi - @if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-test" ]; then echo "Only possible in /akce/mam/www/mamweb-test"; exit 1; fi - rsync -av --delete /akce/mam/www/mamweb-prod/media/ ./media - -# Sync (with drop) test database with production database -sync_test_db_aggressive: - @if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi - pg_dump mam_test > dump-test-`date +"%Y%m%d_%H%M"`.sql - pg_dump -Fc mam_prod > dump-prod.sql - @# I am not sure which shell is used, so I am calling bash to make sure - psql mam_test -c 'DROP OWNED BY "mam-web";' - pg_restore -c --if-exists -d mam_test dump-prod.sql - rm dump-prod.sql - psql mam_test -c "UPDATE django_site SET name='MaMweb (test)', domain='mam-test.ks.matfyz.cz' WHERE id=1" - @echo Done. - -# Sync test with production -# HACK ALERT: using aggressive variant, due to the schemas being too different. -sync_test: sync_test_media sync_test_db_aggressive - - -# Sync media directory with atrey. Useful for local development with production database -# Does not sync Galerie and CACHE (too huge). -sync_local_media: - rsync -ave ssh --exclude Galerie --exclude CACHE\ - mam-web@gimli.ms.mff.cuni.cz:/akce/mam/www/mamweb-prod/media/ ./media/ -# Downloads and restores production database to local database. PostgreSQL only. -sync_local_db: - scp mam-web@gimli.ms.mff.cuni.cz:`ssh mam-web@gimli.ms.mff.cuni.cz 'ls -v /akce/mam/www/backups/mam_prod-*\.pgdump.xz | tail -n 1'` \ - ./last.pgdump.xz - xz -fd last.pgdump.xz - pg_restore -c -d mam-prod last.pgdump - -# Sync database and media. See above lines -sync_local: sync_local_media sync_local_db + @# Nevím, proč to nefunguje bez těla, ale vlastně je mi to jedno… + @make/all -# Push local compiled Vue to gimli test site -push_compiled_vue_to_test: - scp vue_frontend/webpack-stats.json mam-web@gimli:/akce/mam/www/mamweb-test/vue_frontend/ - rsync -ave ssh seminar/static/seminar/vue mam-web@gimli:/akce/mam/www/mamweb-test/seminar/static/seminar/ - ssh mam-web@gimli.ms.mff.cuni.cz 'cd /akce/mam/www/mamweb-test/ && . env/bin/activate && ./manage.py collectstatic --noinput' +.PHONY: all diff --git a/Makefile_old b/Makefile_old new file mode 100644 index 00000000..ccfdb143 --- /dev/null +++ b/Makefile_old @@ -0,0 +1,128 @@ +.PHONY: all venv_check clean install install_web install_venv clean_venv clean_schema run test deploy_test deploy_prod sync_test_media sync_test_db sync_test sync_local_media sync_local_db sync_local + +install: install_web + +install_web: venv_check + @# venv může být příšerně starý, takže nejdříve upgradujeme venvové věci + pip install --upgrade pip + pip install --upgrade setuptools + # Instalace závislostí webu + pip install -r requirements.txt --upgrade + # Pro vygenerování tesdat spusť ./manage.py testdata + # Po vygenerování testdat spusť ./manage.py loaddata data/*, ať máš menu a další modely + # Pro synchronizaci flatpages spusť make sync_prod_flatpages + +install_venv: + ${VENV} ${VENV_PATH} + +clean_venv: + # Možná není 100% foolproof... + @test ! ${VENV_PATH} = . || ! echo "Smaž si prosím venv sám, nebudu mazat celý web" + rm -rfv ${VENV_PATH} + rm -f pip-selfcheck.json +clean_schema: + rm -f schema_seminar.pdf schema_all.pdf + +run: venv_check + ./manage.py runserver + +test: venv_check + ./manage.py test -v2 seminar mamweb + +# DB schemata + +schema: schema_seminar.pdf schema_all.pdf + +schema_seminar.pdf: venv_check + ./manage.py graph_models seminar | dot -Tpdf > schema_seminar.pdf + +schema_all.pdf: venv_check + ./manage.py graph_models -a -g | dot -Tpdf > schema_all.pdf + +# Deploy to current *mamweb-test* directory +deploy: venv_check + @if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi + @if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-test" ]; then echo "Only possible in directory mamweb-test"; exit 1; fi + @echo "Installing version from origin/${BRANCH} ..." + git pull origin ${BRANCH} + git clean -f + make install + ./manage.py migrate + ./manage.py collectstatic --noinput + (chown -R :mam . || true ) + (chmod -R g+rX,go-w . || true ) + @echo Restarting systemd unit + systemctl --user restart mamweb-test.service + @echo Done. + +# Deploy to current *mamweb-prod* directory +deploy_prod: venv_check + @if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi + @if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-prod" ]; then echo "Only possible in directory mamweb-prod"; exit 1; fi + @echo "Backing up production DB ..." + ( cd -P .. && ./backup_prod_db.sh ) + @echo "Installing version from origin/master ..." + git pull origin master + git clean -f + make install + ./manage.py migrate + ./manage.py collectstatic --noinput + (chown -R :mam . || true ) + (chmod -R g+rX,go-w . || true ) + @echo Restarting systemd user unit for MaM web + systemctl --user restart mamweb-prod.service + @echo Done. + + +sync_prod_flatpages: venv_check + @echo Downloading current version of flatpages from mamweb-prod. + ssh mam-web@gimli.ms.mff.cuni.cz \ + "cd /akce/mam/www/mamweb-prod; . env/bin/activate; ./manage.py dumpdata flatpages --indent=2 > flat.json; ./fix_json.py flat.json flat_fixed.json" + rsync -ave ssh mam-web@gimli.ms.mff.cuni.cz:/akce/mam/www/mamweb-prod/flat_fixed.json data/flat.json + @echo "Applying downloaded flatpages." + ./manage.py loaddata data/flat.json + @echo "Done." + +# Sync test media directory with production +sync_test_media: + @if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi + @if [ `readlink -f .` != "/aux/akce/mam/www/mamweb-test" ]; then echo "Only possible in /akce/mam/www/mamweb-test"; exit 1; fi + rsync -av --delete /akce/mam/www/mamweb-prod/media/ ./media + +# Sync (with drop) test database with production database +sync_test_db_aggressive: + @if [ ${USER} != "mam-web" ]; then echo "Only possible by user mam-web"; exit 1; fi + pg_dump mam_test > dump-test-`date +"%Y%m%d_%H%M"`.sql + pg_dump -Fc mam_prod > dump-prod.sql + @# I am not sure which shell is used, so I am calling bash to make sure + psql mam_test -c 'DROP OWNED BY "mam-web";' + pg_restore -c --if-exists -d mam_test dump-prod.sql + rm dump-prod.sql + psql mam_test -c "UPDATE django_site SET name='MaMweb (test)', domain='mam-test.ks.matfyz.cz' WHERE id=1" + @echo Done. + +# Sync test with production +# HACK ALERT: using aggressive variant, due to the schemas being too different. +sync_test: sync_test_media sync_test_db_aggressive + + +# Sync media directory with atrey. Useful for local development with production database +# Does not sync Galerie and CACHE (too huge). +sync_local_media: + rsync -ave ssh --exclude Galerie --exclude CACHE\ + mam-web@gimli.ms.mff.cuni.cz:/akce/mam/www/mamweb-prod/media/ ./media/ +# Downloads and restores production database to local database. PostgreSQL only. +sync_local_db: + scp mam-web@gimli.ms.mff.cuni.cz:`ssh mam-web@gimli.ms.mff.cuni.cz 'ls -v /akce/mam/www/backups/mam_prod-*\.pgdump.xz | tail -n 1'` \ + ./last.pgdump.xz + xz -fd last.pgdump.xz + pg_restore -c -d mam-prod last.pgdump + +# Sync database and media. See above lines +sync_local: sync_local_media sync_local_db + +# Push local compiled Vue to gimli test site +push_compiled_vue_to_test: + scp vue_frontend/webpack-stats.json mam-web@gimli:/akce/mam/www/mamweb-test/vue_frontend/ + rsync -ave ssh seminar/static/seminar/vue mam-web@gimli:/akce/mam/www/mamweb-test/seminar/static/seminar/ + ssh mam-web@gimli.ms.mff.cuni.cz 'cd /akce/mam/www/mamweb-test/ && . env/bin/activate && ./manage.py collectstatic --noinput' diff --git a/make/all b/make/all new file mode 100755 index 00000000..de25100a --- /dev/null +++ b/make/all @@ -0,0 +1,17 @@ +#!/bin/bash + +set -euo pipefail +. make/lib.sh + +echo "Install je trochu magický: + Spusť následující posloupnost příkazů: + make install_venv + . ${VENV_PATH}/bin/activate + make install_web + + Pokud install_web říká Error: pg_config executable not found. nainstaluj si libpq-dev + Pokud chybová hláška obsahuje #include , nainstaluj si python3-dev + + Až skončíš s vývojem webu, spusť "deactivate". Tím zmizí '(${VENV_PATH})' ze začátku promptu." + + diff --git a/make/deploy b/make/deploy new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/deploy @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/deploy_prod b/make/deploy_prod new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/deploy_prod @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/install b/make/install new file mode 120000 index 00000000..d6d811b7 --- /dev/null +++ b/make/install @@ -0,0 +1 @@ +make/install_web \ No newline at end of file diff --git a/make/install_venv b/make/install_venv new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/install_venv @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/install_web b/make/install_web new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/install_web @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/lib.sh b/make/lib.sh new file mode 100644 index 00000000..e4aac157 --- /dev/null +++ b/make/lib.sh @@ -0,0 +1,26 @@ +#!/bin/false Tohle je knihovna, nemá se spouštět, ale načítat pomocí source(1) nebo '.' + +PYTHON="${PYTHON:-python3}" +VENV="${VENV:-${PYTHON} -m venv}" +VENV_PATH="${VENV_PATH:-env}" +BRANCH="${BRANCH:-master}" + +REPO="${REPO:-git@gitea.ks.matfyz.cz:mam/mamweb.git}" +GIMLI='gimli.ms.mff.cuni.cz' +GIMLI_LOGIN="mam-web@$GIMLI" +# Skutečné cesty, jak je vrátí `realpath` +PRODWEB="/aux/akce/mam/www/mamweb-prod" +TESTWEB="/aux/akce/mam/www/mamweb-test" + +function die { + echo "$@" >&2 + exit 1 +} + +# Vždycky chceme zajistit, že běžíme z rootu repozitáře +test -d '.git' || die "Make skript spuštěn ve špatné složce, spusť ho z kořenového adresáře repozitáře." + +function ensure_venv { + test -f "$VENV_PATH/bin/activate" || $VENV "$VENV_PATH" + . "$VENV_PATH/bin/activate" +} diff --git a/make/push_compiled_vue_to_test b/make/push_compiled_vue_to_test new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/push_compiled_vue_to_test @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/run b/make/run new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/run @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/schema b/make/schema new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/schema @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/schema_all.pdf b/make/schema_all.pdf new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/schema_all.pdf @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/schema_seminar.pdf b/make/schema_seminar.pdf new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/schema_seminar.pdf @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/sync_local b/make/sync_local new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/sync_local @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/sync_local_db b/make/sync_local_db new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/sync_local_db @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/sync_local_media b/make/sync_local_media new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/sync_local_media @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/sync_prod_flatpages b/make/sync_prod_flatpages new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/sync_prod_flatpages @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/sync_test b/make/sync_test new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/sync_test @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/sync_test_db_aggressive b/make/sync_test_db_aggressive new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/sync_test_db_aggressive @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/sync_test_media b/make/sync_test_media new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/sync_test_media @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/test b/make/test new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/test @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail diff --git a/make/venv_check b/make/venv_check new file mode 100755 index 00000000..60347f1e --- /dev/null +++ b/make/venv_check @@ -0,0 +1,3 @@ +#!/bin/bash + +set -exuo pipefail