Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. APP="notify-api"
  4. LOCAL_PORT=6666
  5.  
  6. if [[ $# -eq 0 ]]; then
  7. cat <<EOF
  8. Usage: cf-db TARGET-SPACE
  9.  
  10. Example:
  11. cf-db preview
  12. EOF
  13. exit 0;
  14. fi
  15.  
  16. if [[ $# -gt 1 ]]; then
  17. echo "Too many arguments"
  18. exit 1;
  19. fi
  20.  
  21. # Target the given space
  22. cf target -s $1;
  23. if [[ ! $? ]]; then
  24. echo "Could not switch to this space: ${1}"
  25. exit 1;
  26. fi
  27.  
  28. # Get the VCAP_SERVICES var by sshing into the APP
  29. VCAP=`cf ssh ${APP} -c "echo \\$VCAP_SERVICES"`
  30.  
  31. # Extract required vars from VCAP_SERVICES json
  32. # and use `tr` to remove quotes from the extracted values
  33. HOST=`echo $VCAP | jq '.postgres | .[0].credentials.host' | tr -d \"`
  34. PORT=`echo $VCAP | jq '.postgres | .[0].credentials.port' | tr -d \"`
  35. USERNAME=`echo $VCAP | jq '.postgres | .[0].credentials.username' | tr -d \"`
  36. PASSWORD=`echo $VCAP | jq '.postgres | .[0].credentials.password' | tr -d \"`
  37. DB_NAME=`echo $VCAP | jq '.postgres | .[0].credentials.name' | tr -d \"`
  38.  
  39. echo "Copy and paste this in another terminal:"
  40. echo "psql postgres://${USERNAME}:${PASSWORD}@localhost:${LOCAL_PORT}/${DB_NAME}"
  41.  
  42. # Finally create the tunnel
  43. cf ssh ${APP} -L ${LOCAL_PORT}:${HOST}:${PORT}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement