From 839bbbca973d46be3d8401f5e809ad8e7acb9353 Mon Sep 17 00:00:00 2001
From: Jan Mach <jan.mach@cesnet.cz>
Date: Sat, 16 Oct 2021 20:29:44 +0200
Subject: [PATCH] Better integration of Sendria tool.

(Redmine issue: #7041)
---
 Makefile                           | 10 +++-------
 conf/mentat-hawat-dev.py.conf      |  2 +-
 doc/sphinx/_doclib/development.rst |  4 ++--
 etc/systemd/system/sendria.service |  4 +++-
 lib/hawat/test/runner.py           |  2 +-
 lib/vial/test/runner.py            |  2 +-
 vagrantenv/.bashrc                 |  6 +++---
 vagrantenv/provision.sh            |  7 ++++++-
 vagrantenv/system-banner.sh        |  1 -
 9 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/Makefile b/Makefile
index 21e722d0..a4745849 100644
--- a/Makefile
+++ b/Makefile
@@ -49,7 +49,7 @@ CURRENT_DIR = $(shell pwd)
 DEV_SERVER      = localhost
 DEV_PORT        = 5000
 DEV_MAIL_SERVER = localhost
-DEV_MAIL_PORT   = 8025
+DEV_MAIL_PORT   = 1025
 
 #
 # Include common makefile configurations.
@@ -345,13 +345,9 @@ deps-postgresql: FORCE
 
 run-webui-dev:
 	@echo "\n$(GREEN)*** Running development web server with development configuration on $(DEV_SERVER):$(DEV_PORT) ***$(NC)\n"
-	FLASK_ENV=development FLASK_CONFIG=development HAWAT_CONFIG_FILE=$(shell realpath ./conf/mentat-hawat-dev.py.conf) hawat-cli run --host "0.0.0.0" --port $(DEV_PORT)
+	FLASK_ENV=development HAWAT_CONFIG_FILE=$(shell realpath ./conf/mentat-hawat-dev.py.conf) hawat-cli run --host "0.0.0.0" --port $(DEV_PORT)
 
-run-mailserver-sendria:
-	@echo "\n$(GREEN)*** Running development mail server Sendria on $(DEV_MAIL_SERVER):$(DEV_MAIL_PORT) ***$(NC)\n"
-	sendria --db /var/mentat/sendria.db --smtp-ip $(DEV_MAIL_SERVER) --smtp-port $(DEV_MAIL_PORT) --http-ip 0.0.0.0 --http-port 1080 --pidfile /var/mentat/run/sendria.pid --log-file /var/mentat/log/sendria.log
-
-run-mailserver-python:
+run-mailserver-dev:
 	@echo "\n$(GREEN)*** Running development mail server on $(DEV_MAIL_SERVER):$(DEV_MAIL_PORT) ***$(NC)\n"
 	$(PYTHON) -m smtpd -n -c DebuggingServer $(DEV_MAIL_SERVER):$(DEV_MAIL_PORT)
 
diff --git a/conf/mentat-hawat-dev.py.conf b/conf/mentat-hawat-dev.py.conf
index 3e620c8a..597fb861 100644
--- a/conf/mentat-hawat-dev.py.conf
+++ b/conf/mentat-hawat-dev.py.conf
@@ -32,7 +32,7 @@ HAWAT_LIMIT_AODS = 20
 HAWAT_SEARCH_QUERY_QUOTA = 7
 
 MAIL_SERVER = 'localhost'
-MAIL_PORT = 8025
+MAIL_PORT = 1025
 MAIL_DEFAULT_SENDER = 'mentat@cesnet.cz'
 
 HAWAT_LOG_DEFAULT_LEVEL = 'debug'
diff --git a/doc/sphinx/_doclib/development.rst b/doc/sphinx/_doclib/development.rst
index cc64c523..32bd3fdb 100644
--- a/doc/sphinx/_doclib/development.rst
+++ b/doc/sphinx/_doclib/development.rst
@@ -651,7 +651,7 @@ and ready to work:
 
 .. code-block:: shell
 
-	(venv) mentat@mentat-devel /vagrant $
+	(venv) !DEV! mentat@mentat-devel /vagrant $
 
 Keep in mind following:
 
@@ -661,7 +661,7 @@ Keep in mind following:
 * Intended workflow is editing code in your favorite editor from outside of the box and running Mentat inside.
 * To access web interface from your host machine navigate to `https://localhost:4443 <https://localhost:4443>`__.
 * To access Flask`s development web interface from your host machine you must first launch it inside the guest box with ``make run-webui-dev`` and then navigate to `http://localhost:5000 <http://localhost:5000>`__.
-* For your convenience during development you may use awesome `Sendria <https://github.com/msztolcman/sendria>`__ service to view emails being send. First launch it from guest box with ``make run-mailserver-sendria`` and then navigate from your host machine to `http://localhost:1080 <http://localhost:1080>`__.
+* For your convenience during development you may use awesome `Sendria <https://github.com/msztolcman/sendria>`__ service to view emails being send. It can be controlled via Systemd and running on startup. Just navigate from your host machine to `http://localhost:1080 <http://localhost:1080>`__.
 
 At this point you are all set to develop Mentat locally without cluttering your personal
 workstation.
diff --git a/etc/systemd/system/sendria.service b/etc/systemd/system/sendria.service
index 2b076fc3..16a240bf 100644
--- a/etc/systemd/system/sendria.service
+++ b/etc/systemd/system/sendria.service
@@ -4,8 +4,10 @@ After=network.target
 
 [Service]
 Type=simple
-ExecStart=/var/mentat/venv/bin/python -m sendria --db /tmp/mails.sqlite --http-ip 0.0.0.0
+ExecStart=/var/mentat/venv/bin/python -m sendria --db /var/mentat/sendria.db --smtp-ip 127.0.0.1 --smtp-port 1025 --http-ip 0.0.0.0 --http-port 1080 --pidfile /var/run/sendria.pid --log-file /var/log/sendria.log
 Restart=always
+User=mentat
+Group=mentat
 
 [Install]
 WantedBy=multi-user.target
diff --git a/lib/hawat/test/runner.py b/lib/hawat/test/runner.py
index f9086c8c..8e8ea2e7 100644
--- a/lib/hawat/test/runner.py
+++ b/lib/hawat/test/runner.py
@@ -37,7 +37,7 @@ def _config_testapp_hawat(app_config):
     app_config['LOG_FILE'] = '/var/tmp/mentat-hawat-utest.log'
     app_config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://mentat:mentat@localhost/mentat_utest'
     app_config['MAIL_SERVER'] = 'localhost'
-    app_config['MAIL_PORT'] = 8025
+    app_config['MAIL_PORT'] = 1025
     app_config['MAIL_DEFAULT_SENDER'] = 'root@unittest'
     app_config['EMAIL_ADMINS'] = ['admin@unittest']
     app_config['MODELS'] = {
diff --git a/lib/vial/test/runner.py b/lib/vial/test/runner.py
index 093b10bd..01180cc8 100644
--- a/lib/vial/test/runner.py
+++ b/lib/vial/test/runner.py
@@ -33,7 +33,7 @@ def _config_testapp_vial(app_config):
     app_config['LOG_FILE'] = '/var/tmp/vial-utest.log'
     app_config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://mentat:mentat@localhost/mentat_utest'
     app_config['MAIL_SERVER'] = 'localhost'
-    app_config['MAIL_PORT'] = 8025
+    app_config['MAIL_PORT'] = 1025
     app_config['MAIL_DEFAULT_SENDER'] = 'root@unittest'
     app_config['EMAIL_ADMINS'] = ['admin@unittest']
     app_config['MODELS'] = {
diff --git a/vagrantenv/.bashrc b/vagrantenv/.bashrc
index 3bb31227..e379e598 100644
--- a/vagrantenv/.bashrc
+++ b/vagrantenv/.bashrc
@@ -63,14 +63,14 @@ if [ "$color_prompt" = yes ]; then
     # export GIT_PS1_SHOWUPSTREAM="auto verbose"
 
     if [[ ${EUID} == 0 ]] ; then
-       PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
+       PS1='\[\033[01;34m\]!DEV!\[\033[00m\] ${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
        #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\h\[\033[01;34m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;33m\]$(__git_ps1 " [%s]")\[\033[00m\]\$ '
     else
-       PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\] '
+       PS1='\[\033[01;34m\]!DEV!\[\033[00m\] ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\] '
        #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;33m\]$(__git_ps1 " [%s]")\[\033[00m\]\$ '
     fi
 else
-    PS1='${debian_chroot:+($debian_chroot)}\u@\h \w \$ '
+    PS1='!DEV! ${debian_chroot:+($debian_chroot)}\u@\h \w \$ '
 fi
 unset color_prompt force_color_prompt
 
diff --git a/vagrantenv/provision.sh b/vagrantenv/provision.sh
index b648d018..1a9fc654 100644
--- a/vagrantenv/provision.sh
+++ b/vagrantenv/provision.sh
@@ -129,7 +129,7 @@ if [ ! -f /etc/mentat/install.pip ] ; then
   echo '--editable "/vagrant/"' > /etc/mentat/install.pip
 fi
 
-echo "==========> Preparing Python development environment"
+echo "==========> Preparing Python virtual environment"
 cd /vagrant/ && make venv
 source ${MENTAT_VENV}/bin/activate
 
@@ -141,8 +141,13 @@ ensure_link /vagrant/vagrantenv/postfix.main.cf /etc/postfix/main.cf
 systemctl restart postfix.service
 
 ensure_link /vagrant/etc/systemd/system/sendria.service /etc/systemd/system/sendria.service
+mkdir /var/sendria
+chown mentat:mentat /var/sendria
+touch /var/log/sendria.log
+chown -R mentat:mentat /var/log/sendria.log
 systemctl daemon-reload
 systemctl start sendria.service
+systemctl enable sendria.service
 
 echo "==========> Bootstraping runtime environment"
 /etc/mentat/scripts/init-runenv.sh
diff --git a/vagrantenv/system-banner.sh b/vagrantenv/system-banner.sh
index 56ab475b..b08080e3 100755
--- a/vagrantenv/system-banner.sh
+++ b/vagrantenv/system-banner.sh
@@ -25,5 +25,4 @@ echo ""
 echo "    Launching:"
 echo "      mentat-controller.py --command start       # Mentat backend system"
 echo "      make run-webui-dev                         # Mentat web interface (5000)"
-echo "      make run-mailserver-sendria                # Sendria mailer service (1080)"
 echo ""
-- 
GitLab