From d04b207585e1c97a6128a9bde2d58b0d6ab1f8b4 Mon Sep 17 00:00:00 2001
From: Jan Mach <jan.mach@cesnet.cz>
Date: Mon, 20 Sep 2021 15:56:46 +0200
Subject: [PATCH] Optimizations and fixes in Vagrant image provisioning.

(Redmine issue: #7041)
---
 Makefile                                 |  3 ++-
 Vagrantfile                              | 19 -------------------
 conf/cron/mentat-backup-py.cron          |  2 +-
 conf/cron/mentat-cleanup-py.cron         |  2 +-
 conf/cron/mentat-informant-py.cron       |  2 +-
 conf/cron/mentat-netmngr-py.cron         |  2 +-
 conf/cron/mentat-precache-py.cron        |  2 +-
 conf/cron/mentat-reporter-py.cron        |  2 +-
 conf/cron/mentat-statistician-py.cron    |  2 +-
 conf/cron/mentat-watchdog-events-py.cron |  2 +-
 conf/cron/test-mentat-ideagen-py.cron    |  2 +-
 conf/mentat-hawat-dev.py.conf            | 13 ++++---------
 conf/mentat-hawat.py.conf                |  8 ++++----
 doc/sphinx/_doclib/development.rst       | 17 +++++++++++++++--
 vagrantenv/.bashrc                       |  4 ++--
 vagrantenv/provision.sh                  | 22 +++++++++++++++-------
 vagrantenv/system-banner.sh              |  4 ++++
 17 files changed, 55 insertions(+), 53 deletions(-)

diff --git a/Makefile b/Makefile
index 5cec19c5..72bb1ad2 100644
--- a/Makefile
+++ b/Makefile
@@ -321,7 +321,8 @@ deps-webui-upgrade: FORCE
 #
 deps-geoip: FORCE
 	@echo "\n$(GREEN)*** Installing IP geolocation databases ***$(NC)\n"
-	@geoipupdate --verbose
+	@mkdir -p $(shell realpath ./data/geoip)
+	@geoipupdate --verbose --database-directory $(shell realpath ./data/geoip)
 	@echo ""
 
 #
diff --git a/Vagrantfile b/Vagrantfile
index 56a3fb0f..8eacd943 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -26,25 +26,6 @@ Vagrant.configure('2') do |config|
   # your network.
   # config.vm.network "public_network"
 
-  # Share an additional folder to the guest VM. The first argument is
-  # the path on the host to the actual folder. The second argument is
-  # the path on the guest to mount the folder. And the optional third
-  # argument is a set of non-required options.
-  # config.vm.synced_folder "../data", "/vagrant_data"
-
-  # Provider-specific configuration so you can fine-tune various
-  # backing providers for Vagrant. These expose provider-specific options.
-  # Example for VirtualBox:
-  #
-  # config.vm.provider "virtualbox" do |vb|
-  #   # Display the VirtualBox GUI when booting the machine
-  #   vb.gui = true
-  #
-  #   # Customize the amount of memory on the VM:
-  #   vb.memory = "1024"
-  # end
-  #
-
   config.vm.provision 'shell', inline: <<-SHELL
     bash /vagrant/vagrantenv/provision.sh
   SHELL
diff --git a/conf/cron/mentat-backup-py.cron b/conf/cron/mentat-backup-py.cron
index 5b0dc1a6..7c17a9d7 100644
--- a/conf/cron/mentat-backup-py.cron
+++ b/conf/cron/mentat-backup-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Run every day at 5am.
-0 5 * * *   root /usr/local/bin/mentat-backup.py --regular
+0 5 * * *   root /var/mentat/venv/bin/mentat-backup.py --regular
diff --git a/conf/cron/mentat-cleanup-py.cron b/conf/cron/mentat-cleanup-py.cron
index 1bf0428a..dfa9e4fa 100644
--- a/conf/cron/mentat-cleanup-py.cron
+++ b/conf/cron/mentat-cleanup-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Run once in every 6 hours.
-5 */6 * * *   root /usr/local/bin/mentat-cleanup.py --regular
+5 */6 * * *   root /var/mentat/venv/bin/mentat-cleanup.py --regular
diff --git a/conf/cron/mentat-informant-py.cron b/conf/cron/mentat-informant-py.cron
index 63cb4be4..6af8a0ed 100644
--- a/conf/cron/mentat-informant-py.cron
+++ b/conf/cron/mentat-informant-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Provide periodical informational report emails about overall performance of Mentat system.
-0 7 * * *   mentat /usr/local/bin/mentat-informant.py --regular
+0 7 * * *   mentat /var/mentat/venv/bin/mentat-informant.py --regular
diff --git a/conf/cron/mentat-netmngr-py.cron b/conf/cron/mentat-netmngr-py.cron
index b14a2b81..8e36c9b1 100644
--- a/conf/cron/mentat-netmngr-py.cron
+++ b/conf/cron/mentat-netmngr-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Run every day at 6am.
-0 6 * * *   mentat /usr/local/bin/mentat-netmngr.py --regular --command update --whois-file /var/mentat/whois-negistry.json
+0 6 * * *   mentat /var/mentat/venv/bin/mentat-netmngr.py --regular --command update --whois-file /var/mentat/whois-negistry.json
diff --git a/conf/cron/mentat-precache-py.cron b/conf/cron/mentat-precache-py.cron
index eae5e93e..c7bd2144 100644
--- a/conf/cron/mentat-precache-py.cron
+++ b/conf/cron/mentat-precache-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Run once in every 10 minutes.
-*/10 * * * *   mentat /usr/local/bin/mentat-precache.py --regular
+*/10 * * * *   mentat /var/mentat/venv/bin/mentat-precache.py --regular
diff --git a/conf/cron/mentat-reporter-py.cron b/conf/cron/mentat-reporter-py.cron
index 8ade5dea..026887f0 100644
--- a/conf/cron/mentat-reporter-py.cron
+++ b/conf/cron/mentat-reporter-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Run once in every 10 minutes.
-*/10 * * * *  mentat /usr/local/bin/mentat-reporter.py --regular
+*/10 * * * *  mentat /var/mentat/venv/bin/mentat-reporter.py --regular
diff --git a/conf/cron/mentat-statistician-py.cron b/conf/cron/mentat-statistician-py.cron
index ae90661f..02daebd5 100644
--- a/conf/cron/mentat-statistician-py.cron
+++ b/conf/cron/mentat-statistician-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Run once in every 5 minutes.
-*/5 * * * *  mentat /usr/local/bin/mentat-statistician.py --regular
+*/5 * * * *  mentat /var/mentat/venv/bin/mentat-statistician.py --regular
diff --git a/conf/cron/mentat-watchdog-events-py.cron b/conf/cron/mentat-watchdog-events-py.cron
index 9a5dc849..f5974b26 100644
--- a/conf/cron/mentat-watchdog-events-py.cron
+++ b/conf/cron/mentat-watchdog-events-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Run every 15 minutes.
-*/15 * * * *   root /usr/local/bin/mentat-dbmngr.py --command=watchdog-events --regular
+*/15 * * * *   root /var/mentat/venv/bin/mentat-dbmngr.py --command=watchdog-events --regular
diff --git a/conf/cron/test-mentat-ideagen-py.cron b/conf/cron/test-mentat-ideagen-py.cron
index b30f7693..3a1e654b 100644
--- a/conf/cron/test-mentat-ideagen-py.cron
+++ b/conf/cron/test-mentat-ideagen-py.cron
@@ -11,4 +11,4 @@
 #
 
 # Run once in every 2 minutes.
-*/2 * * * *  mentat /usr/local/bin/mentat-ideagen.py --random-count 20
+*/2 * * * *  mentat /var/mentat/venv/bin/mentat-ideagen.py --random-count 20
diff --git a/conf/mentat-hawat-dev.py.conf b/conf/mentat-hawat-dev.py.conf
index 7d3e99a9..752ea68b 100644
--- a/conf/mentat-hawat-dev.py.conf
+++ b/conf/mentat-hawat-dev.py.conf
@@ -7,11 +7,6 @@ DEBUG = True
 # Enable profiler on debug toolbar. DEBUG must be 'True' for toolbar to be visible.
 #DEBUG_TB_PROFILER_ENABLED = True
 
-#FLASK_ENV=production
-#FLASK_ENV=development
-#FLASK_CONFIG=production
-#FLASK_CONFIG=development
-
 # Move the application from webserver root to different location.
 #APPLICATION_ROOT = '/mentat'
 
@@ -25,10 +20,10 @@ SECRET_KEY = '!!!-CHANGE-ME-ASAP-!!!-local-secret-key'
 #HAWAT_REPORT_FEEDBACK_MAILS = ['admin@domain.org']
 
 # Directories with translations used in Hawat.
-BABEL_DEFAULT_LOCALE          = 'en'
-BABEL_DEFAULT_TIMEZONE        = 'UTC'
-#BABEL_DEFAULT_LOCALE         = 'cs'
-#BABEL_DEFAULT_TIMEZONE       = 'Europe/Prague'
+BABEL_DEFAULT_LOCALE    = 'en'
+BABEL_DEFAULT_TIMEZONE  = 'UTC'
+#BABEL_DEFAULT_LOCALE   = 'cs'
+#BABEL_DEFAULT_TIMEZONE = 'Europe/Prague'
 
 # Limit for number of objects for which to automatically fetch additional data services.
 HAWAT_LIMIT_AODS = 20
diff --git a/conf/mentat-hawat.py.conf b/conf/mentat-hawat.py.conf
index 945804d7..1f42ac26 100644
--- a/conf/mentat-hawat.py.conf
+++ b/conf/mentat-hawat.py.conf
@@ -20,10 +20,10 @@ SECRET_KEY = '!!!-CHANGE-ME-ASAP-!!!-local-secret-key'
 #HAWAT_REPORT_FEEDBACK_MAILS = ['admin@domain.org']
 
 # Directories with translations used in Hawat.
-BABEL_DEFAULT_LOCALE          = 'en'
-BABEL_DEFAULT_TIMEZONE        = 'UTC'
-#BABEL_DEFAULT_LOCALE         = 'cs'
-#BABEL_DEFAULT_TIMEZONE       = 'Europe/Prague'
+BABEL_DEFAULT_LOCALE    = 'en'
+BABEL_DEFAULT_TIMEZONE  = 'UTC'
+#BABEL_DEFAULT_LOCALE   = 'cs'
+#BABEL_DEFAULT_TIMEZONE = 'Europe/Prague'
 
 # Limit for number of objects for which to automatically fetch additional data services.
 HAWAT_LIMIT_AODS = 20
diff --git a/doc/sphinx/_doclib/development.rst b/doc/sphinx/_doclib/development.rst
index 689dd0f1..5618efe5 100644
--- a/doc/sphinx/_doclib/development.rst
+++ b/doc/sphinx/_doclib/development.rst
@@ -627,8 +627,21 @@ Development with Vagrant
 	make data-fetch
 	vagrant up
 	vagrant ssh
-	cd /vagrant/
-	make data-import
+
+After connecting to development machine you will be greeted with banner and some
+initial tips:
+
+.. code-block:: shell
+	# Alias to quickly enter work directory:
+	wd # Stands for 'WorkDirectory'
+	# Alias to enable Python virtual environment:
+	ve # Stands for 'VirtualEnvironment'
+
+Keep in mind following:
+
+* You will be connected as user ``vagrant`` with passwordless sudo privileges.
+* User ``mentat`` is also with passwordless sudo privileges.
+* Users ``mentat``, ``vagrant`` and ``root`` are all superusers for local development PostgreSQL database.
 
 At this point you are all set to develop Mentat locally without cluttering your personal
 workstation.
diff --git a/vagrantenv/.bashrc b/vagrantenv/.bashrc
index 08548e21..2ef0780c 100644
--- a/vagrantenv/.bashrc
+++ b/vagrantenv/.bashrc
@@ -106,8 +106,8 @@ alias cd..='cd ..'
 alias ..='cd ..'
 alias ...='cd ../..'
 alias ....='cd ../../..'
-alias cdm='cd /vagrant/'
-alias entervenv='. /var/mentat/venv/bin/activate'
+alias wd='cd /vagrant/'
+alias ve='. /var/mentat/venv/bin/activate'
 
 # enable programmable completion features (you don't need to enable
 # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
diff --git a/vagrantenv/provision.sh b/vagrantenv/provision.sh
index f8b2e787..a0370426 100644
--- a/vagrantenv/provision.sh
+++ b/vagrantenv/provision.sh
@@ -21,12 +21,6 @@ dpkg-reconfigure -f noninteractive tzdata && \
   dpkg-reconfigure --frontend=noninteractive locales && \
   update-locale LANG=en_US.UTF-8
 
-echo "==========> Linking configuration files"
-ensure_link /vagrant/etc/default/mentat /etc/default/mentat
-ensure_link /vagrant/conf /etc/mentat
-ensure_link /vagrant/vagrantenv/.bashrc /home/vagrant/.bashrc
-ensure_link /vagrant/data/GeoIP.conf /etc/GeoIP.conf
-
 echo "==========> Creating Mentat user for development"
 if ! getent passwd ${MENTAT_USER} > /dev/null 2>&1; then
     useradd -m -s /bin/bash -U ${MENTAT_USER} --groups sudo
@@ -35,6 +29,15 @@ if ! getent passwd ${MENTAT_USER} > /dev/null 2>&1; then
     echo "%${MENTAT_USER} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/mentat
 fi
 
+echo "==========> Linking configuration files"
+ensure_link /vagrant/etc/default/mentat /etc/default/mentat
+ensure_link /vagrant/conf /etc/mentat
+ensure_link /vagrant/vagrantenv/.bashrc /root/.bashrc
+ensure_link /vagrant/vagrantenv/.bashrc /home/vagrant/.bashrc
+ensure_link /vagrant/vagrantenv/.bashrc /home/mentat/.bashrc
+ensure_link /vagrant/data/GeoIP.conf /etc/GeoIP.conf
+ensure_link /vagrant/data/geoip /usr/share/GeoIP
+
 echo "==========> Installing essential dependencies"
 apt-get update -qq \
   && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade \
@@ -140,7 +143,7 @@ systemctl daemon-reload
 systemctl start sendria.service
 
 echo "==========> Bootstraping runtime environment"
-/etc/mentat/scripts/runenv-init.sh
+/etc/mentat/scripts/init-runenv.sh
 
 echo "==========> Bootstraping database schema and configuration"
 sudo -u mentat ${MENTAT_VENV}/bin/python /vagrant/bin/mentat-dbmngr.py --command init
@@ -157,6 +160,11 @@ if [ $? -ne 0 ]; then
     echo "Creating default PostgreSQL user 'root'"
     sudo -u postgres psql -c "CREATE USER root WITH SUPERUSER CREATEDB ENCRYPTED PASSWORD 'root';"
 fi
+sudo -u postgres psql -c "SELECT usename FROM pg_catalog.pg_user;" | grep mentat > /dev/null
+if [ $? -ne 0 ]; then
+    echo "Creating default PostgreSQL user 'mentat'"
+    sudo -u postgres psql -c "CREATE USER mentat WITH SUPERUSER CREATEDB ENCRYPTED PASSWORD 'mentat';"
+fi
 sudo -u postgres psql -c "SELECT usename FROM pg_catalog.pg_user;" | grep vagrant > /dev/null
 if [ $? -ne 0 ]; then
     echo "Creating default PostgreSQL user 'vagrant'"
diff --git a/vagrantenv/system-banner.sh b/vagrantenv/system-banner.sh
index b91978e4..b2d7e673 100755
--- a/vagrantenv/system-banner.sh
+++ b/vagrantenv/system-banner.sh
@@ -13,3 +13,7 @@ echo ""
 echo "    Development repository located at:    /vagrant/"
 echo "    Development web interface located at: https://localhost:4443"
 echo ""
+echo "    Tips:"
+echo "      Use alias 'wd' to quickly change to project's Work Directory."
+echo "      Use alias 'entervenv' to activate project's virtual environment."
+echo ""
-- 
GitLab