public
Description: Remote multi-server automation tool
Homepage: http://www.capify.org
Clone URL: git://github.com/jamis/capistrano.git
replace condition#if with when and made it support multiple matches by 
default.
jamis (author)
Thu Aug 21 09:59:35 -0700 2008
commit  c70e595c36ae7cd3f63c1212ddf566efe50e2b5d
tree    37ae7ee59ba204ea8466f711c2071a58442adf67
parent  c70fcd595e68f576002ba0b7d75e49872fba2447
...
9
10
11
 
12
 
 
 
13
14
15
 
16
17
 
18
19
 
20
21
22
 
 
 
 
23
24
25
...
37
38
39
40
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
43
44
45
 
 
 
 
46
47
48
49
 
50
51
52
53
 
54
55
56
57
 
 
58
59
60
61
62
63
 
 
64
65
66
67
 
68
69
70
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
73
74
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
151
152
153
 
 
 
 
 
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 
 
 
172
173
174
175
 
 
 
176
177
178
179
 
 
 
180
181
182
 
 
 
183
184
185
 
186
187
188
...
9
10
11
12
13
14
15
16
17
18
19
20
21
 
22
23
24
25
26
27
28
29
30
31
32
33
34
35
...
47
48
49
 
 
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
 
 
82
83
84
85
86
87
88
 
89
90
91
92
 
93
94
95
96
 
97
98
99
100
101
102
 
 
103
104
105
106
107
 
108
109
110
 
 
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
...
186
187
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
 
 
 
214
215
216
217
218
219
 
 
 
 
 
 
 
 
 
 
 
220
 
221
 
 
 
222
223
224
225
 
 
 
226
227
228
229
 
 
 
230
231
232
233
 
 
234
235
236
237
238
 
239
240
241
242
0
@@ -9,17 +9,27 @@ module Capistrano
0
     include Processable
0
 
0
     class Tree
0
+ attr_reader :configuration
0
       attr_reader :branches
0
+ attr_reader :fallback
0
+
0
+ include Enumerable
0
 
0
       class Branch
0
         attr_accessor :command, :callback
0
+ attr_reader :options
0
 
0
- def initialize(command, callback)
0
+ def initialize(command, options, callback)
0
           @command = command.strip.gsub(/\r?\n/, "\\\n")
0
           @callback = callback || Capistrano::Configuration.default_io_proc
0
+ @options = options
0
           @skip = false
0
         end
0
 
0
+ def last?
0
+ options[:last]
0
+ end
0
+
0
         def skip?
0
           @skip
0
         end
0
@@ -37,38 +47,83 @@ module Capistrano
0
         end
0
       end
0
 
0
- class PatternBranch < Branch
0
- attr_accessor :pattern
0
+ class ConditionBranch < Branch
0
+ attr_accessor :configuration
0
+ attr_accessor :condition
0
+
0
+ class Evaluator
0
+ attr_reader :configuration, :condition, :server
0
+
0
+ def initialize(config, condition, server)
0
+ @configuration = config
0
+ @condition = condition
0
+ @server = server
0
+ end
0
+
0
+ def in?(role)
0
+ configuration.roles[role].include?(server)
0
+ end
0
+
0
+ def result
0
+ eval(condition, binding)
0
+ end
0
+
0
+ def method_missing(sym, *args, &block)
0
+ if server.respond_to?(sym)
0
+ server.send(sym, *args, &block)
0
+ elsif configuration.respond_to?(sym)
0
+ configuration.send(sym, *args, &block)
0
+ else
0
+ super
0
+ end
0
+ end
0
+ end
0
 
0
- def initialize(pattern, command, callback)
0
- @pattern = pattern
0
- super(command, callback)
0
+ def initialize(configuration, condition, command, options, callback)
0
+ @configuration = configuration
0
+ @condition = condition
0
+ super(command, options, callback)
0
         end
0
 
0
         def match(server)
0
- pattern === server.host
0
+ Evaluator.new(configuration, condition, server).result
0
         end
0
 
0
         def to_s
0
- "#{pattern.inspect} :: #{command.inspect}"
0
+ "#{condition.inspect} :: #{command.inspect}"
0
         end
0
       end
0
 
0
- def initialize
0
+ def initialize(config)
0
+ @configuration = config
0
         @branches = []
0
         yield self if block_given?
0
       end
0
 
0
- def if(pattern, command, &block)
0
- branches << PatternBranch.new(pattern, command, block)
0
+ def when(condition, command, options={}, &block)
0
+ branches << ConditionBranch.new(configuration, condition, command, options, block)
0
       end
0
 
0
       def else(command, &block)
0
- branches << Branch.new(command, block)
0
+ @fallback = Branch.new(command, {}, block)
0
       end
0
 
0
- def branch_for(server)
0
- branches.detect { |branch| branch.match(server) }
0
+ def branches_for(server)
0
+ seen_last = false
0
+ matches = branches.select do |branch|
0
+ success = !seen_last && !branch.skip? && branch.match(server)
0
+ seen_last = success && branch.last?
0
+ success
0
+ end
0
+
0
+ matches << fallback if matches.empty? && fallback
0
+ return matches
0
+ end
0
+
0
+ def each
0
+ branches.each { |branch| yield branch }
0
+ yield fallback if fallback
0
+ return self
0
       end
0
     end
0
 
0
@@ -131,58 +186,57 @@ module Capistrano
0
       def open_channels
0
         sessions.map do |session|
0
           server = session.xserver
0
- branch = tree.branch_for(server)
0
- next if branch.skip?
0
-
0
- session.open_channel do |channel|
0
- channel[:server] = server
0
- channel[:host] = server.host
0
- channel[:options] = options
0
- channel[:branch] = branch
0
-
0
- request_pty_if_necessary(channel) do |ch, success|
0
- if success
0
- logger.trace "executing command", ch[:server] if logger
0
- cmd = replace_placeholders(channel[:branch].command, ch)
0
-
0
- if options[:shell] == false
0
- shell = nil
0
+ tree.branches_for(server).map do |branch|
0
+ session.open_channel do |channel|
0
+ channel[:server] = server
0
+ channel[:host] = server.host
0
+ channel[:options] = options
0
+ channel[:branch] = branch
0
+
0
+ request_pty_if_necessary(channel) do |ch, success|
0
+ if success
0
+ logger.trace "executing command", ch[:server] if logger
0
+ cmd = replace_placeholders(channel[:branch].command, ch)
0
+
0
+ if options[:shell] == false
0
+ shell = nil
0
+ else
0
+ shell = "#{options[:shell] || "sh"} -c"
0
+ cmd = cmd.gsub(/[$\\`"]/) { |m| "\\#{m}" }
0
+ cmd = "\"#{cmd}\""
0
+ end
0
+
0
+ command_line = [environment, shell, cmd].compact.join(" ")
0
+
0
+ ch.exec(command_line)
0
+ ch.send_data(options[:data]) if options[:data]
0
                 else
0
- shell = "#{options[:shell] || "sh"} -c"
0
- cmd = cmd.gsub(/[$\\`"]/) { |m| "\\#{m}" }
0
- cmd = "\"#{cmd}\""
0
+ # just log it, don't actually raise an exception, since the
0
+ # process method will see that the status is not zero and will
0
+ # raise an exception then.
0
+ logger.important "could not open channel", ch[:server] if logger
0
+ ch.close
0
                 end
0
-
0
- command_line = [environment, shell, cmd].compact.join(" ")
0
-
0
- ch.exec(command_line)
0
- ch.send_data(options[:data]) if options[:data]
0
- else
0
- # just log it, don't actually raise an exception, since the
0
- # process method will see that the status is not zero and will
0
- # raise an exception then.
0
- logger.important "could not open channel", ch[:server] if logger
0
- ch.close
0
               end
0
- end
0
 
0
- channel.on_data do |ch, data|
0
- ch[:branch].callback[ch, :out, data]
0
- end
0
+ channel.on_data do |ch, data|
0
+ ch[:branch].callback[ch, :out, data]
0
+ end
0
 
0
- channel.on_extended_data do |ch, type, data|
0
- ch[:branch].callback[ch, :err, data]
0
- end
0
+ channel.on_extended_data do |ch, type, data|
0
+ ch[:branch].callback[ch, :err, data]
0
+ end
0
 
0
- channel.on_request("exit-status") do |ch, data|
0
- ch[:status] = data.read_long
0
- end
0
+ channel.on_request("exit-status") do |ch, data|
0
+ ch[:status] = data.read_long
0
+ end
0
 
0
- channel.on_close do |ch|
0
- ch[:closed] = true
0
+ channel.on_close do |ch|
0
+ ch[:closed] = true
0
+ end
0
             end
0
           end
0
- end.compact
0
+ end.flatten
0
       end
0
 
0
       def request_pty_if_necessary(channel)
...
28
29
30
31
 
32
33
34
...
50
51
52
53
 
54
55
56
...
59
60
61
62
 
63
64
65
...
169
170
171
172
173
 
 
174
175
176
...
28
29
30
 
31
32
33
34
...
50
51
52
 
53
54
55
56
...
59
60
61
 
62
63
64
65
...
169
170
171
 
 
172
173
174
175
176
0
@@ -28,7 +28,7 @@ module Capistrano
0
 
0
         def parallel(options={})
0
           raise ArgumentError, "parallel() requires a block" unless block_given?
0
- tree = Command::Tree.new { |t| yield t }
0
+ tree = Command::Tree.new(self) { |t| yield t }
0
           run_tree(tree)
0
         end
0
 
0
@@ -50,7 +50,7 @@ module Capistrano
0
         # stdout), and the data that was received.
0
         def run(cmd, options={}, &block)
0
           block ||= self.class.default_io_proc
0
- tree = Command::Tree.new { |t| t.else(cmd, block) }
0
+ tree = Command::Tree.new(self) { |t| t.else(cmd, block) }
0
           run_tree(tree, options)
0
         end
0
 
0
@@ -59,7 +59,7 @@ module Capistrano
0
             logger.debug "executing #{tree.branches.first}"
0
           else
0
             logger.debug "executing multiple commands in parallel"
0
- tree.branches.each do |branch|
0
+ tree.each do |branch|
0
               logger.trace "-> #{branch}"
0
             end
0
           end
0
@@ -169,8 +169,8 @@ module Capistrano
0
           if tree.branches.length == 1
0
             continue_execution_for_branch(tree.branches.first)
0
           else
0
- tree.branches.each { |branch| branch.skip! unless continue_execution_for_branch(branch) }
0
- tree.branches.any? { |branch| !branch.skip? }
0
+ tree.each { |branch| branch.skip! unless continue_execution_for_branch(branch) }
0
+ tree.any? { |branch| !branch.skip? }
0
           end
0
         end
0
 
...
38
39
40
 
 
 
 
41
42
43
...
38
39
40
41
42
43
44
45
46
47
0
@@ -38,6 +38,10 @@ module Capistrano
0
       @static_servers.clear
0
     end
0
 
0
+ def include?(server)
0
+ servers.include?(server)
0
+ end
0
+
0
     protected
0
 
0
     # This is the combination of a block, a hash of options, and a cached value.

Comments

    No one has commented yet.