fork 0k-charms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.0 KiB

  1. #!/bin/bash
  2. ## Init is run on host
  3. ## For now it is run every time the script is launched, but
  4. ## it should be launched only once after build.
  5. ## Accessible variables are:
  6. ## - SERVICE_NAME Name of current service
  7. ## - DOCKER_BASE_IMAGE Base image from which this service might be built if any
  8. ## - SERVICE_DATASTORE Location on host of the DATASTORE of this service
  9. ## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service
  10. . lib/common
  11. set -e
  12. admin_user=$(options-get admin.user 2>&1) || {
  13. admin_user="admin"
  14. }
  15. OLD_CONFIGDIR="$SERVICE_CONFIGSTORE/var/www/html/config"
  16. if [ -d "$OLD_CONFIGDIR" ]; then
  17. info "Old config directory found"
  18. if [ -d "$CONFIGDIR" ]; then
  19. if ! dir_is_empty "$CONFIGDIR"; then
  20. err "Both old and new config directories found"
  21. exit 1
  22. fi
  23. rmdir "$CONFIGDIR" || {
  24. err "Failed to remove new (empty) config directory"
  25. exit 1
  26. }
  27. fi
  28. mkdir -p "${CONFIGDIR%/*}" || exit 1
  29. mv -v "$OLD_CONFIGDIR" "$CONFIGDIR" || {
  30. err "Failed to move old config directory to new location"
  31. exit 1
  32. }
  33. fi
  34. first_run=0
  35. read-0 current_user current_password < <(get_admin_user_password) || {
  36. info "Probably no admin user and password set before."
  37. first_run=1
  38. }
  39. admin_password=$(options-get admin.password 2>&1) ||
  40. admin_password=$(options-get admin-password 2>&1) || {
  41. if [ "$current_password" ]; then
  42. admin_password="$current_password"
  43. else
  44. info "Generating ${DARKYELLOW}$MASTER_BASE_SERVICE_NAME${NORMAL}'s"\
  45. "admin password"
  46. admin_password=$(gen_password)
  47. fi
  48. }
  49. ## We can't do anything with 'occ' before installation through
  50. ## postgres relation because the way 'entrypoint.sh' provided by
  51. ## nextcloud handles installation : it will not completely install
  52. ## nextcloud (it doesn't launch 'maintenance:install'), making
  53. ## nextcloud half installed and commands like 'user:*' unavailable.
  54. ## Nicely enough, we don't need to do anything password wise if
  55. ## postgres was never launched before.
  56. if [ "$first_run" == 0 ]; then
  57. if [ "$current_password" != "$admin_password" ] ||
  58. [ "$current_user" != "$admin_user" ]; then
  59. set_admin_user_password "$admin_user" "$admin_password" || {
  60. err "Failed to set admin user and password."
  61. exit 2
  62. }
  63. fi
  64. else
  65. mkdir -p "$(dirname "$PASSWORD_FILE")"
  66. p0 "$admin_user" "$admin_password" > "$PASSWORD_FILE"
  67. fi
  68. ## XXXvlab: the directory here for datadir violates DRY as it is also
  69. ## in ``metadata.yml``
  70. init-config-add "\
  71. $MASTER_BASE_SERVICE_NAME:
  72. environment:
  73. NEXTCLOUD_DATA_DIR: /var/lib/nextcloud/data
  74. NEXTCLOUD_TRUSTED_DOMAINS: '\*'
  75. "
  76. ## ensuring data directories are accessible by nextcloud
  77. uid=$(docker_get_uid "$MASTER_BASE_SERVICE_NAME" "www-data")
  78. dirs=("$SERVICE_DATASTORE/var/lib/nextcloud/data" "$SERVICE_DATASTORE/var/www/html")
  79. mkdir -p "${dirs[@]}"
  80. chown -R "$uid" "${dirs[@]}"