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.

173 lines
5.6 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. if ! out=$(
  80. export COMPOSE_IGNORE_ORPHANS=true
  81. compose --debug --without-relation="$SERVICE_NAME":web-proxy run \
  82. -v "$CHARM_PATH"/src/fake-apache:/usr/bin/apache \
  83. --rm --entrypoint /entrypoint.sh "$SERVICE_NAME" apache 2>&1
  84. ); then
  85. err "Initialization of code or database failed unexpectedly"
  86. e "$out" | prefix " | "
  87. return 1
  88. fi
  89. if ! [ -e "$SERVICE_DATASTORE/var/www/html/occ" ]; then
  90. err "Expected last command to create /var/www/html/occ"
  91. return 1
  92. fi
  93. fi
  94. }
  95. occ() {
  96. create_occ_if_not_exists || return 1
  97. ## occ.batch will require /var/www/html to be populated ('occ' is
  98. ## supposed to exist). For that we need to make sure nextcloud have
  99. ## be ran and setup prior to running this next command.
  100. export COMPOSE_IGNORE_ORPHANS=true
  101. compose --debug -q --no-init --without-relation="$SERVICE_NAME":web-proxy run \
  102. "${occ_docker_run_opts[@]}" \
  103. -v "$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/src/occ.batch:/var/www/html/occ.batch" \
  104. -T --rm -u www-data "$SERVICE_NAME" /var/www/html/occ.batch "$@" | cat
  105. if [ "${PIPESTATUS[0]}" != 0 ]; then
  106. err "Failure to execute these ${WHITE}occ${NORMAL} commands:"
  107. printf '%s ' "$@" |
  108. sed -r "s/\\;/\n/g" |
  109. sed -r "s/^\s*(.*)\s*$/${WHITE}\1${NORMAL}/g" |
  110. prefix " ${DARKGRAY}>${NORMAL} " >&2
  111. echo "" >&2
  112. echo "" >&2
  113. echo " If the code of nextcloud is already there (command occ is found), but " >&2
  114. echo " the database is not yet created, this situation will arise." >&2
  115. return "${PIPESTATUS[0]}"
  116. fi
  117. }
  118. nextcloud:config:simple:add() {
  119. local key="$1" value="$2"
  120. create_occ_if_not_exists || return 1
  121. if ![ -e "$CONFIGFILE" ]; then
  122. err "Config file '$CONFIGFILE' does not exist."
  123. return 1
  124. fi
  125. if [ "$#" -lt 2 ]; then
  126. err "No value specified for '$key'."
  127. return 1
  128. fi
  129. ## check for \ and ' in value and key
  130. if [[ "$value" =~ [\\\'] ]]; then
  131. err "Unsupported value for '$key' contains a backslash or a single quote."
  132. return 1
  133. fi
  134. if [[ "$key" =~ [\\\'] ]]; then
  135. err "Key '$key' contains a backslash or a single quote."
  136. return 1
  137. fi
  138. if grep "^ '$key' => '" "$CONFIGFILE" >/dev/null; then
  139. sed -ri "s/^( '$key' => ')(.*)(',)$/\1${value}\3/g" "$CONFIGFILE"
  140. return 0
  141. fi
  142. ## Add '$key' => 'value', to the end of the file, before the closing paren.
  143. sed -ri "s/^(\);)$/ '$key' => '${value}',\n\1/g" "$CONFIGFILE"
  144. }