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.

118 lines
3.7 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. if [ -n "$out" ]; then
  13. err "Command 'occ user:info $user' failed with this output:"
  14. echo "$out" | prefix " | " >&2
  15. else
  16. err "Command 'occ user:info $user' failed with no output."
  17. fi
  18. return 2
  19. fi
  20. fi
  21. return 0
  22. }
  23. set_admin_user_password() {
  24. local user="$1" password="$2" errlvl
  25. [ -z "$password" ] && {
  26. err "Refusing to set admin user an empty password."
  27. return 3
  28. }
  29. has_user "$user"
  30. errlvl=$?
  31. [[ "$errlvl" -gt 1 ]] && {
  32. err "'has_user $user' failed. Bailing out."
  33. return "$errlvl"
  34. }
  35. if [[ "$errlvl" == 1 ]]; then
  36. info "User $user not found. Creating it in default 'admin' group."
  37. (
  38. occ_docker_run_opts=("-e" "OC_PASS=$password")
  39. occ user:add --group=admin --password-from-env --display-name="$user" "$user"
  40. ) || return 1
  41. else
  42. info "User $user found. Resetting password."
  43. (
  44. occ_docker_run_opts=("-e" "OC_PASS=$password")
  45. occ user:resetpassword "$user" "--password-from-env"
  46. ) || {
  47. err "'occ user:resetpassword' failed," \
  48. "common reason include password too simple."
  49. return 1
  50. }
  51. fi
  52. ## XXXvlab: DRY violation: init does the same thing
  53. mkdir -p "$(dirname "$PASSWORD_FILE")"
  54. p0 "$user" "$password" > "$PASSWORD_FILE"
  55. }
  56. get_admin_user_password() {
  57. if [ -e "$PASSWORD_FILE" ]; then
  58. cat "$PASSWORD_FILE"
  59. else
  60. return 1
  61. fi
  62. }
  63. create_occ_if_not_exists() {
  64. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  65. ## Here we use a nasty trick to launch only the initialisation
  66. ## part of the ``entrypoint.sh``. By setting 'apache' as first
  67. ## call argument, we satisfy the big first 'if' condition
  68. ## triggering the installation if necessary, and will fail to
  69. ## launch any apache
  70. ## Last, we do not want the relation web-proxy to run in this
  71. ## bare-minimum nextcloud run AND we will use occ to set some info
  72. ## in this very same relation.
  73. ## Note also that ``init`` is required as it sets
  74. ## NEXTCLOUD_ADMIN_{USER,PASSWORD} that is required to trigger
  75. ## a full installation
  76. export COMPOSE_IGNORE_ORPHANS=true
  77. compose --debug --without-relation="$SERVICE_NAME":web-proxy run \
  78. --rm --entrypoint /entrypoint.sh "$SERVICE_NAME" apache >&2 || true
  79. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  80. err "Expected last command to create /var/www/html/occ"
  81. return 1
  82. fi
  83. fi
  84. }
  85. occ() {
  86. create_occ_if_not_exists || return 1
  87. ## occ.batch will require /var/www/html to be populated ('occ' is
  88. ## supposed to exist). For that we need to make sure nextcloud have
  89. ## be ran and setup prior to running this next command.
  90. export COMPOSE_IGNORE_ORPHANS=true
  91. compose --debug -q --no-init --no-relations run \
  92. "${occ_docker_run_opts[@]}" \
  93. -v "$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/src/occ.batch:/var/www/html/occ.batch" \
  94. -T --rm -u www-data "$SERVICE_NAME" /var/www/html/occ.batch "$@" | cat
  95. return "${PIPESTATUS[0]}"
  96. }