Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /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
Add Comment
Please, Sign In to add comment