Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- APP="notify-api"
- LOCAL_PORT=6666
- if [[ $# -eq 0 ]]; then
- cat <<EOF
- Usage: cf-db TARGET-SPACE
- Example:
- cf-db preview
- EOF
- exit 0;
- fi
- if [[ $# -gt 1 ]]; then
- echo "Too many arguments"
- exit 1;
- fi
- # Target the given space
- cf target -s $1;
- if [[ ! $? ]]; then
- echo "Could not switch to this space: ${1}"
- exit 1;
- fi
- # Get the VCAP_SERVICES var by sshing into the APP
- VCAP=`cf ssh ${APP} -c "echo \\$VCAP_SERVICES"`
- # Extract required vars from VCAP_SERVICES json
- # and use `tr` to remove quotes from the extracted values
- HOST=`echo $VCAP | jq '.postgres | .[0].credentials.host' | tr -d \"`
- PORT=`echo $VCAP | jq '.postgres | .[0].credentials.port' | tr -d \"`
- USERNAME=`echo $VCAP | jq '.postgres | .[0].credentials.username' | tr -d \"`
- PASSWORD=`echo $VCAP | jq '.postgres | .[0].credentials.password' | tr -d \"`
- DB_NAME=`echo $VCAP | jq '.postgres | .[0].credentials.name' | tr -d \"`
- echo "Copy and paste this in another terminal:"
- echo "psql postgres://${USERNAME}:${PASSWORD}@localhost:${LOCAL_PORT}/${DB_NAME}"
- # Finally create the tunnel
- cf ssh ${APP} -L ${LOCAL_PORT}:${HOST}:${PORT}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement