syncassets_r3 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- This rake task will update (delete and copy) all the files under /public on s3, be patient
1
+ This rake task will update (delete and copy) all the files under the public directory to S3, by default is the public directory but you can pass as argument the path to the folder inside the public directory
2
2
 
3
3
  The rake task will verify if the local file exist on s3 and
4
4
  if the local file have been modified since the upload to s3.
@@ -8,7 +8,26 @@ if the local file have been modified since the upload to s3.
8
8
  In your Gemfile
9
9
  gem 'syncassets_r3'
10
10
 
11
- In your config/S3.yml
12
- aws_access_key: YOUR_ACCESS_KEY
13
- aws_secret_access_key: YOUR_SECRET_ACCESS_KEY
14
- aws_bucket_name: YOUR_BUCKET_NAME
11
+ In your config/aws.yml
12
+ development:
13
+ access_key_id: 'YOUR_ACCESS_KEY'
14
+ secret_access_key: 'YOUR_SECRET_ACCESS_KEY'
15
+ bucket: 'YOUR_BUCKET_NAME'
16
+ test:
17
+ access_key_id: 'YOUR_ACCESS_KEY'
18
+ secret_access_key: 'YOUR_SECRET_ACCESS_KEY'
19
+ bucket: 'YOUR_BUCKET_NAME'
20
+ production:
21
+ access_key_id: 'YOUR_ACCESS_KEY'
22
+ secret_access_key: 'YOUR_SECRET_ACCESS_KEY'
23
+ bucket: 'YOUR_BUCKET_NAME'
24
+
25
+ Running the task
26
+ rake syncassets:sync_s3_public_assets
27
+ # this will sync all the folders and files under the /public directory of your app
28
+
29
+ rake syncassets:sync_s3_public_assets[javascripts]
30
+ # this will sync all the folders and files under the /public/javascripts directory of your app
31
+
32
+ rake syncassets:sync_s3_public_assets[javascripts/some_folder]
33
+ # this will sync all the folders and files under the /public/javascripts/some_folder directory of your app
data/Rakefile CHANGED
@@ -15,8 +15,8 @@ Jeweler::Tasks.new do |gem|
15
15
  gem.name = "syncassets_r3"
16
16
  gem.homepage = "http://github.com/uokesita/syncassets_r3"
17
17
  gem.license = "MIT"
18
- gem.summary = %Q{This rake task will update (delete and copy) all the files under /public on s3, be patient}
19
- gem.description = %Q{This rake task will update (delete and copy) all the files under /public on s3, be patient}
18
+ gem.summary = %Q{This rake task will update (delete and copy) all the files under the public directory to S3, by default is the public directory but you can pass as argument the path to the folder inside the public directory}
19
+ gem.description = %Q{This rake task will update (delete and copy) all the files under the public directory to S3, by default is the public directory but you can pass as argument the path to the folder inside the public directory}
20
20
  gem.email = "osledybazo@gmail.com"
21
21
  gem.authors = ["Osledy Bazo"]
22
22
  # Include your dependencies below. Runtime dependencies are required when using your gem,
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.1.0
data/lib/syncassets_r3.rb CHANGED
@@ -1 +1,43 @@
1
1
  require 'railtie' if defined?(Rails)
2
+
3
+ class Auth
4
+ def self.root
5
+ rails_root = Rails.root
6
+ YAML::load(IO.read(File.join(rails_root, 'config', 'aws.yml')))
7
+ end
8
+ def self.env
9
+ rails_env = Rails.env
10
+ end
11
+ end
12
+
13
+
14
+ class Credentials
15
+ def initialize
16
+ # TRIED USING THE INITIALIZE FOR THOSE YAML LOADING DOWN THERE
17
+ # BUT IT WAS GIVING ME CRAP AND HAD TO DUPLICATE THE LINE
18
+ # MY GUEST IS THAT IT IS B/C THEY ARE CLASS METHODS
19
+ # TODO: RESEARCH HOW TO REFACTOR OUT
20
+ end
21
+
22
+ begin
23
+ def self.key
24
+ Auth.root[Auth.env]['access_key_id']
25
+ end
26
+ def self.secret
27
+ Auth.root[Auth.env]['secret_access_key']
28
+ end
29
+ def self.bucket
30
+ Auth.root[Auth.env]['bucket']
31
+ end
32
+ def self.distribution_ids
33
+ unless Auth.root[Auth.env]['distribution_ids'].nil?
34
+ return Auth.root[Auth.env]['distribution_ids'].gsub(' ','').split(',')
35
+ end
36
+ []
37
+ end
38
+ rescue
39
+ puts"syncassets_r3 : AWS Access Key Id needs a subscription for the service."
40
+ end
41
+ end
42
+
43
+
@@ -1,24 +1,50 @@
1
1
  require 'fog'
2
+
2
3
  namespace :syncassets do
3
4
 
4
- desc "Synchronize public folder with s3"
5
- task :sync_s3_public_assets do
5
+ desc "This rake task will update (delete and copy) all the files under the public directory to S3, by default is the public directory but you can pass as argument the path to the folder inside the public directory"
6
+ task :sync_s3_public_assets, :directory do |t, args|
6
7
  puts "#########################################################"
7
- puts "## This rake task will update (delete and copy) "
8
- puts "## all the files under /public on s3, be patient "
8
+ puts "## Syncing folders and files with S3 ##"
9
9
  puts "#########################################################"
10
10
 
11
- @settings = YAML.load_file(File.join(Rails.root, 'config', 's3.yml'))
12
11
  @fog = Fog::Storage.new( :provider => 'AWS',
13
- :aws_access_key_id => @settings['aws_access_key'],
14
- :aws_secret_access_key => @settings['aws_secret_access_key'],
12
+ :aws_access_key_id => Credentials.key,
13
+ :aws_secret_access_key => Credentials.secret,
15
14
  :persistent => false )
16
- @directory = @fog.directories.create( :key => @settings['aws_bucket_name'] )
17
15
 
16
+ @directory = @fog.directories.create( :key => Credentials.bucket )
17
+
18
+ @files_for_invalidation = []
19
+ @distribution_ids = []
20
+ @root_directory = "#{args[:directory]}"
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 )
19
31
  end
20
32
 
21
- def upload_directory(asset='/')
33
+ def get_distribution_ids
34
+ get_cdn_connection
35
+
36
+ if Credentials.distribution_ids.empty?
37
+ distributions = @cdn.get_distribution_list()
38
+ distributions.body["DistributionSummary"].each do |distribution|
39
+ @distribution_ids << distribution["Id"]
40
+ end
41
+ else
42
+ @distribution_ids = Credentials.distribution_ids
43
+ end
44
+ end
45
+
46
+ def upload_directory(asset = @root_directory || '/')
47
+
22
48
  Dir.entries(File.join(Rails.root, 'public', asset)).each do |file|
23
49
  next if file =~ /\A\./
24
50
 
@@ -31,13 +57,20 @@ namespace :syncassets do
31
57
  end
32
58
 
33
59
  def upload_file asset, file
34
- file_name = asset == "/" ? file : "#{asset}/#{file}".sub('/','')
60
+
61
+ if @root_directory.blank?
62
+ file_name = asset == "/" ? file : "#{asset}/#{file}".sub('/','')
63
+ else
64
+ file_name = asset == "/" ? file : "#{asset}/#{file}"
65
+ end
66
+
35
67
  remote_file = get_remote_file(file_name)
36
68
 
37
69
  if check_timestamps(file_name, remote_file)
38
70
  destroy_file(remote_file)
39
71
  file_u = @directory.files.create(:key => "#{file_name}", :body => open(File.join(Rails.root, 'public', asset, file )), :public => true )
40
- puts "copied #{file_name}"
72
+ queue_file_for_invalidation(asset, file)
73
+ puts "Copied: #{file_name}"
41
74
  end
42
75
  end
43
76
 
@@ -46,7 +79,7 @@ namespace :syncassets do
46
79
  end
47
80
 
48
81
  def check_timestamps local_file, remote_file
49
- puts "verifing file: #{local_file}"
82
+ puts "Verifing file: #{local_file}"
50
83
  local = File.mtime(File.join(Rails.root, 'public', local_file))
51
84
  unless remote_file.nil?
52
85
  return local > remote_file.last_modified
@@ -57,7 +90,32 @@ namespace :syncassets do
57
90
  def destroy_file remote_file
58
91
  unless remote_file.nil?
59
92
  remote_file.destroy
60
- puts "delete on s3 #{remote_file.key}"
93
+ puts "Delete on s3: #{remote_file.key}"
94
+ end
95
+ end
96
+
97
+ def queue_file_for_invalidation asset, file
98
+ if @root_directory.blank?
99
+ path_to_file = asset == "/" ? "#{asset}#{file}" : "#{asset}/#{file}"
100
+ else
101
+ path_to_file = asset == "/" ? "/#{asset}#{file}" : "/#{asset}/#{file}"
102
+ end
103
+ @files_for_invalidation << path_to_file
104
+ puts "Queued for invalidation: #{path_to_file}"
105
+ if @files_for_invalidation.size == 200
106
+ invalidate_files
107
+ end
108
+ end
109
+
110
+ def invalidate_files
111
+ unless @files_for_invalidation.size < 1
112
+ get_cdn_connection
113
+ @distribution_ids.each do |id|
114
+ puts "Invalidating files of distribution #{id}"
115
+ @cdn.post_invalidation(id, @files_for_invalidation, caller_reference = Time.now.to_i.to_s)
116
+ puts "Invalidation list queued"
117
+ end
118
+ @files_for_invalidation.clear
61
119
  end
62
120
  end
63
121
 
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{syncassets_r3}
8
- s.version = "0.0.0"
8
+ s.version = "0.1.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-03}
13
- s.description = %q{This rake task will update (delete and copy) all the files under /public on s3, be patient}
12
+ s.date = %q{2011-09-15}
13
+ s.description = %q{This rake task will update (delete and copy) all the files under the public directory to S3, by default is the public directory but you can pass as argument the path to the folder inside the public directory}
14
14
  s.email = %q{osledybazo@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
@@ -36,15 +36,14 @@ Gem::Specification.new do |s|
36
36
  s.homepage = %q{http://github.com/uokesita/syncassets_r3}
37
37
  s.licenses = ["MIT"]
38
38
  s.require_paths = ["lib"]
39
- s.rubygems_version = %q{1.3.7}
40
- s.summary = %q{This rake task will update (delete and copy) all the files under /public on s3, be patient}
39
+ s.rubygems_version = %q{1.4.2}
40
+ s.summary = %q{This rake task will update (delete and copy) all the files under the public directory to S3, by default is the public directory but you can pass as argument the path to the folder inside the public directory}
41
41
  s.test_files = [
42
42
  "test/helper.rb",
43
43
  "test/test_syncassets_r3.rb"
44
44
  ]
45
45
 
46
46
  if s.respond_to? :specification_version then
47
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
47
  s.specification_version = 3
49
48
 
50
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syncassets_r3
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
4
+ hash: 27
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 0.0.0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Osledy Bazo
@@ -15,13 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-03 00:00:00 -04:30
18
+ date: 2011-09-15 00:00:00 -04:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- prerelease: false
23
22
  type: :development
24
- name: shoulda
25
23
  version_requirements: &id001 !ruby/object:Gem::Requirement
26
24
  none: false
27
25
  requirements:
@@ -32,10 +30,10 @@ dependencies:
32
30
  - 0
33
31
  version: "0"
34
32
  requirement: *id001
35
- - !ruby/object:Gem::Dependency
36
33
  prerelease: false
34
+ name: shoulda
35
+ - !ruby/object:Gem::Dependency
37
36
  type: :development
38
- name: bundler
39
37
  version_requirements: &id002 !ruby/object:Gem::Requirement
40
38
  none: false
41
39
  requirements:
@@ -48,10 +46,10 @@ dependencies:
48
46
  - 0
49
47
  version: 1.0.0
50
48
  requirement: *id002
51
- - !ruby/object:Gem::Dependency
52
49
  prerelease: false
50
+ name: bundler
51
+ - !ruby/object:Gem::Dependency
53
52
  type: :development
54
- name: jeweler
55
53
  version_requirements: &id003 !ruby/object:Gem::Requirement
56
54
  none: false
57
55
  requirements:
@@ -64,10 +62,10 @@ dependencies:
64
62
  - 2
65
63
  version: 1.5.2
66
64
  requirement: *id003
67
- - !ruby/object:Gem::Dependency
68
65
  prerelease: false
66
+ name: jeweler
67
+ - !ruby/object:Gem::Dependency
69
68
  type: :development
70
- name: rcov
71
69
  version_requirements: &id004 !ruby/object:Gem::Requirement
72
70
  none: false
73
71
  requirements:
@@ -78,7 +76,9 @@ dependencies:
78
76
  - 0
79
77
  version: "0"
80
78
  requirement: *id004
81
- description: This rake task will update (delete and copy) all the files under /public on s3, be patient
79
+ prerelease: false
80
+ name: rcov
81
+ description: This rake task will update (delete and copy) all the files under the public directory to S3, by default is the public directory but you can pass as argument the path to the folder inside the public directory
82
82
  email: osledybazo@gmail.com
83
83
  executables: []
84
84
 
@@ -133,10 +133,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  requirements: []
134
134
 
135
135
  rubyforge_project:
136
- rubygems_version: 1.3.7
136
+ rubygems_version: 1.4.2
137
137
  signing_key:
138
138
  specification_version: 3
139
- summary: This rake task will update (delete and copy) all the files under /public on s3, be patient
139
+ summary: This rake task will update (delete and copy) all the files under the public directory to S3, by default is the public directory but you can pass as argument the path to the folder inside the public directory
140
140
  test_files:
141
141
  - test/helper.rb
142
142
  - test/test_syncassets_r3.rb