postinstall.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. #
  3. # packager.io postinstall script
  4. #
  5. PATH=/opt/zammad/bin:/opt/zammad/vendor/bundle/bin:/sbin:/bin:/usr/sbin:/usr/bin:
  6. ZAMMAD_DIR="/opt/zammad"
  7. DB="zammad_production"
  8. DB_USER="zammad"
  9. MY_CNF="/etc/mysql/debian.cnf"
  10. # check which init system is used
  11. if [ -n "$(which initctl)" ]; then
  12. INIT_CMD="initctl"
  13. elif [ -n "$(which systemctl)" ]; then
  14. INIT_CMD="systemctl"
  15. else
  16. function sysvinit () {
  17. service $2 $1
  18. }
  19. INIT_CMD="sysvinit"
  20. fi
  21. echo "# (Re)creating init scripts"
  22. zammad scale web=1 websocket=1 worker=1
  23. echo "# Stopping Zammad"
  24. ${INIT_CMD} stop zammad
  25. # check if database.yml exists
  26. if [ -f ${ZAMMAD_DIR}/config/database.yml ]; then
  27. # db migration
  28. echo "# database.yml found. Updating db..."
  29. zammad run rake db:migrate
  30. else
  31. echo "# database.yml not found. Creating db..."
  32. # create new password
  33. DB_PASS="$(tr -dc A-Za-z0-9 < /dev/urandom | head -c10)"
  34. # postgresql
  35. if [ -n "$(which psql)" ]; then
  36. echo "installing zammad on postgresql"
  37. # centos
  38. if [ -n "$(which postgresql-setup)" ]; then
  39. echo "preparing postgresql server"
  40. postgresql-setup initdb
  41. echo "backuping postgres config"
  42. 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
  43. echo "allow login via username and password in postgresql"
  44. sed 's/ident/trust/g' < /var/lib/pgsql/data/pg_hba.conf.bak > /var/lib/pgsql/data/pg_hba.conf
  45. echo "restarting postgresql server"
  46. ${INIT_CMD} restart postgresql
  47. echo "create postgresql bootstart"
  48. ${INIT_CMD} enable postgresql.service
  49. fi
  50. echo "creating zammad postgresql db"
  51. su - postgres -c "createdb -E UTF8 ${DB}"
  52. echo "creating zammad postgresql user"
  53. echo "CREATE USER \"${DB_USER}\" WITH PASSWORD '${DB_PASS}';" | su - postgres -c psql
  54. echo "grant privileges to new postgresql user"
  55. echo "GRANT ALL PRIVILEGES ON DATABASE \"${DB}\" TO \"${DB_USER}\";" | su - postgres -c psql
  56. echo "updating database.yml"
  57. sed -e "s/.*adapter:.*/ adapter: postgresql/" \
  58. -e "s/.*username:.*/ username: ${DB_USER}/" \
  59. -e "s/.*password:.*/ password: ${DB_PASS}/" \
  60. -e "s/.*database:.*/ database: ${DB}/" < ${ZAMMAD_DIR}/config/database.yml.dist > ${ZAMMAD_DIR}/config/database.yml
  61. # mysql / mariadb
  62. elif [ -n "$(which mysql)" ];then
  63. echo "installing zammd on mysql"
  64. if [ -f "${MY_CNF}" ]; then
  65. MYSQL_CREDENTIALS="--defaults-file=${MY_CNF}"
  66. else
  67. echo -n "Please enter your MySQL root password:"
  68. read -s MYSQL_ROOT_PASS
  69. MYSQL_CREDENTIALS="-u root -p${MYSQL_ROOT_PASS}"
  70. fi
  71. echo "creating zammad mysql db"
  72. mysql ${MYSQL_CREDENTIALS} -e "CREATE DATABASE ${DB} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"
  73. echo "creating zammad mysql user"
  74. mysql ${MYSQL_CREDENTIALS} -e "CREATE USER \"${DB_USER}\"@\"${DB_HOST}\" IDENTIFIED BY \"${DB_PASS}\";"
  75. echo "grant privileges to new mysql user"
  76. mysql ${MYSQL_CREDENTIALS} -e "GRANT ALL PRIVILEGES ON ${DB}.* TO \"${DB_USER}\"@\"${DB_HOST}\"; FLUSH PRIVILEGES;"
  77. echo "updating database.yml"
  78. sed -e "s/.*adapter:.*/ adapter: mysql2/" \
  79. -e "s/.*username:.*/ username: ${DB_USER}/" \
  80. -e "s/.*password:.*/ password: ${DB_PASS}/" \
  81. -e "s/.*database:.*/ database: ${DB}/" < ${ZAMMAD_DIR}/config/database.yml.dist > ${ZAMMAD_DIR}/config/database.yml
  82. # sqlite / no local db
  83. elif [ -n "$(which sqlite)" ];then
  84. echo "installing zammad on sqlite"
  85. 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."
  86. fi
  87. # fill database
  88. zammad run rake db:migrate
  89. zammad run rake db:seed
  90. fi
  91. echo "# Starting Zammad"
  92. ${INIT_CMD} start zammad
  93. # on centos, allow nginx to connect to application server
  94. if [ -n "$(which setsebool)" ]; then
  95. setsebool httpd_can_network_connect on -P
  96. fi
  97. # on centos, open port 80 and 443
  98. if [ -n "$(which firewall-cmd)" ]; then
  99. firewall-cmd --zone=public --add-port=80/tcp --permanent
  100. firewall-cmd --zone=public --add-port=443/tcp --permanent
  101. firewall-cmd --reload
  102. fi
  103. # copy nginx config
  104. if [ -n "$(which nginx)" ]; then
  105. # debian / ubuntu
  106. if [ -d /etc/nginx/sites-enabled ]; then
  107. NGINX_CONF="/etc/nginx/sites-enabled/zammad.conf"
  108. test -f /etc/nginx/sites-available/zammad.conf || cp ${ZAMMAD_DIR}/contrib/nginx/zammad.conf /etc/nginx/sites-available/zammad.conf
  109. test -h ${NGINX_CONF} || ln -s /etc/nginx/sites-available/zammad.conf ${NGINX_CONF}
  110. # centos / sles
  111. elif [ -d /etc/nginx/conf.d ]; then
  112. NGINX_CONF="/etc/nginx/conf.d/zammad.conf"
  113. test -f ${NGINX_CONF} || cp ${ZAMMAD_DIR}/contrib/nginx/zammad.conf ${NGINX_CONF}
  114. fi
  115. echo "restarting Nginx"
  116. ${INIT_CMD} restart nginx
  117. echo "create Nginx bootstart"
  118. ${INIT_CMD} enable nginx
  119. echo -e "############################################################################################################"
  120. echo -e "\nAdd your FQDN to servername directive in ${NGINX_CONF} and restart nginx if you're not testing localy"
  121. echo -e "or open http://localhost in your browser to start using Zammad.\n"
  122. echo -e "############################################################################################################"
  123. else
  124. echo -e "############################################################################################################"
  125. echo -e "\nOpen http://localhost:3000 in your browser to start using Zammad.\n"
  126. echo -e "############################################################################################################"
  127. fi