Guest User

Untitled

a guest
Aug 7th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. set -e
  4.  
  5. if [ -z "$CF_BIN" ]; then CF_BIN=$(which cf); fi
  6.  
  7.  
  8. function construct_params_from_environment() {
  9. local serviceName="$1"
  10. local serviceNameVar=$(echo "$serviceName" | tr a-z A-Z)
  11. local jdbc_url=$(eval echo \$${serviceNameVar}_JDBC_URL)
  12. if [ -z "$jdbc_url" ]; then
  13. echo "${serviceNameVar}_JDBC_URL must be set to the $serviceName jdbc.url" >&2
  14. return 1
  15. fi
  16. local username=$(eval echo \$${serviceNameVar}_USERNAME)
  17. if [ -z "$username" ]; then
  18. echo "${serviceNameVar}_USERNAME must be set to the $serviceName username" >&2
  19. return 1
  20. fi
  21. local password=$(eval echo \$${serviceNameVar}_PASSWORD)
  22. if [ -z "$password" ]; then
  23. echo "${serviceNameVar}_PASSWORD must be set to the $serviceName password" >&2
  24. return 1
  25. fi
  26. params="{\"jdbc.url\":\"$jdbc_url\",\"username\":\"$username\",\"password\":\"$password\"}"
  27. echo "$params"
  28. }
  29.  
  30. function log_into_cf() {
  31. if [ -z "$CF_API" ]; then
  32. echo "CF_API must be set to a Cloud Foundry API endpoint" >&2
  33. exit 1
  34. fi
  35. if [ -z "$CF_USERNAME" ]; then
  36. echo "CF_USERNAME must be set to a Cloud Foundry username" >&2
  37. exit 1
  38. fi
  39. if [ -z "$CF_PASSWORD" ]; then
  40. echo "CF_PASSWORD must be set to a Cloud Foundry password" >&2
  41. exit 1
  42. fi
  43. if [ -z "$CF_ORG" ]; then
  44. echo "CF_ORG must be set to a Cloud Foundry organization" >&2
  45. exit 1
  46. fi
  47. if [ -z "$CF_SPACE" ]; then
  48. echo "CF_SPACE must be set to a Cloud Foundry space" >&2
  49. exit 1
  50. fi
  51.  
  52. # cf login [-a API_URL] [-u USERNAME] [-p PASSWORD] [-o ORG] [-s SPACE] [--sso | --sso-passcode PASSCODE]
  53. "$CF_BIN" login -a "$CF_API" -u "$CF_USERNAME" -p "$CF_PASSWORD" \
  54. -o "$CF_ORG" -s "$CF_SPACE"
  55. }
  56.  
  57. function create_service() {
  58. local serviceName="$1"
  59. local parameters=$(construct_params_from_environment "$serviceName")
  60.  
  61. if "$CF_BIN" services | grep "$serviceName"; then
  62. echo "$serviceName already exists, doing nothing"
  63. return 0
  64. fi
  65.  
  66. echo "Configuring $serviceName secrets in $CF_ORG/$CF_SPACE."
  67. if "$CF_BIN" create-user-provided-service "$serviceName" -p "$parameters"; then
  68. echo "Created $serviceName successfully"
  69. else
  70. echo "Failed to create $serviceName" >&2
  71. return 1
  72. fi
  73. "$CF_BIN" cups "$serviceName" -p "$serviceSecrets"
  74. }
  75.  
  76. # If we're executing this script, log in and create services
  77. if [ "$0" == "$BASH_SOURCE" ]; then
  78. log_into_cf
  79. create_service "$@"
  80. fi
Add Comment
Please, Sign In to add comment