le1t0-capistrano 2.5.18.001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gitignore +9 -0
  2. data/CHANGELOG +843 -0
  3. data/README +102 -0
  4. data/Rakefile +36 -0
  5. data/VERSION +1 -0
  6. data/bin/cap +4 -0
  7. data/bin/capify +86 -0
  8. data/lib/capistrano.rb +2 -0
  9. data/lib/capistrano/callback.rb +45 -0
  10. data/lib/capistrano/cli.rb +47 -0
  11. data/lib/capistrano/cli/execute.rb +85 -0
  12. data/lib/capistrano/cli/help.rb +125 -0
  13. data/lib/capistrano/cli/help.txt +78 -0
  14. data/lib/capistrano/cli/options.rb +243 -0
  15. data/lib/capistrano/cli/ui.rb +40 -0
  16. data/lib/capistrano/command.rb +283 -0
  17. data/lib/capistrano/configuration.rb +44 -0
  18. data/lib/capistrano/configuration/actions/file_transfer.rb +52 -0
  19. data/lib/capistrano/configuration/actions/inspect.rb +46 -0
  20. data/lib/capistrano/configuration/actions/invocation.rb +295 -0
  21. data/lib/capistrano/configuration/callbacks.rb +148 -0
  22. data/lib/capistrano/configuration/connections.rb +204 -0
  23. data/lib/capistrano/configuration/execution.rb +143 -0
  24. data/lib/capistrano/configuration/loading.rb +197 -0
  25. data/lib/capistrano/configuration/namespaces.rb +197 -0
  26. data/lib/capistrano/configuration/roles.rb +73 -0
  27. data/lib/capistrano/configuration/servers.rb +98 -0
  28. data/lib/capistrano/configuration/variables.rb +127 -0
  29. data/lib/capistrano/errors.rb +19 -0
  30. data/lib/capistrano/extensions.rb +57 -0
  31. data/lib/capistrano/logger.rb +59 -0
  32. data/lib/capistrano/processable.rb +53 -0
  33. data/lib/capistrano/recipes/compat.rb +32 -0
  34. data/lib/capistrano/recipes/deploy.rb +589 -0
  35. data/lib/capistrano/recipes/deploy/dependencies.rb +44 -0
  36. data/lib/capistrano/recipes/deploy/local_dependency.rb +54 -0
  37. data/lib/capistrano/recipes/deploy/remote_dependency.rb +105 -0
  38. data/lib/capistrano/recipes/deploy/scm.rb +19 -0
  39. data/lib/capistrano/recipes/deploy/scm/accurev.rb +169 -0
  40. data/lib/capistrano/recipes/deploy/scm/base.rb +196 -0
  41. data/lib/capistrano/recipes/deploy/scm/bzr.rb +86 -0
  42. data/lib/capistrano/recipes/deploy/scm/cvs.rb +152 -0
  43. data/lib/capistrano/recipes/deploy/scm/darcs.rb +96 -0
  44. data/lib/capistrano/recipes/deploy/scm/git.rb +278 -0
  45. data/lib/capistrano/recipes/deploy/scm/mercurial.rb +137 -0
  46. data/lib/capistrano/recipes/deploy/scm/none.rb +44 -0
  47. data/lib/capistrano/recipes/deploy/scm/perforce.rb +138 -0
  48. data/lib/capistrano/recipes/deploy/scm/subversion.rb +121 -0
  49. data/lib/capistrano/recipes/deploy/strategy.rb +19 -0
  50. data/lib/capistrano/recipes/deploy/strategy/base.rb +79 -0
  51. data/lib/capistrano/recipes/deploy/strategy/checkout.rb +20 -0
  52. data/lib/capistrano/recipes/deploy/strategy/copy.rb +218 -0
  53. data/lib/capistrano/recipes/deploy/strategy/export.rb +20 -0
  54. data/lib/capistrano/recipes/deploy/strategy/remote.rb +52 -0
  55. data/lib/capistrano/recipes/deploy/strategy/remote_cache.rb +56 -0
  56. data/lib/capistrano/recipes/deploy/templates/maintenance.rhtml +53 -0
  57. data/lib/capistrano/recipes/standard.rb +37 -0
  58. data/lib/capistrano/recipes/templates/maintenance.rhtml +53 -0
  59. data/lib/capistrano/role.rb +102 -0
  60. data/lib/capistrano/server_definition.rb +56 -0
  61. data/lib/capistrano/shell.rb +260 -0
  62. data/lib/capistrano/ssh.rb +99 -0
  63. data/lib/capistrano/task_definition.rb +75 -0
  64. data/lib/capistrano/transfer.rb +216 -0
  65. data/lib/capistrano/version.rb +18 -0
  66. data/test/cli/execute_test.rb +132 -0
  67. data/test/cli/help_test.rb +165 -0
  68. data/test/cli/options_test.rb +329 -0
  69. data/test/cli/ui_test.rb +28 -0
  70. data/test/cli_test.rb +17 -0
  71. data/test/command_test.rb +286 -0
  72. data/test/configuration/actions/file_transfer_test.rb +61 -0
  73. data/test/configuration/actions/inspect_test.rb +65 -0
  74. data/test/configuration/actions/invocation_test.rb +225 -0
  75. data/test/configuration/callbacks_test.rb +220 -0
  76. data/test/configuration/connections_test.rb +349 -0
  77. data/test/configuration/execution_test.rb +175 -0
  78. data/test/configuration/loading_test.rb +132 -0
  79. data/test/configuration/namespace_dsl_test.rb +311 -0
  80. data/test/configuration/roles_test.rb +144 -0
  81. data/test/configuration/servers_test.rb +158 -0
  82. data/test/configuration/variables_test.rb +184 -0
  83. data/test/configuration_test.rb +88 -0
  84. data/test/deploy/local_dependency_test.rb +76 -0
  85. data/test/deploy/remote_dependency_test.rb +114 -0
  86. data/test/deploy/scm/accurev_test.rb +23 -0
  87. data/test/deploy/scm/base_test.rb +55 -0
  88. data/test/deploy/scm/bzr_test.rb +51 -0
  89. data/test/deploy/scm/darcs_test.rb +37 -0
  90. data/test/deploy/scm/git_test.rb +184 -0
  91. data/test/deploy/scm/mercurial_test.rb +134 -0
  92. data/test/deploy/scm/none_test.rb +35 -0
  93. data/test/deploy/scm/subversion_test.rb +32 -0
  94. data/test/deploy/strategy/copy_test.rb +302 -0
  95. data/test/extensions_test.rb +69 -0
  96. data/test/fixtures/cli_integration.rb +5 -0
  97. data/test/fixtures/config.rb +5 -0
  98. data/test/fixtures/custom.rb +3 -0
  99. data/test/logger_test.rb +123 -0
  100. data/test/role_test.rb +11 -0
  101. data/test/server_definition_test.rb +121 -0
  102. data/test/shell_test.rb +90 -0
  103. data/test/ssh_test.rb +104 -0
  104. data/test/task_definition_test.rb +116 -0
  105. data/test/transfer_test.rb +160 -0
  106. data/test/utils.rb +39 -0
  107. metadata +289 -0
@@ -0,0 +1,79 @@
1
+ require 'capistrano/recipes/deploy/dependencies'
2
+
3
+ module Capistrano
4
+ module Deploy
5
+ module Strategy
6
+
7
+ # This class defines the abstract interface for all Capistrano
8
+ # deployment strategies. Subclasses must implement at least the
9
+ # #deploy! method.
10
+ class Base
11
+ attr_reader :configuration
12
+
13
+ # Instantiates a strategy with a reference to the given configuration.
14
+ def initialize(config={})
15
+ @configuration = config
16
+ end
17
+
18
+ # Executes the necessary commands to deploy the revision of the source
19
+ # code identified by the +revision+ variable. Additionally, this
20
+ # should write the value of the +revision+ variable to a file called
21
+ # REVISION, in the base of the deployed revision. This file is used by
22
+ # other tasks, to perform diffs and such.
23
+ def deploy!
24
+ raise NotImplementedError, "`deploy!' is not implemented by #{self.class.name}"
25
+ end
26
+
27
+ # Performs a check on the remote hosts to determine whether everything
28
+ # is setup such that a deploy could succeed.
29
+ def check!
30
+ Dependencies.new(configuration) do |d|
31
+ d.remote.directory(configuration[:releases_path]).or("`#{configuration[:releases_path]}' does not exist. Please run `cap deploy:setup'.")
32
+ d.remote.writable(configuration[:deploy_to]).or("You do not have permissions to write to `#{configuration[:deploy_to]}'.")
33
+ d.remote.writable(configuration[:releases_path]).or("You do not have permissions to write to `#{configuration[:releases_path]}'.")
34
+ end
35
+ end
36
+
37
+ protected
38
+
39
+ # This is to allow helper methods like "run" and "put" to be more
40
+ # easily accessible to strategy implementations.
41
+ def method_missing(sym, *args, &block)
42
+ if configuration.respond_to?(sym)
43
+ configuration.send(sym, *args, &block)
44
+ else
45
+ super
46
+ end
47
+ end
48
+
49
+ # A wrapper for Kernel#system that logs the command being executed.
50
+ def system(*args)
51
+ cmd = args.join(' ')
52
+ if RUBY_PLATFORM =~ /win32/
53
+ cmd = cmd.split(/\s+/).collect {|w| w.match(/^[\w+]+:\/\//) ? w : w.gsub('/', '\\') }.join(' ') # Split command by spaces, change / by \\ unless element is a some+thing://
54
+ cmd.gsub!(/^cd /,'cd /D ') # Replace cd with cd /D
55
+ cmd.gsub!(/&& cd /,'&& cd /D ') # Replace cd with cd /D
56
+ logger.trace "executing locally: #{cmd}"
57
+ super(cmd)
58
+ else
59
+ logger.trace "executing locally: #{cmd}"
60
+ super
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def logger
67
+ @logger ||= configuration[:logger] || Capistrano::Logger.new(:output => STDOUT)
68
+ end
69
+
70
+ # The revision to deploy. Must return a real revision identifier,
71
+ # and not a pseudo-id.
72
+ def revision
73
+ configuration[:real_revision]
74
+ end
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,20 @@
1
+ require 'capistrano/recipes/deploy/strategy/remote'
2
+
3
+ module Capistrano
4
+ module Deploy
5
+ module Strategy
6
+
7
+ # Implements the deployment strategy which does an SCM checkout on each
8
+ # target host. This is the default deployment strategy for Capistrano.
9
+ class Checkout < Remote
10
+ protected
11
+
12
+ # Returns the SCM's checkout command for the revision to deploy.
13
+ def command
14
+ @command ||= source.checkout(revision, configuration[:release_path])
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,218 @@
1
+ require 'capistrano/recipes/deploy/strategy/base'
2
+ require 'fileutils'
3
+ require 'tempfile' # Dir.tmpdir
4
+
5
+ module Capistrano
6
+ module Deploy
7
+ module Strategy
8
+
9
+ # This class implements the strategy for deployments which work
10
+ # by preparing the source code locally, compressing it, copying the
11
+ # file to each target host, and uncompressing it to the deployment
12
+ # directory.
13
+ #
14
+ # By default, the SCM checkout command is used to obtain the local copy
15
+ # of the source code. If you would rather use the export operation,
16
+ # you can set the :copy_strategy variable to :export.
17
+ #
18
+ # set :copy_strategy, :export
19
+ #
20
+ # For even faster deployments, you can set the :copy_cache variable to
21
+ # true. This will cause deployments to do a new checkout of your
22
+ # repository to a new directory, and then copy that checkout. Subsequent
23
+ # deploys will just resync that copy, rather than doing an entirely new
24
+ # checkout. Additionally, you can specify file patterns to exclude from
25
+ # the copy when using :copy_cache; just set the :copy_exclude variable
26
+ # to a file glob (or an array of globs).
27
+ #
28
+ # set :copy_cache, true
29
+ # set :copy_exclude, ".git/*"
30
+ #
31
+ # Note that :copy_strategy is ignored when :copy_cache is set. Also, if
32
+ # you want the copy cache put somewhere specific, you can set the variable
33
+ # to the path you want, instead of merely 'true':
34
+ #
35
+ # set :copy_cache, "/tmp/caches/myapp"
36
+ #
37
+ # This deployment strategy also supports a special variable,
38
+ # :copy_compression, which must be one of :gzip, :bz2, or
39
+ # :zip, and which specifies how the source should be compressed for
40
+ # transmission to each host.
41
+ class Copy < Base
42
+ # Obtains a copy of the source code locally (via the #command method),
43
+ # compresses it to a single file, copies that file to all target
44
+ # servers, and uncompresses it on each of them into the deployment
45
+ # directory.
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
+ # Check the return code of last system command and rollback if not 0
57
+ unless $? == 0
58
+ raise Capistrano::Error, "shell command failed with return code #{$?}"
59
+ end
60
+
61
+ logger.debug "copying cache to deployment staging area #{destination}"
62
+ Dir.chdir(copy_cache) do
63
+ FileUtils.mkdir_p(destination)
64
+ queue = Dir.glob("*", File::FNM_DOTMATCH)
65
+ while queue.any?
66
+ item = queue.shift
67
+ name = File.basename(item)
68
+
69
+ next if name == "." || name == ".."
70
+ next if copy_exclude.any? { |pattern| File.fnmatch(pattern, item) }
71
+
72
+ if File.symlink?(item)
73
+ FileUtils.ln_s(File.readlink(File.join(copy_cache, item)), File.join(destination, item))
74
+ elsif File.directory?(item)
75
+ queue += Dir.glob("#{item}/*", File::FNM_DOTMATCH)
76
+ FileUtils.mkdir(File.join(destination, item))
77
+ else
78
+ FileUtils.ln(File.join(copy_cache, item), File.join(destination, item))
79
+ end
80
+ end
81
+ end
82
+ else
83
+ logger.debug "getting (via #{copy_strategy}) revision #{revision} to #{destination}"
84
+ system(command)
85
+
86
+ if copy_exclude.any?
87
+ logger.debug "processing exclusions..."
88
+ if copy_exclude.any?
89
+ copy_exclude.each do |pattern|
90
+ delete_list = Dir.glob(File.join(destination, pattern), File::FNM_DOTMATCH)
91
+ # avoid the /.. trap that deletes the parent directories
92
+ delete_list.delete_if { |dir| dir =~ /\/\.\.$/ }
93
+ FileUtils.rm_rf(delete_list.compact)
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ File.open(File.join(destination, "REVISION"), "w") { |f| f.puts(revision) }
100
+
101
+ logger.trace "compressing #{destination} to #{filename}"
102
+ Dir.chdir(tmpdir) { system(compress(File.basename(destination), File.basename(filename)).join(" ")) }
103
+
104
+ upload(filename, remote_filename)
105
+ run "cd #{configuration[:releases_path]} && #{decompress(remote_filename).join(" ")} && rm #{remote_filename}"
106
+ ensure
107
+ FileUtils.rm filename rescue nil
108
+ FileUtils.rm_rf destination rescue nil
109
+ end
110
+
111
+ def check!
112
+ super.check do |d|
113
+ d.local.command(source.local.command) if source.local.command
114
+ d.local.command(compress(nil, nil).first)
115
+ d.remote.command(decompress(nil).first)
116
+ end
117
+ end
118
+
119
+ # Returns the location of the local copy cache, if the strategy should
120
+ # use a local cache + copy instead of a new checkout/export every
121
+ # time. Returns +nil+ unless :copy_cache has been set. If :copy_cache
122
+ # is +true+, a default cache location will be returned.
123
+ def copy_cache
124
+ @copy_cache ||= configuration[:copy_cache] == true ?
125
+ File.join(Dir.tmpdir, configuration[:application]) :
126
+ configuration[:copy_cache]
127
+ end
128
+
129
+ private
130
+
131
+ # Specify patterns to exclude from the copy. This is only valid
132
+ # when using a local cache.
133
+ def copy_exclude
134
+ @copy_exclude ||= Array(configuration.fetch(:copy_exclude, []))
135
+ end
136
+
137
+ # Returns the basename of the release_path, which will be used to
138
+ # name the local copy and archive file.
139
+ def destination
140
+ @destination ||= File.join(tmpdir, File.basename(configuration[:release_path]))
141
+ end
142
+
143
+ # Returns the value of the :copy_strategy variable, defaulting to
144
+ # :checkout if it has not been set.
145
+ def copy_strategy
146
+ @copy_strategy ||= configuration.fetch(:copy_strategy, :checkout)
147
+ end
148
+
149
+ # Should return the command(s) necessary to obtain the source code
150
+ # locally.
151
+ def command
152
+ @command ||= case copy_strategy
153
+ when :checkout
154
+ source.checkout(revision, destination)
155
+ when :export
156
+ source.export(revision, destination)
157
+ end
158
+ end
159
+
160
+ # Returns the name of the file that the source code will be
161
+ # compressed to.
162
+ def filename
163
+ @filename ||= File.join(tmpdir, "#{File.basename(destination)}.#{compression.extension}")
164
+ end
165
+
166
+ # The directory to which the copy should be checked out
167
+ def tmpdir
168
+ @tmpdir ||= configuration[:copy_dir] || Dir.tmpdir
169
+ end
170
+
171
+ # The directory on the remote server to which the archive should be
172
+ # copied
173
+ def remote_dir
174
+ @remote_dir ||= configuration[:copy_remote_dir] || "/tmp"
175
+ end
176
+
177
+ # The location on the remote server where the file should be
178
+ # temporarily stored.
179
+ def remote_filename
180
+ @remote_filename ||= File.join(remote_dir, File.basename(filename))
181
+ end
182
+
183
+ # A struct for representing the specifics of a compression type.
184
+ # Commands are arrays, where the first element is the utility to be
185
+ # used to perform the compression or decompression.
186
+ Compression = Struct.new(:extension, :compress_command, :decompress_command)
187
+
188
+ # The compression method to use, defaults to :gzip.
189
+ def compression
190
+ remote_tar = configuration[:copy_remote_tar] || 'tar'
191
+ local_tar = configuration[:copy_local_tar] || 'tar'
192
+
193
+ type = configuration[:copy_compression] || :gzip
194
+ case type
195
+ when :gzip, :gz then Compression.new("tar.gz", [local_tar, 'czf'], [remote_tar, 'xzf'])
196
+ when :bzip2, :bz2 then Compression.new("tar.bz2", [local_tar, 'cjf'], [remote_tar, 'xjf'])
197
+ when :zip then Compression.new("zip", %w(zip -qr), %w(unzip -q))
198
+ else raise ArgumentError, "invalid compression type #{type.inspect}"
199
+ end
200
+ end
201
+
202
+ # Returns the command necessary to compress the given directory
203
+ # into the given file.
204
+ def compress(directory, file)
205
+ compression.compress_command + [file, directory]
206
+ end
207
+
208
+ # Returns the command necessary to decompress the given file,
209
+ # relative to the current working directory. It must also
210
+ # preserve the directory structure in the file.
211
+ def decompress(file)
212
+ compression.decompress_command + [file]
213
+ end
214
+ end
215
+
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,20 @@
1
+ require 'capistrano/recipes/deploy/strategy/remote'
2
+
3
+ module Capistrano
4
+ module Deploy
5
+ module Strategy
6
+
7
+ # Implements the deployment strategy which does an SCM export on each
8
+ # target host.
9
+ class Export < Remote
10
+ protected
11
+
12
+ # Returns the SCM's export command for the revision to deploy.
13
+ def command
14
+ @command ||= source.export(revision, configuration[:release_path])
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,52 @@
1
+ require 'capistrano/recipes/deploy/strategy/base'
2
+
3
+ module Capistrano
4
+ module Deploy
5
+ module Strategy
6
+
7
+ # An abstract superclass, which forms the base for all deployment
8
+ # strategies which work by grabbing the code from the repository directly
9
+ # from remote host. This includes deploying by checkout (the default),
10
+ # and deploying by export.
11
+ class Remote < Base
12
+ # Executes the SCM command for this strategy and writes the REVISION
13
+ # mark file to each host.
14
+ def deploy!
15
+ scm_run "#{command} && #{mark}"
16
+ end
17
+
18
+ def check!
19
+ super.check do |d|
20
+ d.remote.command(source.command)
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ # Runs the given command, filtering output back through the
27
+ # #handle_data filter of the SCM implementation.
28
+ def scm_run(command)
29
+ run(command) do |ch,stream,text|
30
+ ch[:state] ||= { :channel => ch }
31
+ output = source.handle_data(ch[:state], stream, text)
32
+ ch.send_data(output) if output
33
+ end
34
+ end
35
+
36
+ # An abstract method which must be overridden in subclasses, to
37
+ # return the actual SCM command(s) which must be executed on each
38
+ # target host in order to perform the deployment.
39
+ def command
40
+ raise NotImplementedError, "`command' is not implemented by #{self.class.name}"
41
+ end
42
+
43
+ # Returns the command which will write the identifier of the
44
+ # revision being deployed to the REVISION file on each host.
45
+ def mark
46
+ "(echo #{revision} > #{configuration[:release_path]}/REVISION)"
47
+ end
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,56 @@
1
+ require 'capistrano/recipes/deploy/strategy/remote'
2
+
3
+ module Capistrano
4
+ module Deploy
5
+ module Strategy
6
+
7
+ # Implements the deployment strategy that keeps a cached checkout of
8
+ # the source code on each remote server. Each deploy simply updates the
9
+ # cached checkout, and then does a copy from the cached copy to the
10
+ # final deployment location.
11
+ class RemoteCache < Remote
12
+ # Executes the SCM command for this strategy and writes the REVISION
13
+ # mark file to each host.
14
+ def deploy!
15
+ update_repository_cache
16
+ copy_repository_cache
17
+ end
18
+
19
+ def check!
20
+ super.check do |d|
21
+ d.remote.writable(shared_path)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def repository_cache
28
+ File.join(shared_path, configuration[:repository_cache] || "cached-copy")
29
+ end
30
+
31
+ def update_repository_cache
32
+ logger.trace "updating the cached checkout on all servers"
33
+ command = "if [ -d #{repository_cache} ]; then " +
34
+ "#{source.sync(revision, repository_cache)}; " +
35
+ "else #{source.checkout(revision, repository_cache)}; fi"
36
+ scm_run(command)
37
+ end
38
+
39
+ def copy_repository_cache
40
+ logger.trace "copying the cached version to #{configuration[:release_path]}"
41
+ if copy_exclude.empty?
42
+ run "cp -RPp #{repository_cache} #{configuration[:release_path]} && #{mark}"
43
+ else
44
+ exclusions = copy_exclude.map { |e| "--exclude=\"#{e}\"" }.join(' ')
45
+ run "rsync -lrpt #{exclusions} #{repository_cache}/* #{configuration[:release_path]} && #{mark}"
46
+ end
47
+ end
48
+
49
+ def copy_exclude
50
+ @copy_exclude ||= Array(configuration.fetch(:copy_exclude, []))
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end