Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issues #16, #17, #18 and #19 #20

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Invalid < StandardError; end
attr_accessor :aws_region
attr_accessor :existing_remote_files
attr_accessor :gzip_compression
attr_accessor :manifest

validates :aws_access_key, :presence => true
validates :aws_access_secret, :presence => true
Expand All @@ -21,6 +22,7 @@ def initialize
self.aws_region = nil
self.existing_remote_files = 'keep'
self.gzip_compression = false
self.manifest = nil
load_yml! if yml_exists?
end

Expand Down Expand Up @@ -52,6 +54,7 @@ def load_yml!
self.aws_region = yml["aws_region"] if yml.has_key?("aws_region")
self.existing_remote_files = yml["existing_remote_files"] if yml.has_key?("existing_remote_files")
self.gzip_compression = yml["gzip_compression"] if yml.has_key?("gzip_compression")
self.manifest = yml["manifest"] if yml.has_key?("manifest")

# TODO deprecate old style config settings
self.aws_access_key = yml["access_key_id"] if yml.has_key?("access_key_id")
Expand Down
33 changes: 26 additions & 7 deletions lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def connection
end

def bucket
@bucket ||= connection.directories.get(self.config.aws_bucket)
# fixes: https://github.com/rumblelabs/asset_sync/issues/18
@bucket ||= connection.directories.get(self.config.aws_bucket, :prefix => 'assets')
end

def keep_existing_remote_files?
Expand All @@ -26,12 +27,27 @@ def path
end

def local_files
Dir["#{path}/assets/**/**"].map { |f| f[path.length+1,f.length-path.length] }
@local_files ||= get_local_files
end

def get_local_files
if self.config.manifest
path = File.join(self.config.manifest, 'manifest.yml')
if File.exists?(path)
yml = YAML.load(IO.read(path))
return yml.values.map { |f| File.join('assets', f) }
end
end
Dir["#{path}/assets/**/**"].map { |f| f[path.length+1,f.length-path.length] }
end

def get_remote_files
raise BucketNotFound.new("AWS Bucket: #{self.config.aws_bucket} not found.") unless bucket
return bucket.files.map { |f| f.key }
# fixes: https://github.com/rumblelabs/asset_sync/issues/16
# (work-around for https://github.com/fog/fog/issues/596)
files = []
bucket.files.each { |f| files << f.key }
return files
end

def delete_file(f, remote_files_to_delete)
Expand All @@ -44,7 +60,8 @@ def delete_file(f, remote_files_to_delete)
def delete_extra_remote_files
STDERR.puts "Fetching files to flag for delete"
remote_files = get_remote_files
from_remote_files_to_delete = (local_files | remote_files) - (local_files & remote_files)
# fixes: https://github.com/rumblelabs/asset_sync/issues/19
from_remote_files_to_delete = remote_files - local_files

STDERR.puts "Flagging #{from_remote_files_to_delete.size} file(s) for deletion"
# Delete unneeded remote files
Expand Down Expand Up @@ -99,7 +116,8 @@ def upload_file(f)
def upload_files
# get a fresh list of remote files
remote_files = get_remote_files
local_files_to_upload = (remote_files | local_files) - (remote_files & local_files)
# fixes: https://github.com/rumblelabs/asset_sync/issues/19
local_files_to_upload = local_files - remote_files

# Upload new files
local_files_to_upload.each do |f|
Expand All @@ -109,10 +127,11 @@ def upload_files
end

def sync
delete_extra_remote_files unless keep_existing_remote_files?
# fixes: https://github.com/rumblelabs/asset_sync/issues/19
upload_files
delete_extra_remote_files unless keep_existing_remote_files?
STDERR.puts "Done."
end

end
end
end