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,78 @@
1
+ -----------------------------
2
+ <%= color('Capistrano', :bold) %>
3
+ -----------------------------
4
+
5
+ Capistrano is a utility for automating the execution of commands across multiple remote machines. It was originally conceived as an aid to deploy Ruby on Rails web applications, but has since evolved to become a much more general-purpose tool.
6
+
7
+ The command-line interface to Capistrano is via the `cap' command.
8
+
9
+ cap [ option ] ... action ...
10
+
11
+ The following options are understood:
12
+
13
+ <%= color '-d, --debug', :bold %>
14
+ Causes Capistrano to pause and prompt before executing any remote command, giving the user the option to either execute the command, skip the command, or abort execution entirely. This makes it a great way to troubleshoot tasks, or test custom tasks, by executing commands one at a time and checking the server to make sure they worked as expected before moving on to the next command. (Compare this to the --dry-run command.)
15
+
16
+ <%= color '-e, --explain TASK', :bold %>
17
+ Displays the extended description of the given task. Not all tasks will have an extended description, but for those that do, this can provide a wealth of additional usage information, such as describing environment variables or settings that can affect the execution of the task.
18
+
19
+ <%= color '-F, --default-config', :bold %>
20
+ By default, cap will search for a config file named `Capfile' or `capfile' in the current directory, or in any parent directory, and will automatically load it. However, if you specify the -f flag (see below), cap will use that file instead of the default config. If you want to use both the default config, and files loaded via -f, you can specify -F to force cap to search for and load the default config, even if additional files were specified via -f.
21
+
22
+ <%= color '-f, --file FILE', :bold %>
23
+ Causes the named file to be loaded. Capistrano will search both its own recipe directory, as well as the current directory, looking for the named file. An ".rb" extension is optional. The -f option may be given any number of times, but if it is given, it will take the place of the normal `Capfile' or `capfile' detection. Use -F if you want the default capfile to be loaded when you use -f.
24
+
25
+ <%= color '-H, --long-help', :bold %>
26
+ Displays this document and exits.
27
+
28
+ <%= color '-h, --help', :bold %>
29
+ Shows a brief summary of these options and exits.
30
+
31
+ <%= color '-l, --logger [STDERR|STDOUT|file]', :bold %>
32
+ Change the file used to print the output. It offers three options: standard error(stderr), standard output and file. Options are not case sensitive. By default Capistrano uses stderr.
33
+
34
+ <%= color '-n, --dry-run', :bold %>
35
+ Causes Capistrano to simply display each remote command, without executing it. In this sense it is similar to --debug, but without the prompt. Note that commands executed locally are still run--only remote commands are skipped.
36
+
37
+ <%= color '-p, --password', :bold %>
38
+ Normally, cap will prompt for the password on-demand, the first time it is needed. This can make it hard to walk away from Capistrano, since you might not know if it will prompt for a password down the road. In such cases, you can use the -p option to force cap to prompt for the password immediately.
39
+
40
+ <%= color '-q, --quiet', :bold %>
41
+ Display only critical error messages. All other output is suppressed.
42
+
43
+ <%= color '-S, --set-before NAME=VALUE', :bold %>
44
+ Sets the given variable to the given value, before loading any recipe files. This is useful if you have a recipe file that depends on a certain variable being set, at the time it is loaded.
45
+
46
+ <%= color '-s, --set NAME=VALUE', :bold %>
47
+ Sets the given variable to the given value, after loading all recipe files. This is useful when you want to override the value of a variable which is used in a task. Note that this will set the variables too late for them to affect conditions that are executed as the recipes are loaded.
48
+
49
+ <%= color '-T, --tasks PATTERN', :bold %>
50
+ Displays the list of all tasks (matching optional PATTERN) in all loaded recipe files. If a task has no description, or if the description starts with the [internal] tag, the task will not be listed unless you also specify -v.
51
+
52
+ <%= color '-t, --tool', :bold %>
53
+ Abbreviates the output of -T for integration with other tools. Without -t, -T will list tasks with their summaries, and may include additional instructive text at the bottom. When integrating with other tools (e.g., bash auto-expansion and the like) that additional text can get in the way. This switch makes it easier for those tools to parse the list of tasks. (The -t switch has no effect if the -T switch is not specified.)
54
+
55
+ <%= color '-V, --version', :bold %>
56
+ Shows the current Capistrano version number and exits.
57
+
58
+ <%= color '-v, --verbose', :bold %>
59
+ Increase the verbosity. You can specify this option up to three times to further increase verbosity. By default, cap will use maximum verbosity, but if you specify an explicit verbosity, that will be used instead. See also -q.
60
+
61
+ <%= color '-X, --skip-system-config', :bold %>
62
+ By default, cap will look for and (if it exists) load the global system configuration file located in /etc/capistrano.conf. If you don't want cap to load that file, give this option.
63
+
64
+ <%= color '-x, --skip-user-config', :bold %>
65
+ By default, cap will look for and (if it exists) load the user-specific configuration file located in $HOME/.caprc. If you don't want cap to load that file, give this option.
66
+
67
+ -----------------------------
68
+ <%= color('Environment Variables', :bold) %>
69
+ -----------------------------
70
+
71
+ <%= color 'HOSTS', :bold %>
72
+ Execute the tasks against this comma-separated list of hosts. Effectively, this makes the host(s) part of every roles.
73
+
74
+ <%= color 'HOSTFILTER', :bold %>
75
+ Execute tasks against this comma-separated list of host, but only if the host has the proper role for the task.
76
+
77
+ <%= color 'ROLES', :bold %>
78
+ Execute tasks against this comma-separated list of roles. Hosts which do not have the right roles will be skipped.
@@ -0,0 +1,243 @@
1
+ require 'optparse'
2
+
3
+ module Capistrano
4
+ class CLI
5
+ module Options
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ # Return a new CLI instance with the given arguments pre-parsed and
12
+ # ready for execution.
13
+ def parse(args)
14
+ cli = new(args)
15
+ cli.parse_options!
16
+ cli
17
+ end
18
+ end
19
+
20
+ # The hash of (parsed) command-line options
21
+ attr_reader :options
22
+
23
+ # Return an OptionParser instance that defines the acceptable command
24
+ # line switches for Capistrano, and what their corresponding behaviors
25
+ # are.
26
+ def option_parser #:nodoc:
27
+ @logger = Logger.new
28
+ @option_parser ||= OptionParser.new do |opts|
29
+ opts.banner = "Usage: #{File.basename($0)} [options] action ..."
30
+
31
+ opts.on("-d", "--debug",
32
+ "Prompts before each remote command execution."
33
+ ) { |value| options[:debug] = true }
34
+
35
+ opts.on("-e", "--explain TASK",
36
+ "Displays help (if available) for the task."
37
+ ) { |value| options[:explain] = value }
38
+
39
+ opts.on("-F", "--default-config",
40
+ "Always use default config, even with -f."
41
+ ) { options[:default_config] = true }
42
+
43
+ opts.on("-f", "--file FILE",
44
+ "A recipe file to load. May be given more than once."
45
+ ) { |value| options[:recipes] << value }
46
+
47
+ opts.on("-H", "--long-help", "Explain these options and environment variables.") do
48
+ long_help
49
+ exit
50
+ end
51
+
52
+ opts.on("-h", "--help", "Display this help message.") do
53
+ puts opts
54
+ exit
55
+ end
56
+
57
+ opts.on("-l", "--logger [STDERR|STDOUT|file]",
58
+ "Choose logger method. STDERR used by default."
59
+ ) do |value|
60
+ options[:output] = if value.nil? || value.upcase == 'STDERR'
61
+ # Using default logger.
62
+ nil
63
+ elsif value.upcase == 'STDOUT'
64
+ $stdout
65
+ else
66
+ value
67
+ end
68
+ end
69
+
70
+ opts.on("-n", "--dry-run",
71
+ "Prints out commands without running them."
72
+ ) { |value| options[:dry_run] = true }
73
+
74
+ opts.on("-p", "--password",
75
+ "Immediately prompt for the password."
76
+ ) { options[:password] = nil }
77
+
78
+ opts.on("-q", "--quiet",
79
+ "Make the output as quiet as possible."
80
+ ) { options[:verbose] = 0 }
81
+
82
+ opts.on("-r", "--preserve-roles",
83
+ "Preserve task roles"
84
+ ) { options[:preserve_roles] = true }
85
+
86
+ opts.on("-S", "--set-before NAME=VALUE",
87
+ "Set a variable before the recipes are loaded."
88
+ ) do |pair|
89
+ name, value = pair.split(/=/, 2)
90
+ options[:pre_vars][name.to_sym] = value
91
+ end
92
+
93
+ opts.on("-s", "--set NAME=VALUE",
94
+ "Set a variable after the recipes are loaded."
95
+ ) do |pair|
96
+ name, value = pair.split(/=/, 2)
97
+ options[:vars][name.to_sym] = value
98
+ end
99
+
100
+ opts.on("-T", "--tasks [PATTERN]",
101
+ "List all tasks (matching optional PATTERN) in the loaded recipe files."
102
+ ) do |value|
103
+ options[:tasks] = if value
104
+ value
105
+ else
106
+ true
107
+ end
108
+ options[:verbose] ||= 0
109
+ end
110
+
111
+ opts.on("-t", "--tool",
112
+ "Abbreviates the output of -T for tool integration."
113
+ ) { options[:tool] = true }
114
+
115
+ opts.on("-V", "--version",
116
+ "Display the Capistrano version, and exit."
117
+ ) do
118
+ require 'capistrano/version'
119
+ puts "Capistrano v#{Capistrano::Version}"
120
+ exit
121
+ end
122
+
123
+ opts.on("-v", "--verbose",
124
+ "Be more verbose. May be given more than once."
125
+ ) do
126
+ options[:verbose] ||= 0
127
+ options[:verbose] += 1
128
+ end
129
+
130
+ opts.on("-X", "--skip-system-config",
131
+ "Don't load the system config file (capistrano.conf)"
132
+ ) { options.delete(:sysconf) }
133
+
134
+ opts.on("-x", "--skip-user-config",
135
+ "Don't load the user config file (.caprc)"
136
+ ) { options.delete(:dotfile) }
137
+ end
138
+ end
139
+
140
+ # If the arguments to the command are empty, this will print the
141
+ # allowed options and exit. Otherwise, it will parse the command
142
+ # line and set up any default options.
143
+ def parse_options! #:nodoc:
144
+ @options = { :recipes => [], :actions => [],
145
+ :vars => {}, :pre_vars => {},
146
+ :sysconf => default_sysconf, :dotfile => default_dotfile }
147
+
148
+ if args.empty?
149
+ warn "Please specify at least one action to execute."
150
+ warn option_parser
151
+ exit
152
+ end
153
+
154
+ option_parser.parse!(args)
155
+
156
+ coerce_variable_types!
157
+
158
+ # if no verbosity has been specified, be verbose
159
+ options[:verbose] = 3 if !options.has_key?(:verbose)
160
+
161
+ look_for_default_recipe_file! if options[:default_config] || options[:recipes].empty?
162
+ extract_environment_variables!
163
+
164
+ options[:actions].concat(args)
165
+
166
+ password = options.has_key?(:password)
167
+ options[:password] = Proc.new { self.class.password_prompt }
168
+ options[:password] = options[:password].call if password
169
+ end
170
+
171
+ # Extracts name=value pairs from the remaining command-line arguments
172
+ # and assigns them as environment variables.
173
+ def extract_environment_variables! #:nodoc:
174
+ args.delete_if do |arg|
175
+ next unless arg.match(/^(\w+)=(.*)$/)
176
+ ENV[$1] = $2
177
+ end
178
+ end
179
+
180
+ # Looks for a default recipe file in the current directory.
181
+ def look_for_default_recipe_file! #:nodoc:
182
+ current = Dir.pwd
183
+
184
+ loop do
185
+ %w(Capfile capfile).each do |file|
186
+ if File.file?(file)
187
+ options[:recipes] << file
188
+ @logger.info "Using recipes from #{File.join(current,file)}"
189
+ return
190
+ end
191
+ end
192
+
193
+ pwd = Dir.pwd
194
+ Dir.chdir("..")
195
+ break if pwd == Dir.pwd # if changing the directory made no difference, then we're at the top
196
+ end
197
+
198
+ Dir.chdir(current)
199
+ end
200
+
201
+ def default_sysconf #:nodoc:
202
+ File.join(sysconf_directory, "capistrano.conf")
203
+ end
204
+
205
+ def default_dotfile #:nodoc:
206
+ File.join(home_directory, ".caprc")
207
+ end
208
+
209
+ def sysconf_directory #:nodoc:
210
+ # TODO if anyone cares, feel free to submit a patch that uses a more
211
+ # appropriate location for this file in Windows.
212
+ ENV["SystemRoot"] || '/etc'
213
+ end
214
+
215
+ def home_directory #:nodoc:
216
+ ENV["HOME"] ||
217
+ (ENV["HOMEPATH"] && "#{ENV["HOMEDRIVE"]}#{ENV["HOMEPATH"]}") ||
218
+ "/"
219
+ end
220
+
221
+ def coerce_variable_types!
222
+ [:pre_vars, :vars].each do |collection|
223
+ options[collection].keys.each do |key|
224
+ options[collection][key] = coerce_variable(options[collection][key])
225
+ end
226
+ end
227
+ end
228
+
229
+ def coerce_variable(value)
230
+ case value
231
+ when /^"(.*)"$/ then $1
232
+ when /^'(.*)'$/ then $1
233
+ when /^\d+$/ then value.to_i
234
+ when /^\d+\.\d*$/ then value.to_f
235
+ when "true" then true
236
+ when "false" then false
237
+ when "nil" then nil
238
+ else value
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
@@ -0,0 +1,40 @@
1
+ require 'highline'
2
+
3
+ # work around problem where HighLine detects an eof on $stdin and raises an
4
+ # error.
5
+ HighLine.track_eof = false
6
+
7
+ module Capistrano
8
+ class CLI
9
+ module UI
10
+ def self.included(base) #:nodoc:
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+ # Return the object that provides UI-specific methods, such as prompts
16
+ # and more.
17
+ def ui
18
+ @ui ||= HighLine.new
19
+ end
20
+
21
+ # Prompt for a password using echo suppression.
22
+ def password_prompt(prompt="Password: ")
23
+ ui.ask(prompt) { |q| q.echo = false }
24
+ end
25
+
26
+ # Debug mode prompt
27
+ def debug_prompt(cmd)
28
+ ui.say("Preparing to execute command: #{cmd}")
29
+ prompt = "Execute ([Yes], No, Abort) "
30
+ ui.ask("#{prompt}? ") do |q|
31
+ q.overwrite = false
32
+ q.default = 'y'
33
+ q.validate = /(y(es)?)|(no?)|(a(bort)?|\n)/i
34
+ q.responses[:not_valid] = prompt
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,283 @@
1
+ require 'capistrano/errors'
2
+ require 'capistrano/processable'
3
+
4
+ module Capistrano
5
+
6
+ # This class encapsulates a single command to be executed on a set of remote
7
+ # machines, in parallel.
8
+ class Command
9
+ include Processable
10
+
11
+ class Tree
12
+ attr_reader :configuration
13
+ attr_reader :branches
14
+ attr_reader :fallback
15
+
16
+ include Enumerable
17
+
18
+ class Branch
19
+ attr_accessor :command, :callback
20
+ attr_reader :options
21
+
22
+ def initialize(command, options, callback)
23
+ @command = command.strip.gsub(/\r?\n/, "\\\n")
24
+ @callback = callback || Capistrano::Configuration.default_io_proc
25
+ @options = options
26
+ @skip = false
27
+ end
28
+
29
+ def last?
30
+ options[:last]
31
+ end
32
+
33
+ def skip?
34
+ @skip
35
+ end
36
+
37
+ def skip!
38
+ @skip = true
39
+ end
40
+
41
+ def match(server)
42
+ true
43
+ end
44
+
45
+ def to_s
46
+ command.inspect
47
+ end
48
+ end
49
+
50
+ class ConditionBranch < Branch
51
+ attr_accessor :configuration
52
+ attr_accessor :condition
53
+
54
+ class Evaluator
55
+ attr_reader :configuration, :condition, :server
56
+
57
+ def initialize(config, condition, server)
58
+ @configuration = config
59
+ @condition = condition
60
+ @server = server
61
+ end
62
+
63
+ def in?(role)
64
+ configuration.roles[role].include?(server)
65
+ end
66
+
67
+ def result
68
+ eval(condition, binding)
69
+ end
70
+
71
+ def method_missing(sym, *args, &block)
72
+ if server.respond_to?(sym)
73
+ server.send(sym, *args, &block)
74
+ elsif configuration.respond_to?(sym)
75
+ configuration.send(sym, *args, &block)
76
+ else
77
+ super
78
+ end
79
+ end
80
+ end
81
+
82
+ def initialize(configuration, condition, command, options, callback)
83
+ @configuration = configuration
84
+ @condition = condition
85
+ super(command, options, callback)
86
+ end
87
+
88
+ def match(server)
89
+ Evaluator.new(configuration, condition, server).result
90
+ end
91
+
92
+ def to_s
93
+ "#{condition.inspect} :: #{command.inspect}"
94
+ end
95
+ end
96
+
97
+ def initialize(config)
98
+ @configuration = config
99
+ @branches = []
100
+ yield self if block_given?
101
+ end
102
+
103
+ def when(condition, command, options={}, &block)
104
+ branches << ConditionBranch.new(configuration, condition, command, options, block)
105
+ end
106
+
107
+ def else(command, &block)
108
+ @fallback = Branch.new(command, {}, block)
109
+ end
110
+
111
+ def branches_for(server)
112
+ seen_last = false
113
+ matches = branches.select do |branch|
114
+ success = !seen_last && !branch.skip? && branch.match(server)
115
+ seen_last = success && branch.last?
116
+ success
117
+ end
118
+
119
+ matches << fallback if matches.empty? && fallback
120
+ return matches
121
+ end
122
+
123
+ def each
124
+ branches.each { |branch| yield branch }
125
+ yield fallback if fallback
126
+ return self
127
+ end
128
+ end
129
+
130
+ attr_reader :tree, :sessions, :options
131
+
132
+ def self.process(tree, sessions, options={})
133
+ new(tree, sessions, options).process!
134
+ end
135
+
136
+ # Instantiates a new command object. The +command+ must be a string
137
+ # containing the command to execute. +sessions+ is an array of Net::SSH
138
+ # session instances, and +options+ must be a hash containing any of the
139
+ # following keys:
140
+ #
141
+ # * +logger+: (optional), a Capistrano::Logger instance
142
+ # * +data+: (optional), a string to be sent to the command via it's stdin
143
+ # * +env+: (optional), a string or hash to be interpreted as environment
144
+ # variables that should be defined for this command invocation.
145
+ def initialize(tree, sessions, options={}, &block)
146
+ if String === tree
147
+ tree = Tree.new(nil) { |t| t.else(tree, &block) }
148
+ elsif block
149
+ raise ArgumentError, "block given with tree argument"
150
+ end
151
+
152
+ @tree = tree
153
+ @sessions = sessions
154
+ @options = options
155
+ @channels = open_channels
156
+ end
157
+
158
+ # Processes the command in parallel on all specified hosts. If the command
159
+ # fails (non-zero return code) on any of the hosts, this will raise a
160
+ # Capistrano::CommandError.
161
+ def process!
162
+ loop do
163
+ break unless process_iteration { @channels.any? { |ch| !ch[:closed] } }
164
+ end
165
+
166
+ logger.trace "command finished" if logger
167
+
168
+ if (failed = @channels.select { |ch| ch[:status] != 0 }).any?
169
+ commands = failed.inject({}) { |map, ch| (map[ch[:command]] ||= []) << ch[:server]; map }
170
+ message = commands.map { |command, list| "#{command.inspect} on #{list.join(',')}" }.join("; ")
171
+ error = CommandError.new("failed: #{message}")
172
+ error.hosts = commands.values.flatten
173
+ raise error
174
+ end
175
+
176
+ self
177
+ end
178
+
179
+ # Force the command to stop processing, by closing all open channels
180
+ # associated with this command.
181
+ def stop!
182
+ @channels.each do |ch|
183
+ ch.close unless ch[:closed]
184
+ end
185
+ end
186
+
187
+ private
188
+
189
+ def logger
190
+ options[:logger]
191
+ end
192
+
193
+ def open_channels
194
+ sessions.map do |session|
195
+ server = session.xserver
196
+ tree.branches_for(server).map do |branch|
197
+ session.open_channel do |channel|
198
+ channel[:server] = server
199
+ channel[:host] = server.host
200
+ channel[:options] = options
201
+ channel[:branch] = branch
202
+
203
+ request_pty_if_necessary(channel) do |ch, success|
204
+ if success
205
+ logger.trace "executing command", ch[:server] if logger
206
+ cmd = replace_placeholders(channel[:branch].command, ch)
207
+
208
+ if options[:shell] == false
209
+ shell = nil
210
+ else
211
+ shell = "#{options[:shell] || "sh"} -c"
212
+ cmd = cmd.gsub(/'/) { |m| "'\\''" }
213
+ cmd = "'#{cmd}'"
214
+ end
215
+
216
+ command_line = [environment, shell, cmd].compact.join(" ")
217
+ ch[:command] = command_line
218
+
219
+ ch.exec(command_line)
220
+ ch.send_data(options[:data]) if options[:data]
221
+ else
222
+ # just log it, don't actually raise an exception, since the
223
+ # process method will see that the status is not zero and will
224
+ # raise an exception then.
225
+ logger.important "could not open channel", ch[:server] if logger
226
+ ch.close
227
+ end
228
+ end
229
+
230
+ channel.on_data do |ch, data|
231
+ ch[:branch].callback[ch, :out, data]
232
+ end
233
+
234
+ channel.on_extended_data do |ch, type, data|
235
+ ch[:branch].callback[ch, :err, data]
236
+ end
237
+
238
+ channel.on_request("exit-status") do |ch, data|
239
+ ch[:status] = data.read_long
240
+ end
241
+
242
+ channel.on_close do |ch|
243
+ ch[:closed] = true
244
+ end
245
+ end
246
+ end
247
+ end.flatten
248
+ end
249
+
250
+ def request_pty_if_necessary(channel)
251
+ if options[:pty]
252
+ channel.request_pty do |ch, success|
253
+ yield ch, success
254
+ end
255
+ else
256
+ yield channel, true
257
+ end
258
+ end
259
+
260
+ def replace_placeholders(command, channel)
261
+ command.gsub(/\$CAPISTRANO:HOST\$/, channel[:host])
262
+ end
263
+
264
+ # prepare a space-separated sequence of variables assignments
265
+ # intended to be prepended to a command, so the shell sets
266
+ # the environment before running the command.
267
+ # i.e.: options[:env] = {'PATH' => '/opt/ruby/bin:$PATH',
268
+ # 'TEST' => '( "quoted" )'}
269
+ # environment returns:
270
+ # "env TEST=(\ \"quoted\"\ ) PATH=/opt/ruby/bin:$PATH"
271
+ def environment
272
+ return if options[:env].nil? || options[:env].empty?
273
+ @environment ||= if String === options[:env]
274
+ "env #{options[:env]}"
275
+ else
276
+ options[:env].inject("env") do |string, (name, value)|
277
+ value = value.to_s.gsub(/[ "]/) { |m| "\\#{m}" }
278
+ string << " #{name}=#{value}"
279
+ end
280
+ end
281
+ end
282
+ end
283
+ end