Module: Dcmgr::Helpers::CliHelper
- Defined in:
- lib/dcmgr/helpers/cli_helper.rb
Defined Under Namespace
Classes: CommandError, TimeoutError
Instance Method Summary
collapse
Instance Method Details
#sh(cmd, args = [], opts = {}) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/dcmgr/helpers/cli_helper.rb', line 52
def sh(cmd, args=[], opts={})
opts = opts.merge({:expect_exitcode=>0})
cmd = sprintf(cmd, *args.map {|a| Shellwords.shellescape(a.to_s) })
outbuf = errbuf = ''
blk = proc {|pid, stdin, stdout, stderr|
stdin.close
outbuf = stdout.read
errbuf = stderr.read
}
stat = Open4::popen4(cmd, &blk)
if self.respond_to?(:logger)
logger.debug("Exec command (pid=#{stat.pid}): #{cmd}")
msg = "Command output:"
msg << "\nSTDOUT:\n#{outbuf.strip}" if outbuf && outbuf.strip.size > 0
msg << "\nSTDERR:\n#{errbuf.strip}" if errbuf && errbuf.strip.size > 0
logger.debug(msg)
end
if stat.exitstatus != opts[:expect_exitcode]
raise CommandError.new("Unexpected exit code=#{stat.exitstatus} (expected=#{opts[:expect_exitcode]})", \
outbuf, errbuf)
end
true
end
|
#tryagain(opts = {:timeout=>60, :retry=>3}, &blk) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/dcmgr/helpers/cli_helper.rb', line 13
def tryagain(opts={:timeout=>60, :retry=>3}, &blk)
timedout = false
curthread = Thread.current
timersig = EventMachine.add_timer(opts[:timeout]) {
timedout = true
if curthread
curthread.raise(TimeoutError.new("timeout"))
curthread.pass
end
}
count = opts[:retry]
begin
begin
break if blk.call
end while !timedout && ((count -= 1) >= 0)
rescue TimeoutError => e
raise e
rescue RuntimeError => e
if respond_to?(:logger)
logger.debug("Caught Error. To be retrying....: #{e}")
end
retry if (count -= 1) >= 0
ensure
curthread = nil
EventMachine.cancel_timer(timersig) rescue nil
end
end
|