Module: Isono::Util

Defined in:
lib/isono/util.rb

Defined Under Namespace

Classes: DeferedMsg, EmSystemCb

Class Method Summary collapse

Class Method Details

.default_gw_ipaddrObject

Return the IP address assigned to default gw interface



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/isono/util.rb', line 41

def default_gw_ipaddr
  ip = case `/bin/uname -s`.rstrip
       when 'Linux'
         `/sbin/ip route get 8.8.8.8`.split("\n")[0].split.last
       when 'SunOS'
         `/sbin/ifconfig $(/usr/sbin/route -n get 1.1.1.1  | awk '$1 == "interface:" {print $2}') | awk '$1 == "inet" { print $2 }'`
       else
         raise "Unsupported platform to detect gateway IP address: #{`/bin/uname`}"
       end
  ip = ip.rstrip
  raise "Failed to run command lines or empty result" if ip == '' || $?.exitstatus != 0
  ip
end

.gen_id(str = nil) ⇒ Object



35
36
37
# File 'lib/isono/util.rb', line 35

def gen_id(str=nil)
  Digest::SHA1.hexdigest( (str.nil? ? ::Kernel.rand.to_s : str) )
end

.quote_args(cmd_str, args = [], quote_char = '\'') ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/isono/util.rb', line 56

def quote_args(cmd_str, args=[], quote_char='\'')
  quote_helper =
    if quote_char
      proc { |a|
        [quote_char, Shellwords.shellescape(a), quote_char].join
      }
    else
      proc { |a|
        Shellwords.shellescape(a)
      }
    end
  sprintf(cmd_str, *args.map {|a| quote_helper.call(a) })
end

.ruby_bin_pathObject

return ruby binary path which is expected to be used in the current environment.



145
146
147
148
149
# File 'lib/isono/util.rb', line 145

def ruby_bin_path
  #config_section.ruby_bin_path || ENV['_'] =~ /ruby/ ||
  require 'rbconfig'
  File.expand_path(Config::CONFIG['RUBY_INSTALL_NAME'], Config::CONFIG['bindir'])
end

.snake_case(str) ⇒ String

Convert to snake case.

"FooBar".snake_case           #=> "foo_bar"
"HeadlineCNNNews".snake_case  #=> "headline_cnn_news"
"CNN".snake_case              #=> "cnn"

Returns:

  • (String)

    Receiver converted to snake case.



27
28
29
30
31
32
# File 'lib/isono/util.rb', line 27

def snake_case(str)
  return str.downcase if str.match(/\A[A-Z]+\z/)
  str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z])([A-Z])/, '\1_\2').
    downcase
end

.system(cmd_str, args = [], opts = {}) ⇒ Object

system(‘/bin/ls’) second arg gives system(‘/bin/ls %s’, [‘/home’])



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
# File 'lib/isono/util.rb', line 74

def system(cmd_str, args=[], opts={})
  unless EventMachine.reactor_running?
    raise "has to prepare EventMachine context"
  end

  cmd = quote_args(cmd_str, args, (opts[:quote_char] || '\''))

  capture_io = opts[:io] || StringIO.new
  stdin_buf = opts[:stdin_input]
  
  evmsg = {:cmd => cmd}
  wait_q = ::Queue.new
  if opts[:timeout] && opts[:timeout].to_f > 0.0
    EventMachine.add_timer(opts[:timeout].to_f) {
      wait_q.enq(RuntimeError.new("timeout child process wait: #{opts[:timeout].to_f} sec(s)"))
    }
    evmsg[:timeout] = opts[:timeout].to_f        
  end
  popenobj = EventMachine.popen(cmd, EmSystemCb, capture_io, stdin_buf, proc { |exit_stat|
                                  wait_q.enq(exit_stat)
                                })
  pid = EventMachine.get_subprocess_pid(popenobj.signature)
  evmsg[:pid] = pid
  if self.respond_to? :logger
    logger.debug("Exec command (pid=#{pid}): #{cmd}")
  end

  stat = wait_q.deq
  evmsg = {}
  
  case stat
  when Process::Status
    evmsg[:exit_code] = stat.exitstatus
    if stat.exited? && stat.exitstatus == 0
    else
      raise "Unexpected status from child: #{stat}"
    end
  when Exception
    raise stat
  else
    raise "Unknown signal from child: #{stat}"
  end
  
end