From 85c72da85184ee6f874bf231e523f66ad4a37095 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Wed, 18 Jun 2025 09:40:07 -0400 Subject: [PATCH] Added script to create db --- conf/mailleur.conf | 2 +- mailleur.spec.in | 3 ++- support/crdb.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++ support/starting.sh | 50 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 107 insertions(+), 4 deletions(-) create mode 100755 support/crdb.sh mode change 100644 => 100755 support/starting.sh diff --git a/conf/mailleur.conf b/conf/mailleur.conf index cc8622f..0d6e1e0 100644 --- a/conf/mailleur.conf +++ b/conf/mailleur.conf @@ -30,7 +30,7 @@ CA_KEY_CLT="./certs/localhost-key.pem" CA_VERIFY_CLT=0 #to check PEER/server remote certificate #------------------------------------------------ #Configured for Postgresql database -#DB_TYPE can be either POSTGRES,MYSQL +#DB_TYPE can be either POSTGRESQL,MYSQL DB_TYPE=TO_BE_DEFINED DB_NAME=mailleur DB_HOST=localhost diff --git a/mailleur.spec.in b/mailleur.spec.in index d17a9e1..7ba6786 100644 --- a/mailleur.spec.in +++ b/mailleur.spec.in @@ -39,6 +39,7 @@ search about email exchange within time and transaction context. %attr(0755,%{name},mail) %{_sbindir}/sorter %attr(0754,root,root) %{_libdir}/%{name}/shell/*.sh %attr(0754,root,root) %{_libdir}/%{name}/support/starting.sh +%attr(0754,root,root) %{_libdir}/%{name}/support/crdb.sh %attr(0755,%{name},mail) %dir %{spooldir}/%{name}/{queue,in.log,out.log} #----------------------------------------------------------------------------- @@ -148,7 +149,7 @@ Obsoletes : %{name}-mysql <= %{version}-%{release} %post postgresql if [ "$1" = 1 ]; then sed -i \ - -e "s/DB_TO_BE_DEFINED/POSTGRES/" \ + -e "s/DB_TO_BE_DEFINED/POSTGRESQL/" \ %{_sysconfdir}/%{name}/%{name}.conf fi diff --git a/support/crdb.sh b/support/crdb.sh new file mode 100755 index 0000000..cf406be --- /dev/null +++ b/support/crdb.sh @@ -0,0 +1,56 @@ +#! /bin/sh +#------------------------------------------------------------- +#procedure to create the clement data-base the first +#time the application is installed. +#------------------------------------------------------------- +#loading the variable value +. /etc/mailleur/mailleur.conf +#------------------------------------------------------------- + +#setting the Variable +#first the Data-base type +if [ -z "$DB_TYPE" ] ; then + DB_TYPE=POSTGRESQL; + fi + +#Then the data-base name +if [ -z "$DB_NAME" ] ; then + DB_NAME=$APPNAME; + fi +#------------------------------------------------------------- +#moving to the right directory +cd $1 +LOG=/etc/$APPNAME/dbinst.log +USER=`id -nu` +#building data_base +case "$DB_TYPE" in + "MYSQL" ) + ;; + "POSTGRESQL" ) + SQL="psql -q" + if [ ! -z "$DB_PORT" ] ; then + SQLPORT="-p $DB_PORT" + fi + if [ ! -z "$DB_HOST" ] ; then + SQLHOST="-h $DB_HOST" + fi + /bin/echo "creating $DB_TYPE:$DB_NAME data-base" >> $LOG + if [ "$USER" = "root" ] ; then + chown $APPNAME $LOG + /bin/su - -m postgres >> $LOG << !EOT + echo "CREATE ROLE $APPNAME WITH LOGIN CREATEDB SUPERUSER" | \ + $SQL $SQLHOST $SQLPORT template1 + echo "CREATE ROLE apache WITH LOGIN" | \ + $SQL $SQLHOST $SQLPORT template1 + echo "CREATE ROLE dovecot WITH LOGIN" | \ + $SQL $SQLHOST $SQLPORT template1 + /usr/bin/createdb -T template0 -E $DB_LANG -O \ + $APPNAME $SQLHOST $SQLPORT $DB_NAME +!EOT + /bin/echo "data-base is now created" >> $LOG + fi + ;; + "*" ) + #undefined database type at that stage? + ;; + esac diff --git a/support/starting.sh b/support/starting.sh old mode 100644 new mode 100755 index 9de26be..a90c1db --- a/support/starting.sh +++ b/support/starting.sh @@ -7,8 +7,8 @@ OS=$1 #set the OS Type #Hard coded variables APPNAME=mailleur #=============================================================== -#---------------------------------------------------------------------- #reporting a big trouble +#---------------------------------------------------------------------- sh_failure() { @@ -30,7 +30,6 @@ case "$OS" in } #---------------------------------------------------------------------- #procedure to check if the config is properly done -#you MUST AT LEAST, specify the Data Base type to be used by clement. #---------------------------------------------------------------------- chk_config() @@ -42,8 +41,54 @@ if [ -z "$DB_TYPE" -o "$DB_TYPE" = "DB_TO_BE_DEFINED" ] ; then echo -e "\tAborting start, Exiting at once" exit -1; fi +#---------------------------------------------------------------------- +#building application data-base +#---------------------------------------------------------------------- +do_mkdb() + +{ +case "$DB_TYPE" in + "MYSQL" ) + ;; + "POSTGRESQL" ) + /usr/lib/$APPNAME/support/crdb.sh "/" + ;; + "TO_BE_DEFINED" ) + echo "You must install $APPNAME with a DB flavor (ex: $APPNAME-pgsql, $APPNAME-mysql)" + echo "Exiting now" + exit -2; + ;; + "*" ) + echo "Unexpectd DB_TYPE variable within /etc/sysconfig/$APPNAME, please check" + echo "Exiting now" + exit -3; + ;; + esac +} } +#---------------------------------------------------------------------- +#setting local mailleur configuration +#---------------------------------------------------------------------- +do_mkconf() +{ +if [ ! -f /etc/$APPNAME/config.done ] ; then + do_mkdb ; + case "$OS" in + osukiss ) + ;; + sysvinit ) + ;; + systemd ) + ;; + * ) + echo "sh_mkconf: Unexpected OS=$OS, exiting!" + exit 1 + ;; + esac + date > /etc/$APPNAME/config.done +fi +} #=============================================================== #main script #---------------------------------------------------------------------- @@ -53,5 +98,6 @@ if [ -z "$DB_TYPE" -o "$DB_TYPE" = "DB_TO_BE_DEFINED" ] ; then [ -f /etc/sysconfig/$APPNAME ] && . /etc/sysconfig/$APPNAME chk_config $1; +do_mkconf $1; exit 0; #--------------------------------------------------------------- -- 2.47.3