Module: Dcmgr::Helpers::CliHelper
- Included in:
- Cli::KeyPair, Drivers::Comstar, Drivers::Kvm, Drivers::LinuxIscsi, Drivers::LocalStorage, Drivers::Lxc, Drivers::Raw, Drivers::SnapshotStorage, Drivers::SunIscsi, Drivers::Zfs, NodeModules::StaTgtInitializer, Rpc::HvaHandler, Rpc::StaHandler
- 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/dcmgr/helpers/cli_helper.rb', line 55
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
{:stdout => outbuf, :stderr => errbuf}
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
42
43
44
|
# 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.alive?
curthread.raise(TimeoutError.new("timeout"))
begin
curthread.pass
rescue ::Exception => e
end
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
EventMachine.cancel_timer(timersig) rescue nil
end
end
|