locomotive_jammit-s3 0.5.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Jacques Crocker
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'jammit-s3'
3
+
4
+ Jammit::S3CommandLine.new
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'locomotive_jammit-s3'
3
+ s.version = '0.5.4.4'
4
+
5
+ s.homepage = "http://documentcloud.github.com/jammit/"
6
+ s.summary = "Asset Packaging for Rails with Deployment to S3/Cloudfront"
7
+ s.description = <<-EOS
8
+ Jammit-S3 is an extension to the awesome Jammit library that handles deployment to s3 and cloudfront.
9
+ EOS
10
+
11
+ s.authors = ['Jacques Crocker']
12
+ s.email = 'railsjedi@gmail.com'
13
+ s.rubyforge_project = 'jammit-s3'
14
+
15
+ s.require_paths = ['lib']
16
+ s.executables = ['jammit-s3']
17
+
18
+ s.add_dependency 'jammit', '>= 0.5.4'
19
+ s.add_dependency 'mimemagic', '>= 0.1.7'
20
+ s.add_dependency 's3', ">= 0.3.7"
21
+
22
+ s.files = Dir['lib/**/*', 'bin/*', 'jammit-s3.gemspec', 'LICENSE', 'README']
23
+ end
@@ -0,0 +1,39 @@
1
+ require 'jammit/command_line'
2
+ require 'jammit/s3_command_line'
3
+ require 'jammit/s3_uploader'
4
+
5
+ module Jammit
6
+ def self.upload_to_s3!(options = {})
7
+ S3Uploader.new(options).upload
8
+ end
9
+ end
10
+
11
+ if defined?(Rails)
12
+ module Jammit
13
+ class JammitRailtie < Rails::Railtie
14
+ initializer "set asset host and asset id" do
15
+ config.after_initialize do
16
+ if Jammit.configuration[:use_cloudfront] && Jammit.configuration[:cloudfront_domain].present?
17
+ asset_hostname = Jammit.configuration[:cloudfront_domain]
18
+ elsif Jammit.configuration[:s3_bucket].present?
19
+ asset_hostname = "#{Jammit.configuration[:s3_bucket]}.s3.amazonaws.com"
20
+ else
21
+ asset_hostname = nil
22
+ end
23
+
24
+ if Jammit.package_assets and asset_hostname.present?
25
+ ActionController::Base.asset_host = Proc.new do |source, request|
26
+ if Jammit.configuration.has_key?(:ssl)
27
+ protocol = Jammit.configuration[:ssl] ? "https://" : "http://"
28
+ else
29
+ protocol = request.protocol
30
+ end
31
+ "#{protocol}#{asset_hostname}"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,41 @@
1
+ require 's3'
2
+ module Jammit
3
+ class S3CommandLine < CommandLine
4
+ def initialize
5
+ super
6
+ ensure_s3_configuration
7
+
8
+ begin
9
+ Jammit.upload_to_s3!
10
+ rescue S3::Error::BucketAlreadyExists => e
11
+ # tell them to pick another name
12
+ puts e.message
13
+ exit(1)
14
+ end
15
+ end
16
+
17
+ protected
18
+ def ensure_s3_configuration
19
+ bucket_name = Jammit.configuration[:s3_bucket]
20
+ unless bucket_name.is_a?(String) && bucket_name.length > 0
21
+ puts "\nA valid s3_bucket name is required."
22
+ puts "Please add one to your Jammit config (config/assets.yml):\n\n"
23
+ puts "s3_bucket: my-bucket-name\n\n"
24
+ exit(1)
25
+ end
26
+
27
+ access_key_id = Jammit.configuration[:s3_access_key_id]
28
+ unless access_key_id.is_a?(String) && access_key_id.length > 0
29
+ puts "access_key_id: '#{access_key_id}' is not valid"
30
+ exit(1)
31
+ end
32
+
33
+ secret_access_key = Jammit.configuration[:s3_secret_access_key]
34
+ unless secret_access_key.is_a?(String) && secret_access_key.length > 0
35
+ puts "secret_access_key: '#{secret_access_key}' is not valid"
36
+ exit(1)
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,95 @@
1
+ require 'mimemagic'
2
+
3
+ module Jammit
4
+ class S3Uploader
5
+ def initialize(options = {})
6
+ @bucket = options[:bucket]
7
+ unless @bucket
8
+ @bucket_name = options[:bucket_name] || Jammit.configuration[:s3_bucket]
9
+ @access_key_id = options[:access_key_id] || Jammit.configuration[:s3_access_key_id]
10
+ @secret_access_key = options[:secret_access_key] || Jammit.configuration[:s3_secret_access_key]
11
+ @bucket_location = options[:bucket_location] || Jammit.configuration[:s3_bucket_location]
12
+ @cache_control = options[:cache_control] || Jammit.configuration[:s3_cache_control]
13
+ @acl = options[:acl] || Jammit.configuration[:s3_permission]
14
+
15
+ @bucket = find_or_create_bucket
16
+ end
17
+ end
18
+
19
+ def upload
20
+ log "Pushing assets to S3 bucket: #{@bucket.name}"
21
+ globs = []
22
+
23
+ # add default package path
24
+ if Jammit.gzip_assets
25
+ globs << "public/#{Jammit.package_path}/**/*.gz"
26
+ else
27
+ globs << "public/#{Jammit.package_path}/**/*.css"
28
+ globs << "public/#{Jammit.package_path}/**/*.js"
29
+ end
30
+
31
+ # add images
32
+ globs << "public/images/**/*"
33
+
34
+ # add custom configuration if defined
35
+ s3_upload_files = Jammit.configuration[:s3_upload_files]
36
+ globs << s3_upload_files if s3_upload_files.is_a?(String)
37
+ globs += s3_upload_files if s3_upload_files.is_a?(Array)
38
+
39
+ # upload all the globs
40
+ globs.each do |glob|
41
+ upload_from_glob(glob)
42
+ end
43
+ end
44
+
45
+ def upload_from_glob(glob)
46
+ log "Pushing files from #{glob}"
47
+ log "#{ASSET_ROOT}/#{glob}"
48
+ Dir["#{ASSET_ROOT}/#{glob}"].each do |local_path|
49
+ next if File.directory?(local_path)
50
+ remote_path = local_path.gsub(/^#{ASSET_ROOT}\/public\//, "")
51
+
52
+ use_gzip = false
53
+
54
+ # handle gzipped files
55
+ if File.extname(remote_path) == ".gz"
56
+ use_gzip = true
57
+ remote_path = remote_path.gsub(/\.gz$/, "")
58
+ end
59
+
60
+ log "pushing file to s3: #{remote_path}"
61
+
62
+ # save to s3
63
+ new_object = @bucket.objects.build(remote_path)
64
+ new_object.cache_control = @cache_control if @cache_control
65
+ new_object.content_type = MimeMagic.by_path(remote_path)
66
+ new_object.content = open(local_path)
67
+ new_object.content_encoding = "gzip" if use_gzip
68
+ new_object.acl = @acl if @acl
69
+ new_object.save
70
+ end
71
+ end
72
+
73
+ def find_or_create_bucket
74
+ s3_service = S3::Service.new(:access_key_id => @access_key_id, :secret_access_key => @secret_access_key)
75
+
76
+ # find or create the bucket
77
+ begin
78
+ s3_service.buckets.find(@bucket_name)
79
+ rescue S3::Error::NoSuchBucket
80
+ log "Bucket not found. Creating '#{@bucket_name}'..."
81
+ bucket = s3_service.buckets.build(@bucket_name)
82
+
83
+ location = (@bucket_location.to_s.strip.downcase == "eu") ? :eu : :us
84
+ bucket.save(location)
85
+ bucket
86
+ end
87
+ end
88
+
89
+ def log(msg)
90
+ puts msg
91
+ end
92
+
93
+ end
94
+
95
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locomotive_jammit-s3
3
+ version: !ruby/object:Gem::Version
4
+ hash: 127
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 4
10
+ - 4
11
+ version: 0.5.4.4
12
+ platform: ruby
13
+ authors:
14
+ - Jacques Crocker
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-01-03 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ type: :runtime
24
+ prerelease: false
25
+ name: jammit
26
+ version_requirements: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 3
32
+ segments:
33
+ - 0
34
+ - 5
35
+ - 4
36
+ version: 0.5.4
37
+ requirement: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ type: :runtime
40
+ prerelease: false
41
+ name: mimemagic
42
+ version_requirements: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 21
48
+ segments:
49
+ - 0
50
+ - 1
51
+ - 7
52
+ version: 0.1.7
53
+ requirement: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ type: :runtime
56
+ prerelease: false
57
+ name: s3
58
+ version_requirements: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 29
64
+ segments:
65
+ - 0
66
+ - 3
67
+ - 7
68
+ version: 0.3.7
69
+ requirement: *id003
70
+ description: " Jammit-S3 is an extension to the awesome Jammit library that handles deployment to s3 and cloudfront.\n"
71
+ email: railsjedi@gmail.com
72
+ executables:
73
+ - jammit-s3
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - lib/jammit/s3_command_line.rb
80
+ - lib/jammit/s3_uploader.rb
81
+ - lib/jammit-s3.rb
82
+ - bin/jammit-s3
83
+ - jammit-s3.gemspec
84
+ - LICENSE
85
+ has_rdoc: true
86
+ homepage: http://documentcloud.github.com/jammit/
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project: jammit-s3
115
+ rubygems_version: 1.3.7
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Asset Packaging for Rails with Deployment to S3/Cloudfront
119
+ test_files: []
120
+