77 lines
2.4 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. first_run=0
  16. read-0 current_user current_password < <(get_admin_user_password) || {
  17. info "Probably no admin user and password set before."
  18. first_run=1
  19. }
  20. admin_password=$(options-get admin.password 2>&1) ||
  21. admin_password=$(options-get admin-password 2>&1) || {
  22. if [ "$current_password" ]; then
  23. admin_password="$current_password"
  24. else
  25. info "Generating ${DARKYELLOW}$MASTER_BASE_SERVICE_NAME${NORMAL}'s"\
  26. "admin password"
  27. admin_password=$(gen_password)
  28. fi
  29. }
  30. ## We can't do anything with 'occ' before installation through
  31. ## postgres relation because the way 'entrypoint.sh' provided by
  32. ## nextcloud handles installation : it will not completely install
  33. ## nextcloud (it doesn't launch 'maintenance:install'), making
  34. ## nextcloud half installed and commands like 'user:*' unavailable.
  35. ## Nicely enough, we don't need to do anything password wise if
  36. ## postgres was never launched before.
  37. if [ "$first_run" == 0 ]; then
  38. if [ "$current_password" != "$admin_password" ] ||
  39. [ "$current_user" != "$admin_user" ]; then
  40. set_admin_user_password "$admin_user" "$admin_password" || {
  41. err "Failed to set admin user and password."
  42. exit 2
  43. }
  44. fi
  45. else
  46. mkdir -p "$(dirname "$PASSWORD_FILE")"
  47. p0 "$admin_user" "$admin_password" > "$PASSWORD_FILE"
  48. fi
  49. ## XXXvlab: the directory here for datadir violates DRY as it is also
  50. ## in ``metadata.yml``
  51. init-config-add "\
  52. $MASTER_BASE_SERVICE_NAME:
  53. environment:
  54. NEXTCLOUD_ADMIN_USER: $admin_user
  55. NEXTCLOUD_ADMIN_PASSWORD: $admin_password
  56. NEXTCLOUD_DATA_DIR: /var/lib/nextcloud/data
  57. "
  58. ## ensuring data directories are accessible by nextcloud
  59. uid=$(docker_get_uid "$MASTER_BASE_SERVICE_NAME" "www-data")
  60. dirs=("$SERVICE_DATASTORE/var/lib/nextcloud/data" "$SERVICE_DATASTORE/var/www/html")
  61. mkdir -p "${dirs[@]}"
  62. chown -R "$uid" "${dirs[@]}"