Class: Syncoku::S3
- Inherits:
-
Object
- Object
- Syncoku::S3
- Defined in:
- lib/syncoku/s3.rb
Instance Attribute Summary collapse
-
#from_bucket ⇒ Object
readonly
Returns the value of attribute from_bucket.
-
#from_keys ⇒ Object
readonly
Returns the value of attribute from_keys.
-
#from_name ⇒ Object
readonly
Returns the value of attribute from_name.
-
#to_bucket ⇒ Object
readonly
Returns the value of attribute to_bucket.
-
#to_env ⇒ Object
readonly
Returns the value of attribute to_env.
-
#to_keys ⇒ Object
readonly
Returns the value of attribute to_keys.
-
#to_name ⇒ Object
readonly
Returns the value of attribute to_name.
Class Method Summary collapse
Instance Method Summary collapse
- #config ⇒ Object
- #config_value(path) ⇒ Object
- #get_keys ⇒ Object
-
#initialize(to_env) ⇒ S3
constructor
A new instance of S3.
- #remove_spare ⇒ Object
- #simple_sync ⇒ Object
- #sync(args) ⇒ Object
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_bucket ⇒ Object (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_keys ⇒ Object (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_name ⇒ Object (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_bucket ⇒ Object (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_env ⇒ Object (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_keys ⇒ Object (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_name ⇒ Object (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
87 88 89 |
# File 'lib/syncoku/s3.rb', line 87 def self.config? File.exist?("syncoku.yml") end |
Instance Method Details
#config ⇒ Object
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_keys ⇒ Object
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_spare ⇒ Object
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_sync ⇒ Object
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 |