dak-setup.sh 3.2 KB

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