Skip to content

Instantly share code, notes, and snippets.

@drnic
Created May 22, 2015 22:12
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 drnic/cb695417db200bd10f6c to your computer and use it in GitHub Desktop.
Save drnic/cb695417db200bd10f6c to your computer and use it in GitHub Desktop.
Rebind all applications for a Cloud Foundry service
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'pp'
# for the #sh helper
require "rake"
require "rake/file_utils"
include FileUtils
## PREREQ: Using CF cli tools, make sure you've logged in as a user (admin) who can see all orgs/spaces
service_name ="postgresql93"
# Get the service plan url for specified service
service_plan_url = `cf curl "/v2/services?results-per-page=100" | jq '.resources[].entity | select(.label=="#{service_name}")' | jq -r .service_plans_url`.strip
puts "Found service plan URL: #{service_plan_url}"
# Get the Service Instances URL reference for the Service Plan
service_instances_url = `cf curl "#{service_plan_url}?results-per-page=100" | jq -r ".resources[].entity.service_instances_url"`.strip
puts "Found service plan URL: #{service_instances_url}"
# Get a list of applications that are using the service plan based on service_instances_url
# Create a new JSON Array with App Name, and Space URL
app_info =`cf curl "#{service_instances_url}?results-per-page=100" | jq '[.resources[].entity]' | jq '[.[] | {svc_name: .name, details: {space_url: .space_url, svc_bindings_url: .service_bindings_url}}]' | jq .`.strip
# Parse JSON response so we can get Space Name and Org Name
app_info_json = JSON.parse(app_info)
app_info_json.each do |x|
# Get the Space URL and set it for use as environment variable
space_url = x['details']['space_url']
# Get Space Name and Org URL
space_deets = `cf curl #{space_url} | jq '[. | {details: {space_name: .entity.name, org_url: .entity.organization_url}}]' | jq .`.strip
space_deets_json = JSON.parse(space_deets)
# Update JSON object with the Space Name, Get Org name using Org URL
space_deets_json.each do |y|
# Get Org Name using Org URL
org_url = y['details']['org_url']
org_name = `cf curl #{org_url} | jq -r .entity.name`.strip
# Update JSON object with Org Name and Space Name
x['details']['org_name'] = org_name.chomp
x['details']['space_name'] = "#{y['details']['space_name']}"
end
# We also need the App name the service is bound to. If app name is null, service instance is not bound
# There may be more than one app bound to the service instance
# Get the App URL using the Service Bindings URL, convert to an array.
app_url = `cf curl #{x['details']['svc_bindings_url']} | jq '[.resources[] | .entity.app_url]'`.strip
app_url_array = eval app_url
unless app_url_array.size == 0
app_name_array = Array.new
app_url_array.each do |z|
app_name = `cf curl #{z} | jq -r .entity.name`
unless app_name == "null"
puts "Rebinding #{x['details']['org_name']}/#{x['details']['space_name']} #{app_name.chomp}->#{x['svc_name']}"
sh "cf target -o #{x['details']['org_name']} -s #{x['details']['space_name']}"
sh "cf us #{app_name.chomp} #{x['svc_name']}"
sh "cf bs #{app_name.chomp} #{x['svc_name']}"
sh "cf restage #{app_name.chomp}"
puts
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment