Guest User

Untitled

a guest
May 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #! /usr/bin/env bash
  2.  
  3. set -e
  4.  
  5. readonly ARGS="$#"
  6. readonly SERVICE="$1"
  7. readonly SERVICE_PLAN="$2"
  8. readonly SERVICE_INSTANCE="$3"
  9. readonly SERVICE_CREDENTIALS_JSON="$4"
  10. readonly PROGNAME=$(basename $0)
  11.  
  12. usage() {
  13. cat <<- EOF
  14. usage: $PROGNAME service-name service-plan service-instance
  15.  
  16. Program creates a clound foundry service in the currently selected space
  17. if necessary. If a service already exists with the given instance name,
  18. then the service will be updated to use the new plan. Note, will not try
  19. to chang the service broker.
  20.  
  21. Examples:
  22. Create a mysql database using the 512mb plan named bet-mysql:
  23. $PROGNAME p-mysql 512mb bet-mysql
  24. EOF
  25. }
  26.  
  27. array_contains () {
  28. local seeking=$1; shift
  29. local in=false
  30. for element; do
  31. if [[ $element == $seeking ]]; then
  32. in=true
  33. break
  34. fi
  35. done
  36. echo $in
  37. }
  38.  
  39. does_service_exist() {
  40. local services=$(cf services | awk '{print $1}' | tail -n +4)
  41. echo $(array_contains $SERVICE_INSTANCE $services)
  42. }
  43.  
  44. main() {
  45. if [ $ARGS -gt 4 ] || [ $ARGS -lt 3 ]; then
  46. usage
  47. return 1
  48. fi
  49. if [ "$(does_service_exist $SERVICE_INSTANCE)" = false ]; then
  50. echo "Creating Service..."
  51. if [ -z "$SERVICE_CREDENTIALS_JSON" ]; then
  52. cf create-service $SERVICE $SERVICE_PLAN $SERVICE_INSTANCE
  53. else
  54. cf create-service $SERVICE $SERVICE_PLAN $SERVICE_INSTANCE -c "$SERVICE_CREDENTIALS_JSON"
  55. fi
  56. else
  57. echo "Service $SERVICE_INSTANCE Already Exists, Updating..."
  58. if [ -z "$SERVICE_CREDENTIALS_JSON" ]; then
  59. cf update-service $SERVICE_INSTANCE -p $SERVICE_PLAN
  60. else
  61. cf update-service $SERVICE_INSTANCE -p $SERVICE_PLAN -c "$SERVICE_CREDENTIALS_JSON"
  62. fi
  63. fi
  64. }
  65.  
  66. main
Add Comment
Please, Sign In to add comment