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.

197 lines
6.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. ## 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. config:system:set maintenance_window_start --type=integer --value=1 \; \
  71. config:system:set trusted_proxies 0 --value="0.0.0.0/0" \; \
  72. db:add-missing-columns \; \
  73. db:add-missing-indices \; \
  74. db:add-missing-primary-keys \; \
  75. maintenance:repair --include-expensive
  76. }
  77. create_occ_if_not_exists() {
  78. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  79. ## Here we use a nasty trick to launch only the initialisation
  80. ## part of the ``entrypoint.sh``. By setting 'apache' as first
  81. ## call argument, we satisfy the big first 'if' condition
  82. ## triggering the installation if necessary, and will fail to
  83. ## launch any apache
  84. ## Last, we do not want the relation web-proxy to run in this
  85. ## bare-minimum nextcloud run AND we will use occ to set some info
  86. ## in this very same relation.
  87. ## Note also that we need to set NEXTCLOUD_ADMIN_{USER,PASSWORD}
  88. ## that is required to trigger a full installation
  89. if ! out=$(
  90. export COMPOSE_IGNORE_ORPHANS=true
  91. read-0 LOGIN PASSWORD < "$PASSWORD_FILE" || exit 1
  92. compose --debug --no-init --without-relation="$SERVICE_NAME":web-proxy \
  93. --without-relation="$SERVICE_NAME":log-rotate \
  94. run \
  95. -v "$CHARM_PATH"/src/fake-apache:/usr/bin/apache \
  96. -e NEXTCLOUD_DATA_DIR=/var/lib/nextcloud/data \
  97. -e NEXTCLOUD_ADMIN_USER=$LOGIN \
  98. -e NEXTCLOUD_ADMIN_PASSWORD=$PASSWORD \
  99. --rm --entrypoint /entrypoint.sh "$SERVICE_NAME" apache 2>&1
  100. ); then
  101. err "Initialization of code or database failed unexpectedly"
  102. e "$out" | prefix " | "
  103. return 1
  104. fi
  105. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  106. err "Expected last command to create /var/www/html/occ"
  107. return 1
  108. fi
  109. nextcloud:init || return 1
  110. fi
  111. }
  112. occ() {
  113. create_occ_if_not_exists || return 1
  114. ## occ.batch will require /var/www/html to be populated ('occ' is
  115. ## supposed to exist). For that we need to make sure nextcloud have
  116. ## be ran and setup prior to running this next command.
  117. ## We need here actually only the relation sql-database. Any other hook
  118. ## using `occ` would make the call infinitively recursive.
  119. export COMPOSE_IGNORE_ORPHANS=true
  120. compose --debug -q --no-init --no-pre-deploy \
  121. --without-relation="$SERVICE_NAME":web-proxy \
  122. --without-relation="$SERVICE_NAME":log-rotate \
  123. run \
  124. "${occ_docker_run_opts[@]}" \
  125. -e NEXTCLOUD_DATA_DIR=/var/lib/nextcloud/data \
  126. -v "$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/src/occ.batch:/var/www/html/occ.batch" \
  127. -T --rm -u www-data "$SERVICE_NAME" /var/www/html/occ.batch "$@" | cat
  128. if [ "${PIPESTATUS[0]}" != 0 ]; then
  129. err "Failure to execute these ${WHITE}occ${NORMAL} commands:"
  130. printf '%s ' "$@" |
  131. sed -r "s/\\;/\n/g" |
  132. sed -r "s/^\s*(.*)\s*$/${WHITE}\1${NORMAL}/g" |
  133. prefix " ${DARKGRAY}>${NORMAL} " >&2
  134. echo "" >&2
  135. echo "" >&2
  136. echo " If the code of nextcloud is already there (command occ is found), but " >&2
  137. echo " the database is not yet created, this situation will arise." >&2
  138. return "${PIPESTATUS[0]}"
  139. fi
  140. }
  141. nextcloud:config:simple:add() {
  142. local key="$1" value="$2"
  143. create_occ_if_not_exists || return 1
  144. if ! [ -e "$CONFIGFILE" ]; then
  145. err "Config file '$CONFIGFILE' does not exist."
  146. return 1
  147. fi
  148. if [ -z "$value" ]; then
  149. err "Value for '$key' is empty. Skipping."
  150. return 1
  151. fi
  152. ## check for \ and ' in value and key
  153. if [[ "$value" =~ [\\\'] ]]; then
  154. err "Unsupported value for '$key' contains a backslash or a single quote."
  155. return 1
  156. fi
  157. if [[ "$key" =~ [\\\'] ]]; then
  158. err "Key '$key' contains a backslash or a single quote."
  159. return 1
  160. fi
  161. if grep "^ '$key' => '" "$CONFIGFILE" >/dev/null; then
  162. sed -ri "s|^( '$key' => ')(.*)(',)$|\1${value}\3|g" "$CONFIGFILE"
  163. return 0
  164. fi
  165. ## Add '$key' => 'value', to the end of the file, before the closing paren.
  166. sed -ri "s|^(\);)$| '$key' => '${value}',\n\1|g" "$CONFIGFILE"
  167. }