PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/data/rpm/post.sh

https://github.com/mroonga/mroonga
Shell | 142 lines | 125 code | 15 blank | 2 comment | 21 complexity | fed44b3a19b9706d9d7a1b649333df05 MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/bin/bash
  2. # set -eux
  3. mode=$1
  4. variant=$2
  5. data_dir=$3
  6. groonga_required_version=$4
  7. if [ "${mode}" = "1" ]; then
  8. action=install
  9. elif [ "${mode}" = "2" ]; then
  10. action=upgrade
  11. fi
  12. may_have_auto_generated_password=no
  13. case "${variant}" in
  14. mysql)
  15. service_name=mysqld
  16. variant_label=MySQL
  17. may_have_auto_generated_password=yes
  18. ;;
  19. mariadb)
  20. service_name=mariadb
  21. variant_label=MariaDB
  22. ;;
  23. percona)
  24. service_name=mysqld
  25. variant_label="Percona Server"
  26. may_have_auto_generated_password=yes
  27. ;;
  28. esac
  29. try_auto_prepare=no
  30. need_stop=no
  31. have_auto_generated_password=no
  32. if systemctl is-active ${service_name} > /dev/null; then
  33. try_auto_prepare=yes
  34. else
  35. if systemctl start ${service_name} > /dev/null; then
  36. need_stop=yes
  37. if [ "${may_have_auto_generated_password}" = "yes" ]; then
  38. auto_generated_password=$(awk '/root@localhost/{print $NF}' /var/log/mysqld.log | tail -n 1)
  39. if [ -n "${auto_generated_password}" ]; then
  40. have_auto_generated_password=yes
  41. fi
  42. fi
  43. try_auto_prepare=yes
  44. fi
  45. fi
  46. need_manual_register=no
  47. need_manual_restart=no
  48. need_manual_update=no
  49. case "${action}" in
  50. install)
  51. need_manual_register=yes
  52. need_manual_update=yes
  53. ;;
  54. update)
  55. need_manual_restart=yes
  56. need_manual_update=yes
  57. ;;
  58. esac
  59. install_sql=${data_dir}/mroonga/install.sql
  60. uninstall_sql=${data_dir}/mroonga/uninstall.sql
  61. update_sql=${data_dir}/mroonga/update.sql
  62. need_password_expire=no
  63. if [ "${try_auto_prepare}" = "yes" ]; then
  64. mysql_command=$(which mysql)
  65. password_option=""
  66. if [ "${have_auto_generated_password}" = "yes" ]; then
  67. if "${mysql_command}" \
  68. -u root \
  69. --connect-expired-password \
  70. -p"${auto_generated_password}" \
  71. -e "quit" > /dev/null 2>&1; then
  72. if "${mysql_command}" \
  73. -u root \
  74. --connect-expired-password \
  75. -p"${auto_generated_password}" \
  76. -e "ALTER USER root@localhost IDENTIFIED BY '$auto_generated_password'"; then
  77. need_password_expire=yes
  78. password_option="-p${auto_generated_password}"
  79. fi
  80. fi
  81. fi
  82. if [ -z "${password_option}" ]; then
  83. if ! "${mysql_command}" -u root -e "quit" > /dev/null 2>&1; then
  84. password_option="-p"
  85. echo "${variant_label}'s root password will be asked several times to"
  86. echo "${action} Mroonga. You don't need to input the root password"
  87. echo "in this package ${action} process. But you need to ${action}"
  88. echo "Mroonga manually later. The way to ${action} Mroonga manually"
  89. echo "will be showed later."
  90. fi
  91. fi
  92. mysql="${mysql_command} -u root ${password_option}"
  93. if [ "${action}" = "install" ]; then
  94. if ${mysql} < ${install_sql}; then
  95. need_manual_register=no
  96. fi
  97. else
  98. if systemctl restart ${service_name}; then
  99. need_manual_restart=no
  100. fi
  101. fi
  102. if ${mysql} < ${update_sql}; then
  103. need_manual_update=no
  104. fi
  105. else
  106. mysql="mysql -u root"
  107. fi
  108. if [ "${need_password_expire}" = "yes" ]; then
  109. ${mysql} -e "ALTER USER root@localhost PASSWORD EXPIRE"
  110. fi
  111. if [ "${need_stop}" = "yes" ]; then
  112. if systemctl stop ${service_name}; then
  113. need_manual_restart=no
  114. fi
  115. fi
  116. if [ "${need_manual_register}" = "yes" ]; then
  117. echo "Run the following command line to register Mroonga:"
  118. echo " ${mysql} < ${install_sql}"
  119. fi
  120. if [ "${need_manual_restart}" = "yes" ]; then
  121. echo "Run the following command line to reload Mroonga:"
  122. echo " systemctl restart ${service_name}"
  123. fi
  124. if [ "${need_manual_update}" = "yes" ]; then
  125. echo "Run the following command lines to update Mroonga:"
  126. echo " ${mysql} < ${update}"
  127. fi