Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#remote_map(remote_data, context) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/capistrano/s3-mirror.rb', line 111

def remote_map (remote_data, context)
  remote_data.map do |object|
    cur_path = object.path.split("/")
    
    if cur_path[0] == ""
      cur_path = cur_path.drop(1)
    end
    
    cur_context = context.clone
    
    cur_path = cur_path.drop_while do |element|
      element == cur_context.shift
    end
    
    if cur_context.count > 0
      abort "file error: didn't fit the bucket/base/path description, offending path: #{object.path}"
    end
    
    [File.join(cur_path), object]
  end
end

#sync(key_id, secret_access_key, bucket_name, base_path, s3_region_endpoint) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/capistrano/s3-mirror.rb', line 54

def sync (key_id, secret_access_key, bucket_name, base_path, s3_region_endpoint)
  AWS::S3::DEFAULT_HOST.replace(s3_region_endpoint)
  AWS::S3::Base.establish_connection!(:access_key_id => key_id, :secret_access_key => secret_access_key)
  
  bucket = AWS::S3::Bucket.find(bucket_name)
  
  #/bucket_name/path/to/File
  #/path/to/file
  
  context = File.join(bucket_name, base_path).split("/")
  
  # filter out directories
  remote_data = bucket.objects.select{ |object| object.content_type != "application/x-directory"}
  # remove the bucket name from the path
  remote_data = remote_map(remote_data, context)
  remote = Hash[remote_data]
  
  local_data = Dir.chdir("public") do
    # map local files to path => md5(file)
    Dir["**/**"].select{ |d| !File.directory? d }.map { |path| [ path, Digest::MD5.file(path).hexdigest ] }
  end
  local = Hash[local_data]
  
  new = local.keys - remote.keys
  deleted = remote.keys - local.keys
  both = local.keys & remote.keys
  
  uploaded_count = 0
  deleted_count = 0
  updated_count = 0
  
  Dir.chdir("public") do
    new.each do |entry|
      AWS::S3::S3Object.store(entry, open(entry), bucket_name, :access => :public_read)
      uploaded_count += 1
    end
    
    deleted.each do |entry|
      object = AWS::S3::S3Object.find(entry, bucket_name)
      object.delete
      
      deleted_count += 1
    end
    
    both.each do |entry|
      if local[entry] != remote[entry].etag
        AWS::S3::S3Object.store(entry, open(entry), bucket_name)
        
        updated_count += 1
      end
    end
  end

  puts "uploaded #{uploaded_count}, deleted #{deleted_count}, updated #{updated_count}"
  
end