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.

194 lines
6.2 KiB

1 month ago
  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. CONFIGDIR="$SERVICE_DATASTORE/var/www/html/config"
  9. CONFIGFILE="$CONFIGDIR/config.php"
  10. has_user() {
  11. local user="$1"
  12. if ! out=$(occ user:info "$user"); then
  13. if [ "$out" == "user not found" ]; then
  14. return 1
  15. else
  16. if [ -n "$out" ]; then
  17. err "Command 'occ user:info $user' failed with this output:"
  18. echo "$out" | prefix " | " >&2
  19. else
  20. err "Command 'occ user:info $user' failed with no output."
  21. fi
  22. return 2
  23. fi
  24. fi
  25. return 0
  26. }
  27. set_admin_user_password() {
  28. local user="$1" password="$2" errlvl
  29. [ -z "$password" ] && {
  30. err "Refusing to set admin user an empty password."
  31. return 3
  32. }
  33. has_user "$user"
  34. errlvl=$?
  35. [[ "$errlvl" -gt 1 ]] && {
  36. err "'has_user $user' failed. Bailing out."
  37. return "$errlvl"
  38. }
  39. if [[ "$errlvl" == 1 ]]; then
  40. info "User $user not found. Creating it in default 'admin' group."
  41. (
  42. occ_docker_run_opts=("-e" "OC_PASS=$password")
  43. occ user:add --group=admin --password-from-env --display-name="$user" "$user"
  44. ) || return 1
  45. else
  46. info "User $user found. Resetting password."
  47. (
  48. occ_docker_run_opts=("-e" "OC_PASS=$password")
  49. occ user:resetpassword "$user" "--password-from-env"
  50. ) || {
  51. err "'occ user:resetpassword' failed," \
  52. "common reason include password too simple."
  53. return 1
  54. }
  55. fi
  56. ## XXXvlab: DRY violation: init does the same thing
  57. mkdir -p "$(dirname "$PASSWORD_FILE")"
  58. p0 "$user" "$password" > "$PASSWORD_FILE"
  59. }
  60. get_admin_user_password() {
  61. if [ -e "$PASSWORD_FILE" ]; then
  62. cat "$PASSWORD_FILE"
  63. else
  64. return 1
  65. fi
  66. }
  67. ## only called after first install and occ is available
  68. nextcloud:init() {
  69. occ app:disable updatenotification nextcloud_announcements
  70. }
  71. create_occ_if_not_exists() {
  72. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  73. ## Here we use a nasty trick to launch only the initialisation
  74. ## part of the ``entrypoint.sh``. By setting 'apache' as first
  75. ## call argument, we satisfy the big first 'if' condition
  76. ## triggering the installation if necessary, and will fail to
  77. ## launch any apache
  78. ## Last, we do not want the relation web-proxy to run in this
  79. ## bare-minimum nextcloud run AND we will use occ to set some info
  80. ## in this very same relation.
  81. ## Note also that we need to set NEXTCLOUD_ADMIN_{USER,PASSWORD}
  82. ## that is required to trigger a full installation
  83. if ! out=$(
  84. export COMPOSE_IGNORE_ORPHANS=true
  85. read-0 LOGIN PASSWORD < "$PASSWORD_FILE" || exit 1
  86. compose --debug --no-init --without-relation="$SERVICE_NAME":web-proxy run \
  87. -v "$CHARM_PATH"/src/fake-apache:/usr/bin/apache \
  88. -e NEXTCLOUD_ADMIN_USER=$LOGIN \
  89. -e NEXTCLOUD_ADMIN_PASSWORD=$PASSWORD \
  90. --rm --entrypoint /entrypoint.sh "$SERVICE_NAME" apache 2>&1
  91. ); then
  92. err "Initialization of code or database failed unexpectedly"
  93. e "$out" | prefix " | "
  94. return 1
  95. fi
  96. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  97. err "Expected last command to create /var/www/html/occ"
  98. return 1
  99. fi
  100. nextcloud:init
  101. fi
  102. }
  103. occ() {
  104. create_occ_if_not_exists || return 1
  105. ## occ.batch will require /var/www/html to be populated ('occ' is
  106. ## supposed to exist). For that we need to make sure nextcloud have
  107. ## be ran and setup prior to running this next command.
  108. export COMPOSE_IGNORE_ORPHANS=true
  109. compose --debug -q --no-init --without-relation="$SERVICE_NAME":web-proxy run \
  110. "${occ_docker_run_opts[@]}" \
  111. -v "$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/src/occ.batch:/var/www/html/occ.batch" \
  112. -T --rm -u www-data "$SERVICE_NAME" /var/www/html/occ.batch "$@" | cat
  113. if [ "${PIPESTATUS[0]}" != 0 ]; then
  114. err "Failure to execute these ${WHITE}occ${NORMAL} commands:"
  115. printf '%s ' "$@" |
  116. sed -r "s/\\;/\n/g" |
  117. sed -r "s/^\s*(.*)\s*$/${WHITE}\1${NORMAL}/g" |
  118. prefix " ${DARKGRAY}>${NORMAL} " >&2
  119. echo "" >&2
  120. echo "" >&2
  121. echo " If the code of nextcloud is already there (command occ is found), but " >&2
  122. echo " the database is not yet created, this situation will arise." >&2
  123. return "${PIPESTATUS[0]}"
  124. fi
  125. }
  126. nextcloud:config:simple:add() {
  127. local key="$1" value="$2"
  128. create_occ_if_not_exists || return 1
  129. if ! [ -e "$CONFIGFILE" ]; then
  130. err "Config file '$CONFIGFILE' does not exist."
  131. return 1
  132. fi
  133. if [ -z "$value" ]; then
  134. err "Value for '$key' is empty. Skipping."
  135. return 1
  136. fi
  137. ## check for \ and ' in value and key
  138. if [[ "$value" =~ [\\\'] ]]; then
  139. err "Unsupported value for '$key' contains a backslash or a single quote."
  140. return 1
  141. fi
  142. if [[ "$key" =~ [\\\'] ]]; then
  143. err "Key '$key' contains a backslash or a single quote."
  144. return 1
  145. fi
  146. if grep "^ '$key' => '" "$CONFIGFILE" >/dev/null; then
  147. sed -ri "s/^( '$key' => ')(.*)(',)$/\1${value}\3/g" "$CONFIGFILE"
  148. return 0
  149. fi
  150. ## Add '$key' => 'value', to the end of the file, before the closing paren.
  151. sed -ri "s/^(\);)$/ '$key' => '${value}',\n\1/g" "$CONFIGFILE"
  152. }
  153. nextcloud:config:version() {
  154. for f in {"$SERVICE_CONFIGSTORE","$SERVICE_DATASTORE"}/var/www/html/config/config.php; do
  155. if [ -e "$f" ]; then
  156. cat "$f"
  157. break
  158. fi
  159. done |
  160. grep "'version' =>" |
  161. cut -f 4 -d \' |
  162. cut -f 1-3 -d .
  163. }