Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. run_as_user() {
  2. mastodon_dir="/usr/local/www/mastodon"
  3. _cmd="$@"
  4. _user="${mastodon_web_user}"
  5. [ -z "${_user}" ] && _user="${mastodon_stream_user}"
  6. [ -z "${_user}" ] && _user="${mastodon_workers_user}"
  7. if [ -n "${_user}" ]; then
  8. _cmd="su -m ${_user} -c 'cd "${mastodon_dir}" && sh -c \"$_cmd\"'"
  9. fi
  10. eval "${_cmd}"
  11. }
  12.  
  13. query() {
  14. psql_env="_=1"
  15. _db_name=$1
  16. shift
  17. _sql="$*"
  18. rc=0
  19.  
  20. if [ -n "${mastodon_db_password}" ]; then
  21. passwd_directory=$(mktemp -d) || exit 1
  22. passwd_file="${passwd_directory}/.pgpass"
  23. echo "\*:\*:\*:\*:${mastodon_db_password}" > "${passwd_file}"
  24. chmod 600 "${passwd_file}"
  25. psql_env="HOME=${passwd_directory}"
  26. fi
  27. if [ -n "${mastodon_db_url}" ]; then
  28. _psql_cmd="env ${psql_env} psql -qtA \"${mastodon_db_url}\" dbname=\"${_db_name}\""
  29. elif [ -n "${mastodon_db_name}" ]; then
  30. _psql_cmd="env ${psql_env} psql -qtA \
  31. ${mastodon_db_user:+-U ${mastodon_db_user} } \
  32. ${mastodon_db_host:+-h ${mastodon_db_host} } \
  33. ${mastodon_db_port:+-p ${mastodon_db_port} } \
  34. \"${_db_name}\""
  35. else
  36. rc=1
  37. fi
  38. if [ "$rc" = 0 ]; then
  39. echo "${_sql}" | run_as_user "${_psql_cmd}" || rc=1
  40. fi
  41. if [ -n "${passwd_directory}" -a -d "${passwd_directory}" ]; then
  42. rm -rf "${passwd_directory}"
  43. fi
  44. return $rc
  45. }
  46.  
  47. db_quick_test() {
  48. [ -n "${mastodon_db_url}" ] && return 0 # Our connection test would always fail
  49. out=$(query template1 'select 1') && test "${out}" -eq 1
  50. }
  51.  
  52. db_schema_version() {
  53. query "${mastodon_db_name}" 'select max(version) from schema_migrations'
  54. }
  55.  
  56. check_db_schema() {
  57. required_schema_version="$1"
  58. shift
  59. if db_quick_test; then
  60. schema_version=$(db_schema_version)
  61. if [ -n "${schema_version}" ]; then
  62. if [ "${schema_version}" -eq "${required_schema_version}" ]; then
  63. return 0
  64. elif [ "${schema_version}" -lt 20170604144747 ] ; then
  65. return 4
  66. else
  67. return 3
  68. fi
  69. else
  70. return 2
  71. fi
  72. else
  73. return 1
  74. fi
  75. }
  76.  
  77. run_rails() {
  78. if [ -n "${mastodon_db_url}" ]; then
  79. _rails_cmd="env \
  80. DATABASE_URL=\"${mastodon_db_url}\" \
  81. RAILS_ENV=production \
  82. rails $1"
  83. else
  84. _rails_cmd="env \
  85. ${mastodon_db_host+DB_HOST=\"${mastodon_db_host}\"} \
  86. ${mastodon_db_name+DB_NAME=\"${mastodon_db_name}\"} \
  87. ${mastodon_db_port+DB_PORT=\"${mastodon_db_port}\"} \
  88. ${mastodon_db_user+DB_USER=\"${mastodon_db_user}\"} \
  89. ${mastodon_db_password+DB_PASS=\"${mastodon_db_password}\"} \
  90. RAILS_ENV=production \
  91. rails $1"
  92. fi
  93. run_as_user "${_rails_cmd}"
  94. }
  95.  
  96. do_schema() {
  97. check_db_schema 20170913000752
  98. rc=$?
  99. case $rc in
  100. 0) ;;
  101. 1) echo "Cannot connect to Mastodon database"
  102. return 1
  103. ;;
  104. 2) run_rails db:setup ;;
  105. 3) run_rails db:migrate ;;
  106. 4) run_rails mastodon:maintenance:prepare_for_foreign_keys ;;
  107. esac
  108. }
  109.  
  110. check_mastodon() {
  111. do_schema
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement