functions 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/bin/bash
  2. #
  3. # packager.io postinstall script functions
  4. #
  5. function debug() {
  6. if [ "${DEBUG}" == "yes" ]; then
  7. echo "DEBUG MODE ON"
  8. set -ex
  9. fi
  10. }
  11. function detect_os () {
  12. . /etc/os-release
  13. if [ "${ID}" == "debian" ] || [ "${ID}" == "ubuntu" ]; then
  14. OS="DEBIAN"
  15. elif [ "${ID}" == "centos" ] || [ "${ID}" == "fedora" ] || [ "${ID}" == "rhel" ]; then
  16. OS="REDHAT"
  17. elif [ "${ID}" == "opensuse" ] || [ "${ID}" == "sles" ] || [ "${ID}" == "suse" ]; then
  18. OS="SUSE"
  19. else
  20. OS="UNKNOWN"
  21. fi
  22. if [ "${DEBUG}" == "yes" ]; then
  23. echo "OS is ${OS} based"
  24. fi
  25. }
  26. function detect_docker() {
  27. if [ -n "$(grep docker < /proc/1/cgroup)" ]; then
  28. DOCKER="yes"
  29. else
  30. DOCKER="no"
  31. fi
  32. if [ "${DEBUG}" == "yes" ]; then
  33. echo "os runs in docker container = ${DOCKER}"
  34. fi
  35. }
  36. function detect_initcmd () {
  37. if [ -n "$(which systemctl 2> /dev/null)" ]; then
  38. INIT_CMD="systemctl"
  39. elif [ -n "$(which initctl 2> /dev/null)" ]; then
  40. INIT_CMD="initctl"
  41. else
  42. function sysvinit () {
  43. service $2 $1
  44. }
  45. INIT_CMD="sysvinit"
  46. fi
  47. if [ "${DOCKER}" == "yes" ]; then
  48. INIT_CMD="initctl"
  49. fi
  50. if [ "${DEBUG}" == "yes" ]; then
  51. echo "INIT CMD = ${INIT_CMD}"
  52. fi
  53. }
  54. function detect_database () {
  55. if [ -n "$(which psql 2> /dev/null)" ]; then
  56. ADAPTER="postgresql"
  57. elif [ -n "$(which mysql 2> /dev/null)" ]; then
  58. ADAPTER="mysql2"
  59. fi
  60. if [ "${DEBUG}" == "yes" ]; then
  61. echo "Use ${ADAPTER} adapter in database.yml"
  62. fi
  63. }
  64. function detect_webserver () {
  65. if [ -n "$(which nginx 2> /dev/null)" ] ; then
  66. WEBSERVER="nginx"
  67. WEBSERVER_CMD="nginx"
  68. if [ "${OS}" == "DEBIAN" ]; then
  69. WEBSERVER_CONF="/etc/nginx/sites-enabled/zammad.conf"
  70. elif [ "${OS}" == "REDHAT" ]; then
  71. WEBSERVER_CONF="/etc/nginx/conf.d/zammad.conf"
  72. elif [ "${OS}" == "SUSE" ]; then
  73. WEBSERVER_CONF="/etc/nginx/vhosts.d/zammad.conf"
  74. fi
  75. elif [ -n "$(which apache2 2> /dev/null)" ]; then
  76. WEBSERVER="apache2"
  77. WEBSERVER_CMD="apache2"
  78. if [ "${OS}" == "DEBIAN" ]; then
  79. WEBSERVER_CONF="/etc/apache2/sites-enabled/zammad.conf"
  80. fi
  81. elif [ -n "$(which httpd 2> /dev/null)" ]; then
  82. WEBSERVER="apache2"
  83. WEBSERVER_CMD="httpd"
  84. if [ "${OS}" == "REDHAT" ]; then
  85. WEBSERVER_CONF="/etc/httpd/conf.d/zammad.conf"
  86. elif [ "${OS}" == "SUSE" ]; then
  87. WEBSERVER_CONF="/etc/apache2/vhosts.d/zammad.conf"
  88. fi
  89. fi
  90. if [ "${DEBUG}" == "yes" ]; then
  91. echo "Webserver is ${WEBSERVER_CMD}"
  92. fi
  93. }
  94. function create_initscripts () {
  95. echo "# (Re)creating init scripts"
  96. zammad scale web=${ZAMMAD_WEBS} websocket=${ZAMMAD_WEBSOCKETS} worker=${ZAMMAD_WORKERS}
  97. echo "# Enabling Zammad on boot"
  98. ${INIT_CMD} enable zammad
  99. }
  100. function start_zammad () {
  101. echo "# Starting Zammad"
  102. ${INIT_CMD} start zammad
  103. }
  104. function stop_zammad () {
  105. echo "# Stopping Zammad"
  106. ${INIT_CMD} stop zammad
  107. }
  108. function create_database_password () {
  109. DB_PASS="$(tr -dc A-Za-z0-9 < /dev/urandom | head -c10)"
  110. }
  111. function create_postgresql_db () {
  112. if [ -n "$(which postgresql-setup 2> /dev/null)" ]; then
  113. echo "# Preparing postgresql server"
  114. postgresql-setup initdb
  115. fi
  116. echo "# Creating postgresql bootstart"
  117. ${INIT_CMD} enable postgresql.service
  118. echo "# Restarting postgresql server"
  119. ${INIT_CMD} restart postgresql
  120. echo "# Creating zammad postgresql db"
  121. su - postgres -c "createdb -E UTF8 ${DB}"
  122. echo "# Creating zammad postgresql user"
  123. echo "CREATE USER \"${DB_USER}\" WITH PASSWORD '${DB_PASS}';" | su - postgres -c psql
  124. echo "# Grant privileges to new postgresql user"
  125. echo "GRANT ALL PRIVILEGES ON DATABASE \"${DB}\" TO \"${DB_USER}\";" | su - postgres -c psql
  126. }
  127. function create_mysql_db () {
  128. if [ -f "${MY_CNF}" ]; then
  129. MYSQL_CREDENTIALS="--defaults-file=${MY_CNF}"
  130. else
  131. echo -n "Please enter your MySQL root password:"
  132. read -p 'Password: ' MYSQL_ROOT_PASS
  133. MYSQL_CREDENTIALS="-u root -p${MYSQL_ROOT_PASS}"
  134. fi
  135. echo "# Creating zammad mysql db"
  136. mysql ${MYSQL_CREDENTIALS} -e "CREATE DATABASE ${DB} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"
  137. echo "# Creating zammad mysql user"
  138. mysql ${MYSQL_CREDENTIALS} -e "CREATE USER \"${DB_USER}\"@\"${DB_HOST}\" IDENTIFIED BY \"${DB_PASS}\";"
  139. echo "# Grant privileges to new mysql user"
  140. mysql ${MYSQL_CREDENTIALS} -e "GRANT ALL PRIVILEGES ON ${DB}.* TO \"${DB_USER}\"@\"${DB_HOST}\"; FLUSH PRIVILEGES;"
  141. }
  142. function update_database_yml () {
  143. if [ "${OS}" == "REDHAT" ] || [ "${OS}" == "SUSE" ]; then
  144. if [ "${ADAPTER}" == "postgresql" ]; then
  145. DB_PASS=""
  146. fi
  147. fi
  148. echo "# Updating database.yml"
  149. sed -e "s/.*adapter:.*/ adapter: ${ADAPTER}/" \
  150. -e "s/.*username:.*/ username: ${DB_USER}/" \
  151. -e "s/.*password:.*/ password: ${DB_PASS}/" \
  152. -e "s/.*database:.*/ database: ${DB}/" < ${ZAMMAD_DIR}/config/database.yml.pkgr > ${ZAMMAD_DIR}/config/database.yml
  153. }
  154. function initialise_database () {
  155. zammad run rake db:migrate
  156. zammad run rake db:seed
  157. }
  158. function update_database () {
  159. echo "# database.yml found. Updating db..."
  160. zammad run rake db:migrate
  161. }
  162. function create_webserver_config () {
  163. if [ "${OS}" == "DEBIAN" ]; then
  164. test -f /etc/${WEBSERVER}/sites-available/zammad.conf || cp ${ZAMMAD_DIR}/contrib/${WEBSERVER}/zammad.conf /etc/${WEBSERVER}/sites-available/zammad.conf
  165. test -h ${WEBSERVER_CONF} || ln -s /etc/${WEBSERVER}/sites-available/zammad.conf ${WEBSERVER_CONF}
  166. if [ "${WEBSERVER}" == "apache2" ]; then
  167. a2enmod proxy
  168. a2enmod proxy_http
  169. a2enmod proxy_wstunnel
  170. fi
  171. else
  172. test -f ${WEBSERVER_CONF} || cp ${ZAMMAD_DIR}/contrib/${WEBSERVER}/zammad.conf ${WEBSERVER_CONF}
  173. fi
  174. echo "# Creating webserver bootstart"
  175. ${INIT_CMD} enable ${WEBSERVER_CMD}
  176. echo "# Restarting webserver ${WEBSERVER_CMD}"
  177. ${INIT_CMD} restart ${WEBSERVER_CMD}
  178. }
  179. function update_or_install () {
  180. if [ -f ${ZAMMAD_DIR}/config/database.yml ]; then
  181. update_database
  182. else
  183. create_database_password
  184. if [ "${ADAPTER}" == "postgresql" ]; then
  185. echo "# Installing zammad on postgresql"
  186. create_postgresql_db
  187. elif [ "${ADAPTER}" == "mysql2" ]; then
  188. echo "# Installing zammad on mysql"
  189. create_mysql_db
  190. fi
  191. update_database_yml
  192. initialise_database
  193. fi
  194. }
  195. function final_message () {
  196. echo -e "####################################################################################"
  197. echo -e "\nAdd your FQDN to servername directive in ${WEBSERVER_CONF}"
  198. echo -e "and restart your webserver if you're not testing locally"
  199. echo -e "or open http://localhost/ in your browser to start using Zammad.\n"
  200. if [ "${OS}" == "REDHAT" ]; then
  201. echo -e "\n Remember to enable selinux and firewall rules!\n"
  202. echo -e "Use the follwing commands:"
  203. echo -e " setsebool httpd_can_network_connect on -P"
  204. echo -e " firewall-cmd --zone=public --add-service=http --permanent"
  205. echo -e " firewall-cmd --zone=public --add-service=https --permanent"
  206. echo -e " firewall-cmd --reload\n"
  207. fi
  208. echo -e "####################################################################################"
  209. }