dak-setup.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. set -e
  20. set -u
  21. set -o pipefail
  22. dak-setup() {
  23. # Get the parent directory of the current script
  24. if [[ ! -v DAK_ROOT ]]; then
  25. local DAK_ROOT="$(cd $(dirname "$0")/..; pwd)"
  26. fi
  27. local setupdir="${DAK_ROOT}/setup"
  28. # This script can be used both for the integration tests and for actual
  29. # creation of a system dak. This is governed by the DAK_INTEGRATION_TEST var.
  30. if [[ ! -v DAK_INTEGRATION_TEST ]]; then
  31. PG_CMD="sudo -E -u postgres"
  32. SYS_CMD="sudo -E"
  33. USER_CMD="sudo -E -u dak -s -H"
  34. PYTHON_COVERAGE=
  35. else
  36. PG_CMD=""
  37. SYS_CMD=""
  38. USER_CMD=""
  39. if [ "${RUN_COVERAGE:-n}" = "y" ]; then
  40. PYTHON_COVERAGE="python3-coverage run --rcfile ${DAK_ROOT}/.coveragerc --parallel-mode"
  41. else
  42. PYTHON_COVERAGE=
  43. fi
  44. fi
  45. # Get default values from init_vars.
  46. # This sets the DAKBASE variable in case it didn't have a value.
  47. . ${setupdir}/init_vars
  48. # Ensure that DAKBASE exists
  49. $SYS_CMD mkdir -p ${DAKBASE}
  50. # Ensure the right permissions when not running tests
  51. if [[ ! -v DAK_INTEGRATION_TEST ]]; then
  52. $SYS_CMD chown dak:ftpmaster ${DAKBASE}
  53. $SYS_CMD chmod 2775 ${DAKBASE}
  54. fi
  55. # When setting up the system DB, this needs to be run as postgres
  56. (cd ${setupdir}; $PG_CMD ./init_db)
  57. if [[ ${PGUSER:-} != dak && -v ${PGUSER} ]]; then
  58. $PG_CMD psql -c "GRANT dak TO \"${PGUSER}\""
  59. fi
  60. $USER_CMD mkdir -p ${DAKBASE}/etc ${DAKBASE}/bin ${DAKBASE}/keyrings ${DAKBASE}/tmp
  61. # Copy/Link the email templates into the /srv/dak tree.
  62. if [[ ! -v DAK_INTEGRATION_TEST ]]; then
  63. $USER_CMD cp -r ${DAK_ROOT}/templates ${DAKBASE}/
  64. else
  65. $USER_CMD ln -s ${DAK_ROOT}/templates ${DAKBASE}/
  66. fi
  67. # Import the schema. We redirect STDOUT to /dev/null as otherwise it's
  68. # impossible to see if something fails.
  69. $USER_CMD psql -f ${setupdir}/current_schema.sql -d ${PGDATABASE} >/dev/null
  70. # Set up some core data in PGDATABASE to get started
  71. (cd ${setupdir}; $USER_CMD ./init_core)
  72. # Create a minimal dak.conf
  73. export DAK_CONFIG="${DAKBASE}/etc/dak.conf"
  74. (cd ${setupdir}; ./init_minimal_conf | $USER_CMD tee ${DAK_CONFIG} >/dev/null)
  75. $USER_CMD echo 'DB::Role "dak";' | tee -a ${DAK_CONFIG} >/dev/null
  76. if [[ ! -v DAK_INTEGRATION_TEST ]]; then
  77. ln -s ${DAK_ROOT}/dak/dak.py ${DAKBASE}/bin/dak
  78. else
  79. # wrapper to collect coverage information
  80. ln -s ${DAK_ROOT}/integration-tests/dak-coverage.sh ${DAKBASE}/bin/dak
  81. fi
  82. # Update the database schema
  83. $USER_CMD ${DAKBASE}/bin/dak update-db --yes
  84. # Run dak init-dirs to set up the initial /srv/dak tree
  85. $USER_CMD ${DAKBASE}/bin/dak init-dirs
  86. }
  87. dak-setup