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.

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