Skip to content

Instantly share code, notes, and snippets.

@bthelen
Last active May 16, 2018 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bthelen/51886feb0e08ef70f3f1336954d0c07a to your computer and use it in GitHub Desktop.
Save bthelen/51886feb0e08ef70f3f1336954d0c07a to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
set -e
readonly ARGS="$#"
readonly SERVICE="$1"
readonly SERVICE_PLAN="$2"
readonly SERVICE_INSTANCE="$3"
readonly SERVICE_CREDENTIALS_JSON="$4"
readonly PROGNAME=$(basename $0)
usage() {
cat <<- EOF
usage: $PROGNAME service-name service-plan service-instance
Program creates a clound foundry service in the currently selected space
if necessary. If a service already exists with the given instance name,
then the service will be updated to use the new plan. Note, will not try
to chang the service broker.
Examples:
Create a mysql database using the 512mb plan named bet-mysql:
$PROGNAME p-mysql 512mb bet-mysql
EOF
}
array_contains () {
local seeking=$1; shift
local in=false
for element; do
if [[ $element == $seeking ]]; then
in=true
break
fi
done
echo $in
}
does_service_exist() {
local services=$(cf services | awk '{print $1}' | tail -n +4)
echo $(array_contains $SERVICE_INSTANCE $services)
}
main() {
if [ $ARGS -gt 4 ] || [ $ARGS -lt 3 ]; then
usage
return 1
fi
if [ "$(does_service_exist $SERVICE_INSTANCE)" = false ]; then
echo "Creating Service..."
if [ -z "$SERVICE_CREDENTIALS_JSON" ]; then
cf create-service $SERVICE $SERVICE_PLAN $SERVICE_INSTANCE
else
cf create-service $SERVICE $SERVICE_PLAN $SERVICE_INSTANCE -c "$SERVICE_CREDENTIALS_JSON"
fi
else
echo "Service $SERVICE_INSTANCE Already Exists, Updating..."
if [ -z "$SERVICE_CREDENTIALS_JSON" ]; then
cf update-service $SERVICE_INSTANCE -p $SERVICE_PLAN
else
cf update-service $SERVICE_INSTANCE -p $SERVICE_PLAN -c "$SERVICE_CREDENTIALS_JSON"
fi
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment