From f4a90d398eb1f15cef23de9bb032fab93e2b1b76 Mon Sep 17 00:00:00 2001
From: Marko Mecina <marko.mecina@univie.ac.at>
Date: Fri, 2 Sep 2022 17:16:15 +0200
Subject: [PATCH] add make target for creating DB user

---
 Makefile         | 71 ++++++++++++++++++++++++------------------------
 README           | 13 ++++++---
 mk_mysql_user.sh |  8 ++++++
 3 files changed, 53 insertions(+), 39 deletions(-)
 create mode 100755 mk_mysql_user.sh

diff --git a/Makefile b/Makefile
index 5ee2380..6401c89 100644
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,23 @@
-.PHONY: install-confignator ccs-storage codeblockreusefeature install-testlib
+.PHONY: confignator ccs-storage codeblockreusefeature install-testlib python-requirements db-user database-dev-env databases
 
-# all: install install-database-dev-env
-
-install: install-python-requirements install-confignator ccs-storage codeblockreusefeature
+install: python-requirements confignator ccs-storage codeblockreusefeature
 
 databases: ccs-storage codeblockreusefeature
 
-install-confignator:
+db-user:
+	sudo ./mk_mysql_user.sh $(USER) $(PW)
+
+python-requirements:
+	@echo "+-----------------------------+"
+	@echo "| installing Python modules   |"
+	@echo "+-----------------------------+"
+		if [ -z $VIRTUAL_ENV ]; then pip install --user -U -r $(CURDIR)/requirements.txt; else pip install -U -r $(CURDIR)/requirements.txt; fi
+	@echo "+-----------------------------+"
+	@echo "| installed Python modules    |"
+	@echo "+-----------------------------+"
+	@echo
+
+confignator:
 	@echo "+---------------------------------------+"
 	@echo "| installing confignator Python package |"
 	@echo "+---------------------------------------+"
@@ -18,36 +29,6 @@ install-confignator:
 	@echo "+--------------------------------------+"
 	@echo
 
-install-testlib:
-	@echo "+-----------------------------------+"
-	@echo "| installing testlib Python package |"
-	@echo "+-----------------------------------+"
-	    $(MAKE) all -C $(CURDIR)/Tst/testing_library
-	@echo "+----------------------------------+"
-	@echo "| installed testlib Python package |"
-	@echo "+----------------------------------+"
-	@echo
-
-install-database-dev-env:
-	@echo "+---------------------------------------------------+"
-	@echo "| installing Python dev packages for the database   |"
-	@echo "+---------------------------------------------------+"
-	    $(MAKE) install-devenv -C $(CURDIR)/Ccs/database
-	@echo "+---------------------------------------------------+"
-	@echo "| installed Python dev packages for the database    |"
-	@echo "+---------------------------------------------------+"
-	@echo
-
-install-python-requirements:
-	@echo "+-----------------------------+"
-	@echo "| installing Python modules   |"
-	@echo "+-----------------------------+"
-		if [ -z $VIRTUAL_ENV ]; then pip install --user -U -r $(CURDIR)/requirements.txt; else pip install -U -r $(CURDIR)/requirements.txt; fi
-	@echo "+-----------------------------+"
-	@echo "| installed Python modules    |"
-	@echo "+-----------------------------+"
-	@echo
-	
 ccs-storage:
 	@echo "+----------------------------------------+"
 	@echo "| setting up the storage database schema |"
@@ -68,6 +49,26 @@ codeblockreusefeature:
 	@echo "+---------------------------------------------------+"
 	@echo
 
+database-dev-env:
+	@echo "+---------------------------------------------------+"
+	@echo "| installing Python dev packages for the database   |"
+	@echo "+---------------------------------------------------+"
+	    $(MAKE) install-devenv -C $(CURDIR)/Ccs/database
+	@echo "+---------------------------------------------------+"
+	@echo "| installed Python dev packages for the database    |"
+	@echo "+---------------------------------------------------+"
+	@echo
+
+install-testlib:
+	@echo "+-----------------------------------+"
+	@echo "| installing testlib Python package |"
+	@echo "+-----------------------------------+"
+	    $(MAKE) all -C $(CURDIR)/Tst/testing_library
+	@echo "+----------------------------------+"
+	@echo "| installed testlib Python package |"
+	@echo "+----------------------------------+"
+	@echo
+
 set-start-scripts-permissions:
 	@echo "+-----------------------------------------------------+"
 	@echo "| setting permissions for the start scripts (execute) |"
diff --git a/README b/README
index 7249c0a..b4057b0 100644
--- a/README
+++ b/README
@@ -19,12 +19,18 @@ This file documents the required steps to get the UVIE EGSE software up and runn
     $> sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
     $> sudo systemctl enable --now mariadb
     $> sudo mariadb-secure-installation  (optional)
+
+    To create a MariaDB user, in the CCS base directory execute
+
+    $> make db-user USER=<user> PW=<password>
+
+    Alternatively, execute the following SQL statements:
     mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
     mysql> GRANT ALL PRIVILEGES ON * . * TO 'user'@'localhost';
     mysql> FLUSH PRIVILEGES;
 
 4) Install Python packages. To install the required modules with pip in userspace or a venv, run
-   'make install-python-requirements' in the CCS base directory or use 'pip install [--user] -r requirements.txt'.
+   'make python-requirements' in the CCS base directory or use 'pip install [--user] -r requirements.txt'.
    If using a venv, it must have been created using the --system-site-packages option.
    For more details, see requirements.txt
 
@@ -35,9 +41,8 @@ In the CCS base directory,
     - first configure the [database] section in egse.cfg!
     - then execute:
     
-    $> make install-confignator
-    $> make ccs-storage
-    $> make codeblockreusefeature
+    $> make confignator
+    $> make databases
     
 # MIB
 Use CCS/Ccs/tools/import_mib.py to import a set of SCOS2000 MIB files into the MySQL mib-schema set in egse.cfg
diff --git a/mk_mysql_user.sh b/mk_mysql_user.sh
new file mode 100755
index 0000000..e33b7ac
--- /dev/null
+++ b/mk_mysql_user.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+USER=$1
+PW=$2
+
+mysql -e "CREATE USER '${USER}'@'localhost' IDENTIFIED BY '${PW}';"
+mysql -e "GRANT ALL PRIVILEGES ON * . * TO '${USER}'@'localhost';"
+mysql -e "FLUSH PRIVILEGES;"
\ No newline at end of file
-- 
GitLab