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.

75 lines
2.2 KiB

  1. #!/bin/bash
  2. . lib/common
  3. set -e
  4. TYPE="$(relation-get type)" || {
  5. err "No ${WHITE}type${NORMAL} set in relation."
  6. exit 1
  7. }
  8. PASSWORD="$(relation-get password)"
  9. USER="$(relation-get user)"
  10. DBNAME="$(relation-get dbname)"
  11. ## This check adds purely arbitrary limits to what could be a password
  12. ## if we need to open that more, just consider the next script where we'll
  13. ## need to write in a PHP structure, or in YAML structure.
  14. ## Note that here, "[]" chars are not accepted just because it doesn't seem evident
  15. ## to test for those in bash.
  16. if ! [[ "$PASSWORD" =~ ^[a-zA-Z0-9~\`\&+=@\#^\*/\\_%\$:\;\!?.,\<\>{}()\"\'|-]*$ ]]; then
  17. err "Invalid password chosen for $type database."
  18. exit 1
  19. fi
  20. ## if config is not existent
  21. if [ -e "$CONFIGFILE" ] && grep "^ 'dbuser' => '" "$CONFIGFILE" >/dev/null; then
  22. ## 'occ' can't be used as it will try to connect to db before running and
  23. ## will fail if user/password is not correct
  24. ## We need to get through bash, and sed interpretation, then PHP single quoted strings.
  25. quoted_user="${USER//\\/\\\\\\\\\\}"
  26. quoted_user="${quoted_user//\'/\\\\\'}"
  27. quoted_password="${PASSWORD//\\/\\\\\\\\\\}"
  28. quoted_password="${quoted_password//\'/\\\\\'}"
  29. case "$TYPE" in
  30. mysql)
  31. nextcloud_type="mysql";;
  32. postgres)
  33. nextcloud_type="pgsql";;
  34. *)
  35. err "Unknown type '$TYPE' for database."
  36. exit 1
  37. ;;
  38. esac
  39. sed -ri "s/^( 'dbuser' => ')(.*)(',)$/\1${quoted_user}\3/g;\
  40. s/^( 'dbpassword' => ')(.*)(',)$/\1${quoted_password}\3/g;\
  41. s/^( 'dbtype' => ')(.*)(',)$/\1${nextcloud_type}\3/g;\
  42. s/^( 'dbhost' => ')(.*)(',)$/\1${MASTER_TARGET_SERVICE_NAME}\3/g;\
  43. " "$CONFIGFILE"
  44. else
  45. ## These variable are not used by current docker image after first install
  46. if [ "$TYPE" == "mysql" ]; then
  47. database_env_label="DATABASE"
  48. else
  49. database_env_label="DB"
  50. fi
  51. config-add "\
  52. services:
  53. $MASTER_BASE_SERVICE_NAME:
  54. environment:
  55. ${TYPE^^}_HOST: $MASTER_TARGET_SERVICE_NAME
  56. ${TYPE^^}_${database_env_label}: $DBNAME
  57. ${TYPE^^}_PASSWORD: $PASSWORD
  58. ${TYPE^^}_USER: $USER
  59. "
  60. fi
  61. info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."