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
24
25
26
27
# 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]
  #  This error handling was commented for IAM role
  #  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?

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

Instance Method Details

#aws_environmentObject



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

def aws_environment
  @aws_environment
end

#bindingObject



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

def binding
  super
end

#bucket_nameObject



77
78
79
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 77

def bucket_name
  @bucket_name
end

#build_aws_install_scriptObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 56

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} aws s3 put-object --bucket #{bucket_name} --region #{region_name} --key #{rails_env}/aws_install.sh --body #{local_output_file} 2>&1"
end

#check!Object



29
30
31
32
33
34
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 29

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

#distribute!Object

Distributes the file to the remote servers



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 37

def distribute!
  package_path = filename
  package_name = File.basename(package_path)
  s3_push_cmd = "#{aws_environment} aws s3 put-object --bucket #{bucket_name} --region #{region_name} --key #{rails_env}/#{package_name} --body #{package_path} 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} aws s3 get-object --bucket #{bucket_name} --region #{region_name} --key #{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

#region_nameObject



81
82
83
# File 'lib/capistrano/recipes/deploy/strategy/s3_copy.rb', line 81

def region_name
  @region_name
end