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.

51 lines
1.6 KiB

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