fork 0k-charms
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.

76 lines
2.4 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 -i 's/\r//' "$CONFIGFILE" ## Remove DOS/WIN carriage returns if any
  40. sed -ri "s/^( 'dbuser' => ')(.*)(',)$/\1${quoted_user}\3/g;\
  41. s/^( 'dbpassword' => ')(.*)(',)$/\1${quoted_password}\3/g;\
  42. s/^( 'dbtype' => ')(.*)(',)$/\1${nextcloud_type}\3/g;\
  43. s/^( 'dbhost' => ')(.*)(',)$/\1${MASTER_TARGET_SERVICE_NAME}\3/g;\
  44. s/^( 'dbname' => ')(.*)(',)$/\1${DBNAME}\3/g;\
  45. " "$CONFIGFILE"
  46. else
  47. ## These variable are not used by current docker image after first install
  48. if [ "$TYPE" == "mysql" ]; then
  49. database_env_label="DATABASE"
  50. else
  51. database_env_label="DB"
  52. fi
  53. config-add "\
  54. services:
  55. $MASTER_BASE_SERVICE_NAME:
  56. environment:
  57. ${TYPE^^}_HOST: $MASTER_TARGET_SERVICE_NAME
  58. ${TYPE^^}_${database_env_label}: $DBNAME
  59. ${TYPE^^}_PASSWORD: $PASSWORD
  60. ${TYPE^^}_USER: $USER
  61. "
  62. fi
  63. info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."