Class: Syncoku::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/syncoku/s3.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to_env) ⇒ S3

Returns a new instance of S3.



7
8
9
# File 'lib/syncoku/s3.rb', line 7

def initialize(to_env)
  @to_env = to_env
end

Instance Attribute Details

#from_bucketObject (readonly)

Returns the value of attribute from_bucket.



5
6
7
# File 'lib/syncoku/s3.rb', line 5

def from_bucket
  @from_bucket
end

#from_keysObject (readonly)

Returns the value of attribute from_keys.



5
6
7
# File 'lib/syncoku/s3.rb', line 5

def from_keys
  @from_keys
end

#from_nameObject (readonly)

Returns the value of attribute from_name.



5
6
7
# File 'lib/syncoku/s3.rb', line 5

def from_name
  @from_name
end

#to_bucketObject (readonly)

Returns the value of attribute to_bucket.



5
6
7
# File 'lib/syncoku/s3.rb', line 5

def to_bucket
  @to_bucket
end

#to_envObject (readonly)

Returns the value of attribute to_env.



5
6
7
# File 'lib/syncoku/s3.rb', line 5

def to_env
  @to_env
end

#to_keysObject (readonly)

Returns the value of attribute to_keys.



5
6
7
# File 'lib/syncoku/s3.rb', line 5

def to_keys
  @to_keys
end

#to_nameObject (readonly)

Returns the value of attribute to_name.



5
6
7
# File 'lib/syncoku/s3.rb', line 5

def to_name
  @to_name
end

Class Method Details

.config?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/syncoku/s3.rb', line 87

def self.config?
  File.exist?("syncoku.yml")
end

Instance Method Details

#configObject



83
84
85
# File 'lib/syncoku/s3.rb', line 83

def config
  @config ||= YAML.load(File.read("syncoku.yml"))
end

#config_value(path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/syncoku/s3.rb', line 71

def config_value(path)
  value = config.dup["s3"]
  path.split(".").each do |name|
    value = value[name]
    if value.nil?
      @missing << path
      return nil
    end
  end
  value
end

#get_keysObject



34
35
36
37
38
39
40
41
# File 'lib/syncoku/s3.rb', line 34

def get_keys
  @from_keys = from_bucket.objects.map(&:key)
  @to_keys = to_bucket.objects.map(&:key)
  true
rescue AWS::S3::Errors::SignatureDoesNotMatch => e
  puts "Can't sync S3 because #{e.message.sub(/^T/, 't')}"
  false
end

#remove_spareObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/syncoku/s3.rb', line 57

def remove_spare
  spare = to_keys - from_keys
  return false if spare.empty?
  puts "On #{to_name} but not on #{from_name}:   #{"%7d" % spare.size}"
  puts "Deleting from #{to_name}"
  spare.each do |key|
    to_bucket.objects[key].delete
    print "."
    STDOUT.flush
  end
  puts " done"
  true
end

#simple_syncObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/syncoku/s3.rb', line 43

def simple_sync
  missing = from_keys - to_keys
  return false if missing.empty?
  puts "On #{from_name} but not on #{to_name}: #{"%7d" % missing.size}"
  puts "Copying to #{to_name}"
  missing.each do |key|
    to_bucket.objects[key].copy_from key, { bucket: from_bucket, acl: :public_read}
    print "."
    STDOUT.flush
  end
  puts " done"
  true
end

#sync(args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/syncoku/s3.rb', line 11

def sync(args)
  @missing = []
  @from_name = config_value "production.bucket"
  @to_name = config_value "#{to_env}.bucket"
  access_key_id = config_value "access_key_id"
  secret_access_key = config_value "secret_access_key"
  if @missing.any?
    puts "Missing syncoku.yml values prevented S3 sync"
    @missing.each do |path|
      puts "  s3.#{path}"
    end
    return
  end
  puts "Syncing S3 from #{from_name} to #{to_name}..."
  AWS.config access_key_id: access_key_id, secret_access_key: secret_access_key
  @from_bucket = AWS::S3.new.buckets[from_name]
  @to_bucket = AWS::S3.new.buckets[to_name]
  return unless get_keys
  if !simple_sync & !remove_spare
    puts "S3 is in sync. Nothing to do :)"
  end
end