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.

97 lines
2.9 KiB

  1. # -*- mode: shell-script -*-
  2. ## This place is not accessible from container on purpose: container
  3. ## don't need that. This should be stored in /var/lib/compose/ in a
  4. ## project, service directory a little like relation data.
  5. PASSWORD_FILE="$SERVICE_CONFIGSTORE/etc/$SERVICE_NAME/pass"
  6. has_user() {
  7. local user="$1"
  8. if ! out=$(occ user:info "$user"); then
  9. if [ "$out" == "user not found" ]; then
  10. return 1
  11. else
  12. err "Command 'occ user:info $user' failed:"
  13. echo "OUT: '$out'" | prefix " | " >&2
  14. return 2
  15. fi
  16. fi
  17. return 0
  18. }
  19. set_admin_user_password() {
  20. local user="$1" password="$2"
  21. [ -z "$password" ] && {
  22. err "Refusing to set admin user an empty password."
  23. return 3
  24. }
  25. if ! has_user "$user"; then
  26. info "User $user not found. Creating it in default 'admin' group."
  27. (
  28. occ_docker_run_opts=("-e" "OC_PASS=$password")
  29. occ user:add --group=admin --password-from-env --display-name="$user" "$user"
  30. )
  31. else
  32. (
  33. occ_docker_run_opts=("-e" "OC_PASS=$password")
  34. occ user:resetpassword "$user" "--password-from-env"
  35. )
  36. fi
  37. mkdir -p "$(dirname "$PASSWORD_FILE")"
  38. p0 "$user" "$password" > "$PASSWORD_FILE"
  39. }
  40. get_admin_user_password() {
  41. if [ -e "$PASSWORD_FILE" ]; then
  42. cat "$PASSWORD_FILE"
  43. else
  44. return 1
  45. fi
  46. }
  47. create_occ_if_not_exists() {
  48. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  49. ## Here we use a nasty trick to launch only the initialisation
  50. ## part of the ``entrypoint.sh``. By setting 'apache' as first
  51. ## call argument, we satisfy the big first 'if' condition
  52. ## triggering the installation if necessary, and will fail to
  53. ## launch any apache
  54. ## Last, we do not want the relation web-proxy to run in this
  55. ## bare-minimum nextcloud run AND we will use occ to set some info
  56. ## in this very same relation.
  57. export COMPOSE_IGNORE_ORPHANS=true
  58. compose --debug --no-init --without-relation="$SERVICE_NAME":web-proxy run \
  59. --rm --entrypoint /entrypoint.sh "$SERVICE_NAME" apache >&2 || true
  60. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  61. err "Expected last command to create /var/www/html/occ"
  62. return 1
  63. fi
  64. fi
  65. }
  66. occ() {
  67. create_occ_if_not_exists
  68. ## occ.batch will require /var/www/html to be populated ('occ' is
  69. ## supposed to exist). For that we need to make sure nextcloud have
  70. ## be ran and setup prior to running this next command.
  71. export COMPOSE_IGNORE_ORPHANS=true
  72. compose --debug -q --no-init --no-relations run \
  73. "${occ_docker_run_opts[@]}" \
  74. -v "$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/src/occ.batch:/var/www/html/occ.batch" \
  75. -T --rm -u www-data "$SERVICE_NAME" /var/www/html/occ.batch "$@" | cat
  76. return "${PIPESTATUS[0]}"
  77. }