Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rearranging Uploader so it has a simpler UI

  • Loading branch information...
commit 6d9be1c2caf0b1c5bf61603d2c5db11944a1759c 1 parent 223123e
Jacques Crocker authored
2  bin/jammit-s3
View
@@ -1,4 +1,4 @@
#!/usr/bin/env ruby
require 'jammit-s3'
-Jammit::S3CommandLine.new
+Jammit::S3CommandLine.new
9 lib/jammit-s3.rb
View
@@ -1,2 +1,9 @@
+require 'jammit/command_line'
+require 'jammit/s3_command_line'
require 'jammit/s3_uploader'
-require 'jammit/s3_command_line'
+
+module Jammit
+ def self.upload_to_s3!(options = {})
+ S3Uploader.new(options).upload
+ end
+end
93 lib/jammit/s3_command_line.rb
View
@@ -1,92 +1,41 @@
-require 'jammit/command_line'
-
require 's3'
module Jammit
class S3CommandLine < CommandLine
def initialize
super
- upload_assets_to_s3
- end
+ ensure_s3_configuration
- def upload_assets_to_s3
- verify_s3_configuration
- ensure_bucket_exists
- upload_assets
+ begin
+ Jammit.upload_to_s3!
+ rescue S3::Error::BucketAlreadyExists => e
+ # tell them to pick another name
+ puts e.message
+ exit(1)
+ end
end
protected
- def verify_s3_configuration
- unless get_bucket_name.is_a?(String) && get_bucket_name.length > 0
+ def ensure_s3_configuration
+ bucket_name = Jammit.configuration[:s3_bucket]
+ unless bucket_name.is_a?(String) && bucket_name.length > 0
puts "\nA valid s3_bucket name is required."
puts "Please add one to your Jammit config (config/assets.yml):\n\n"
puts "s3_bucket: my-bucket-name\n\n"
- exit
+ exit(1)
end
- unless get_access_key_id.is_a?(String) && get_access_key_id.length > 0
- puts "access_key_id: '#{get_access_key_id}' is not valid"
- exit
- end
- unless get_secret_access_key.is_a?(String) && get_secret_access_key.length > 0
- puts "secret_access_key: '#{get_secret_access_key}' is not valid"
- exit
- end
- end
-
- def upload_assets
- S3Uploader.new(s3_bucket).upload
- end
-
- def s3_service
- @s3_service ||= S3::Service.new(:access_key_id => get_access_key_id, :secret_access_key => get_secret_access_key)
- end
-
- def s3_bucket
- @s3_bucket ||= ensure_bucket_exists
- end
-
- def ensure_bucket_exists
- bucket_name = get_bucket_name
-
- # find or create the bucket
- bucket = begin
- s3_service.buckets.find(bucket_name)
- rescue S3::Error::NoSuchBucket
- puts "Bucket not found. Creating '#{bucket_name}'..."
- bucket = s3_service.buckets.build(bucket_name)
- begin
- bucket.save(get_bucket_location)
- rescue S3::Error::BucketAlreadyExists => e
- # tell them to pick another name
- puts e.message
- exit
- end
+ access_key_id = Jammit.configuration[:s3_access_key_id]
+ unless access_key_id.is_a?(String) && access_key_id.length > 0
+ puts "access_key_id: '#{access_key_id}' is not valid"
+ exit(1)
end
- bucket
- end
-
- def get_bucket_name
- # TODO: use highline if needed
- Jammit.configuration[:s3_bucket]
- end
- def get_access_key_id
- # TODO: use highline if needed
- Jammit.configuration[:s3_access_key_id]
- end
-
- def get_secret_access_key
- # TODO: use highline if needed
- Jammit.configuration[:s3_secret_access_key]
- end
-
- def get_bucket_location
- location = Jammit.configuration[:s3_bucket_location]
- if location == "eu"
- :eu
- else
- :us
+ secret_access_key = Jammit.configuration[:s3_secret_access_key]
+ unless secret_access_key.is_a?(String) && secret_access_key.length > 0
+ puts "secret_access_key: '#{secret_access_key}' is not valid"
+ exit(1)
end
end
+
end
end
41 lib/jammit/s3_uploader.rb
View
@@ -2,12 +2,21 @@
module Jammit
class S3Uploader
- def initialize(bucket)
- @bucket = bucket
+ def initialize(options = {})
+ @bucket = options[:bucket]
+ unless @bucket
+ @bucket_name = options[:bucket_name] || Jammit.configuration[:s3_bucket]
+ @access_key_id = options[:access_key_id] || Jammit.configuration[:s3_access_key_id]
+ @secret_access_key = options[:secret_access_key] || Jammit.configuration[:s3_secret_access_key]
+ @bucket_location = options[:bucket_location] || Jammit.configuration[:s3_bucket_location]
+
+ @bucket = find_or_create_bucket
+ end
end
def upload
- puts "Pushing assets to S3 bucket: #{@bucket.name}"
+
+ log "Pushing assets to S3 bucket: #{@bucket.name}"
globs = []
# add default package path
@@ -33,8 +42,8 @@ def upload
end
def upload_from_glob(glob)
- puts "Pushing files from #{glob}"
- puts "#{ASSET_ROOT}/#{glob}"
+ log "Pushing files from #{glob}"
+ log "#{ASSET_ROOT}/#{glob}"
Dir["#{ASSET_ROOT}/#{glob}"].each do |local_path|
next if File.directory?(local_path)
remote_path = local_path.gsub(/^#{ASSET_ROOT}\/public\//, "")
@@ -47,7 +56,7 @@ def upload_from_glob(glob)
remote_path = remote_path.gsub(/\.gz$/, "")
end
- puts "pushing file to s3: #{remote_path}"
+ log "pushing file to s3: #{remote_path}"
# save to s3
new_object = @bucket.objects.build(remote_path)
@@ -58,6 +67,26 @@ def upload_from_glob(glob)
end
end
+ def find_or_create_bucket
+ s3_service = S3::Service.new(:access_key_id => @access_key_id, :secret_access_key => @secret_access_key)
+
+ # find or create the bucket
+ bucket = begin
+ s3_service.buckets.find(@bucket_name)
+ rescue S3::Error::NoSuchBucket
+ log "Bucket not found. Creating '#{@bucket_name}'..."
+ bucket = s3_service.buckets.build(@bucket_name)
+
+ location = (@bucket_location.strip.to_s.downcase == "eu") ? :eu : :us
+ bucket.save(location)
+ end
+ bucket
+ end
+
+ def log(msg)
+ puts msg
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.