dak-setup.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # -*- mode: sh -*-
  3. #
  4. # © 2017-2018 Ansgar Burchardt <ansgar@debian.org>
  5. # License: GPL-2+
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. dak-setup() {
  20. # Get the parent directory of the current script
  21. local DAK_ROOT="$(cd $(dirname "$0")/..; pwd)"
  22. local setupdir="${DAK_ROOT}/setup"
  23. # This script can be used both for the integration tests and for actual
  24. # creation of a system dak. This is governed by the DAK_INTEGRATION_TEST var.
  25. if [[ ! -v DAK_INTEGRATION_TEST ]]; then
  26. PG_CMD="sudo -E -u postgres"
  27. SYS_CMD="sudo -E"
  28. USER_CMD="sudo -E -u dak -s -H"
  29. else
  30. PG_CMD=""
  31. SYS_CMD=""
  32. USER_CMD=""
  33. fi
  34. # Get default values from init_vars.
  35. # This sets the DAKBASE variable in case it didn't have a value.
  36. . ${setupdir}/init_vars
  37. # Ensure that DAKBASE exists
  38. $SYS_CMD mkdir -p ${DAKBASE}
  39. # Ensure the right permissions when not running tests
  40. if [[ ! -v DAK_INTEGRATION_TEST ]]; then
  41. $SYS_CMD chown dak:ftpmaster ${DAKBASE}
  42. $SYS_CMD chmod 2775 ${DAKBASE}
  43. fi
  44. # When setting up the system DB, this needs to be run as postgres
  45. (cd ${setupdir}; $PG_CMD ./init_db)
  46. if [[ ${PGUSER:-} != dak && -v ${PGUSER} ]]; then
  47. $PG_CMD psql -c "GRANT dak TO \"${PGUSER}\""
  48. fi
  49. $USER_CMD mkdir -p ${DAKBASE}/etc ${DAKBASE}/bin ${DAKBASE}/keyrings ${DAKBASE}/tmp
  50. # Copy/Link the email templates into the /srv/dak tree.
  51. if [[ ! -v DAK_INTEGRATION_TEST ]]; then
  52. $USER_CMD cp -r ${DAK_ROOT}/templates ${DAKBASE}/
  53. else
  54. $USER_CMD ln -s ${DAK_ROOT}/templates ${DAKBASE}/
  55. fi
  56. # Import the schema. We redirect STDOUT to /dev/null as otherwise it's
  57. # impossible to see if something fails.
  58. $USER_CMD psql -f ${setupdir}/current_schema.sql -d ${PGDATABASE} >/dev/null
  59. # Set up some core data in PGDATABASE to get started
  60. (cd ${setupdir}; $USER_CMD ./init_core)
  61. # Create a minimal dak.conf
  62. export DAK_CONFIG="${DAKBASE}/etc/dak.conf"
  63. (cd ${setupdir}; ./init_minimal_conf | $USER_CMD tee ${DAK_CONFIG} >/dev/null)
  64. $USER_CMD echo 'DB::Role "dak";' | tee -a ${DAK_CONFIG} >/dev/null
  65. ln -s ${DAK_ROOT}/dak/dak.py ${DAKBASE}/bin/dak
  66. # Update the database schema
  67. $USER_CMD ${DAK_ROOT}/dak/dak.py update-db --yes
  68. # Run dak init-dirs to set up the initial /srv/dak tree
  69. $USER_CMD ${DAK_ROOT}/dak/dak.py init-dirs
  70. }
  71. dak-setup