From 698e5b7e2230636c3a24fa782836e7d9b9d7a7ff Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 19 Aug 2021 23:12:42 +0200 Subject: [PATCH] controle de la config, doc, exemple .env --- .env-exemple | 20 ++++++++++++++++++++ README.md | 13 +++++++++---- config.py | 12 +++++++++++- scodoc.py | 4 ++-- tools/etc/scodoc.service | 26 -------------------------- tools/etc/scodoc9.service | 22 ++++++++++++++++++++++ 6 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 .env-exemple delete mode 100644 tools/etc/scodoc.service create mode 100644 tools/etc/scodoc9.service diff --git a/.env-exemple b/.env-exemple new file mode 100644 index 00000000..b78080f6 --- /dev/null +++ b/.env-exemple @@ -0,0 +1,20 @@ +# Fichier à configurer et renommer en .env +# (dans /opt/scodoc) +# Il doit appartenir à (ou être lisible par) "scodoc" + + +FLASK_APP=scodoc.py +FLASK_ENV=production # ou "development" si vous développez + +# Envois de mails: décommenter et adapter +# MAIL_SERVER=localhost +# MAIL_PORT=25 + +SCODOC_ADMIN_MAIL="emmanuel@viennet.net" + +# Remplacer cette chaine +# Vous pouvez utiliser +# python3 -c "import uuid; print(uuid.uuid4().hex)" +# pour en créer une de ce genre, aléatoire +SECRET_KEY="53ffeff44a3940dea4964d628af33dd9" + diff --git a/README.md b/README.md index 29cbedd4..8e027adf 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ d'autres pas: merci de signaler les erreurs. - nettoyage du code, finalisation tests et intégration. -## Installation (sur Debian 11 / python3.9.2) +## Installation (sur Debian 11 / python 3.9.2) On peut installer à partir de zéro, ou sur une machine ayant déjà un ScoDoc 7 et migrer les données. @@ -110,15 +110,20 @@ qui est lu automatiquement au démarrage: # .env for ScoDoc _development_ FLASK_APP=scodoc.py - FLASK_ENV=development + FLASK_ENV=development # ou production MAIL_SERVER=votre.serveur.de.mail.net # ou vide si pas de mail MAIL_PORT=25 SCODOC_ADMIN_MAIL="adresse.admin@toto.fr" # important: le mail de admin SECRET_KEY="CGGAJAKlh6789JJK?KNAb=" # une chaine aléatoire - # comment out to use CDN: - BOOTSTRAP_SERVE_LOCAL=1 + +Le fichier `/opt/scodoc/.env-exemple`est donné à titre... d'exemple. Vous pouvez faire: + + # en tant qu'utilisateur scodoc + cd /opt/scodoc/ + cp .env-exemple .env + nano .env # édition ### Initialisation de la base utilisateur par Flask diff --git a/config.py b/config.py index cf15053b..a86e6539 100755 --- a/config.py +++ b/config.py @@ -1,6 +1,7 @@ # -*- coding: UTF-8 -* import os +import uuid from dotenv import load_dotenv BASEDIR = os.path.abspath(os.path.dirname(__file__)) @@ -10,7 +11,7 @@ load_dotenv(os.path.join(BASEDIR, ".env")) class Config: """General configuration. Mostly loaded from environment via .env""" - SECRET_KEY = os.environ.get("SECRET_KEY") or "un-grand-secret-introuvable" + SECRET_KEY = os.environ.get("SECRET_KEY") or "90e01e75831e4176a3c70d29564b425f" SQLALCHEMY_DATABASE_URI = ( os.environ.get("USERS_DATABASE_URI") or "postgresql://scodoc@localhost:5432/SCO8USERS" @@ -63,3 +64,12 @@ class TestConfig(DevConfig): ) SERVER_NAME = "test.gr" DEPT_TEST = "TEST_" # nom du département, ne pas l'utiliser pour un "vrai" + + +mode = os.environ.get("FLASK_ENV", "production") +if mode == "production": + RunningConfig = ProdConfig +elif mode == "development": + RunningConfig = DevConfig +elif mode == "test": + RunningConfig = TestConfig diff --git a/scodoc.py b/scodoc.py index 23900e36..599bf176 100755 --- a/scodoc.py +++ b/scodoc.py @@ -27,9 +27,9 @@ from app.models import ScoPreference from app.views import notes, scolar, absences import tools -from config import DevConfig +from config import RunningConfig -app = create_app(DevConfig) +app = create_app(RunningConfig) cli.register(app) diff --git a/tools/etc/scodoc.service b/tools/etc/scodoc.service deleted file mode 100644 index c91c3f08..00000000 --- a/tools/etc/scodoc.service +++ /dev/null @@ -1,26 +0,0 @@ -# ScoDoc7 service -# Zope based -# Depends on {{postgresql}} (replaced by installation script by -# postgresql@11-main.service on Debian 10 -# postgresql on Debian <= 9 -# => is restarted when {{postgresql}} restarts -# -[Unit] -Description=ScoDoc 7 service -After=network.target {{postgresql}} -Requires={{postgresql}} -PartOf={{postgresql}} -StartLimitIntervalSec=0 - -[Service] -Type=forking -PIDFile=/opt/scodoc/var/Z2.pid -Restart=always -RestartSec=1 -User=root -ExecStart=/opt/scodoc/bin/zopectl start -ExecStop=/opt/scodoc/bin/zopectl stop -ExecReload=/opt/scodoc/bin/zopectl restart - -[Install] -WantedBy={{postgresql}} diff --git a/tools/etc/scodoc9.service b/tools/etc/scodoc9.service new file mode 100644 index 00000000..0e364b5a --- /dev/null +++ b/tools/etc/scodoc9.service @@ -0,0 +1,22 @@ +# ScoDoc 9 systemd service +# Flask based +# a priori on ne dépend plus de postgresql ? XXX +# inspiré par https://blog.miguelgrinberg.com/post/running-a-flask-application-as-a-service-with-systemd +[Unit] +Description=ScoDoc 9 service +After=network.target +#Requires={{postgresql}} +#PartOf={{postgresql}} +#StartLimitIntervalSec=0 + +[Service] +User=scodoc +Group=scodoc +WorkingDirectory=/opt/scodoc +ExecStart=/opt/scodoc/venv/bin/gunicorn -b localhost:8000 -w 4 scodoc:app +Restart=always +# Environment=FLASK_CONFIG=production +Restart=always + +[Install] +WantedBy=multi-user.target \ No newline at end of file