2016-10-25 13:29:10 +00:00
#!/bin/bash
2016-10-25 13:51:46 +00:00
#
# packager.io postinstall script
#
2016-10-25 15:59:31 +00:00
PATH = /opt/zammad/bin:/opt/zammad/vendor/bundle/bin:/sbin:/bin:/usr/sbin:/usr/bin:
2016-10-25 16:07:10 +00:00
ZAMMAD_DIR = "/opt/zammad"
2016-10-25 15:59:31 +00:00
DB = "zammad_production"
DB_USER = "zammad"
2016-11-02 17:18:29 +00:00
MY_CNF = "/etc/mysql/debian.cnf"
2016-10-25 15:59:31 +00:00
2016-10-27 10:15:51 +00:00
# check which init system is used
2016-11-14 12:54:04 +00:00
if [ -n " $( which initctl 2> /dev/null) " ] ; then
2016-10-27 11:50:18 +00:00
INIT_CMD = "initctl"
2016-11-14 12:54:04 +00:00
elif [ -n " $( which systemctl 2> /dev/null) " ] ; then
2016-10-27 11:50:18 +00:00
INIT_CMD = "systemctl"
2016-10-27 10:15:51 +00:00
else
function sysvinit ( ) {
service $2 $1
}
INIT_CMD = "sysvinit"
fi
2016-10-26 14:28:59 +00:00
echo "# (Re)creating init scripts"
2016-10-26 11:45:14 +00:00
zammad scale web = 1 websocket = 1 worker = 1
2016-10-26 14:28:59 +00:00
echo "# Stopping Zammad"
2016-10-27 10:15:51 +00:00
${ INIT_CMD } stop zammad
2016-10-25 15:59:31 +00:00
2016-10-27 10:15:51 +00:00
# check if database.yml exists
if [ -f ${ ZAMMAD_DIR } /config/database.yml ] ; then
2016-10-26 11:45:14 +00:00
# db migration
2016-11-02 17:18:29 +00:00
echo "# database.yml found. Updating db..."
2016-10-26 11:45:14 +00:00
zammad run rake db:migrate
else
2016-11-02 17:18:29 +00:00
echo "# database.yml not found. Creating db..."
2016-10-26 14:28:59 +00:00
# create new password
2016-10-25 15:59:31 +00:00
DB_PASS = " $( tr -dc A-Za-z0-9 < /dev/urandom | head -c10) "
2016-11-02 17:18:29 +00:00
# postgresql
2016-11-14 12:54:04 +00:00
if [ -n " $( which psql 2> /dev/null) " ] ; then
echo "# Installing zammad on postgresql"
2016-10-31 14:46:37 +00:00
2016-11-02 17:18:29 +00:00
# centos
2016-11-14 12:54:04 +00:00
if [ -n " $( which postgresql-setup 2> /dev/null) " ] ; then
2016-11-15 09:29:31 +00:00
echo "# Preparing postgresql server"
postgresql-setup initdb
2016-10-31 14:46:37 +00:00
2016-11-15 09:29:31 +00:00
echo "# Backuping postgres config"
test -f /var/lib/pgsql/data/pg_hba.conf.bak || cp /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.bak
2016-10-31 14:46:37 +00:00
2016-11-15 09:29:31 +00:00
echo "# Allow login via username and password in postgresql"
sed 's/ident/trust/g' < /var/lib/pgsql/data/pg_hba.conf.bak > /var/lib/pgsql/data/pg_hba.conf
2016-11-02 17:18:29 +00:00
2016-11-15 09:29:31 +00:00
echo "# Restarting postgresql server"
${ INIT_CMD } restart postgresql
2016-11-02 17:18:29 +00:00
2016-11-15 09:29:31 +00:00
echo "# Creating postgresql bootstart"
${ INIT_CMD } enable postgresql.service
2016-11-02 17:18:29 +00:00
fi
2016-11-14 12:54:04 +00:00
echo "# Creating zammad postgresql db"
2016-11-02 17:18:29 +00:00
su - postgres -c " createdb -E UTF8 ${ DB } "
2016-11-14 12:54:04 +00:00
echo "# Creating zammad postgresql user"
2016-11-02 17:18:29 +00:00
echo " CREATE USER \" ${ DB_USER } \" WITH PASSWORD ' ${ DB_PASS } '; " | su - postgres -c psql
2016-11-14 12:54:04 +00:00
echo "# Grant privileges to new postgresql user"
2016-11-02 17:18:29 +00:00
echo " GRANT ALL PRIVILEGES ON DATABASE \" ${ DB } \" TO \" ${ DB_USER } \"; " | su - postgres -c psql
2016-11-14 12:54:04 +00:00
echo "# Updating database.yml"
2016-11-02 17:18:29 +00:00
sed -e "s/.*adapter:.*/ adapter: postgresql/" \
-e " s/.*username:.*/ username: ${ DB_USER } / " \
-e " s/.*password:.*/ password: ${ DB_PASS } / " \
-e " s/.*database:.*/ database: ${ DB } / " < ${ ZAMMAD_DIR } /config/database.yml.dist > ${ ZAMMAD_DIR } /config/database.yml
# mysql / mariadb
2016-11-14 12:54:04 +00:00
elif [ -n " $( which mysql 2> /dev/null) " ] ; then
echo "# Installing zammd on mysql"
2016-10-25 15:59:31 +00:00
2016-11-14 02:22:58 +00:00
if [ -f " ${ MY_CNF } " ] ; then
MYSQL_CREDENTIALS = " --defaults-file= ${ MY_CNF } "
else
echo -n "Please enter your MySQL root password:"
read -s MYSQL_ROOT_PASS
MYSQL_CREDENTIALS = " -u root -p ${ MYSQL_ROOT_PASS } "
fi
2016-10-25 15:59:31 +00:00
2016-11-14 12:54:04 +00:00
echo "# Creating zammad mysql db"
2016-11-14 02:22:58 +00:00
mysql ${ MYSQL_CREDENTIALS } -e " CREATE DATABASE ${ DB } DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; "
2016-10-25 15:59:31 +00:00
2016-11-14 12:54:04 +00:00
echo "# Creating zammad mysql user"
2016-11-14 02:22:58 +00:00
mysql ${ MYSQL_CREDENTIALS } -e " CREATE USER \" ${ DB_USER } \"@\" ${ DB_HOST } \" IDENTIFIED BY \" ${ DB_PASS } \"; "
2016-11-02 17:18:29 +00:00
2016-11-14 12:54:04 +00:00
echo "# Grant privileges to new mysql user"
2016-11-14 02:22:58 +00:00
mysql ${ MYSQL_CREDENTIALS } -e " GRANT ALL PRIVILEGES ON ${ DB } .* TO \" ${ DB_USER } \"@\" ${ DB_HOST } \"; FLUSH PRIVILEGES; "
2016-11-14 12:54:04 +00:00
echo "# Updating database.yml"
2016-11-14 02:22:58 +00:00
sed -e "s/.*adapter:.*/ adapter: mysql2/" \
-e " s/.*username:.*/ username: ${ DB_USER } / " \
-e " s/.*password:.*/ password: ${ DB_PASS } / " \
-e " s/.*database:.*/ database: ${ DB } / " < ${ ZAMMAD_DIR } /config/database.yml.dist > ${ ZAMMAD_DIR } /config/database.yml
2016-11-02 17:18:29 +00:00
# sqlite / no local db
2016-11-14 12:54:04 +00:00
elif [ -n " $( which sqlite 2> /dev/null) " ] ; then
echo "# Installing zammad on sqlite"
echo "# In fact this does nothing at the moment. use this to install zammad without a local database. sqlite should only be used in dev environment anyway."
2016-11-02 17:18:29 +00:00
fi
2016-10-26 14:28:59 +00:00
2016-10-25 15:59:31 +00:00
# fill database
2016-11-02 17:18:29 +00:00
zammad run rake db:migrate
2016-10-25 15:59:31 +00:00
zammad run rake db:seed
2016-11-02 17:18:29 +00:00
2016-10-25 15:59:31 +00:00
fi
2016-10-25 13:29:10 +00:00
2016-10-26 14:28:59 +00:00
echo "# Starting Zammad"
2016-10-27 10:15:51 +00:00
${ INIT_CMD } start zammad
2016-10-25 16:07:10 +00:00
2016-11-14 02:22:58 +00:00
# on centos, allow nginx to connect to application server
2016-11-14 12:54:04 +00:00
if [ -n " $( which setsebool 2> /dev/null) " ] ; then
echo "# Adding SE Linux rules"
2016-11-14 02:22:58 +00:00
setsebool httpd_can_network_connect on -P
fi
# on centos, open port 80 and 443
2016-11-14 12:54:04 +00:00
if [ -n " $( which firewall-cmd 2> /dev/null) " ] ; then
echo "# Adding firewall rules"
2016-11-14 02:22:58 +00:00
firewall-cmd --zone= public --add-port= 80/tcp --permanent
firewall-cmd --zone= public --add-port= 443/tcp --permanent
firewall-cmd --reload
fi
2016-11-02 17:18:29 +00:00
# copy nginx config
2016-11-14 12:54:04 +00:00
if [ -n " $( which nginx 2> /dev/null) " ] ; then
2016-10-28 07:44:11 +00:00
# debian / ubuntu
if [ -d /etc/nginx/sites-enabled ] ; then
NGINX_CONF = "/etc/nginx/sites-enabled/zammad.conf"
test -f /etc/nginx/sites-available/zammad.conf || cp ${ ZAMMAD_DIR } /contrib/nginx/zammad.conf /etc/nginx/sites-available/zammad.conf
2016-10-28 08:05:36 +00:00
test -h ${ NGINX_CONF } || ln -s /etc/nginx/sites-available/zammad.conf ${ NGINX_CONF }
2016-11-02 17:18:29 +00:00
2016-10-28 07:44:11 +00:00
# centos / sles
elif [ -d /etc/nginx/conf.d ] ; then
NGINX_CONF = "/etc/nginx/conf.d/zammad.conf"
2016-10-28 08:05:36 +00:00
test -f ${ NGINX_CONF } || cp ${ ZAMMAD_DIR } /contrib/nginx/zammad.conf ${ NGINX_CONF }
2016-10-26 09:04:36 +00:00
fi
2016-11-14 12:54:04 +00:00
echo "# Restarting Nginx"
2016-10-27 10:15:51 +00:00
${ INIT_CMD } restart nginx
2016-11-14 02:22:58 +00:00
2016-11-14 12:54:04 +00:00
echo "# Creating Nginx bootstart"
2016-11-14 02:22:58 +00:00
${ INIT_CMD } enable nginx
echo -e "############################################################################################################"
2016-11-04 10:13:11 +00:00
echo -e " \nAdd your FQDN to servername directive in ${ NGINX_CONF } and restart nginx if you're not testing localy "
echo -e "or open http://localhost in your browser to start using Zammad.\n"
2016-11-14 02:22:58 +00:00
echo -e "############################################################################################################"
2016-11-04 10:13:11 +00:00
else
2016-11-14 02:22:58 +00:00
echo -e "############################################################################################################"
2016-11-04 10:13:11 +00:00
echo -e "\nOpen http://localhost:3000 in your browser to start using Zammad.\n"
2016-11-14 02:22:58 +00:00
echo -e "############################################################################################################"
2016-10-25 16:07:10 +00:00
fi
2016-11-02 17:18:29 +00:00