Class: Capistrano::Deploy::Strategy::S3Copy

Inherits:
Copy
  • Object
show all
Defined in:
lib/capistrano/recipes/deploy/strategy/s3_copy.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ S3Copy

Returns a new instance of S3Copy.

Raises:

  • (Capistrano::Error)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 10

def initialize(config={})
  super(config)

  s3cmd_vars = []
  ["aws_access_key_id", "aws_secret_access_key"].each do |var|
    value = configuration[var.to_sym]
    raise Capistrano::Error, "Missing configuration[:#{var}] setting" if value.nil?
    s3cmd_vars << "#{var.upcase}=#{value}"
  end
  @aws_environment = s3cmd_vars.join(" ")

  @bucket_name = configuration[:aws_releases_bucket]
  raise Capistrano::Error, "Missing configuration[:aws_releases_bucket]" if @bucket_name.nil?
end

Instance Method Details

#aws_environmentObject



69
70
71
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 69

def aws_environment
  @aws_environment
end

#bindingObject



65
66
67
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 65

def binding
  super
end

#bucket_nameObject



73
74
75
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 73

def bucket_name
  @bucket_name
end

#build_aws_install_scriptObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 52

def build_aws_install_script
  template_text = configuration[:aws_install_script]
  template_text = File.read(File.join(File.dirname(__FILE__), "aws_install.sh.erb")) if template_text.nil?
  template_text = template_text.gsub("\r\n?", "\n")
  template = ERB.new(template_text, nil, '<>-')
  output = template.result(self.binding)
  local_output_file = File.join(copy_dir, "aws_install.sh")
  File.open(local_output_file, "w") do  |f|
    f.write(output)
  end
  configuration[:s3_copy_aws_install_cmd] = "#{aws_environment} s3cmd put #{bucket_name}:#{rails_env}/aws_install.sh #{local_output_file} x-amz-server-side-encryption:AES256 2>&1"
end

#check!Object



25
26
27
28
29
30
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 25

def check!
  super.check do |d|
    d.local.command("s3cmd")
    d.remote.command("s3cmd")
  end
end

#distribute!Object

Distributes the file to the remote servers



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 33

def distribute!
  package_path = filename
  package_name = File.basename(package_path)
  s3_push_cmd = "#{aws_environment} s3cmd put #{bucket_name}:#{rails_env}/#{package_name} #{package_path} x-amz-server-side-encryption:AES256 2>&1"

  if configuration.dry_run
    logger.debug s3_push_cmd
  else
    system(s3_push_cmd)
    raise Capistrano::Error, "shell command failed with return code #{$?}" if $? != 0
  end

  run "#{aws_environment} s3cmd get #{bucket_name}:#{rails_env}/#{package_name} #{remote_filename} 2>&1"
  run "cd #{configuration[:releases_path]} && #{decompress(remote_filename).join(" ")} && rm #{remote_filename}"
  logger.debug "done!"

  build_aws_install_script
end