Skip to content

Commit 28edbb1

Browse files
committed
Be more verbose
Pass -v to the receiving lvmsync, and immediately print any output from the receiver, too.
1 parent 2f52660 commit 28edbb1

1 file changed

Lines changed: 32 additions & 11 deletions

File tree

bin/lvmsync

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ def run_apply(opts)
112112
infile = opts[:apply] == '-' ? $stdin : File.open(opts[:apply], 'r')
113113
destdev = opts[:device]
114114

115-
process_dumpdata(infile, destdev, snapfile)
115+
process_dumpdata(infile, destdev, snapfile, opts)
116116
ensure
117117
snapfile.close unless snapfile.nil?
118118
infile.close unless infile.nil? or infile == $stdin
119119
end
120120

121-
def process_dumpdata(instream, destdev, snapback = nil)
121+
def process_dumpdata(instream, destdev, snapback = nil, opts)
122122
handshake = instream.readline.chomp
123123
unless handshake == PROTOCOL_VERSION
124124
$stderr.puts "Handshake failed; protocol mismatch? (saw '#{handshake}' expected '#{PROTOCOL_VERSION}'"
@@ -190,25 +190,38 @@ def run_client(opts)
190190
if opts[:stdout]
191191
dump_changes(lv, $stdout, opts)
192192
else
193+
verbose = opts[:verbose] ? '-v' : ''
193194
server_cmd = if desthost
194-
"ssh #{desthost} lvmsync --apply - #{snapback} #{destdev}"
195+
"ssh #{desthost} lvmsync --apply - #{snapback} #{verbose} #{destdev}"
195196
else
196-
"#{$0} --apply - #{snapback} #{destdev}"
197+
"#{$0} --apply - #{snapback} #{verbose} #{destdev}"
197198
end
198199

199200
exit_status = nil
200201
errors = nil
201202

202203
Open3.popen3(server_cmd) do |stdin_fd, stdout_fd, stderr_fd, wait_thr|
203-
dump_changes(lv, stdin_fd, opts)
204+
dump_changes(lv, stdin_fd, opts) do
205+
more_to_read = true
206+
while more_to_read
207+
more_to_read = false
208+
(IO.select([stdout_fd, stderr_fd], [], [], 0) || [[]])[0].each do |fd|
209+
more_to_read = true
210+
$stderr.puts "\e[2K\rremote:#{fd.readline}"
211+
end
212+
end
213+
end
204214
stdin_fd.close
205-
errors = stderr_fd.read
215+
[stderr_fd, stdout_fd].each do |fd|
216+
until fd.eof?
217+
$stderr.puts "\e[2K\rremote:#{fd.readline}"
218+
end
219+
end
206220
exit_status = wait_thr.value if wait_thr
207221
end
208222

209223
if (exit_status or $?).exitstatus != 0
210224
$stderr.puts "APPLY FAILED."
211-
$stderr.puts errors.split("\n").map { |l| "remote: #{l}" }.join("\n")
212225
end
213226
end
214227
end
@@ -220,6 +233,7 @@ def dump_changes(lv, outfd, opts)
220233
xfer_count = 0
221234
xfer_size = 0
222235
total_size = 0
236+
change_count = lv.changes.length
223237

224238
File.open(lv.origin.path, 'r') do |origindev|
225239
lv.changes.each do |r|
@@ -228,21 +242,28 @@ def dump_changes(lv, outfd, opts)
228242
xfer_size += chunk_size
229243

230244
$stderr.puts "Sending chunk #{r.to_s}..." if opts[:verbose]
231-
$stderr.puts "Seeking to #{r.first} in #{originfile}" if opts[:verbose]
245+
$stderr.puts "Seeking to #{r.first} in #{lv.origin.path}" if opts[:verbose]
232246

233247
origindev.seek(r.first, IO::SEEK_SET)
234248

235-
outfd.print [htonq(r.first), chunk_size].pack("QN")
236-
outfd.print origindev.read(chunk_size)
249+
begin
250+
outfd.print [htonq(r.first), chunk_size].pack("QN")
251+
outfd.print origindev.read(chunk_size)
252+
rescue Errno::EPIPE
253+
$stderr.puts "Remote prematurely closed the connection"
254+
yield if block_given?
255+
return
256+
end
237257

238258
# Progress bar!
239259
if xfer_count % 100 == 50
240260
$stderr.printf "\e[2K\rSending chunk %i of %i, %.2fMB/s",
241261
xfer_count,
242-
snap.differences.length,
262+
change_count,
243263
xfer_size / (Time.now - start_time) / 1048576
244264
$stderr.flush
245265
end
266+
yield if block_given?
246267
end
247268

248269
origindev.seek(0, IO::SEEK_END)

0 commit comments

Comments
 (0)