Skip to content

Instantly share code, notes, and snippets.

@JohannesRudolph
Created June 26, 2018 16:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohannesRudolph/e85da0bd45c1683810764b8d219e6455 to your computer and use it in GitHub Desktop.
Save JohannesRudolph/e85da0bd45c1683810764b8d219e6455 to your computer and use it in GitHub Desktop.
connect to cloud foundry db services via cf ssh tunnels
#!/bin/bash
set -e
container="$1:dbssh"
VCAP_SERVICES=$(cf ssh $container -c 'echo $VCAP_SERVICES')
MONGO_URI=$(echo $VCAP_SERVICES | jq -r .MongoDB[0].credentials.uri)
PARSED_URI=$(./parse-url $MONGO_URI)
MONGO_PORT=$(echo $PARSED_URI | jq -r .port)
MONGO_HOST=$(echo $PARSED_URI | jq -r .hostname)
MONGO_USER=$(echo $PARSED_URI | jq -r .username)
MONGO_PASSWORD=$(echo $PARSED_URI | jq -r .password)
MONGO_DATABASE=$(echo $PARSED_URI | jq -r .path)
echo "launching cf ssh, you can connect to MongoDB. Here's some handy commands for you:"
echo ""
echo "mongo --host=127.0.0.1 --port=27017 --username=$MONGO_USER --password=$MONGO_PASSWORD --authenticationDatabase=$MONGO_DATABASE --db=$MONGO_DATABASE"
echo "mongodump --host=127.0.0.1 --port=27017 --username=$MONGO_USER --password=$MONGO_PASSWORD --authenticationDatabase=$MONGO_DATABASE --db=$MONGO_DATABASE --gzip --archive=db.agz"
echo "mongorestore --host=127.0.0.1 --port=27017 --username=$MONGO_USER --password=$MONGO_PASSWORD --authenticationDatabase=$MONGO_DATABASE --nsFrom="*" --nsTo="$MONGO_DATABASE.*" --gzip --archive=db.agz"
echo ""
cf ssh -L 27017:$MONGO_HOST:$MONGO_PORT $container
#!/bin/bash
set -e
container="${1:-dbssh}"
serviceType="${2:-MySQL}"
echo "targeting $container"
VCAP_SERVICES=$(cf ssh $container -c 'echo $VCAP_SERVICES')
MYSQL_HOST=$(echo $VCAP_SERVICES | jq -r .[\"$serviceType\"][0].credentials.host)
MYSQL_PORT=$(echo $VCAP_SERVICES | jq -r .[\"$serviceType\"][0].credentials.port)
MYSQL_DATABASE=$(echo $VCAP_SERVICES | jq -r .[\"$serviceType\"][0].credentials.database)
MYSQL_PASSWORD=$(echo $VCAP_SERVICES | jq -r .[\"$serviceType\"][0].credentials.password)
MYSQL_USER=$(echo $VCAP_SERVICES | jq -r .[\"$serviceType\"][0].credentials.username)
echo "Launching cf ssh. Here's some handy MySQL commands for you"
echo ""
echo "mysql -h 127.0.0.1 -P 63306 -u$MYSQL_USER -p$MYSQL_PASSWORD -D $MYSQL_DATABASE"
echo "mysqldump -h 127.0.0.1 -P 63306 -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE"
echo ""
cf ssh -L 63306:$MYSQL_HOST:$MYSQL_PORT $container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment