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.
There is a possibility to pass a build command that will get executed if your code needs to be compiled or something needs to be done before the code is ready to run.
set :build_script, "make all"
Note that if you use :copy_cache, the :build_script is used on the cache and thus you get faster compilation if your script does not recompile everything.
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.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 66 66: def build directory 67: execute "running build script on #{directory}" do 68: Dir.chdir(directory) { system(build_script) } 69: end if build_script 70: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 72 72: def check! 73: super.check do |d| 74: d.local.command(source.local.command) if source.local.command 75: d.local.command(compress(nil, nil).first) 76: d.remote.command(decompress(nil).first) 77: end 78: 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.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 84 84: def copy_cache 85: @copy_cache ||= configuration[:copy_cache] == true ? 86: File.expand_path(configuration[:application], Dir.tmpdir) : 87: File.expand_path(configuration[:copy_cache], Dir.pwd) rescue nil 88: end
Obtains a copy of the source code locally (via the # 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.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 56 56: def deploy! 57: copy_cache ? run_copy_cache_strategy : run_copy_strategy 58: 59: create_revision_file 60: compress_repository 61: distribute! 62: ensure 63: rollback_changes 64: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 225 225: def build_script 226: configuration[:build_script] 227: end
Should return the command(s) necessary to obtain the source code locally.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 249 249: def command 250: @command ||= case copy_strategy 251: when :checkout 252: source.checkout(revision, destination) 253: when :export 254: source.export(revision, destination) 255: end 256: end
Returns the command necessary to compress the given directory into the given file.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 302 302: def compress(directory, file) 303: compression.compress_command + [file, directory] 304: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 209 209: def compress_repository 210: execute "Compressing #{destination} to #{filename}" do 211: Dir.chdir(copy_dir) { system(compress(File.basename(destination), File.basename(filename)).join(" ")) } 212: end 213: end
The compression method to use, defaults to :gzip.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 287 287: def compression 288: remote_tar = configuration[:copy_remote_tar] || 'tar' 289: local_tar = configuration[:copy_local_tar] || 'tar' 290: 291: type = configuration[:copy_compression] || :gzip 292: case type 293: when :gzip, :gz then Compression.new("tar.gz", [local_tar, 'czf'], [remote_tar, 'xzf']) 294: when :bzip2, :bz2 then Compression.new("tar.bz2", [local_tar, 'cjf'], [remote_tar, 'xjf']) 295: when :zip then Compression.new("zip", %(zip -qyr), %(unzip -q)) 296: else raise ArgumentError, "invalid compression type #{type.inspect}" 297: end 298: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 134 134: def copy_cache_to_staging_area 135: execute "copying cache to deployment staging area #{destination}" do 136: create_destination 137: Dir.chdir(copy_cache) { copy_files(queue_files) } 138: end 139: end
The directory to which the copy should be checked out
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 265 265: def copy_dir 266: @copy_dir ||= File.expand_path(configuration[:copy_dir] || Dir.tmpdir, Dir.pwd) 267: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 163 163: def copy_directory name 164: FileUtils.mkdir(File.join(destination, name)) 165: copy_files(queue_files(name)) 166: end
Specify patterns to exclude from the copy. This is only valid when using a local cache.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 231 231: def copy_exclude 232: @copy_exclude ||= Array(configuration.fetch(:copy_exclude, [])) 233: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 168 168: def copy_file name 169: FileUtils.ln(name, File.join(destination, name)) 170: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 145 145: def copy_files files 146: files.each { |name| process_file(name) } 147: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 159 159: def copy_link name 160: FileUtils.ln_s(File.readlink(name), File.join(destination, name)) 161: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 220 220: def copy_repository_to_local_cache 221: return refresh_local_cache if File.exists?(copy_cache) 222: create_local_cache 223: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 184 184: def copy_repository_to_server 185: execute "getting (via #{copy_strategy}) revision #{revision} to #{destination}" do 186: copy_repository_via_strategy 187: end 188: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 190 190: def copy_repository_via_strategy 191: system(command) 192: end
Returns the value of the :copy_strategy variable, defaulting to :checkout if it has not been set.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 243 243: def copy_strategy 244: @copy_strategy ||= configuration.fetch(:copy_strategy, :checkout) 245: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 141 141: def create_destination 142: FileUtils.mkdir_p(destination) 143: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 120 120: def create_local_cache 121: execute "preparing local cache at #{copy_cache}" do 122: system(source.checkout(revision, copy_cache)) 123: end 124: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 205 205: def create_revision_file 206: File.open(File.join(destination, "REVISION"), "w") { |f| f.puts(revision) } 207: end
Returns the command necessary to decompress the given file, relative to the current working directory. It must also preserve the directory structure in the file.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 309 309: def decompress(file) 310: compression.decompress_command + [file] 311: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 313 313: def decompress_remote_file 314: run "cd #{configuration[:releases_path]} && #{decompress(remote_filename).join(" ")} && rm #{remote_filename}" 315: end
Returns the basename of the release_path, which will be used to name the local copy and archive file.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 237 237: def destination 238: @destination ||= File.join(copy_dir, File.basename(configuration[:release_path])) 239: end
Distributes the file to the remote servers
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 318 318: def distribute! 319: upload(filename, remote_filename) 320: decompress_remote_file 321: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 180 180: def excluded_files_contain? file 181: copy_exclude.any? { |p| File.fnmatch(p, file) } or [ ".", ".."].include? File.basename(file) 182: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 104 104: def execute description, &block 105: logger.debug description 106: handle_system_errors &block 107: end
Returns the name of the file that the source code will be compressed to.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 260 260: def filename 261: @filename ||= File.join(copy_dir, "#{File.basename(destination)}.#{compression.extension}") 262: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 153 153: def filetype name 154: filetype = File.ftype name 155: filetype = "file" unless ["link", "directory"].include? filetype 156: filetype 157: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 109 109: def handle_system_errors &block 110: block.call 111: raise_command_failed if last_command_failed? 112: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 130 130: def last_command_failed? 131: $? != 0 132: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 176 176: def pattern_for directory 177: !directory.nil? ? "#{directory}/*" : "*" 178: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 149 149: def process_file name 150: send "copy_#{filetype(name)}", name 151: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 172 172: def queue_files directory=nil 173: Dir.glob(pattern_for(directory), File::FNM_DOTMATCH).reject! { |file| excluded_files_contain? file } 174: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 126 126: def raise_command_failed 127: raise Capistrano::Error, "shell command failed with return code #{$?}" 128: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 114 114: def refresh_local_cache 115: execute "refreshing local cache to revision #{revision} at #{copy_cache}" do 116: system(source.sync(revision, copy_cache)) 117: end 118: end
The directory on the remote server to which the archive should be copied
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 271 271: def remote_dir 272: @remote_dir ||= configuration[:copy_remote_dir] || "/tmp" 273: end
The location on the remote server where the file should be temporarily stored.
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 277 277: def remote_filename 278: @remote_filename ||= File.join(remote_dir, File.basename(filename)) 279: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 194 194: def remove_excluded_files 195: logger.debug "processing exclusions..." 196: 197: copy_exclude.each do |pattern| 198: delete_list = Dir.glob(File.join(destination, pattern), File::FNM_DOTMATCH) 199: # avoid the /.. trap that deletes the parent directories 200: delete_list.delete_if { |dir| dir =~ /\/\.\.$/ } 201: FileUtils.rm_rf(delete_list.compact) 202: end 203: end
# File lib/capistrano/recipes/deploy/strategy/copy.rb, line 215 215: def rollback_changes 216: FileUtils.rm filename rescue nil 217: FileUtils.rm_rf destination rescue nil 218: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.