Created
February 6, 2015 23:51
-
-
Save danhigham/10a1aa198e8c3a16b27a to your computer and use it in GitHub Desktop.
Script to sync files between Riak CS buckets bound to a CF app to a single S3 account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'uri' | |
require 'bundler' | |
require 'fog' | |
Bundler.require | |
Excon.defaults[:ssl_verify_peer] = false | |
def create_aws_client() | |
key = ENV['AWS_KEY'] | |
secret = ENV['AWS_SECRET'] | |
fog_options = { | |
provider: 'AWS', | |
path_style: true, | |
aws_access_key_id: key, | |
aws_secret_access_key: secret | |
} | |
Fog::Storage.new(fog_options) | |
end | |
def blobstore_client(blobstore_service) | |
uri = URI(blobstore_service.fetch('uri')) | |
key = blobstore_service.fetch('access_key_id') | |
secret = blobstore_service.fetch('secret_access_key') | |
fog_options = { | |
provider: 'AWS', | |
path_style: true, | |
host: uri.host, | |
port: uri.port, | |
scheme: uri.scheme, | |
aws_access_key_id: key, | |
aws_secret_access_key: secret | |
} | |
Fog::Storage.new(fog_options) | |
end | |
def load_service_by_name(service_name) | |
services = JSON.parse(ENV['VCAP_SERVICES']) | |
services.values.each do |v| | |
v.each do |s| | |
if s["name"] == service_name\ | |
return s["credentials"] | |
end | |
end | |
end | |
raise "service with name #{service_name} not found in bound services" | |
end | |
def load_riak_services() | |
matches = [] | |
services = JSON.parse(ENV['VCAP_SERVICES']) | |
services.values.each do |v| | |
v.each do |s| | |
if s["label"] == "p-riakcs" | |
matches << s["credentials"] | |
end | |
end | |
end | |
matches | |
end | |
aws_client = create_aws_client | |
services = load_riak_services | |
services.each do |s| | |
client = blobstore_client(s) | |
bucket.files.each do |f| | |
aws_bucket.files.create :key => f.key, :body => f.body | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment