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.

60 lines
1.9 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. sed -ri "s/^( 'dbuser' => ')(.*)(',)$/\1${quoted_user}\3/g;\
  30. s/^( 'dbpassword' => ')(.*)(',)$/\1${quoted_password}\3/g;" "$CONFIGFILE"
  31. else
  32. ## These variable are not used by current docker image after first install
  33. if [ "$TYPE" == "mysql" ]; then
  34. database_env_label="DATABASE"
  35. else
  36. database_env_label="DB"
  37. fi
  38. config-add "\
  39. services:
  40. $MASTER_BASE_SERVICE_NAME:
  41. environment:
  42. ${TYPE^^}_HOST: $MASTER_TARGET_SERVICE_NAME
  43. ${TYPE^^}_${database_env_label}: $DBNAME
  44. ${TYPE^^}_PASSWORD: $PASSWORD
  45. ${TYPE^^}_USER: $USER
  46. "
  47. fi
  48. info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."