jammit-s3 0.5.3.0 → 0.5.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/jammit-s3 CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'jammit-s3'
3
3
 
4
- Jammit::S3CommandLine.new
4
+ Jammit::S3CommandLine.new
data/jammit-s3.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jammit-s3'
3
- s.version = '0.5.3.0'
3
+ s.version = '0.5.3.1'
4
4
 
5
5
  s.homepage = "http://documentcloud.github.com/jammit/"
6
6
  s.summary = "Asset Packaging for Rails with Deployment to S3/Cloudfront"
data/lib/jammit-s3.rb CHANGED
@@ -1,2 +1,9 @@
1
+ require 'jammit/command_line'
2
+ require 'jammit/s3_command_line'
1
3
  require 'jammit/s3_uploader'
2
- require 'jammit/s3_command_line'
4
+
5
+ module Jammit
6
+ def self.upload_to_s3!(options = {})
7
+ S3Uploader.new(options).upload
8
+ end
9
+ end
@@ -1,92 +1,41 @@
1
- require 'jammit/command_line'
2
-
3
1
  require 's3'
4
2
  module Jammit
5
3
  class S3CommandLine < CommandLine
6
4
  def initialize
7
5
  super
8
- upload_assets_to_s3
9
- end
6
+ ensure_s3_configuration
10
7
 
11
- def upload_assets_to_s3
12
- verify_s3_configuration
13
- ensure_bucket_exists
14
- upload_assets
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
15
  end
16
16
 
17
17
  protected
18
- def verify_s3_configuration
19
- unless get_bucket_name.is_a?(String) && get_bucket_name.length > 0
18
+ def ensure_s3_configuration
19
+ bucket_name = Jammit.configuration[:s3_bucket]
20
+ unless bucket_name.is_a?(String) && bucket_name.length > 0
20
21
  puts "\nA valid s3_bucket name is required."
21
22
  puts "Please add one to your Jammit config (config/assets.yml):\n\n"
22
23
  puts "s3_bucket: my-bucket-name\n\n"
23
- exit
24
+ exit(1)
24
25
  end
25
- unless get_access_key_id.is_a?(String) && get_access_key_id.length > 0
26
- puts "access_key_id: '#{get_access_key_id}' is not valid"
27
- exit
28
- end
29
- unless get_secret_access_key.is_a?(String) && get_secret_access_key.length > 0
30
- puts "secret_access_key: '#{get_secret_access_key}' is not valid"
31
- exit
32
- end
33
- end
34
-
35
- def upload_assets
36
- S3Uploader.new(s3_bucket).upload
37
- end
38
-
39
- def s3_service
40
- @s3_service ||= S3::Service.new(:access_key_id => get_access_key_id, :secret_access_key => get_secret_access_key)
41
- end
42
-
43
- def s3_bucket
44
- @s3_bucket ||= ensure_bucket_exists
45
- end
46
-
47
- def ensure_bucket_exists
48
- bucket_name = get_bucket_name
49
-
50
- # find or create the bucket
51
- bucket = begin
52
- s3_service.buckets.find(bucket_name)
53
- rescue S3::Error::NoSuchBucket
54
- puts "Bucket not found. Creating '#{bucket_name}'..."
55
- bucket = s3_service.buckets.build(bucket_name)
56
26
 
57
- begin
58
- bucket.save(get_bucket_location)
59
- rescue S3::Error::BucketAlreadyExists => e
60
- # tell them to pick another name
61
- puts e.message
62
- exit
63
- end
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)
64
31
  end
65
- bucket
66
- end
67
-
68
- def get_bucket_name
69
- # TODO: use highline if needed
70
- Jammit.configuration[:s3_bucket]
71
- end
72
32
 
73
- def get_access_key_id
74
- # TODO: use highline if needed
75
- Jammit.configuration[:s3_access_key_id]
76
- end
77
-
78
- def get_secret_access_key
79
- # TODO: use highline if needed
80
- Jammit.configuration[:s3_secret_access_key]
81
- end
82
-
83
- def get_bucket_location
84
- location = Jammit.configuration[:s3_bucket_location]
85
- if location == "eu"
86
- :eu
87
- else
88
- :us
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)
89
37
  end
90
38
  end
39
+
91
40
  end
92
41
  end
@@ -2,12 +2,21 @@ require 'mimemagic'
2
2
 
3
3
  module Jammit
4
4
  class S3Uploader
5
- def initialize(bucket)
6
- @bucket = bucket
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
+
13
+ @bucket = find_or_create_bucket
14
+ end
7
15
  end
8
16
 
9
17
  def upload
10
- puts "Pushing assets to S3 bucket: #{@bucket.name}"
18
+
19
+ log "Pushing assets to S3 bucket: #{@bucket.name}"
11
20
  globs = []
12
21
 
13
22
  # add default package path
@@ -33,8 +42,8 @@ module Jammit
33
42
  end
34
43
 
35
44
  def upload_from_glob(glob)
36
- puts "Pushing files from #{glob}"
37
- puts "#{ASSET_ROOT}/#{glob}"
45
+ log "Pushing files from #{glob}"
46
+ log "#{ASSET_ROOT}/#{glob}"
38
47
  Dir["#{ASSET_ROOT}/#{glob}"].each do |local_path|
39
48
  next if File.directory?(local_path)
40
49
  remote_path = local_path.gsub(/^#{ASSET_ROOT}\/public\//, "")
@@ -47,7 +56,7 @@ module Jammit
47
56
  remote_path = remote_path.gsub(/\.gz$/, "")
48
57
  end
49
58
 
50
- puts "pushing file to s3: #{remote_path}"
59
+ log "pushing file to s3: #{remote_path}"
51
60
 
52
61
  # save to s3
53
62
  new_object = @bucket.objects.build(remote_path)
@@ -58,6 +67,26 @@ module Jammit
58
67
  end
59
68
  end
60
69
 
70
+ def find_or_create_bucket
71
+ s3_service = S3::Service.new(:access_key_id => @access_key_id, :secret_access_key => @secret_access_key)
72
+
73
+ # find or create the bucket
74
+ bucket = begin
75
+ s3_service.buckets.find(@bucket_name)
76
+ rescue S3::Error::NoSuchBucket
77
+ log "Bucket not found. Creating '#{@bucket_name}'..."
78
+ bucket = s3_service.buckets.build(@bucket_name)
79
+
80
+ location = (@bucket_location.strip.to_s.downcase == "eu") ? :eu : :us
81
+ bucket.save(location)
82
+ end
83
+ bucket
84
+ end
85
+
86
+ def log(msg)
87
+ puts msg
88
+ end
89
+
61
90
  end
62
91
 
63
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jammit-s3
3
3
  version: !ruby/object:Gem::Version
4
- hash: 107
4
+ hash: 105
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
9
  - 3
10
- - 0
11
- version: 0.5.3.0
10
+ - 1
11
+ version: 0.5.3.1
12
12
  platform: ruby
13
13
  authors:
14
14
  - Jacques Crocker
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-10-03 00:00:00 -07:00
19
+ date: 2010-10-04 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency