Class Capistrano::Deploy::Strategy::Copy
In: lib/capistrano/recipes/deploy/strategy/copy.rb
lib/capistrano/recipes/deploy/strategy/copy.rb
Parent: Base

This class implements the strategy for deployments which work by preparing the source code locally, compressing it, copying the file to each target host, and uncompressing it to the deployment directory.

By default, the SCM checkout command is used to obtain the local copy of the source code. If you would rather use the export operation, you can set the :copy_strategy variable to :export.

  set :copy_strategy, :export

For even faster deployments, you can set the :copy_cache variable to true. This will cause deployments to do a new checkout of your repository to a new directory, and then copy that checkout. Subsequent deploys will just resync that copy, rather than doing an entirely new checkout. Additionally, you can specify file patterns to exclude from the copy when using :copy_cache; just set the :copy_exclude variable to a file glob (or an array of globs).

  set :copy_cache, true
  set :copy_exclude, ".git/*"

Note that :copy_strategy is ignored when :copy_cache is set. Also, if you want the copy cache put somewhere specific, you can set the variable to the path you want, instead of merely ‘true’:

  set :copy_cache, "/tmp/caches/myapp"

This deployment strategy also supports a special variable, :copy_compression, which must be one of :gzip, :bz2, or :zip, and which specifies how the source should be compressed for transmission to each host.

Methods

check!   check!   copy_cache   copy_cache   deploy!   deploy!  

Constants

Compression = Struct.new(:extension, :compress_command, :decompress_command)   A struct for representing the specifics of a compression type. Commands are arrays, where the first element is the utility to be used to perform the compression or decompression.
Compression = Struct.new(:extension, :compress_command, :decompress_command)   A struct for representing the specifics of a compression type. Commands are arrays, where the first element is the utility to be used to perform the compression or decompression.

Public Instance methods

[Source]

     # File lib/capistrano/recipes/deploy/strategy/copy.rb, line 99
 99:         def check!
100:           super.check do |d|
101:             d.local.command(source.local.command) if source.local.command
102:             d.local.command(compress(nil, nil).first)
103:             d.remote.command(decompress(nil).first)
104:           end
105:         end

[Source]

     # File lib/capistrano/recipes/deploy/strategy/copy.rb, line 99
 99:         def check!
100:           super.check do |d|
101:             d.local.command(source.local.command) if source.local.command
102:             d.local.command(compress(nil, nil).first)
103:             d.remote.command(decompress(nil).first)
104:           end
105:         end

Returns the location of the local copy cache, if the strategy should use a local cache + copy instead of a new checkout/export every time. Returns nil unless :copy_cache has been set. If :copy_cache is true, a default cache location will be returned.

[Source]

     # File lib/capistrano/recipes/deploy/strategy/copy.rb, line 111
111:         def copy_cache
112:           @copy_cache ||= configuration[:copy_cache] == true ?
113:             File.join(Dir.tmpdir, configuration[:application]) :
114:             configuration[:copy_cache]
115:         end

Returns the location of the local copy cache, if the strategy should use a local cache + copy instead of a new checkout/export every time. Returns nil unless :copy_cache has been set. If :copy_cache is true, a default cache location will be returned.

[Source]

     # File lib/capistrano/recipes/deploy/strategy/copy.rb, line 111
111:         def copy_cache
112:           @copy_cache ||= configuration[:copy_cache] == true ?
113:             File.join(Dir.tmpdir, configuration[:application]) :
114:             configuration[:copy_cache]
115:         end

Obtains a copy of the source code locally (via the command method), compresses it to a single file, copies that file to all target servers, and uncompresses it on each of them into the deployment directory.

[Source]

    # File lib/capistrano/recipes/deploy/strategy/copy.rb, line 46
46:         def deploy!
47:           if copy_cache
48:             if File.exists?(copy_cache)
49:               logger.debug "refreshing local cache to revision #{revision} at #{copy_cache}"
50:               system(source.sync(revision, copy_cache))
51:             else
52:               logger.debug "preparing local cache at #{copy_cache}"
53:               system(source.checkout(revision, copy_cache))
54:             end
55: 
56:             logger.debug "copying cache to deployment staging area #{destination}"
57:             Dir.chdir(copy_cache) do
58:               FileUtils.mkdir_p(destination)
59:               queue = Dir.glob("*", File::FNM_DOTMATCH)
60:               while queue.any?
61:                 item = queue.shift
62:                 name = File.basename(item)
63: 
64:                 next if name == "." || name == ".."
65:                 next if copy_exclude.any? { |pattern| File.fnmatch(pattern, item) }
66: 
67:                 if File.symlink?(item)
68:                   FileUtils.ln_s(File.readlink(File.join(copy_cache, item)), File.join(destination, item))
69:                 elsif File.directory?(item)
70:                   queue += Dir.glob("#{item}/*", File::FNM_DOTMATCH)
71:                   FileUtils.mkdir(File.join(destination, item))
72:                 else
73:                   FileUtils.ln(File.join(copy_cache, item), File.join(destination, item))
74:                 end
75:               end
76:             end
77:           else
78:             logger.debug "getting (via #{copy_strategy}) revision #{revision} to #{destination}"
79:             system(command)
80: 
81:             if copy_exclude.any?
82:               logger.debug "processing exclusions..."
83:               copy_exclude.each { |pattern| FileUtils.rm_rf(File.join(destination, pattern)) }
84:             end
85:           end
86: 
87:           File.open(File.join(destination, "REVISION"), "w") { |f| f.puts(revision) }
88: 
89:           logger.trace "compressing #{destination} to #{filename}"
90:           Dir.chdir(tmpdir) { system(compress(File.basename(destination), File.basename(filename)).join(" ")) }
91: 
92:           upload(filename, remote_filename)
93:           run "cd #{configuration[:releases_path]} && #{decompress(remote_filename).join(" ")} && rm #{remote_filename}"
94:         ensure
95:           FileUtils.rm filename rescue nil
96:           FileUtils.rm_rf destination rescue nil
97:         end

Obtains a copy of the source code locally (via the command method), compresses it to a single file, copies that file to all target servers, and uncompresses it on each of them into the deployment directory.

[Source]

    # File lib/capistrano/recipes/deploy/strategy/copy.rb, line 46
46:         def deploy!
47:           if copy_cache
48:             if File.exists?(copy_cache)
49:               logger.debug "refreshing local cache to revision #{revision} at #{copy_cache}"
50:               system(source.sync(revision, copy_cache))
51:             else
52:               logger.debug "preparing local cache at #{copy_cache}"
53:               system(source.checkout(revision, copy_cache))
54:             end
55: 
56:             logger.debug "copying cache to deployment staging area #{destination}"
57:             Dir.chdir(copy_cache) do
58:               FileUtils.mkdir_p(destination)
59:               queue = Dir.glob("*", File::FNM_DOTMATCH)
60:               while queue.any?
61:                 item = queue.shift
62:                 name = File.basename(item)
63: 
64:                 next if name == "." || name == ".."
65:                 next if copy_exclude.any? { |pattern| File.fnmatch(pattern, item) }
66: 
67:                 if File.symlink?(item)
68:                   FileUtils.ln_s(File.readlink(File.join(copy_cache, item)), File.join(destination, item))
69:                 elsif File.directory?(item)
70:                   queue += Dir.glob("#{item}/*", File::FNM_DOTMATCH)
71:                   FileUtils.mkdir(File.join(destination, item))
72:                 else
73:                   FileUtils.ln(File.join(copy_cache, item), File.join(destination, item))
74:                 end
75:               end
76:             end
77:           else
78:             logger.debug "getting (via #{copy_strategy}) revision #{revision} to #{destination}"
79:             system(command)
80: 
81:             if copy_exclude.any?
82:               logger.debug "processing exclusions..."
83:               copy_exclude.each { |pattern| FileUtils.rm_rf(File.join(destination, pattern)) }
84:             end
85:           end
86: 
87:           File.open(File.join(destination, "REVISION"), "w") { |f| f.puts(revision) }
88: 
89:           logger.trace "compressing #{destination} to #{filename}"
90:           Dir.chdir(tmpdir) { system(compress(File.basename(destination), File.basename(filename)).join(" ")) }
91: 
92:           upload(filename, remote_filename)
93:           run "cd #{configuration[:releases_path]} && #{decompress(remote_filename).join(" ")} && rm #{remote_filename}"
94:         ensure
95:           FileUtils.rm filename rescue nil
96:           FileUtils.rm_rf destination rescue nil
97:         end

[Validate]