2017-09-23 17:12:54 +02:00
#!/bin/bash
2013-03-24 18:24:08 +01:00
2019-11-14 12:16:02 +01:00
set -o nounset
set -o errexit
set -o pipefail
2013-03-24 18:24:08 +01:00
2019-11-14 12:16:02 +01:00
SCRIPT_PATH = " $( readlink -f " $0 " ) "
SCRIPT_DIR = " ${ SCRIPT_PATH %/* } "
INDEXER = " $( command -v indexer) "
SEARCHD = " $( command -v searchd) "
2013-03-24 18:24:08 +01:00
CRON_ORIG = "/tmp/crontab.piler.orig"
CRON_TMP = "/tmp/crontab.piler"
2013-09-20 20:51:00 +02:00
PILERCONF_TMP = "/tmp/config.piler.88"
2014-01-21 11:18:34 +01:00
SOCKET_HELPER_SCRIPT = "aaa.pl"
2019-11-14 12:16:02 +01:00
PILER_CONFIG_DIR = " ${ SYSCONFDIR } /piler "
CONFIG_SITE_PHP = " ${ PILER_CONFIG_DIR } /config-site.php "
2013-09-28 21:47:34 +02:00
load_default_values( ) {
PILERUSER = "piler"
SYSCONFDIR = __SYSCONFDIR
LOCALSTATEDIR = __LOCALSTATEDIR
LIBEXECDIR = __LIBEXECDIR
2013-10-11 14:13:18 +02:00
DATAROOTDIR = __DATAROOTDIR
2013-09-28 21:47:34 +02:00
KEYTMPFILE = "piler.key"
2019-11-14 12:16:02 +01:00
KEYFILE = " ${ PILER_CONFIG_DIR } /piler.key "
2013-09-28 21:47:34 +02:00
2019-11-14 12:16:02 +01:00
HOSTNAME = " $( hostname --fqdn) "
2013-09-28 21:47:34 +02:00
MYSQL_HOSTNAME = "localhost"
MYSQL_DATABASE = "piler"
MYSQL_USERNAME = "piler"
MYSQL_PASSWORD = ""
MYSQL_ROOT_PASSWORD = ""
2013-11-01 13:06:48 +01:00
MYSQL_SOCKET = ""
2013-09-28 21:47:34 +02:00
2019-11-14 12:16:02 +01:00
SPHINXCFG = " ${ PILER_CONFIG_DIR } /sphinx.conf "
2013-09-28 21:47:34 +02:00
WWWGROUP = "apache"
2017-09-23 17:12:54 +02:00
DOCROOT = "/var/piler/www"
2013-09-28 21:47:34 +02:00
SMARTHOST = ""
SMARTHOST_PORT = 25
SSL_CERT_DATA = "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com"
}
make_certificate( ) {
2019-11-14 12:16:02 +01:00
local piler_pem = " ${ PILER_CONFIG_DIR } /piler.pem "
if [ [ ! -f " $piler_pem " ] ] ; then
2013-09-28 21:47:34 +02:00
echo -n "Making an ssl certificate ... "
2019-11-14 12:16:02 +01:00
openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 -subj " $SSL_CERT_DATA " -keyout " $piler_pem " -out 1.cert -sha1
cat 1.cert >> " $piler_pem "
chmod 640 " $piler_pem "
chgrp " $PILERUSER " " $piler_pem "
rm -f 1.cert
2013-09-28 21:47:34 +02:00
fi
}
display_install_intro( ) {
echo ""
echo ""
echo "This is the postinstall utility for piler"
echo "It should be run only at the first install. DO NOT run on an existing piler installation!"
echo ""
askYN "Continue? [Y/N]" "N"
2017-09-23 17:12:54 +02:00
if [ [ " $response " != "yes" ] ] ; then
2013-09-28 21:47:34 +02:00
echo "Aborted."
2019-11-14 12:16:02 +01:00
exit 1
2013-09-28 21:47:34 +02:00
fi
echo ""
}
check_user( ) {
2017-09-23 17:12:54 +02:00
user = " $1 "
2013-09-28 21:47:34 +02:00
2017-09-23 17:12:54 +02:00
if [ [ $( whoami) != " $user " ] ] ; then echo " ERROR: postinstaller must be run as ${ user } user " ; exit 1; fi
2013-09-28 21:47:34 +02:00
}
isFQDN( ) {
# we need min. 2 dots
2019-11-14 12:16:02 +01:00
if [ [ x" $1 " = "xdogfood" ] ] ; then
2013-09-28 21:47:34 +02:00
echo 1
return
fi
2019-11-14 12:16:02 +01:00
if [ [ x" $1 " = "x" ] ] ; then
2013-09-28 21:47:34 +02:00
echo 0
return
fi
2019-11-14 12:16:02 +01:00
NF = " $( echo " $1 " | awk -F. '{print NF}' ) "
if [ [ $NF -ge 2 ] ] ; then
2013-09-28 21:47:34 +02:00
echo 1
2019-11-13 09:37:48 +01:00
else
2013-09-28 21:47:34 +02:00
echo 0
fi
}
ask( ) {
PROMPT = $1
DEFAULT = $2
echo ""
echo -n " $PROMPT [ $DEFAULT ] "
2019-11-14 12:16:02 +01:00
read -r response
2013-09-28 21:47:34 +02:00
2019-11-14 12:16:02 +01:00
if [ [ -z " $response " ] ] ; then
2013-09-28 21:47:34 +02:00
response = $DEFAULT
fi
}
2013-11-01 11:57:53 +01:00
askNoEcho( ) {
PROMPT = $1
DEFAULT = $2
stty -echo
ask " $PROMPT " " $DEFAULT "
stty echo
echo ""
}
2013-09-28 21:47:34 +02:00
askNonBlankNoEcho( ) {
PROMPT = $1
DEFAULT = $2
2019-11-14 12:16:02 +01:00
while true; do
2013-09-28 21:47:34 +02:00
stty -echo
ask " $PROMPT " " $DEFAULT "
stty echo
echo ""
2019-11-14 12:16:02 +01:00
if [ [ -n " $response " ] ] ; then
2013-09-28 21:47:34 +02:00
break
fi
echo "A non-blank answer is required"
done
}
askNonBlank( ) {
2019-11-14 12:16:02 +01:00
PROMPT = " $1 "
DEFAULT = " $2 "
2013-09-28 21:47:34 +02:00
2019-11-14 12:16:02 +01:00
while true; do
2013-09-28 21:47:34 +02:00
ask " $PROMPT " " $DEFAULT "
2019-11-14 12:16:02 +01:00
if [ [ -n " $response " ] ] ; then
2013-09-28 21:47:34 +02:00
break
fi
echo "A non-blank answer is required"
done
}
askYN( ) {
PROMPT = $1
DEFAULT = $2
2019-11-14 12:16:02 +01:00
if [ [ " x $DEFAULT " = = "xyes" || " x $DEFAULT " = = "xYes" || " x $DEFAULT " = = "xy" || " x $DEFAULT " = = "xY" ] ] ; then
2013-09-28 21:47:34 +02:00
DEFAULT = "Y"
else
DEFAULT = "N"
fi
2019-11-13 09:37:48 +01:00
2019-11-14 12:16:02 +01:00
while true; do
2013-09-28 21:47:34 +02:00
ask " $PROMPT " " $DEFAULT "
response = $( perl -e " print lc(\" $response \"); " )
2019-11-14 12:16:02 +01:00
if [ [ -z " $response " ] ] ; then
2013-09-28 21:47:34 +02:00
:
else
2019-11-14 12:16:02 +01:00
if [ [ " $response " = = "yes" || " $response " = = "y" ] ] ; then
2013-09-28 21:47:34 +02:00
response = "yes"
break
2019-11-13 09:37:48 +01:00
else
2019-11-14 12:16:02 +01:00
if [ [ " $response " = = "no" || " $response " = = "n" ] ] ; then
2013-09-28 21:47:34 +02:00
response = "no"
break
fi
fi
fi
echo "A Yes/No answer is required"
done
}
2013-03-24 18:24:08 +01:00
preinstall_check( ) {
check_user root
2017-09-23 17:12:54 +02:00
if [ [ " $INDEXER " = = "" ] ] ; then "ERROR: cannot find sphinx indexer" ; echo "" ; exit ; fi
2013-03-24 18:24:08 +01:00
2017-09-23 17:12:54 +02:00
if [ [ " $SEARCHD " = = "" ] ] ; then "ERROR: cannot find sphinx searchd" ; echo "" ; exit 0; fi
2013-03-24 18:24:08 +01:00
2017-09-23 17:12:54 +02:00
if [ [ -f " $KEYFILE " ] ] ; then echo " ERROR: found existing keyfile ( ${ KEYFILE } ), aborting install " ; echo "" ; exit 0; fi
2013-03-24 18:24:08 +01:00
}
gather_webserver_data( ) {
askNonBlank "Please enter the webserver groupname" " $WWWGROUP "
2017-09-23 17:12:54 +02:00
WWWGROUP = " $response "
2013-03-24 18:24:08 +01:00
}
gather_mysql_account( ) {
2017-09-23 17:12:54 +02:00
if [ [ -e /var/lib/mysql/mysql.sock ] ] ; then MYSQL_SOCKET = "/var/lib/mysql/mysql.sock" ; fi
if [ [ -e /var/run/mysqld/mysqld.sock ] ] ; then MYSQL_SOCKET = "/var/run/mysqld/mysqld.sock" ; fi
2013-11-01 13:06:48 +01:00
2013-03-24 18:24:08 +01:00
askNonBlank "Please enter mysql hostname" " $MYSQL_HOSTNAME "
2017-09-23 17:12:54 +02:00
MYSQL_HOSTNAME = " $response "
2013-03-24 18:24:08 +01:00
2017-09-23 17:12:54 +02:00
if [ [ $MYSQL_HOSTNAME = = "localhost" ] ] ; then
2013-11-01 13:40:40 +01:00
askNonBlank "Please enter mysql socket path" " $MYSQL_SOCKET "
2017-09-23 17:12:54 +02:00
MYSQL_SOCKET = " $response "
2013-11-01 13:06:48 +01:00
else
MYSQL_SOCKET = ""
fi
2017-09-23 17:12:54 +02:00
askNonBlank "Please enter mysql database" " ${ MYSQL_DATABASE } "
MYSQL_DATABASE = " $response "
2013-03-24 18:24:08 +01:00
2017-09-23 17:12:54 +02:00
askNonBlank "Please enter mysql user name" " ${ MYSQL_USERNAME } "
MYSQL_USERNAME = " $response "
2013-03-24 18:24:08 +01:00
2017-09-23 17:12:54 +02:00
askNoEcho " Please enter mysql password for ${ MYSQL_USERNAME } " ""
MYSQL_PASSWORD = " $response "
2013-03-24 18:24:08 +01:00
askNonBlankNoEcho "Please enter mysql root password" ""
2017-09-23 17:12:54 +02:00
MYSQL_ROOT_PASSWORD = " $response "
2013-03-24 18:24:08 +01:00
2017-09-23 17:12:54 +02:00
s = $( echo " use information_schema; select TABLE_NAME from TABLES where TABLE_SCHEMA=' ${ MYSQL_DATABASE } ' " | mysql -h " $MYSQL_HOSTNAME " -u root --password= " $MYSQL_ROOT_PASSWORD " )
2013-03-24 18:24:08 +01:00
if [ $? -eq 0 ] ;
then
echo "mysql connection successful" ; echo;
2019-11-14 12:16:02 +01:00
if [ [ $( echo " $s " | grep -c metadata) -eq 1 ] ] ; then echo " ERROR: Detected metadata table in ${ MYSQL_DATABASE } . Aborting " ; exit 0; fi
2013-03-24 18:24:08 +01:00
else
echo "ERROR: failed to connect to mysql" ;
gather_mysql_account
fi
}
gather_smtp_relay_data( ) {
ask "Please enter smtp relay" " $SMARTHOST "
2017-09-23 17:12:54 +02:00
SMARTHOST = " $response "
2013-03-24 18:24:08 +01:00
2013-07-09 10:12:42 +02:00
ask "Please enter smtp relay port" " $SMARTHOST_PORT "
2017-09-23 17:12:54 +02:00
SMARTHOST_PORT = " $response "
2013-03-24 18:24:08 +01:00
}
make_cron_entries( ) {
2017-09-23 17:12:54 +02:00
crontab -u " $PILERUSER " -l > " $CRON_ORIG "
2013-03-24 18:24:08 +01:00
2019-11-14 12:16:02 +01:00
if grep PILERSTART " $CRON_ORIG " > /dev/null 2>& 1 != 0; then
2017-09-23 17:12:54 +02:00
cat /dev/null > " $CRON_ORIG "
2013-03-24 18:24:08 +01:00
fi
2019-11-14 12:16:02 +01:00
if grep PILEREND " $CRON_ORIG " > /dev/null 2>& 1 != 0; then
2017-09-23 17:12:54 +02:00
cat /dev/null > " $CRON_ORIG "
2013-03-24 18:24:08 +01:00
fi
2017-09-23 17:12:54 +02:00
rm -f " $CRON_TMP "
2013-03-24 18:24:08 +01:00
2019-11-14 12:16:02 +01:00
{
echo "" ;
echo "### PILERSTART" ;
echo " 5,35 * * * * ${ LIBEXECDIR } /piler/indexer.delta.sh " ;
echo " 30 2 * * * ${ LIBEXECDIR } /piler/indexer.main.sh " ;
echo " 3 * * * * ${ LIBEXECDIR } /piler/watch_sphinx_main_index.sh " ;
echo " */15 * * * * ${ INDEXER } --quiet tag1 --rotate --config ${ PILER_CONFIG_DIR } /sphinx.conf " ;
echo " */15 * * * * ${ INDEXER } --quiet note1 --rotate --config ${ PILER_CONFIG_DIR } /sphinx.conf " ;
echo " 30 6 * * * /usr/bin/php ${ LIBEXECDIR } /piler/generate_stats.php --webui ${ DOCROOT } >/dev/null " ;
echo " */5 * * * * /usr/bin/find ${ LOCALSTATEDIR } /piler/error -type f|wc -l > ${ LOCALSTATEDIR } /piler/stat/error " ;
echo " */5 * * * * /usr/bin/find ${ DOCROOT } /tmp -type f -name i.\* -exec rm -f {} \; " ;
echo "### PILEREND" ;
} >> " $CRON_TMP "
2013-03-24 18:24:08 +01:00
}
make_new_key( ) {
2017-09-23 17:12:54 +02:00
dd if = /dev/urandom bs = 56 count = 1 of = " $KEYTMPFILE " 2>/dev/null
2013-03-24 18:24:08 +01:00
2019-11-14 12:16:02 +01:00
if [ " $( stat -c '%s' " $KEYTMPFILE " ) " -ne 56 ] ; then echo " could not read 56 bytes from /dev/urandom to ${ KEYTMPFILE } " ; exit 1; fi
2013-03-24 18:24:08 +01:00
}
show_summary( ) {
echo
echo
echo "INSTALLATION SUMMARY:"
echo
2017-09-23 17:12:54 +02:00
echo " piler user: ${ PILERUSER } "
echo " keyfile: ${ KEYFILE } "
2013-03-24 18:24:08 +01:00
echo
2017-09-23 17:12:54 +02:00
echo " mysql host: ${ MYSQL_HOSTNAME } "
echo " mysql socket: ${ MYSQL_SOCKET } "
echo " mysql database: ${ MYSQL_DATABASE } "
echo " mysql username: ${ MYSQL_USERNAME } "
2013-03-24 18:24:08 +01:00
echo "mysql password: *******"
echo
2017-09-23 17:12:54 +02:00
echo " sphinx indexer: ${ INDEXER } "
echo " sphinx config file: ${ SPHINXCFG } "
2013-03-24 18:24:08 +01:00
echo
2017-09-23 17:12:54 +02:00
echo " vhost docroot: ${ DOCROOT } "
echo " www group: ${ WWWGROUP } "
2013-03-24 18:24:08 +01:00
echo
2017-09-23 17:12:54 +02:00
echo " smtp relay host: ${ SMARTHOST } "
echo " smtp relay port: ${ SMARTHOST_PORT } "
2013-03-24 18:24:08 +01:00
echo
echo "piler crontab:"
2017-09-23 17:12:54 +02:00
cat " $CRON_TMP "
2013-03-24 18:24:08 +01:00
echo; echo;
askYN "Correct? [Y/N]" "N"
2017-09-23 17:12:54 +02:00
if [ [ $response != "yes" ] ] ; then
2013-03-24 18:24:08 +01:00
echo "Aborted."
exit
fi
}
execute_post_install_tasks( ) {
askYN "Continue and modify system? [Y/N]" "N"
2017-09-23 17:12:54 +02:00
if [ [ " $response " != "yes" ] ] ; then
2013-03-24 18:24:08 +01:00
echo "Aborted."
exit
fi
echo;
echo -n "Creating mysql database... " ;
2017-09-23 17:12:54 +02:00
sed -e " s%MYSQL_HOSTNAME% ${ MYSQL_HOSTNAME } %g " -e " s%MYSQL_DATABASE% ${ MYSQL_DATABASE } %g " -e " s%MYSQL_USERNAME% ${ MYSQL_USERNAME } %g " -e " s%MYSQL_PASSWORD% ${ MYSQL_PASSWORD } %g " " ${ DATAROOTDIR } /piler/db-mysql-root.sql.in " | mysql -h " $MYSQL_HOSTNAME " -u root --password= " $MYSQL_ROOT_PASSWORD "
mysql -h " $MYSQL_HOSTNAME " -u " $MYSQL_USERNAME " --password= " $MYSQL_PASSWORD " " $MYSQL_DATABASE " < " ${ DATAROOTDIR } /piler/db-mysql.sql "
2013-03-24 18:24:08 +01:00
echo "Done."
2013-10-13 21:39:34 +02:00
echo -n "Writing sphinx configuration... " ;
2017-09-23 17:12:54 +02:00
sed -e " s%MYSQL_HOSTNAME% ${ MYSQL_HOSTNAME } % " -e " s%MYSQL_DATABASE% ${ MYSQL_DATABASE } % " -e " s%MYSQL_USERNAME% ${ MYSQL_USERNAME } % " -e " s%MYSQL_PASSWORD% ${ MYSQL_PASSWORD } % " " ${ SYSCONFDIR } /piler/sphinx.conf.dist " > " $SPHINXCFG "
2013-03-24 18:24:08 +01:00
echo "Done."
echo -n "Initializing sphinx indices... " ;
2017-09-23 17:12:54 +02:00
su " $PILERUSER " -c " indexer --all --config ${ SPHINXCFG } "
2013-03-24 18:24:08 +01:00
echo "Done."
2017-09-23 17:12:54 +02:00
echo -n " installing cron entries for ${ PILERUSER } ... "
crontab -u " $PILERUSER " " $CRON_TMP "
2013-03-24 18:24:08 +01:00
echo "Done."
2017-09-23 17:12:54 +02:00
echo -n " installing keyfile ( ${ KEYTMPFILE } ) to ${ KEYFILE } ... "
cp " $KEYTMPFILE " " $KEYFILE "
chgrp " $PILERUSER " " $KEYFILE "
chmod 640 " $KEYFILE "
rm -f " $KEYTMPFILE "
2013-03-24 18:24:08 +01:00
echo "Done."
2013-09-28 21:47:34 +02:00
make_certificate
2017-09-23 17:12:54 +02:00
cat <<SOCKHELPER > "$SOCK ET_HELPER_SCRIPT"
2014-01-21 11:18:34 +01:00
\$ a = \$ ARGV[ 0] ;
\$ a = ~s/\/ /\\ \\ \/ /g;
print \$ a;
SOCKHELPER
2017-09-23 17:12:54 +02:00
MYSQL_SOCKET = $( perl " $SOCKET_HELPER_SCRIPT " " $MYSQL_SOCKET " )
2013-11-01 13:06:48 +01:00
2017-09-23 17:12:54 +02:00
sed -e " s/mysqlpwd=verystrongpassword/mysqlpwd= ${ MYSQL_PASSWORD } / " -e "s/tls_enable=0/tls_enable=1/" -e " s/mysqlsocket=\/var\/run\/mysqld\/mysqld.sock/mysqlsocket= ${ MYSQL_SOCKET } / " " ${ SYSCONFDIR } /piler/piler.conf.dist " > " $PILERCONF_TMP "
cat " $PILERCONF_TMP " > " ${ SYSCONFDIR } /piler/piler.conf "
rm -f " $PILERCONF_TMP "
2013-09-20 20:51:00 +02:00
2017-09-23 17:12:54 +02:00
chmod 755 " ${ LOCALSTATEDIR } /piler/stat "
2013-08-04 12:33:34 +02:00
2017-09-23 17:12:54 +02:00
if [ [ -d webui ] ] ; then
echo -n " Copying www files to ${ DOCROOT } ... "
mkdir -p " $DOCROOT " || exit 1
cp -R webui/* " $DOCROOT "
cp webui/.htaccess " $DOCROOT "
2013-11-02 15:02:35 +01:00
fi
2017-09-23 17:12:54 +02:00
if [ [ -d /var/www/piler.yourdomain.com ] ] ; then
mv /var/www/piler.yourdomain.com " $DOCROOT "
2013-11-02 15:02:35 +01:00
fi
2019-11-15 15:50:31 +01:00
if [ [ -d " $DOCROOT " ] ] ; then webui_install; fi
2013-10-13 22:29:45 +02:00
}
webui_install( ) {
2017-09-23 17:12:54 +02:00
chmod 770 " ${ DOCROOT } /tmp " " ${ DOCROOT } /images "
2019-11-15 15:50:31 +01:00
chown " $PILERUSER " " ${ DOCROOT } /tmp "
2013-03-24 18:24:08 +01:00
2019-11-15 15:50:31 +01:00
envsubst < " ${ SCRIPT_DIR } /config-site.php.in " | sed s%config%\$ config%g > " $CONFIG_SITE_PHP "
2013-03-24 18:24:08 +01:00
echo "Done."
}
clean_up_temp_stuff( ) {
2017-09-23 17:12:54 +02:00
rm -f " $CRON_TMP "
2013-03-24 18:24:08 +01:00
echo; echo "Done post installation tasks." ; echo
}
load_default_values
preinstall_check
display_install_intro
gather_webserver_data
gather_mysql_account
gather_smtp_relay_data
make_cron_entries
make_new_key
show_summary
execute_post_install_tasks
clean_up_temp_stuff