Skip to content

Instantly share code, notes, and snippets.

@danhigham
Last active October 27, 2018 15:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save danhigham/4705713 to your computer and use it in GitHub Desktop.
Save danhigham/4705713 to your computer and use it in GitHub Desktop.
Simple ruby script to download the contents of an application running on Cloud Foundry to a zip file
#!/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
source :rubygems
gem 'cfoundry'
gem 'uuidtools'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment