functions 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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}" =~ suse|sles ]]; 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-available/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-available/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. # Mimik pgsql-Installation.
  116. # Upcoming start of postgresql server would fail without it ...
  117. mkdir -p /var/run/postgresql/
  118. chown postgres:postgres /var/run/postgresql/
  119. chmod -R 755 /var/run/postgresql/
  120. fi
  121. ${INIT_CMD} daemon-reload
  122. echo "# Creating postgresql bootstart"
  123. ${INIT_CMD} enable postgresql.service
  124. echo "# Restarting postgresql server"
  125. ${INIT_CMD} stop postgresql
  126. ${INIT_CMD} start postgresql
  127. # TODO
  128. # sleep 30
  129. echo "# Creating zammad postgresql user"
  130. echo "CREATE USER \"${DB_USER}\" WITH PASSWORD '${DB_PASS}';" | su - postgres -c psql
  131. echo "# Creating zammad postgresql db"
  132. su - postgres -c "createdb -E UTF8 ${DB} -O ${DB_USER}"
  133. echo "# Grant privileges to new postgresql user"
  134. echo "GRANT ALL PRIVILEGES ON DATABASE \"${DB}\" TO \"${DB_USER}\";" | su - postgres -c psql
  135. }
  136. function create_mysql_db () {
  137. if [ -f "${MY_CNF}" ]; then
  138. MYSQL_CREDENTIALS="--defaults-file=${MY_CNF}"
  139. else
  140. echo -n "Please enter your MySQL root password:"
  141. read -p 'Password: ' MYSQL_ROOT_PASS
  142. MYSQL_CREDENTIALS="-u root -p${MYSQL_ROOT_PASS}"
  143. fi
  144. echo "# Creating zammad mysql db"
  145. mysql ${MYSQL_CREDENTIALS} -e "CREATE DATABASE ${DB} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"
  146. echo "# Creating zammad mysql user"
  147. mysql ${MYSQL_CREDENTIALS} -e "CREATE USER \"${DB_USER}\"@\"${DB_HOST}\" IDENTIFIED BY \"${DB_PASS}\";"
  148. echo "# Grant privileges to new mysql user"
  149. mysql ${MYSQL_CREDENTIALS} -e "GRANT ALL PRIVILEGES ON ${DB}.* TO \"${DB_USER}\"@\"${DB_HOST}\"; FLUSH PRIVILEGES;"
  150. }
  151. function update_database_yml () {
  152. echo "# Updating database.yml"
  153. sed -e "s/.*adapter:.*/ adapter: ${ADAPTER}/" \
  154. -e "s/.*username:.*/ username: ${DB_USER}/" \
  155. -e "s/.*password:.*/ password: ${DB_PASS}/" \
  156. -e "s/.*database:.*/ database: ${DB}/" < ${ZAMMAD_DIR}/contrib/packager.io/database.yml.pkgr > ${ZAMMAD_DIR}/config/database.yml
  157. echo "# ... Fixing permission database.yml"
  158. chown zammad:zammad ${ZAMMAD_DIR}/config/database.yml
  159. }
  160. function initialise_database () {
  161. zammad run rake db:migrate
  162. zammad run rake db:seed
  163. }
  164. function update_database () {
  165. echo "# database.yml found. Updating db..."
  166. zammad run rake db:migrate
  167. }
  168. function update_translations () {
  169. echo "# Updating translations..."
  170. zammad run rails r 'Locale.sync'
  171. zammad run rails r 'Translation.sync'
  172. }
  173. function create_webserver_config () {
  174. if [ "${OS}" == "DEBIAN" ]; then
  175. if [ ! -f "${WEBSERVER_CONF}" ]; then
  176. if [ -f "/etc/${WEBSERVER}/sites-enabled/zammad.conf" ]; then
  177. mv /etc/${WEBSERVER}/sites-enabled/zammad.conf ${WEBSERVER_CONF}
  178. else
  179. cp ${ZAMMAD_DIR}/contrib/${WEBSERVER}/zammad.conf ${WEBSERVER_CONF}
  180. fi
  181. ln -s ${WEBSERVER_CONF} /etc/${WEBSERVER}/sites-enabled/zammad.conf
  182. fi
  183. if [ "${WEBSERVER}" == "apache2" ]; then
  184. a2enmod proxy
  185. a2enmod proxy_http
  186. a2enmod proxy_wstunnel
  187. fi
  188. else
  189. test -f ${WEBSERVER_CONF} || cp ${ZAMMAD_DIR}/contrib/${WEBSERVER}/zammad.conf ${WEBSERVER_CONF}
  190. fi
  191. echo "# Creating webserver bootstart"
  192. ${INIT_CMD} enable ${WEBSERVER_CMD}
  193. echo "# Restarting webserver ${WEBSERVER_CMD}"
  194. ${INIT_CMD} restart ${WEBSERVER_CMD}
  195. }
  196. function enforce_redis () {
  197. REDIS_URL=$(zammad config:get REDIS_URL)
  198. if [ -n "${REDIS_URL}" ]; then
  199. echo "# REDIS_URL variable set to ${REDIS_URL}."
  200. if ! echo ${REDIS_URL} | grep -E 'redis://([a-zA-Z0-9.-]+|[0-9]{1,3}(\.[0-9]{1,3}){3}):[0-9]+/?' > /dev/null; then
  201. echo "ERROR - Malformed REDIS_URL."
  202. echo -e "REDIS_URL must follow one of these formats:\nredis://<fqdn>:<port_number>\nredis://<ip_address>:<port_number>"
  203. exit 1
  204. else
  205. echo "# Performing connection check."
  206. REDIS_URL_HOST=$(echo ${REDIS_URL} | cut -d'/' -f3 | cut -d':' -f1)
  207. REDIS_URL_PORT=$(echo ${REDIS_URL} | cut -d'/' -f3 | cut -d':' -f2)
  208. if echo 2> /dev/null > /dev/tcp/$REDIS_URL_HOST/$REDIS_URL_PORT; then
  209. echo "# Connection check successful."
  210. return 0
  211. else
  212. echo "# Connection check unsuccessful."
  213. echo "ERROR - No redis server found at ${REDIS_URL}."
  214. echo "Please install Redis following the instructions at https://redis.io/docs/getting-started/ or set the REDIS_URL environment variable with the following command."
  215. echo -e "zammad config:set REDIS_URL=redis://your.redis.server:6379\n"
  216. echo "Please fix these issues, then run the package installation again."
  217. exit 1
  218. fi
  219. fi
  220. fi
  221. echo "# Enforcing Redis..."
  222. local REDIS_SERVICE_NAME=""
  223. if [ "${OS}" == "DEBIAN" ]; then
  224. REDIS_SERVICE_NAME="redis-server"
  225. elif [ "${OS}" == "SUSE" ]; then
  226. REDIS_PID=$(fuser 6379/tcp 2> /dev/null)
  227. if [ -z "${REDIS_PID}" ]; then
  228. echo -e "ERROR - SUSE Linux detected but no running redis-instance listening on port 6379 or REDIS_URL set.\n"
  229. echo "SUSE Linux uses a systemd template for its redis package."
  230. echo "If redis was installed via zypper, please make sure an instance of its template-service is running (e.g. redis@zammad.service) and it is listening on port 6379."
  231. echo "Alternatively, you can set the REDIS_URL environment variable with the following command."
  232. echo -e "zammad config:set REDIS_URL=redis://your.redis.server:6379\n"
  233. echo "Please fix these issues, then run the package installation again."
  234. exit 1
  235. else
  236. REDIS_SERVICE_NAME=$(ps -p $REDIS_PID -o unit=)
  237. echo "# Redis service ${REDIS_SERVICE_NAME} detected."
  238. fi
  239. else
  240. REDIS_SERVICE_NAME="redis"
  241. fi
  242. if [ -n "$(which redis-server 2> /dev/null)" ]; then
  243. echo "# Creating Redis bootstart"
  244. ${INIT_CMD} enable ${REDIS_SERVICE_NAME}
  245. echo "# Starting Redis server"
  246. if ${INIT_CMD} restart ${REDIS_SERVICE_NAME}; then
  247. echo "# Redis server is running."
  248. return 0
  249. else
  250. echo -e "\nSomething went wrong. Please check any error output above and fix the issues."
  251. echo "Then run the package installation again."
  252. exit 1
  253. fi
  254. fi
  255. }
  256. function setup_elasticsearch () {
  257. echo "# Configuring Elasticsearch..."
  258. ES_CONNECTION="$(zammad run rails r "puts '',Setting.get('es_url')"| tail -n 1 2>> /dev/null)"
  259. if [ -z "${ES_CONNECTION}" ]; then
  260. echo "-- Nevermind, no es_url is set, leaving Elasticsearch untouched ...!"
  261. echo "-- The above is all right if you don't want to use Elasticsearch (locally) - if this is not intended, consult https://docs.zammad.org !"
  262. return 0
  263. fi
  264. if [[ "$(/usr/share/elasticsearch/bin/elasticsearch --version| cut -c 10)" -gt 7 ]]; then
  265. # We're skipping the plugin reinstallation process for ES 8+ as it's part of the ES core
  266. return 0
  267. fi
  268. if [ -n "$(/usr/share/elasticsearch/bin/elasticsearch-plugin list | grep mapper-attachments)" ]; then
  269. REBUILD_ES_SEARCHINDEX="yes"
  270. echo "# Deleting old elasticsearch index..."
  271. zammad run rake zammad:searchindex:drop
  272. yes | /usr/share/elasticsearch/bin/elasticsearch-plugin -s remove mapper-attachments
  273. elif [ -n "$(/usr/share/elasticsearch/bin/elasticsearch-plugin list | grep ingest-attachment)" ]; then
  274. yes | /usr/share/elasticsearch/bin/elasticsearch-plugin -s remove ingest-attachment
  275. fi
  276. yes | /usr/share/elasticsearch/bin/elasticsearch-plugin -s install ingest-attachment
  277. if [ "${ES_CONNECTION}" == "http://127.0.0.1:9200" ] || [ "${ES_CONNECTION}" == "http://localhost:9200" ]; then
  278. ${INIT_CMD} restart elasticsearch
  279. else
  280. echo -e "\n It seems you're running an external Elasticsearch server on ${ES_CONNECTION}"
  281. echo -e "\n We'll not touch your Elasticsearch on the local and remote system."
  282. echo -e "\n Please get sure to install the 'ingest-attachment' plugin on your Elasticsearch server by:"
  283. echo -e "/usr/share/elasticsearch/bin/elasticsearch-plugin -s install ingest-attachment"
  284. echo -e "\nAfter this you might need to rebuild the searchindex by:"
  285. echo -e "zammad run rake zammad:searchindex:rebuild"
  286. fi
  287. }
  288. function elasticsearch_searchindex_rebuild () {
  289. zammad run rails r "Setting.set('es_url', \"${ES_CONNECTION}\")"
  290. if [ "${REBUILD_ES_SEARCHINDEX}" == "yes" ]; then
  291. echo "# (Re)building Elasticsearch searchindex..."
  292. nohup zammad run rake zammad:searchindex:rebuild &> ${ZAMMAD_DIR}/log/searchindex-rebuild.log &
  293. fi
  294. }
  295. function detect_zammad_packages () {
  296. if [ "$(zammad run rails r 'puts Package.count.positive?')" == "true" ] && [ -n "$(which pnpm 2> /dev/null)" ] ; then
  297. echo "# Detected custom packages..."
  298. ZAMMAD_PACKAGES="yes"
  299. else
  300. echo "# No custom packages detected..."
  301. ZAMMAD_PACKAGES="no"
  302. fi
  303. }
  304. function zammad_packages_reinstall_all () {
  305. detect_zammad_packages
  306. if [ "${ZAMMAD_PACKAGES}" == "yes" ]; then
  307. echo "# Setup custom packages files..."
  308. zammad run rake zammad:package:reinstall_all
  309. zammad run rake zammad:package:post_install
  310. fi
  311. }
  312. function update_or_install () {
  313. if [ -f ${ZAMMAD_DIR}/config/database.yml ]; then
  314. echo "# Clear cache..."
  315. zammad run rails r Rails.cache.clear
  316. update_database
  317. update_translations
  318. zammad_packages_reinstall_all
  319. else
  320. REBUILD_ES_SEARCHINDEX="yes"
  321. create_database_password
  322. if [ "${ADAPTER}" == "postgresql" ]; then
  323. echo "# Installing zammad on postgresql"
  324. create_postgresql_db
  325. elif [ "${ADAPTER}" == "mysql2" ]; then
  326. echo "# Installing zammad on mysql"
  327. create_mysql_db
  328. fi
  329. update_database_yml
  330. initialise_database
  331. fi
  332. setup_elasticsearch
  333. elasticsearch_searchindex_rebuild
  334. echo "# Enforcing 0600 on database.yml ..."
  335. chmod 600 ${ZAMMAD_DIR}/config/database.yml
  336. }
  337. function set_env_vars () {
  338. zammad config:set RUBY_MALLOC_ARENA_MAX=${ZAMMAD_RUBY_MALLOC_ARENA_MAX:=2}
  339. zammad config:set RUBY_GC_MALLOC_LIMIT=${ZAMMAD_RUBY_GC_MALLOC_LIMIT:=1077216}
  340. zammad config:set RUBY_GC_MALLOC_LIMIT_MAX=${ZAMMAD_RUBY_GC_MALLOC_LIMIT_MAX:=2177216}
  341. zammad config:set RUBY_GC_OLDMALLOC_LIMIT=${ZAMMAD_RUBY_GC_OLDMALLOC_LIMIT:=2177216}
  342. zammad config:set RUBY_GC_OLDMALLOC_LIMIT_MAX=${ZAMMAD_RUBY_GC_OLDMALLOC_LIMIT_MAX:=3000100}
  343. if [[ "$(zammad config:get RAILS_LOG_TO_STDOUT)" == "enabled" ]];then
  344. echo 'Setting default Logging to file, set via "zammad config:set RAILS_LOG_TO_STDOUT=true" if you want to log to STDOUT!'
  345. zammad config:set RAILS_LOG_TO_STDOUT=
  346. fi
  347. }
  348. function final_message () {
  349. echo -e "####################################################################################"
  350. echo -e "\nAdd your fully qualified domain name or public IP to servername directive of"
  351. echo -e "${WEBSERVER}, if this installation is done on a remote server. You have to change:"
  352. echo -e "${WEBSERVER_CONF} and restart ${WEBSERVER_CMD} process."
  353. echo -e "Otherwise just open http://localhost/ in your browser to start using Zammad.\n"
  354. if [ "${OS}" == "REDHAT" ]; then
  355. echo -e "\n Remember to enable selinux and firewall rules!\n"
  356. echo -e "Use the following commands:"
  357. echo -e " setsebool httpd_can_network_connect on -P"
  358. echo -e " firewall-cmd --zone=public --add-service=http --permanent"
  359. echo -e " firewall-cmd --zone=public --add-service=https --permanent"
  360. echo -e " firewall-cmd --reload\n"
  361. elif [ "${OS}" == "SUSE" ]; then
  362. echo -e "\n Make sure that the firewall is not blocking port 80 & 443!\n"
  363. echo -e "Use 'yast firewall' or 'SuSEfirewall2' commands to configure it"
  364. fi
  365. echo -e "####################################################################################"
  366. }