HeSYINUvSBZfxqA-capistrano 2.5.21
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/CHANGELOG +866 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +31 -0
- data/HeSYINUvSBZfxqA-capistrano.gemspec +49 -0
- data/README.mdown +65 -0
- data/Rakefile +11 -0
- data/VERSION +1 -0
- data/bin/cap +4 -0
- data/bin/capify +86 -0
- data/lib/capistrano.rb +2 -0
- data/lib/capistrano/callback.rb +45 -0
- data/lib/capistrano/cli.rb +47 -0
- data/lib/capistrano/cli/execute.rb +85 -0
- data/lib/capistrano/cli/help.rb +125 -0
- data/lib/capistrano/cli/help.txt +78 -0
- data/lib/capistrano/cli/options.rb +243 -0
- data/lib/capistrano/cli/ui.rb +40 -0
- data/lib/capistrano/command.rb +286 -0
- data/lib/capistrano/configuration.rb +44 -0
- data/lib/capistrano/configuration/actions/file_transfer.rb +47 -0
- data/lib/capistrano/configuration/actions/inspect.rb +46 -0
- data/lib/capistrano/configuration/actions/invocation.rb +295 -0
- data/lib/capistrano/configuration/callbacks.rb +148 -0
- data/lib/capistrano/configuration/connections.rb +204 -0
- data/lib/capistrano/configuration/execution.rb +143 -0
- data/lib/capistrano/configuration/loading.rb +197 -0
- data/lib/capistrano/configuration/namespaces.rb +197 -0
- data/lib/capistrano/configuration/roles.rb +73 -0
- data/lib/capistrano/configuration/servers.rb +98 -0
- data/lib/capistrano/configuration/variables.rb +127 -0
- data/lib/capistrano/errors.rb +19 -0
- data/lib/capistrano/extensions.rb +57 -0
- data/lib/capistrano/logger.rb +59 -0
- data/lib/capistrano/processable.rb +53 -0
- data/lib/capistrano/recipes/compat.rb +32 -0
- data/lib/capistrano/recipes/deploy.rb +597 -0
- data/lib/capistrano/recipes/deploy/dependencies.rb +44 -0
- data/lib/capistrano/recipes/deploy/local_dependency.rb +54 -0
- data/lib/capistrano/recipes/deploy/remote_dependency.rb +111 -0
- data/lib/capistrano/recipes/deploy/scm.rb +19 -0
- data/lib/capistrano/recipes/deploy/scm/accurev.rb +169 -0
- data/lib/capistrano/recipes/deploy/scm/base.rb +196 -0
- data/lib/capistrano/recipes/deploy/scm/bzr.rb +86 -0
- data/lib/capistrano/recipes/deploy/scm/cvs.rb +153 -0
- data/lib/capistrano/recipes/deploy/scm/darcs.rb +96 -0
- data/lib/capistrano/recipes/deploy/scm/git.rb +274 -0
- data/lib/capistrano/recipes/deploy/scm/mercurial.rb +137 -0
- data/lib/capistrano/recipes/deploy/scm/none.rb +44 -0
- data/lib/capistrano/recipes/deploy/scm/perforce.rb +138 -0
- data/lib/capistrano/recipes/deploy/scm/subversion.rb +121 -0
- data/lib/capistrano/recipes/deploy/strategy.rb +19 -0
- data/lib/capistrano/recipes/deploy/strategy/base.rb +88 -0
- data/lib/capistrano/recipes/deploy/strategy/checkout.rb +20 -0
- data/lib/capistrano/recipes/deploy/strategy/copy.rb +223 -0
- data/lib/capistrano/recipes/deploy/strategy/export.rb +20 -0
- data/lib/capistrano/recipes/deploy/strategy/remote.rb +52 -0
- data/lib/capistrano/recipes/deploy/strategy/remote_cache.rb +57 -0
- data/lib/capistrano/recipes/deploy/templates/maintenance.rhtml +53 -0
- data/lib/capistrano/recipes/standard.rb +37 -0
- data/lib/capistrano/recipes/templates/maintenance.rhtml +53 -0
- data/lib/capistrano/role.rb +102 -0
- data/lib/capistrano/server_definition.rb +56 -0
- data/lib/capistrano/shell.rb +260 -0
- data/lib/capistrano/ssh.rb +101 -0
- data/lib/capistrano/task_definition.rb +75 -0
- data/lib/capistrano/transfer.rb +216 -0
- data/lib/capistrano/version.rb +18 -0
- data/rvmrc.sample +1 -0
- data/test/cli/execute_test.rb +132 -0
- data/test/cli/help_test.rb +165 -0
- data/test/cli/options_test.rb +329 -0
- data/test/cli/ui_test.rb +28 -0
- data/test/cli_test.rb +17 -0
- data/test/command_test.rb +286 -0
- data/test/configuration/actions/file_transfer_test.rb +61 -0
- data/test/configuration/actions/inspect_test.rb +65 -0
- data/test/configuration/actions/invocation_test.rb +225 -0
- data/test/configuration/callbacks_test.rb +220 -0
- data/test/configuration/connections_test.rb +349 -0
- data/test/configuration/execution_test.rb +175 -0
- data/test/configuration/loading_test.rb +132 -0
- data/test/configuration/namespace_dsl_test.rb +311 -0
- data/test/configuration/roles_test.rb +144 -0
- data/test/configuration/servers_test.rb +158 -0
- data/test/configuration/variables_test.rb +190 -0
- data/test/configuration_test.rb +88 -0
- data/test/deploy/local_dependency_test.rb +76 -0
- data/test/deploy/remote_dependency_test.rb +135 -0
- data/test/deploy/scm/accurev_test.rb +23 -0
- data/test/deploy/scm/base_test.rb +55 -0
- data/test/deploy/scm/bzr_test.rb +51 -0
- data/test/deploy/scm/darcs_test.rb +37 -0
- data/test/deploy/scm/git_test.rb +184 -0
- data/test/deploy/scm/mercurial_test.rb +134 -0
- data/test/deploy/scm/none_test.rb +35 -0
- data/test/deploy/scm/subversion_test.rb +32 -0
- data/test/deploy/strategy/copy_test.rb +302 -0
- data/test/extensions_test.rb +69 -0
- data/test/fixtures/cli_integration.rb +5 -0
- data/test/fixtures/config.rb +5 -0
- data/test/fixtures/custom.rb +3 -0
- data/test/logger_test.rb +123 -0
- data/test/role_test.rb +11 -0
- data/test/server_definition_test.rb +121 -0
- data/test/shell_test.rb +90 -0
- data/test/ssh_test.rb +113 -0
- data/test/task_definition_test.rb +116 -0
- data/test/transfer_test.rb +160 -0
- data/test/utils.rb +39 -0
- metadata +271 -0
@@ -0,0 +1,286 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
require 'capistrano/errors'
|
3
|
+
require 'capistrano/processable'
|
4
|
+
|
5
|
+
module Capistrano
|
6
|
+
|
7
|
+
# This class encapsulates a single command to be executed on a set of remote
|
8
|
+
# machines, in parallel.
|
9
|
+
class Command
|
10
|
+
include Processable
|
11
|
+
|
12
|
+
class Tree
|
13
|
+
attr_reader :configuration
|
14
|
+
attr_reader :branches
|
15
|
+
attr_reader :fallback
|
16
|
+
|
17
|
+
include Enumerable
|
18
|
+
|
19
|
+
class Branch
|
20
|
+
attr_accessor :command, :callback
|
21
|
+
attr_reader :options
|
22
|
+
|
23
|
+
def initialize(command, options, callback)
|
24
|
+
@command = command.strip.gsub(/\r?\n/, "\\\n")
|
25
|
+
@callback = callback || Capistrano::Configuration.default_io_proc
|
26
|
+
@options = options
|
27
|
+
@skip = false
|
28
|
+
end
|
29
|
+
|
30
|
+
def last?
|
31
|
+
options[:last]
|
32
|
+
end
|
33
|
+
|
34
|
+
def skip?
|
35
|
+
@skip
|
36
|
+
end
|
37
|
+
|
38
|
+
def skip!
|
39
|
+
@skip = true
|
40
|
+
end
|
41
|
+
|
42
|
+
def match(server)
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_s
|
47
|
+
command.inspect
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class ConditionBranch < Branch
|
52
|
+
attr_accessor :configuration
|
53
|
+
attr_accessor :condition
|
54
|
+
|
55
|
+
class Evaluator
|
56
|
+
attr_reader :configuration, :condition, :server
|
57
|
+
|
58
|
+
def initialize(config, condition, server)
|
59
|
+
@configuration = config
|
60
|
+
@condition = condition
|
61
|
+
@server = server
|
62
|
+
end
|
63
|
+
|
64
|
+
def in?(role)
|
65
|
+
configuration.roles[role].include?(server)
|
66
|
+
end
|
67
|
+
|
68
|
+
def result
|
69
|
+
eval(condition, binding)
|
70
|
+
end
|
71
|
+
|
72
|
+
def method_missing(sym, *args, &block)
|
73
|
+
if server.respond_to?(sym)
|
74
|
+
server.send(sym, *args, &block)
|
75
|
+
elsif configuration.respond_to?(sym)
|
76
|
+
configuration.send(sym, *args, &block)
|
77
|
+
else
|
78
|
+
super
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def initialize(configuration, condition, command, options, callback)
|
84
|
+
@configuration = configuration
|
85
|
+
@condition = condition
|
86
|
+
super(command, options, callback)
|
87
|
+
end
|
88
|
+
|
89
|
+
def match(server)
|
90
|
+
Evaluator.new(configuration, condition, server).result
|
91
|
+
end
|
92
|
+
|
93
|
+
def to_s
|
94
|
+
"#{condition.inspect} :: #{command.inspect}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def initialize(config)
|
99
|
+
@configuration = config
|
100
|
+
@branches = []
|
101
|
+
yield self if block_given?
|
102
|
+
end
|
103
|
+
|
104
|
+
def when(condition, command, options={}, &block)
|
105
|
+
branches << ConditionBranch.new(configuration, condition, command, options, block)
|
106
|
+
end
|
107
|
+
|
108
|
+
def else(command, &block)
|
109
|
+
@fallback = Branch.new(command, {}, block)
|
110
|
+
end
|
111
|
+
|
112
|
+
def branches_for(server)
|
113
|
+
seen_last = false
|
114
|
+
matches = branches.select do |branch|
|
115
|
+
success = !seen_last && !branch.skip? && branch.match(server)
|
116
|
+
seen_last = success && branch.last?
|
117
|
+
success
|
118
|
+
end
|
119
|
+
|
120
|
+
matches << fallback if matches.empty? && fallback
|
121
|
+
return matches
|
122
|
+
end
|
123
|
+
|
124
|
+
def each
|
125
|
+
branches.each { |branch| yield branch }
|
126
|
+
yield fallback if fallback
|
127
|
+
return self
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
attr_reader :tree, :sessions, :options
|
132
|
+
|
133
|
+
def self.process(tree, sessions, options={})
|
134
|
+
new(tree, sessions, options).process!
|
135
|
+
end
|
136
|
+
|
137
|
+
# Instantiates a new command object. The +command+ must be a string
|
138
|
+
# containing the command to execute. +sessions+ is an array of Net::SSH
|
139
|
+
# session instances, and +options+ must be a hash containing any of the
|
140
|
+
# following keys:
|
141
|
+
#
|
142
|
+
# * +logger+: (optional), a Capistrano::Logger instance
|
143
|
+
# * +data+: (optional), a string to be sent to the command via it's stdin
|
144
|
+
# * +env+: (optional), a string or hash to be interpreted as environment
|
145
|
+
# variables that should be defined for this command invocation.
|
146
|
+
def initialize(tree, sessions, options={}, &block)
|
147
|
+
if String === tree
|
148
|
+
tree = Tree.new(nil) { |t| t.else(tree, &block) }
|
149
|
+
elsif block
|
150
|
+
raise ArgumentError, "block given with tree argument"
|
151
|
+
end
|
152
|
+
|
153
|
+
@tree = tree
|
154
|
+
@sessions = sessions
|
155
|
+
@options = options
|
156
|
+
@channels = open_channels
|
157
|
+
end
|
158
|
+
|
159
|
+
# Processes the command in parallel on all specified hosts. If the command
|
160
|
+
# fails (non-zero return code) on any of the hosts, this will raise a
|
161
|
+
# Capistrano::CommandError.
|
162
|
+
def process!
|
163
|
+
elapsed = Benchmark.realtime do
|
164
|
+
loop do
|
165
|
+
break unless process_iteration { @channels.any? { |ch| !ch[:closed] } }
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
logger.trace "command finished in #{(elapsed * 1000).round}ms" if logger
|
170
|
+
|
171
|
+
if (failed = @channels.select { |ch| ch[:status] != 0 }).any?
|
172
|
+
commands = failed.inject({}) { |map, ch| (map[ch[:command]] ||= []) << ch[:server]; map }
|
173
|
+
message = commands.map { |command, list| "#{command.inspect} on #{list.join(',')}" }.join("; ")
|
174
|
+
error = CommandError.new("failed: #{message}")
|
175
|
+
error.hosts = commands.values.flatten
|
176
|
+
raise error
|
177
|
+
end
|
178
|
+
|
179
|
+
self
|
180
|
+
end
|
181
|
+
|
182
|
+
# Force the command to stop processing, by closing all open channels
|
183
|
+
# associated with this command.
|
184
|
+
def stop!
|
185
|
+
@channels.each do |ch|
|
186
|
+
ch.close unless ch[:closed]
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
|
192
|
+
def logger
|
193
|
+
options[:logger]
|
194
|
+
end
|
195
|
+
|
196
|
+
def open_channels
|
197
|
+
sessions.map do |session|
|
198
|
+
server = session.xserver
|
199
|
+
tree.branches_for(server).map do |branch|
|
200
|
+
session.open_channel do |channel|
|
201
|
+
channel[:server] = server
|
202
|
+
channel[:host] = server.host
|
203
|
+
channel[:options] = options
|
204
|
+
channel[:branch] = branch
|
205
|
+
|
206
|
+
request_pty_if_necessary(channel) do |ch, success|
|
207
|
+
if success
|
208
|
+
logger.trace "executing command", ch[:server] if logger
|
209
|
+
cmd = replace_placeholders(channel[:branch].command, ch)
|
210
|
+
|
211
|
+
if options[:shell] == false
|
212
|
+
shell = nil
|
213
|
+
else
|
214
|
+
shell = "#{options[:shell] || "sh"} -c"
|
215
|
+
cmd = cmd.gsub(/'/) { |m| "'\\''" }
|
216
|
+
cmd = "'#{cmd}'"
|
217
|
+
end
|
218
|
+
|
219
|
+
command_line = [environment, shell, cmd].compact.join(" ")
|
220
|
+
ch[:command] = command_line
|
221
|
+
|
222
|
+
ch.exec(command_line)
|
223
|
+
ch.send_data(options[:data]) if options[:data]
|
224
|
+
else
|
225
|
+
# just log it, don't actually raise an exception, since the
|
226
|
+
# process method will see that the status is not zero and will
|
227
|
+
# raise an exception then.
|
228
|
+
logger.important "could not open channel", ch[:server] if logger
|
229
|
+
ch.close
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
channel.on_data do |ch, data|
|
234
|
+
ch[:branch].callback[ch, :out, data]
|
235
|
+
end
|
236
|
+
|
237
|
+
channel.on_extended_data do |ch, type, data|
|
238
|
+
ch[:branch].callback[ch, :err, data]
|
239
|
+
end
|
240
|
+
|
241
|
+
channel.on_request("exit-status") do |ch, data|
|
242
|
+
ch[:status] = data.read_long
|
243
|
+
end
|
244
|
+
|
245
|
+
channel.on_close do |ch|
|
246
|
+
ch[:closed] = true
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end.flatten
|
251
|
+
end
|
252
|
+
|
253
|
+
def request_pty_if_necessary(channel)
|
254
|
+
if options[:pty]
|
255
|
+
channel.request_pty do |ch, success|
|
256
|
+
yield ch, success
|
257
|
+
end
|
258
|
+
else
|
259
|
+
yield channel, true
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
def replace_placeholders(command, channel)
|
264
|
+
command.gsub(/\$CAPISTRANO:HOST\$/, channel[:host])
|
265
|
+
end
|
266
|
+
|
267
|
+
# prepare a space-separated sequence of variables assignments
|
268
|
+
# intended to be prepended to a command, so the shell sets
|
269
|
+
# the environment before running the command.
|
270
|
+
# i.e.: options[:env] = {'PATH' => '/opt/ruby/bin:$PATH',
|
271
|
+
# 'TEST' => '( "quoted" )'}
|
272
|
+
# environment returns:
|
273
|
+
# "env TEST=(\ \"quoted\"\ ) PATH=/opt/ruby/bin:$PATH"
|
274
|
+
def environment
|
275
|
+
return if options[:env].nil? || options[:env].empty?
|
276
|
+
@environment ||= if String === options[:env]
|
277
|
+
"env #{options[:env]}"
|
278
|
+
else
|
279
|
+
options[:env].inject("env") do |string, (name, value)|
|
280
|
+
value = value.to_s.gsub(/[ "]/) { |m| "\\#{m}" }
|
281
|
+
string << " #{name}=#{value}"
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'capistrano/logger'
|
2
|
+
|
3
|
+
require 'capistrano/configuration/callbacks'
|
4
|
+
require 'capistrano/configuration/connections'
|
5
|
+
require 'capistrano/configuration/execution'
|
6
|
+
require 'capistrano/configuration/loading'
|
7
|
+
require 'capistrano/configuration/namespaces'
|
8
|
+
require 'capistrano/configuration/roles'
|
9
|
+
require 'capistrano/configuration/servers'
|
10
|
+
require 'capistrano/configuration/variables'
|
11
|
+
|
12
|
+
require 'capistrano/configuration/actions/file_transfer'
|
13
|
+
require 'capistrano/configuration/actions/inspect'
|
14
|
+
require 'capistrano/configuration/actions/invocation'
|
15
|
+
|
16
|
+
module Capistrano
|
17
|
+
# Represents a specific Capistrano configuration. A Configuration instance
|
18
|
+
# may be used to load multiple recipe files, define and describe tasks,
|
19
|
+
# define roles, and set configuration variables.
|
20
|
+
class Configuration
|
21
|
+
# The logger instance defined for this configuration.
|
22
|
+
attr_accessor :debug, :logger, :dry_run, :preserve_roles
|
23
|
+
|
24
|
+
def initialize(options={}) #:nodoc:
|
25
|
+
@debug = false
|
26
|
+
@dry_run = false
|
27
|
+
@preserve_roles = false
|
28
|
+
@logger = Logger.new(options)
|
29
|
+
end
|
30
|
+
|
31
|
+
# make the DSL easier to read when using lazy evaluation via lambdas
|
32
|
+
alias defer lambda
|
33
|
+
|
34
|
+
# The includes must come at the bottom, since they may redefine methods
|
35
|
+
# defined in the base class.
|
36
|
+
include Connections, Execution, Loading, Namespaces, Roles, Servers, Variables
|
37
|
+
|
38
|
+
# Mix in the actions
|
39
|
+
include Actions::FileTransfer, Actions::Inspect, Actions::Invocation
|
40
|
+
|
41
|
+
# Must mix last, because it hooks into previously defined methods
|
42
|
+
include Callbacks
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'capistrano/transfer'
|
2
|
+
|
3
|
+
module Capistrano
|
4
|
+
class Configuration
|
5
|
+
module Actions
|
6
|
+
module FileTransfer
|
7
|
+
|
8
|
+
# Store the given data at the given location on all servers targetted
|
9
|
+
# by the current task. If <tt>:mode</tt> is specified it is used to
|
10
|
+
# set the mode on the file.
|
11
|
+
def put(data, path, options={})
|
12
|
+
opts = options.dup
|
13
|
+
upload(StringIO.new(data), path, opts)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Get file remote_path from FIRST server targeted by
|
17
|
+
# the current task and transfer it to local machine as path.
|
18
|
+
#
|
19
|
+
# get "#{deploy_to}/current/log/production.log", "log/production.log.web"
|
20
|
+
def get(remote_path, path, options={}, &block)
|
21
|
+
download(remote_path, path, options.merge(:once => true), &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def upload(from, to, options={}, &block)
|
25
|
+
mode = options.delete(:mode)
|
26
|
+
transfer(:up, from, to, options, &block)
|
27
|
+
if mode
|
28
|
+
mode = mode.is_a?(Numeric) ? mode.to_s(8) : mode.to_s
|
29
|
+
run "chmod #{mode} #{to}", options
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def download(from, to, options={}, &block)
|
34
|
+
transfer(:down, from, to, options, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def transfer(direction, from, to, options={}, &block)
|
38
|
+
execute_on_servers(options) do |servers|
|
39
|
+
targets = servers.map { |s| sessions[s] }
|
40
|
+
Transfer.process(direction, from, to, targets, options.merge(:logger => logger), &block)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'capistrano/errors'
|
2
|
+
|
3
|
+
module Capistrano
|
4
|
+
class Configuration
|
5
|
+
module Actions
|
6
|
+
module Inspect
|
7
|
+
|
8
|
+
# Streams the result of the command from all servers that are the
|
9
|
+
# target of the current task. All these streams will be joined into a
|
10
|
+
# single one, so you can, say, watch 10 log files as though they were
|
11
|
+
# one. Do note that this is quite expensive from a bandwidth
|
12
|
+
# perspective, so use it with care.
|
13
|
+
#
|
14
|
+
# The command is invoked via #invoke_command.
|
15
|
+
#
|
16
|
+
# Usage:
|
17
|
+
#
|
18
|
+
# desc "Run a tail on multiple log files at the same time"
|
19
|
+
# task :tail_fcgi, :roles => :app do
|
20
|
+
# stream "tail -f #{shared_path}/log/fastcgi.crash.log"
|
21
|
+
# end
|
22
|
+
def stream(command, options={})
|
23
|
+
invoke_command(command, options) do |ch, stream, out|
|
24
|
+
puts out if stream == :out
|
25
|
+
warn "[err :: #{ch[:server]}] #{out}" if stream == :err
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Executes the given command on the first server targetted by the
|
30
|
+
# current task, collects it's stdout into a string, and returns the
|
31
|
+
# string. The command is invoked via #invoke_command.
|
32
|
+
def capture(command, options={})
|
33
|
+
output = ""
|
34
|
+
invoke_command(command, options.merge(:once => true)) do |ch, stream, data|
|
35
|
+
case stream
|
36
|
+
when :out then output << data
|
37
|
+
when :err then warn "[err :: #{ch[:server]}] #{data}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
output
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,295 @@
|
|
1
|
+
require 'capistrano/command'
|
2
|
+
|
3
|
+
module Capistrano
|
4
|
+
class Configuration
|
5
|
+
module Actions
|
6
|
+
module Invocation
|
7
|
+
def self.included(base) #:nodoc:
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
|
10
|
+
base.send :alias_method, :initialize_without_invocation, :initialize
|
11
|
+
base.send :alias_method, :initialize, :initialize_with_invocation
|
12
|
+
|
13
|
+
base.default_io_proc = Proc.new do |ch, stream, out|
|
14
|
+
level = stream == :err ? :important : :info
|
15
|
+
ch[:options][:logger].send(level, out, "#{stream} :: #{ch[:server]}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
attr_accessor :default_io_proc
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize_with_invocation(*args) #:nodoc:
|
24
|
+
initialize_without_invocation(*args)
|
25
|
+
set :default_environment, {}
|
26
|
+
set :default_run_options, {}
|
27
|
+
end
|
28
|
+
|
29
|
+
# Executes different commands in parallel. This is useful for commands
|
30
|
+
# that need to be different on different hosts, but which could be
|
31
|
+
# otherwise run in parallel.
|
32
|
+
#
|
33
|
+
# The +options+ parameter is currently unused.
|
34
|
+
#
|
35
|
+
# Example:
|
36
|
+
#
|
37
|
+
# task :restart_everything do
|
38
|
+
# parallel do |session|
|
39
|
+
# session.when "in?(:app)", "/path/to/restart/mongrel"
|
40
|
+
# session.when "in?(:web)", "/path/to/restart/apache"
|
41
|
+
# session.when "in?(:db)", "/path/to/restart/mysql"
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# Each command may have its own callback block, for capturing and
|
46
|
+
# responding to output, with semantics identical to #run:
|
47
|
+
#
|
48
|
+
# session.when "in?(:app)", "/path/to/restart/mongrel" do |ch, stream, data|
|
49
|
+
# # ch is the SSH channel for this command, used to send data
|
50
|
+
# # back to the command (e.g. ch.send_data("password\n"))
|
51
|
+
# # stream is either :out or :err, for which stream the data arrived on
|
52
|
+
# # data is a string containing data sent from the remote command
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# Also, you can specify a fallback command, to use when none of the
|
56
|
+
# conditions match a server:
|
57
|
+
#
|
58
|
+
# session.else "/execute/something/else"
|
59
|
+
#
|
60
|
+
# The string specified as the first argument to +when+ may be any valid
|
61
|
+
# Ruby code. It has access to the following variables and methods:
|
62
|
+
#
|
63
|
+
# * +in?(role)+ returns true if the server participates in the given role
|
64
|
+
# * +server+ is the ServerDefinition object for the server. This can be
|
65
|
+
# used to get the host-name, etc.
|
66
|
+
# * +configuration+ is the current Capistrano::Configuration object, which
|
67
|
+
# you can use to get the value of variables, etc.
|
68
|
+
#
|
69
|
+
# For example:
|
70
|
+
#
|
71
|
+
# session.when "server.host =~ /app/", "/some/command"
|
72
|
+
# session.when "server.host == configuration[:some_var]", "/another/command"
|
73
|
+
# session.when "in?(:web) || in?(:app)", "/more/commands"
|
74
|
+
#
|
75
|
+
# See #run for a description of the valid +options+.
|
76
|
+
def parallel(options={})
|
77
|
+
raise ArgumentError, "parallel() requires a block" unless block_given?
|
78
|
+
tree = Command::Tree.new(self) { |t| yield t }
|
79
|
+
run_tree(tree, options)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Invokes the given command. If a +via+ key is given, it will be used
|
83
|
+
# to determine what method to use to invoke the command. It defaults
|
84
|
+
# to :run, but may be :sudo, or any other method that conforms to the
|
85
|
+
# same interface as run and sudo.
|
86
|
+
def invoke_command(cmd, options={}, &block)
|
87
|
+
options = options.dup
|
88
|
+
via = options.delete(:via) || :run
|
89
|
+
send(via, cmd, options, &block)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Execute the given command on all servers that are the target of the
|
93
|
+
# current task. If a block is given, it is invoked for all output
|
94
|
+
# generated by the command, and should accept three parameters: the SSH
|
95
|
+
# channel (which may be used to send data back to the remote process),
|
96
|
+
# the stream identifier (<tt>:err</tt> for stderr, and <tt>:out</tt> for
|
97
|
+
# stdout), and the data that was received.
|
98
|
+
#
|
99
|
+
# The +options+ hash may include any of the following keys:
|
100
|
+
#
|
101
|
+
# * :hosts - this is either a string (for a single target host) or an array
|
102
|
+
# of strings, indicating which hosts the command should run on. By default,
|
103
|
+
# the hosts are determined from the task definition.
|
104
|
+
# * :roles - this is either a string or symbol (for a single target role) or
|
105
|
+
# an array of strings or symbols, indicating which roles the command should
|
106
|
+
# run on. If :hosts is specified, :roles will be ignored.
|
107
|
+
# * :only - specifies a condition limiting which hosts will be selected to
|
108
|
+
# run the command. This should refer to values set in the role definition.
|
109
|
+
# For example, if a role is defined with :primary => true, then you could
|
110
|
+
# select only hosts with :primary true by setting :only => { :primary => true }.
|
111
|
+
# * :except - specifies a condition limiting which hosts will be selected to
|
112
|
+
# run the command. This is the inverse of :only (hosts that do _not_ match
|
113
|
+
# the condition will be selected).
|
114
|
+
# * :once - if true, only the first matching server will be selected. The default
|
115
|
+
# is false (all matching servers will be selected).
|
116
|
+
# * :max_hosts - specifies the maximum number of hosts that should be selected
|
117
|
+
# at a time. If this value is less than the number of hosts that are selected
|
118
|
+
# to run, then the hosts will be run in groups of max_hosts. The default is nil,
|
119
|
+
# which indicates that there is no maximum host limit. Please note this does not
|
120
|
+
# limit the number of SSH channels that can be open, only the number of hosts upon
|
121
|
+
# which this will be called.
|
122
|
+
# * :shell - says which shell should be used to invoke commands. This
|
123
|
+
# defaults to "sh". Setting this to false causes Capistrano to invoke
|
124
|
+
# the commands directly, without wrapping them in a shell invocation.
|
125
|
+
# * :data - if not nil (the default), this should be a string that will
|
126
|
+
# be passed to the command's stdin stream.
|
127
|
+
# * :pty - if true, a pseudo-tty will be allocated for each command. The
|
128
|
+
# default is false. Note that there are benefits and drawbacks both ways.
|
129
|
+
# Empirically, it appears that if a pty is allocated, the SSH server daemon
|
130
|
+
# will _not_ read user shell start-up scripts (e.g. bashrc, etc.). However,
|
131
|
+
# if a pty is _not_ allocated, some commands will refuse to run in
|
132
|
+
# interactive mode and will not prompt for (e.g.) passwords.
|
133
|
+
# * :env - a hash of environment variable mappings that should be made
|
134
|
+
# available to the command. The keys should be environment variable names,
|
135
|
+
# and the values should be their corresponding values. The default is
|
136
|
+
# empty, but may be modified by changing the +default_environment+
|
137
|
+
# Capistrano variable.
|
138
|
+
#
|
139
|
+
# Note that if you set these keys in the +default_run_options+ Capistrano
|
140
|
+
# variable, they will apply for all invocations of #run, #invoke_command,
|
141
|
+
# and #parallel.
|
142
|
+
def run(cmd, options={}, &block)
|
143
|
+
block ||= self.class.default_io_proc
|
144
|
+
tree = Command::Tree.new(self) { |t| t.else(cmd, &block) }
|
145
|
+
run_tree(tree, options)
|
146
|
+
end
|
147
|
+
|
148
|
+
# Executes a Capistrano::Command::Tree object. This is not for direct
|
149
|
+
# use, but should instead be called indirectly, via #run or #parallel,
|
150
|
+
# or #invoke_command.
|
151
|
+
def run_tree(tree, options={}) #:nodoc:
|
152
|
+
if tree.branches.empty? && tree.fallback
|
153
|
+
logger.debug "executing #{tree.fallback}"
|
154
|
+
elsif tree.branches.any?
|
155
|
+
logger.debug "executing multiple commands in parallel"
|
156
|
+
tree.each do |branch|
|
157
|
+
logger.trace "-> #{branch}"
|
158
|
+
end
|
159
|
+
else
|
160
|
+
raise ArgumentError, "attempt to execute without specifying a command"
|
161
|
+
end
|
162
|
+
|
163
|
+
return if dry_run || (debug && continue_execution(tree) == false)
|
164
|
+
|
165
|
+
options = add_default_command_options(options)
|
166
|
+
|
167
|
+
tree.each do |branch|
|
168
|
+
if branch.command.include?(sudo)
|
169
|
+
branch.callback = sudo_behavior_callback(branch.callback)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
execute_on_servers(options) do |servers|
|
174
|
+
targets = servers.map { |s| sessions[s] }
|
175
|
+
Command.process(tree, targets, options.merge(:logger => logger))
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
# Returns the command string used by capistrano to invoke a comamnd via
|
180
|
+
# sudo.
|
181
|
+
#
|
182
|
+
# run "#{sudo :as => 'bob'} mkdir /path/to/dir"
|
183
|
+
#
|
184
|
+
# It can also be invoked like #run, but executing the command via sudo.
|
185
|
+
# This assumes that the sudo password (if required) is the same as the
|
186
|
+
# password for logging in to the server.
|
187
|
+
#
|
188
|
+
# sudo "mkdir /path/to/dir"
|
189
|
+
#
|
190
|
+
# Also, this method understands a <tt>:sudo</tt> configuration variable,
|
191
|
+
# which (if specified) will be used as the full path to the sudo
|
192
|
+
# executable on the remote machine:
|
193
|
+
#
|
194
|
+
# set :sudo, "/opt/local/bin/sudo"
|
195
|
+
#
|
196
|
+
# If you know what you're doing, you can also set <tt>:sudo_prompt</tt>,
|
197
|
+
# which tells capistrano which prompt sudo should use when asking for
|
198
|
+
# a password. (This is so that capistrano knows what prompt to look for
|
199
|
+
# in the output.) If you set :sudo_prompt to an empty string, Capistrano
|
200
|
+
# will not send a preferred prompt.
|
201
|
+
def sudo(*parameters, &block)
|
202
|
+
options = parameters.last.is_a?(Hash) ? parameters.pop.dup : {}
|
203
|
+
command = parameters.first
|
204
|
+
user = options[:as] && "-u #{options.delete(:as)}"
|
205
|
+
|
206
|
+
sudo_prompt_option = "-p '#{sudo_prompt}'" unless sudo_prompt.empty?
|
207
|
+
sudo_command = [fetch(:sudo, "sudo"), sudo_prompt_option, user].compact.join(" ")
|
208
|
+
|
209
|
+
if command
|
210
|
+
command = sudo_command + " " + command
|
211
|
+
run(command, options, &block)
|
212
|
+
else
|
213
|
+
return sudo_command
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
# Returns a Proc object that defines the behavior of the sudo
|
218
|
+
# callback. The returned Proc will defer to the +fallback+ argument
|
219
|
+
# (which should also be a Proc) for any output it does not
|
220
|
+
# explicitly handle.
|
221
|
+
def sudo_behavior_callback(fallback) #:nodoc:
|
222
|
+
# in order to prevent _each host_ from prompting when the password
|
223
|
+
# was wrong, let's track which host prompted first and only allow
|
224
|
+
# subsequent prompts from that host.
|
225
|
+
prompt_host = nil
|
226
|
+
|
227
|
+
Proc.new do |ch, stream, out|
|
228
|
+
if out =~ /^Sorry, try again/
|
229
|
+
if prompt_host.nil? || prompt_host == ch[:server]
|
230
|
+
prompt_host = ch[:server]
|
231
|
+
logger.important out, "#{stream} :: #{ch[:server]}"
|
232
|
+
reset! :password
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
if out =~ /^#{Regexp.escape(sudo_prompt)}/
|
237
|
+
ch.send_data "#{self[:password]}\n"
|
238
|
+
elsif fallback
|
239
|
+
fallback.call(ch, stream, out)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
# Merges the various default command options into the options hash and
|
245
|
+
# returns the result. The default command options that are understand
|
246
|
+
# are:
|
247
|
+
#
|
248
|
+
# * :default_environment: If the :env key already exists, the :env
|
249
|
+
# key is merged into default_environment and then added back into
|
250
|
+
# options.
|
251
|
+
# * :default_shell: if the :shell key already exists, it will be used.
|
252
|
+
# Otherwise, if the :default_shell key exists in the configuration,
|
253
|
+
# it will be used. Otherwise, no :shell key is added.
|
254
|
+
def add_default_command_options(options)
|
255
|
+
defaults = self[:default_run_options]
|
256
|
+
options = defaults.merge(options)
|
257
|
+
|
258
|
+
env = self[:default_environment]
|
259
|
+
env = env.merge(options[:env]) if options[:env]
|
260
|
+
options[:env] = env unless env.empty?
|
261
|
+
|
262
|
+
shell = options[:shell] || self[:default_shell]
|
263
|
+
options[:shell] = shell unless shell.nil?
|
264
|
+
|
265
|
+
options
|
266
|
+
end
|
267
|
+
|
268
|
+
# Returns the prompt text to use with sudo
|
269
|
+
def sudo_prompt
|
270
|
+
fetch(:sudo_prompt, "sudo password: ")
|
271
|
+
end
|
272
|
+
|
273
|
+
def continue_execution(tree)
|
274
|
+
if tree.branches.length == 1
|
275
|
+
continue_execution_for_branch(tree.branches.first)
|
276
|
+
else
|
277
|
+
tree.each { |branch| branch.skip! unless continue_execution_for_branch(branch) }
|
278
|
+
tree.any? { |branch| !branch.skip? }
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
def continue_execution_for_branch(branch)
|
283
|
+
case Capistrano::CLI.debug_prompt(branch)
|
284
|
+
when "y"
|
285
|
+
true
|
286
|
+
when "n"
|
287
|
+
false
|
288
|
+
when "a"
|
289
|
+
exit(-1)
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|