Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Simple ruby script to download the contents of an application running on Cloud Foundry to a zip file

View Gemfile
1 2 3 4
source :rubygems
 
gem 'cfoundry'
gem 'uuidtools'
View Gemfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'cfoundry'
require 'uuidtools'
 
# client = CFoundry::Client.new 'http://api.cloudfoundry.com'
# client.login ENV['cf_user'], ENV['cf_pass']
 
home = ENV['HOME']
endpoint = 'https://api.cloudfoundry.com'
 
config = YAML.load File.read("#{home}/.vmc/tokens.yml")
token = CFoundry::AuthToken.from_hash config[endpoint]
 
client = CFoundry::Client.new endpoint, token
client.proxy = ARGV[2] if ARGV[2] # proxy as another user?
 
def is_folder(f)
return true if f.length == 1
return true if f[1] =~ /\/$/
false
end
 
app = client.app_by_name ARGV[0]
# start zip stream
zip_path = [app.name, '-', UUIDTools::UUID.random_create, '.zip'].join
 
folder_queue = app.files(ARGV[1] || '/')
retry_list = {}
 
Zip::ZipOutputStream.open(zip_path) do |zip|
 
next_file = folder_queue.pop
 
while next_file do
puts next_file.join
 
if is_folder(next_file)
app.files(next_file.join).each { |f| folder_queue.insert 0, f }
else
path = next_file.join
 
zip.put_next_entry([app.name, "/", path].join)
begin
zip.print app.file(path)
rescue
# file failed, come back for it later
puts "Couldn't get #{next_file.join}, moving to the back of the queue!"
retry_list[next_file.join] = 0 if retry_list[next_file.join].nil?
retry_list[next_file.join] += 1
folder_queue.insert 0, next_file if retry_list[next_file.join] < 5
end
end
 
next_file = folder_queue.pop
end
 
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.