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.

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