s3deployer 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- s3deployer (0.0.2)
4
+ s3deployer (0.0.5)
5
5
  aws-s3
6
6
 
7
7
  GEM
data/README.markdown CHANGED
@@ -11,6 +11,6 @@ Because sometimes, you just want to shove a bunch of stuff onto S3
11
11
 
12
12
  Just `cd` to the directory you want to deploy and run
13
13
 
14
- s3deploy BUCKET_NAME
14
+ s3deploy -b BUCKET_NAME
15
15
 
16
16
  ![ShoveIt](s3deployer/raw/master/ShoveItOverThere.jpg)
data/bin/s3deploy CHANGED
@@ -1,14 +1,44 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 's3deployer'
4
+ require 'optparse'
5
+
6
+ options = {}
7
+
8
+ optparse = OptionParser.new do|opts|
9
+
10
+ opts.on( '-h', '--help', 'Display this dialog' ) do
11
+ puts opts
12
+ exit
13
+ end
14
+
15
+ options[:force_upload] = false
16
+ opts.on("-f", "--force", "Force Reuploading of files regardless of checksums" ) do
17
+ options[:force_upload] = true
18
+ end
19
+
20
+ options[:bucket_name] = ""
21
+ opts.on( '-b', '--bucket BUCKET', 'S3 Bucket to upload to' ) do|b|
22
+ options[:bucket_name] = b
23
+ end
24
+
25
+ options[:directory] = "."
26
+ opts.on( '-d', '--directory DIRECTORY', 'Directory to deploy' ) do|d|
27
+ if File.directory? d
28
+ options[:directory] = d
29
+ else
30
+ puts "Directory #{d} does not exist"
31
+ exit
32
+ end
33
+ end
4
34
 
5
- if ARGV[0].nil?
6
- raise "Please supply a bucket name"
7
- exit 1
8
35
  end
9
36
 
10
- bucket = ARGV[0]
11
- access_key,secret_key = ENV['ACCESS_KEY'],ENV['SECRET_KEY']
12
- deployer = S3Deployer.new access_key, secret_key
13
- deployer.deploy '.', bucket
37
+ optparse.parse!
38
+
39
+ options[:access_key] ||= ENV['AMAZON_ACCESS_KEY_ID']
40
+ options[:secret_key] ||= ENV['AMAZON_SECRET_ACCESS_KEY']
41
+
42
+ deployer = S3Deployer.new(options[:access_key], options[:secret_key], options)
43
+ deployer.deploy options[:directory], options[:bucket_name]
14
44
 
@@ -1,3 +1,3 @@
1
1
  class S3Deployer
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/s3deployer.rb CHANGED
@@ -1,13 +1,22 @@
1
1
  require 'aws/s3'
2
2
  require 'pathname'
3
+ require 'digest/md5'
3
4
 
4
5
  class S3Deployer
6
+
7
+ DEFAULT_OPTIONS={
8
+ force_upload: false
9
+ }
5
10
  attr_accessor :directory_to_deploy, :bucket_name
6
- def initialize(access_key, secret_key)
11
+
12
+ def initialize(access_key, secret_key, options=DEFAULT_OPTIONS)
7
13
  AWS::S3::Base.establish_connection!({
8
14
  :access_key_id => access_key,
9
- :secret_access_key => secret_key})
15
+ :secret_access_key => secret_key
16
+ })
17
+ @force_upload = options[:force_upload]
10
18
  end
19
+
11
20
  def deploy(directory_to_deploy, bucket_name)
12
21
  @bucket_name = bucket_name
13
22
  @directory_to_deploy = File.expand_path(directory_to_deploy)
@@ -29,7 +38,21 @@ class S3Deployer
29
38
  def upload_item(item)
30
39
  item_as_path = Pathname.new(item)
31
40
  s3_name = item_as_path.sub("#{@directory_to_deploy}/", '').cleanpath.to_s
32
- puts "uploading #{item} as #{s3_name}"
33
- AWS::S3::S3Object.store(s3_name, open(item), bucket_name)
41
+ if (@force_upload or not item_exists?(item))
42
+ puts "uploading #{item} as #{s3_name}"
43
+ AWS::S3::S3Object.store(s3_name, open(item), bucket_name)
44
+ else
45
+ puts "not uploading #{item}, it exists on S3 as #{s3_name}"
46
+ end
34
47
  end
48
+
49
+ def item_exists?(item)
50
+ item_as_path = Pathname.new(item)
51
+ s3_name = item_as_path.sub("#{directory_to_deploy}/", '').cleanpath.to_s
52
+ return false unless AWS::S3::S3Object.exists?(s3_name, bucket_name)
53
+ existing = AWS::S3::S3Object.find(s3_name, bucket_name)
54
+ md5 = Digest::MD5.hexdigest(File.read(item))
55
+ existing.etag == md5
56
+ end
57
+
35
58
  end
@@ -5,18 +5,26 @@ describe S3Deployer do
5
5
  AWS::S3::Base.stub(:establish_connection!)
6
6
  Dir.mkdir('files') unless Dir.exists?('files')
7
7
  AWS::S3::S3Object.stub(:store)
8
+ AWS::S3::S3Object.stub(:exists?).and_return(true)
9
+ AWS::S3::S3Object.stub(:find).and_return(stub(etag: '1234'))
8
10
  subject.stub(:open).and_return('asdf')
9
11
  end
10
- subject { S3Deployer.new("abc", "123") }
12
+
13
+
14
+ subject { S3Deployer.new('abc', '123') }
11
15
  describe "instantiation" do
12
16
  it "Connects to Amazon S3 with access and secret keys" do
13
- S3Deployer.new("abc", "123")
17
+ options = {}
18
+ options[:force_upload] = false
19
+ options[:directory] = '.'
20
+ S3Deployer.new('abc', '123', options)
14
21
  AWS::S3::Base.should have_received(:establish_connection!).with({
15
22
  :access_key_id=>'abc',
16
23
  :secret_access_key=>'123'
17
24
  })
18
25
  end
19
26
  end
27
+
20
28
  describe "deploy" do
21
29
  context "with multiple files" do
22
30
  it "uploads them to S3" do
@@ -29,6 +37,19 @@ describe S3Deployer do
29
37
  File.delete("#{Dir.pwd}/files/hiphopper")
30
38
  end
31
39
  end
40
+
41
+ context "when a file exists in the same state" do
42
+ it "Does not store the file" do
43
+ create_file("word", "asdf")
44
+ md5 = Digest::MD5.hexdigest(File.read("#{Dir.pwd}/files/word"))
45
+ AWS::S3::S3Object.stub(:find).and_return(stub(etag: md5))
46
+ subject.deploy "#{Dir.pwd}/files", 'bucket'
47
+ AWS::S3::S3Object.should_not have_received(:store).with('word', 'asdf', 'bucket')
48
+ File.delete("#{Dir.pwd}/files/word")
49
+ end
50
+
51
+ end
52
+
32
53
  context "with . as directory to deploy" do
33
54
  it "doesn't strip out .'s in file names" do
34
55
  create_file("blah.html", "asdf")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3deployer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-15 00:00:00.000000000 Z
12
+ date: 2012-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-s3
16
- requirement: &70175425134860 !ruby/object:Gem::Requirement
16
+ requirement: &70179732131360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70175425134860
24
+ version_requirements: *70179732131360
25
25
  description: A simple way to deploy a directory to Amazon s3
26
26
  email:
27
27
  - zee@zeespencer.com