Class: S3Deployer

Inherits:
Object
  • Object
show all
Defined in:
lib/s3deployer.rb,
lib/s3deployer/version.rb

Defined Under Namespace

Classes: DeploymentLocked

Constant Summary collapse

DEFAULT_OPTIONS =
{
  force_upload: false
}
VERSION =
"0.0.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key, options = DEFAULT_OPTIONS) ⇒ S3Deployer

Returns a new instance of S3Deployer.



14
15
16
17
18
19
20
# File 'lib/s3deployer.rb', line 14

def initialize(access_key, secret_key, options=DEFAULT_OPTIONS)
  AWS::S3::Base.establish_connection!({
      :access_key_id => access_key,
      :secret_access_key => secret_key
  })
  @force_upload = options[:force_upload]
end

Instance Attribute Details

#bucket_nameObject

Returns the value of attribute bucket_name.



12
13
14
# File 'lib/s3deployer.rb', line 12

def bucket_name
  @bucket_name
end

#directory_to_deployObject

Returns the value of attribute directory_to_deploy.



12
13
14
# File 'lib/s3deployer.rb', line 12

def directory_to_deploy
  @directory_to_deploy
end

Instance Method Details

#deploy(directory_to_deploy, bucket_name = nil) ⇒ Object



22
23
24
25
26
# File 'lib/s3deployer.rb', line 22

def deploy(directory_to_deploy, bucket_name=nil)
  @bucket_name = bucket_name unless bucket_name.nil?
  @directory_to_deploy = File.expand_path(directory_to_deploy)
  iterate_through_and_deploy(directory_to_deploy)
end

#item_exists?(item) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/s3deployer.rb', line 55

def item_exists?(item)
  s3_name = path_to_file(item)
  return false unless AWS::S3::S3Object.exists?(s3_name, bucket_name)
  existing = AWS::S3::S3Object.find(s3_name, bucket_name)
  md5 = Digest::MD5.hexdigest(File.read(item))
  existing.etag == md5
end

#iterate_through_and_deploy(dir) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/s3deployer.rb', line 28

def iterate_through_and_deploy(dir)
  Dir.foreach(dir) do |item|
    next if item[0] == "."
    item_location = "#{dir}/#{item}"
    if File.directory?(item_location)
      iterate_through_and_deploy(item_location)
    else
      upload_item(item_location)
    end
  end
end

#lockfile_nameObject



63
64
65
# File 'lib/s3deployer.rb', line 63

def lockfile_name
  path_to_file "deployment_in_progress"
end

#path_to_file(file) ⇒ Object



40
41
42
43
# File 'lib/s3deployer.rb', line 40

def path_to_file(file)
  item_as_path = Pathname.new(file)
  item_as_path.sub("#{@directory_to_deploy}/", '').cleanpath.to_s
end

#upload_item(item) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/s3deployer.rb', line 45

def upload_item(item)
  s3_name = path_to_file(item)
  if (@force_upload or not item_exists?(item))
    puts "uploading #{item} as #{s3_name}"
    AWS::S3::S3Object.store(s3_name, open(item), bucket_name)
  else
    puts "not uploading #{item}, it exists on S3 as #{s3_name}"
  end
end

#with_lock(bucket_name, &block) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
# File 'lib/s3deployer.rb', line 67

def with_lock(bucket_name, &block)
  raise DeploymentLocked.new if AWS::S3::S3Object.exists?(lockfile_name, bucket_name)

  begin
    AWS::S3::S3Object.store(lockfile_name, "", bucket_name)
    block.call(self, bucket_name)
  ensure
    AWS::S3::S3Object.delete(lockfile_name, bucket_name)
  end
end