Guest User

Untitled

a guest
Oct 27th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. # Two possibilities to parse a Postgres DB URL from heroku into environment variables
  2. # that Spring Boot understands.
  3. # You would need that, if you do not build on heroku but push docker images
  4. # from another source
  5. # Does not need bash. Works on alpine linux / busybox. Tested with openjdk:8-jdk-alpine base image.
  6.  
  7. export DATABASE_URL=postgres://user:password@host:port/database
  8.  
  9. # Naive way, would break with [@:/] in username or password
  10. DB_TYPE=$(echo $DATABASE_URL | awk -F'[:@/]' '{print $1}')"ql"
  11. DB_USER=$(echo $DATABASE_URL | awk -F'[:@/]' '{print $4}')
  12. DB_PASSWORD=$(echo $DATABASE_URL | awk -F'[:@/]' '{print $5}')
  13. DB_HOST=$(echo $DATABASE_URL | awk -F'[:@/]' '{print $6}')
  14. DB_PORT=$(echo $DATABASE_URL | awk -F'[:@/]' '{print $7}')
  15. DB_DATABASE=$(echo $DATABASE_URL | awk -F'[:@/]' '{print $8}')
  16.  
  17. export SPRING_DATASOURCE_URL=$(echo "jdbc:$DB_TYPE:$DB_HOST://$DB_PORT/$DB_DATABASE")
  18. export SPRING_DATASOURCE_USERNAME=$DB_USER
  19. export SPRING_DATASOURCE_PASSWORD=$DB_PORT
  20.  
  21. echo $SPRING_DATASOURCE_URL
  22.  
  23.  
  24. # More robust way(?)
  25. DB_TYPE=$(echo $DATABASE_URL | cut -d':' -f1)"ql"
  26. DB_TMP=$(echo $DATABASE_URL | cut -d':' -f2- | sed 's/^\/\///')
  27. DB_TMP_USER_PASS=$(echo $DB_TMP | cut -d'@' -f1)
  28.  
  29. DB_HOST_PORT_DB=$(echo $DB_TMP | cut -d'@' -f2-)
  30. DB_USER=$(echo $DB_TMP_USER_PASS | cut -d':' -f1)
  31. DB_PASS=$(echo $DB_TMP_USER_PASS | cut -d':' -f2)
  32.  
  33.  
  34. export SPRING_DATASOURCE_URL=$(echo "jdbc:$DB_TYPE://$DB_HOST_PORT_DB")
  35. export SPRING_DATASOURCE_USERNAME=$DB_USER
  36. export SPRING_DATASOURCE_PASSWORD=$DB_PASS
  37.  
  38. echo $SPRING_DATASOURCE_URL
  39. echo $SPRING_DATASOURCE_USERNAME
  40. echo $SPRING_DATASOURCE_PASSWORD
Add Comment
Please, Sign In to add comment