Class: Capistrano::Deploy::Strategy::ScaleCopy

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ ScaleCopy

Returns a new instance of ScaleCopy.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 10

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

  # Check configuration keys and raise error if something is missing.
  %w(aws_access_key_id aws_secret_access_key aws_releases_bucket aws_install_script aws_ec2_iam_role).each do |var|
    value = configuration[var.to_sym]
    if value.nil?
      raise(Capistrano::Error, "Missing configuration[:#{var}] setting")
    end
  end
  # Build up variables needed in the later execution.
  @aws_credentials = {
    :aws_access_key_id     => configuration[:aws_access_key_id],
    :aws_secret_access_key => configuration[:aws_secret_access_key]
  }
  @bucket_name = configuration[:aws_releases_bucket]
  @aws_shell_environment = @aws_credentials.collect{ |k,v| "#{k.upcase}=#{v}" }.join(' ')
  @aws_connection = Fog::Storage::AWS.new(aws_credentials)
  @aws_directory = @aws_connection.directories.create(
    :key        => bucket_name,
    :public     => false
  )
end

Instance Method Details

#aws_connectionObject



111
112
113
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 111

def aws_connection
  @aws_connection
end

#aws_credentialsObject



99
100
101
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 99

def aws_credentials
  @aws_credentials
end

#aws_directoryObject



115
116
117
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 115

def aws_directory
  @aws_directory
end

#aws_shell_environmentObject



103
104
105
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 103

def aws_shell_environment
  @aws_shell_environment
end

#bindingObject



95
96
97
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 95

def binding
  super
end

#bucket_nameObject



107
108
109
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 107

def bucket_name
  @bucket_name
end

#check!Object

Check if curl is installed on the remote server.



35
36
37
38
39
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 35

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

#distribute!Object

Distributes the file to the remote servers.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/capistrano/recipes/deploy/strategy/scale_copy.rb', line 42

def distribute!
  package_path = filename
  package_name = File.basename(package_path)

  package_key  = "#{rails_env}/#{application}/#{package_name}"

  file = aws_directory.files.new(
    :key        => package_key,
    :body       => File.open(package_path),
    :public     => false,
    :encryption => 'AES256'
  )

  logger.debug "Copying the package on S3: #{package_key}"
  if configuration.dry_run
    logger.debug file.inspect
  else
    begin
      file.save
    rescue => e
      raise(Capistrano::Error, "S3 File upload failed: #{e.class.to_s}:#{e.message}")
    end
  end
  logger.debug "Package copied."

  expiring_url = file.url(Time.now + 600)

  logger.debug "Fetching the release archive on the server"
  run "curl -s -L -o #{remote_filename} --url '#{expiring_url}'"
  logger.debug "Decompressing the release archive on the server"
  run "cd #{configuration[:releases_path]} && #{decompress(remote_filename).join(" ")} && rm #{remote_filename}"
  logger.debug "Release uncompressed, ready for hooks"

  logger.debug "Creating instance initialization script locally"

  template_text = File.read(configuration[:aws_install_script])
  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, "#{rails_env}_aws_install.sh")

  File.open(local_output_file, "w") do  |f|
    f.write(output)
  end

  logger.debug "Script created at: #{local_output_file}"

  # Will be picked up by an internal Capistrano hook after a deploy has finished
  # to put this manifest on S3.
  configuration[:s3_install_cmd_path] = local_output_file
end