Class: Microstatic::S3Deployer

Inherits:
Object
  • Object
show all
Defined in:
lib/microstatic/s3_deployer.rb

Overview

The following is based on code generously shared by Giles Alexander (@gga)

Instance Method Summary collapse

Constructor Details

#initialize(local_dir, bucket, aws_creds) ⇒ S3Deployer

Returns a new instance of S3Deployer.



9
10
11
12
13
14
15
16
17
# File 'lib/microstatic/s3_deployer.rb', line 9

def initialize( local_dir, bucket, aws_creds )
  [:access_key_id,:secret_access_key].each do |required_key|
    raise ArgumentError, "must supply :#{required_key}" unless aws_creds.key?(required_key)
  end

  @local_dir = Pathname.new(local_dir)
  @bucket = bucket
  @aws_creds = aws_creds
end

Instance Method Details

#log_action(action, file) ⇒ Object



52
53
54
55
# File 'lib/microstatic/s3_deployer.rb', line 52

def log_action(action,file)
  message = action.to_s.rjust(10) + "  " + file
  puts message
end

#uploadObject



19
20
21
22
23
24
25
# File 'lib/microstatic/s3_deployer.rb', line 19

def upload
  AWS::S3::Base.establish_connection!(@aws_creds)

  Pathname.glob(@local_dir+"**/*") do |child|
    upload_file(child) unless child.directory?
  end
end

#upload_file(file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/microstatic/s3_deployer.rb', line 27

def upload_file( file )
  s3_key = file.relative_path_from(@local_dir).to_s

  begin
    s3_object = AWS::S3::S3Object.find(s3_key, @bucket)
  rescue AWS::S3::NoSuchKey
    s3_object = false
  end

  if !s3_object
    log_action('CREATE', s3_key)
    AWS::S3::S3Object.store(s3_key, file.open, @bucket)
  else
    s3_md5 = s3_object.about['etag'].sub(/"(.*)"/,'\1')
    local_md5 = Digest::MD5.hexdigest( file.read )

    if( s3_md5 == local_md5 )
      log_action('NO CHANGE', s3_key)
    else
      log_action('UPDATE', s3_key)
      AWS::S3::S3Object.store(s3_key, file.open, @bucket)
    end
  end
end