Class: S3Ranger::SyncCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/s3ranger/sync.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, source, destination) ⇒ SyncCommand

Returns a new instance of SyncCommand.



120
121
122
123
124
# File 'lib/s3ranger/sync.rb', line 120

def initialize args, source, destination
  @args = args
  @source = source
  @destination = destination
end

Class Method Details

.cmp(list1, list2) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/s3ranger/sync.rb', line 98

def SyncCommand.cmp list1, list2
  l1 = {}; list1.each {|e| l1[e.path] = e}
  l2 = {}; list2.each {|e| l2[e.path] = e}

  same, to_add_to_2, to_remove_from_2 = [], [], []

  l1.each do |key, value|
    value2 = l2.delete key
    if value2.nil?
      to_add_to_2 << value
    elsif value2.size == value.size
      same << value
    else
      to_add_to_2 << value
    end
  end

  to_remove_from_2 = l2.values

  [same, to_add_to_2, to_remove_from_2]
end

.parse_params(args) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/s3ranger/sync.rb', line 157

def SyncCommand.parse_params args
  # Reading the arbitrary parameters from the command line and getting
  # modifiable copies to parse
  source, destination = args; return nil if source.nil? or destination.nil?

  # Sync from one s3 to another is currently not supported
  if SyncCommand.remote_prefix? source and SyncCommand.remote_prefix? destination
    raise WrongUsage.new(nil, 'Both arguments can\'t be on S3')
  end

  # C'mon, there's rsync out there
  if !SyncCommand.remote_prefix? source and !SyncCommand.remote_prefix? destination
    raise WrongUsage.new(nil, 'One argument must be on S3')
  end

  source, destination = SyncCommand.process_destination source, destination
  return [Location.new(*source), Location.new(*destination)]
end

.process_destination(source, destination) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/s3ranger/sync.rb', line 211

def SyncCommand.process_destination source, destination
  source, destination = source.dup, destination.dup

  # don't repeat slashes
  source.squeeze! '/'
  destination.squeeze! '/'

  # Making sure that local paths won't break our stuff later
  source.gsub! /^\.\//, ''
  destination.gsub! /^\.\//, ''

  # Parsing the final destination
  destination = SyncCommand.process_file_destination source, destination, ""

  # here's where we find out what direction we're going
  source_is_s3 = remote_prefix? source

  # alias these variables to the other strings (in ruby = does not make
  # copies of strings)
  remote_prefix = source_is_s3 ? source : destination
  local_prefix = source_is_s3 ? destination : source

  # canonicalize the S3 stuff
  bucket, remote_prefix = remote_prefix.split ":"
  remote_prefix ||= ""

  # Just making sure we preserve the direction
  if source_is_s3
    [[remote_prefix, bucket], destination]
  else
    [source, [remote_prefix, bucket]]
  end
end

.process_file_destination(source, destination, file = "") ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/s3ranger/sync.rb', line 182

def SyncCommand.process_file_destination source, destination, file=""
  if not file.empty?
    sub = (remote_prefix? source) ? source.split(":")[1] : source
    file = file.gsub /^#{sub}/, ''
  end

  # no slash on end of source means we need to append the last src dir to
  # dst prefix testing for empty isn't good enough here.. needs to be
  # "empty apart from potentially having 'bucket:'"
  if source =~ %r{/$}
    File.join [destination, file]
  else
    if remote_prefix? source
      _, name = source.split ":"
      File.join [destination, File.basename(name || ""), file]
    else
      source = /^\/?(.*)/.match(source)[1]

      # Corner case: the root of the remote path is empty, we don't want to
      # add an unnecessary slash here.
      if destination.end_with? ':'
        File.join [destination + source, file]
      else
        File.join [destination, source, file]
      end
    end
  end
end

.remote_prefix?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
180
# File 'lib/s3ranger/sync.rb', line 176

def SyncCommand.remote_prefix?(prefix)
  # allow for dos-like things e.g. C:\ to be treated as local even with
  # colon.
  prefix.include? ':' and not prefix.match '^[A-Za-z]:[\\\\/]'
end

Instance Method Details

#download_files(destination, source, list) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/s3ranger/sync.rb', line 300

def download_files destination, source, list
  list.each {|e|
    path = File.join destination.path, e.path

    if @args.verbose
      puts " + #{source}#{e.path} => #{path}"
    end

    unless @args.dry_run
      obj = @args.s3.buckets[source.bucket].objects[e.path]

      # Making sure this new file will have a safe shelter
      FileUtils.mkdir_p File.dirname(path)

      # Downloading and saving the files
      File.open(path, 'wb') do |file|
        obj.read do |chunk|
          file.write chunk
        end
      end
    end
  }
end

#read_tree_remote(location) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/s3ranger/sync.rb', line 245

def read_tree_remote location
  begin
    dir = location.path
    dir += '/' if not (dir.empty? or dir.end_with? '/')
    @args.s3.buckets[location.bucket].objects.with_prefix(dir || "").to_a.collect {|obj|
      Node.new location.path, obj.key, obj.content_length
    }
  rescue AWS::S3::Errors::NoSuchBucket
    raise FailureFeedback.new("There's no bucket named `#{location.bucket}'")
  rescue AWS::S3::Errors::NoSuchKey
    raise FailureFeedback.new("There's no key named `#{location.path}' in the bucket `#{location.bucket}'")
  rescue AWS::S3::Errors::AccessDenied
    raise FailureFeedback.new("Access denied")
  end
end

#read_trees(source, destination) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/s3ranger/sync.rb', line 261

def read_trees source, destination
  if source.local?
    source_tree = LocalDirectory.new(source.path).list_files
    destination_tree = read_tree_remote destination
  else
    source_tree = read_tree_remote source
    destination_tree = LocalDirectory.new(destination.path).list_files
  end

  [source_tree, destination_tree]
end

#remove_files(remote, list) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/s3ranger/sync.rb', line 287

def remove_files remote, list

  if @args.verbose
    list.each {|e|
      puts " - #{remote}#{e.path}"
    }
  end

  unless @args.dry_run
    @args.s3.buckets[remote.bucket].objects.delete_if { |obj| list.include? obj.key }
  end
end

#remove_local_files(destination, source, list) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/s3ranger/sync.rb', line 324

def remove_local_files destination, source, list
  list.each {|e|
    path = File.join destination.path, e.path

    if @args.verbose
      puts " * #{e.path} => #{path}"
    end

    unless @args.dry_run
      FileUtils.rm_rf path
    end
  }
end

#runObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/s3ranger/sync.rb', line 126

def run
  # Reading the source and destination using our helper method
  if (source, destination, bucket = SyncCommand.parse_params [@source, @destination]).nil?
    raise WrongUsage.new(nil, 'Need a source and a destination')
  end

  # Getting the trees
  source_tree, destination_tree = read_trees source, destination

  # Getting the list of resources to be exchanged between the two peers
  _, to_add, to_remove = SyncCommand.cmp source_tree, destination_tree

  # Removing the items matching the exclude pattern if requested
  to_add.select! { |e|
    begin
      (e.path =~ /#{@args.exclude}/).nil?
    rescue RegexpError => exc
      raise WrongUsage.new nil, exc.message
    end
  } if @args.exclude

  # Calling the methods that perform the actual IO
  if source.local?
    upload_files destination, to_add
    remove_files destination, to_remove unless @args.keep
  else
    download_files destination, source, to_add
    remove_local_files destination, source, to_remove unless @args.keep
  end
end

#upload_files(remote, list) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/s3ranger/sync.rb', line 273

def upload_files remote, list
  list.each do |e|
    if @args.verbose
      puts " + #{e.full} => #{remote}#{e.path}"
    end

    unless @args.dry_run
      if File.file? e.path
        @args.s3.buckets[remote.bucket].objects[e.path].write Pathname.new e.path
      end
    end
  end
end