syncassets_r2 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -4
- data/VERSION +1 -1
- data/lib/syncassets_r2.rb +34 -0
- data/lib/tasks/syncassets_r2.rake +45 -7
- data/syncassets_r2.gemspec +2 -2
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -11,7 +11,16 @@ In your Gemfile
|
|
11
11
|
In your Rakefile
|
12
12
|
Dir["#{Gem.searcher.find('sync-s3-gem').full_gem_path}/lib/tasks/**/*.rake"].each { |ext| load ext }
|
13
13
|
|
14
|
-
In your config/
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
In your config/aws.yml
|
15
|
+
development:
|
16
|
+
access_key_id: 'YOUR_ACCESS_KEY'
|
17
|
+
secret_access_key: 'YOUR_SECRET_ACCESS_KEY'
|
18
|
+
bucket: 'YOUR_BUCKET_NAME'
|
19
|
+
test:
|
20
|
+
access_key_id: 'YOUR_ACCESS_KEY'
|
21
|
+
secret_access_key: 'YOUR_SECRET_ACCESS_KEY'
|
22
|
+
bucket: 'YOUR_BUCKET_NAME'
|
23
|
+
production:
|
24
|
+
access_key_id: 'YOUR_ACCESS_KEY'
|
25
|
+
secret_access_key: 'YOUR_SECRET_ACCESS_KEY'
|
26
|
+
bucket: 'YOUR_BUCKET_NAME'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/syncassets_r2.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
class Auth
|
2
|
+
def self.root
|
3
|
+
rails_root = (Rails.version < "2.1.2") ? RAILS_ROOT : Rails.root
|
4
|
+
YAML::load(IO.read(File.join(rails_root, 'config', 'aws.yml')))
|
5
|
+
end
|
6
|
+
def self.env
|
7
|
+
rails_env = (Rails.version < "2.1.2") ? RAILS_ENV : Rails.env
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
class Credentials
|
13
|
+
def initialize
|
14
|
+
# TRIED USING THE INITIALIZE FOR THOSE YAML LOADING DOWN THERE
|
15
|
+
# BUT IT WAS GIVING ME CRAP AND HAD TO DUPLICATE THE LINE
|
16
|
+
# MY GUEST IS THAT IT IS B/C THEY ARE CLASS METHODS
|
17
|
+
# TODO: RESEARCH HOW TO REFACTOR OUT
|
18
|
+
end
|
19
|
+
|
20
|
+
begin
|
21
|
+
def self.key
|
22
|
+
Auth.root[Auth.env]['access_key_id']
|
23
|
+
end
|
24
|
+
def self.secret
|
25
|
+
Auth.root[Auth.env]['secret_access_key']
|
26
|
+
end
|
27
|
+
def self.bucket
|
28
|
+
Auth.root[Auth.env]['bucket']
|
29
|
+
end
|
30
|
+
rescue
|
31
|
+
puts"syncassets_r3 : AWS Access Key Id needs a subscription for the service."
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'fog'
|
2
|
+
require 'syncassets_r2'
|
2
3
|
namespace :syncassets do
|
3
4
|
|
4
5
|
desc "Synchronize public folder with s3"
|
@@ -8,14 +9,34 @@ namespace :syncassets do
|
|
8
9
|
puts "## all the files under /public on s3, be patient "
|
9
10
|
puts "#########################################################"
|
10
11
|
|
11
|
-
@settings = YAML.load_file(File.join(Rails.root, 'config', 's3.yml'))
|
12
12
|
@fog = Fog::Storage.new( :provider => 'AWS',
|
13
|
-
:aws_access_key_id =>
|
14
|
-
:aws_secret_access_key =>
|
13
|
+
:aws_access_key_id => Credentials.key,
|
14
|
+
:aws_secret_access_key => Credentials.secret,
|
15
15
|
:persistent => false )
|
16
|
-
@directory = @fog.directories.create( :key => @settings['aws_bucket_name'] )
|
17
16
|
|
17
|
+
@directory = @fog.directories.create( :key => Credentials.bucket )
|
18
|
+
|
19
|
+
@files_for_invalidation = []
|
20
|
+
@distribution_ids = []
|
21
|
+
|
22
|
+
get_distribution_ids
|
18
23
|
upload_directory
|
24
|
+
invalidate_files
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_cdn_connection
|
28
|
+
@cdn = Fog::CDN.new( :provider => 'AWS',
|
29
|
+
:aws_access_key_id => Credentials.key,
|
30
|
+
:aws_secret_access_key => Credentials.secret )
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_distribution_ids
|
34
|
+
get_cdn_connection
|
35
|
+
|
36
|
+
distributions = @cdn.get_distribution_list()
|
37
|
+
distributions.body["DistributionSummary"].each do |distribution|
|
38
|
+
@distribution_ids << distribution["Id"]
|
39
|
+
end
|
19
40
|
end
|
20
41
|
|
21
42
|
def upload_directory(asset='/')
|
@@ -37,7 +58,8 @@ namespace :syncassets do
|
|
37
58
|
if check_timestamps(file_name, remote_file)
|
38
59
|
destroy_file(remote_file)
|
39
60
|
file_u = @directory.files.create(:key => "#{file_name}", :body => open(File.join(Rails.root, 'public', asset, file )), :public => true )
|
40
|
-
|
61
|
+
queue_file_for_invalidation(asset, file)
|
62
|
+
puts "Copied: #{file_name}"
|
41
63
|
end
|
42
64
|
end
|
43
65
|
|
@@ -46,7 +68,7 @@ namespace :syncassets do
|
|
46
68
|
end
|
47
69
|
|
48
70
|
def check_timestamps local_file, remote_file
|
49
|
-
puts "
|
71
|
+
puts "Verifing file: #{local_file}"
|
50
72
|
local = File.mtime(File.join(Rails.root, 'public', local_file))
|
51
73
|
unless remote_file.nil?
|
52
74
|
return local > remote_file.last_modified
|
@@ -57,7 +79,23 @@ namespace :syncassets do
|
|
57
79
|
def destroy_file remote_file
|
58
80
|
unless remote_file.nil?
|
59
81
|
remote_file.destroy
|
60
|
-
puts "
|
82
|
+
puts "Delete on s3: #{remote_file.key}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def queue_file_for_invalidation asset, file
|
87
|
+
path_to_file = asset == "/" ? "#{asset}#{file}" : "#{asset}/#{file}"
|
88
|
+
@files_for_invalidation << path_to_file
|
89
|
+
puts "Queued for invalidation: #{path_to_file}"
|
90
|
+
end
|
91
|
+
|
92
|
+
def invalidate_files
|
93
|
+
get_cdn_connection
|
94
|
+
|
95
|
+
@distribution_ids.each do |id|
|
96
|
+
puts "Invalidating files of distribution #{id}"
|
97
|
+
@cdn.post_invalidation(id, @files_for_invalidation, caller_reference = Time.now.to_i.to_s)
|
98
|
+
puts "Invalidation list queued"
|
61
99
|
end
|
62
100
|
end
|
63
101
|
|
data/syncassets_r2.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{syncassets_r2}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Osledy Bazo"]
|
12
|
-
s.date = %q{2011-05-
|
12
|
+
s.date = %q{2011-05-11}
|
13
13
|
s.description = %q{This rake task will update (delete and copy) all the files under /public on s3, be patient}
|
14
14
|
s.email = %q{osledybazo@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syncassets_r2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Osledy Bazo
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-11 00:00:00 -04:30
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|