s3deployer 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -13,4 +13,17 @@ Just `cd` to the directory you want to deploy and run
13
13
 
14
14
  s3deploy -b BUCKET_NAME
15
15
 
16
+ ## Deploying with multiple people
17
+
18
+ When working with multiple people who may be deploying code at the same time
19
+ it's helpful to make sure that only one deployment happens at a time. Use
20
+ with\_lock for this.
21
+
22
+ deployer = S3Deployer.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
23
+ deployer.bucket_name = "amazon.bucket.name"
24
+
25
+ deployer.with_lock do |deployer|
26
+ deployer.deploy("#{Dir.pwd}/directory_to_deploy")
27
+ end
28
+
16
29
  ![ShoveIt](s3deployer/raw/master/ShoveItOverThere.jpg)
data/lib/s3deployer.rb CHANGED
@@ -4,6 +4,8 @@ require 'digest/md5'
4
4
 
5
5
  class S3Deployer
6
6
 
7
+ class DeploymentLocked < Exception; end
8
+
7
9
  DEFAULT_OPTIONS={
8
10
  force_upload: false
9
11
  }
@@ -17,8 +19,8 @@ class S3Deployer
17
19
  @force_upload = options[:force_upload]
18
20
  end
19
21
 
20
- def deploy(directory_to_deploy, bucket_name)
21
- @bucket_name = bucket_name
22
+ def deploy(directory_to_deploy, bucket_name=nil)
23
+ @bucket_name = bucket_name unless bucket_name.nil?
22
24
  @directory_to_deploy = File.expand_path(directory_to_deploy)
23
25
  iterate_through_and_deploy(directory_to_deploy)
24
26
  end
@@ -35,9 +37,13 @@ class S3Deployer
35
37
  end
36
38
  end
37
39
 
40
+ def path_to_file(file)
41
+ item_as_path = Pathname.new(file)
42
+ item_as_path.sub("#{@directory_to_deploy}/", '').cleanpath.to_s
43
+ end
44
+
38
45
  def upload_item(item)
39
- item_as_path = Pathname.new(item)
40
- s3_name = item_as_path.sub("#{@directory_to_deploy}/", '').cleanpath.to_s
46
+ s3_name = path_to_file(item)
41
47
  if (@force_upload or not item_exists?(item))
42
48
  puts "uploading #{item} as #{s3_name}"
43
49
  AWS::S3::S3Object.store(s3_name, open(item), bucket_name)
@@ -47,12 +53,26 @@ class S3Deployer
47
53
  end
48
54
 
49
55
  def item_exists?(item)
50
- item_as_path = Pathname.new(item)
51
- s3_name = item_as_path.sub("#{directory_to_deploy}/", '').cleanpath.to_s
56
+ s3_name = path_to_file(item)
52
57
  return false unless AWS::S3::S3Object.exists?(s3_name, bucket_name)
53
58
  existing = AWS::S3::S3Object.find(s3_name, bucket_name)
54
59
  md5 = Digest::MD5.hexdigest(File.read(item))
55
60
  existing.etag == md5
56
61
  end
57
62
 
63
+ def lockfile_name
64
+ path_to_file "deployment_in_progress"
65
+ end
66
+
67
+ def with_lock(bucket_name, &block)
68
+ raise DeploymentLocked.new if AWS::S3::S3Object.exists?(lockfile_name, bucket_name)
69
+
70
+ begin
71
+ AWS::S3::S3Object.store(lockfile_name, "", bucket_name)
72
+ block.call(self, bucket_name)
73
+ ensure
74
+ AWS::S3::S3Object.delete(lockfile_name, bucket_name)
75
+ end
76
+ end
77
+
58
78
  end
@@ -1,3 +1,3 @@
1
1
  class S3Deployer
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -5,6 +5,7 @@ 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(:delete)
8
9
  AWS::S3::S3Object.stub(:exists?).and_return(true)
9
10
  AWS::S3::S3Object.stub(:find).and_return(stub(etag: '1234'))
10
11
  subject.stub(:open).and_return('asdf')
@@ -69,6 +70,27 @@ describe S3Deployer do
69
70
  File.delete("#{Dir.pwd}/files/sub/blah.html")
70
71
  end
71
72
  end
73
+
74
+ context "locks deployment" do
75
+ before do
76
+ AWS::S3::S3Object.stub(:exists?).and_return(false)
77
+ end
78
+
79
+ it "doesn't deploy if there is a lockfile" do
80
+ AWS::S3::S3Object.stub(:exists?).and_return(true)
81
+ expect { subject.with_lock('bucket') { } }.to raise_error(S3Deployer::DeploymentLocked)
82
+ end
83
+
84
+ it "creates a lockfile" do
85
+ subject.with_lock('bucket') { }
86
+ AWS::S3::S3Object.should have_received(:store).with "deployment_in_progress", "", 'bucket'
87
+ end
88
+
89
+ it "removes the lockfile" do
90
+ subject.with_lock('bucket') { }
91
+ AWS::S3::S3Object.should have_received(:delete).with "deployment_in_progress", 'bucket'
92
+ end
93
+ end
72
94
  end
73
95
  end
74
96
  def create_file(filename, content)
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.6
4
+ version: 0.0.7
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-18 00:00:00.000000000 Z
12
+ date: 2012-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-s3
16
- requirement: &70179732131360 !ruby/object:Gem::Requirement
16
+ requirement: &70122798704360 !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: *70179732131360
24
+ version_requirements: *70122798704360
25
25
  description: A simple way to deploy a directory to Amazon s3
26
26
  email:
27
27
  - zee@zeespencer.com