Class: Embarista::S3sync

Inherits:
Object
  • Object
show all
Defined in:
lib/embarista/s3sync.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, opts = {}) ⇒ S3sync

Returns a new instance of S3sync.



7
8
9
10
11
12
# File 'lib/embarista/s3sync.rb', line 7

def initialize(root, opts={})
  @local_manifest_path = opts[:local_manifest_path]
  @remote_manifest_path = opts[:remote_manifest_path]
  @root = Pathname.new(root).expand_path
  @s3 = S3.new(opts.fetch(:bucket_name), root: root) 
end

Instance Attribute Details

#local_manifest_pathObject (readonly)

Returns the value of attribute local_manifest_path.



5
6
7
# File 'lib/embarista/s3sync.rb', line 5

def local_manifest_path
  @local_manifest_path
end

#remote_manifest_pathObject (readonly)

Returns the value of attribute remote_manifest_path.



5
6
7
# File 'lib/embarista/s3sync.rb', line 5

def remote_manifest_path
  @remote_manifest_path
end

#s3Object (readonly)

Returns the value of attribute s3.



5
6
7
# File 'lib/embarista/s3sync.rb', line 5

def s3
  @s3
end

Class Method Details

.sync(root, opts = {}) ⇒ Object



14
15
16
# File 'lib/embarista/s3sync.rb', line 14

def self.sync(root, opts={})
  new(root, opts).sync
end

Instance Method Details

#build_delta_manifestObject



43
44
45
46
47
48
49
# File 'lib/embarista/s3sync.rb', line 43

def build_delta_manifest
  return local_manifest unless remote_manifest

  new_manifest_values = local_manifest.values - remote_manifest.values

  local_manifest.select {|key, value| new_manifest_values.include? value }
end

#local_manifestObject



62
63
64
# File 'lib/embarista/s3sync.rb', line 62

def local_manifest
  @local_manifest ||= YAML.load_file(@root + local_manifest_path)
end

#local_manifest_file_nameObject



39
40
41
# File 'lib/embarista/s3sync.rb', line 39

def local_manifest_file_name
  File.basename(local_manifest_path)
end

#remote_manifestObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/embarista/s3sync.rb', line 51

def remote_manifest
  @remote_manifest ||= begin
    if remote_manifest = s3.read(remote_manifest_file_name)
      YAML.load(remote_manifest)
    else
      puts 'no remote existing manifest, uploading everything'
      nil
    end
  end
end

#remote_manifest_file_nameObject



35
36
37
# File 'lib/embarista/s3sync.rb', line 35

def remote_manifest_file_name
  File.basename(remote_manifest_path)
end

#syncObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/embarista/s3sync.rb', line 18

def sync
  delta_manifest = build_delta_manifest

  if delta_manifest.empty?
    puts 'everything is up to date'
    return
  end

  delta_manifest.values.each do |file_name|
    file_name[0] = ''
    s3.store(file_name)
  end

  s3.store(remote_manifest_file_name, local_manifest_path)
  s3.store(local_manifest_file_name, local_manifest_path)
end