Class: S3MetaSync::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_meta_sync/syncer.rb

Constant Summary collapse

DEFAULT_REGION =
'us-east-1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Syncer

Returns a new instance of Syncer.



22
23
24
# File 'lib/s3_meta_sync/syncer.rb', line 22

def initialize(config)
  @config = config
end

Class Method Details

.swap_in_directory(destination, dir) ⇒ Object

almost atomic when destination and temp dir are not on the same device



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/s3_meta_sync/syncer.rb', line 93

def self.swap_in_directory(destination, dir)
  next_dir = "#{destination}-next"
  delete = "#{destination}-delete"

  # clean up potential leftovers from last run
  FileUtils.remove_dir(next_dir) if File.exist?(next_dir)
  FileUtils.remove_dir(delete) if File.exist?(delete)

  # move onto the same device
  FileUtils.mv(dir, next_dir)

  # copy permissions
  FileUtils.chmod_R(File.stat(destination).mode, next_dir)

  # swap
  FileUtils.mv(destination, delete)
  FileUtils.mv(next_dir, destination)

  # cleanup old
  FileUtils.remove_dir(delete)
end

Instance Method Details

#sync(source, destination) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/s3_meta_sync/syncer.rb', line 26

def sync(source, destination)
  raise if source.end_with?("/") or destination.end_with?("/")

  if destination.include?(":")
    @bucket, destination = destination.split(":")
    upload(source, destination)
  else
    @bucket, source = source.split(":")
    download(source, destination)
  end
end