Class: CMD

Inherits:
Hash
  • Object
show all
Defined in:
lib/cmd.rb,
lib/cmd_windows.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, options = nil) ⇒ CMD

Returns a new instance of CMD.



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
36
37
# File 'lib/cmd.rb', line 10

def initialize(cmd, options=nil)
 initialize_defaults if(@@default_options.nil?)
 self[:output] = ''
 self[:error] = ''
 
 #1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL

 #5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE

 #9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2

 #13) SIGPIPE     14) SIGALRM     15) SIGTERM     17) SIGCHLD

 #18) SIGCONT     19) SIGSTOP     20) SIGTSTP     21) SIGTTIN

 #22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ

 #26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO

 #30) SIGPWR      31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1

 #36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4  39) SIGRTMIN+5

 #40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8  43) SIGRTMIN+9

 #44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13

 #48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13

 #52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9

 #56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6  59) SIGRTMAX-5

 #60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2  63) SIGRTMAX-1

 #64) SIGRTMAX

 self[:timeout_signal] = 2
 self[:timeout_raise_error] = true
 
 @@default_options.each { |key, value| self[key] = value}
 options.each { |key, value| self[key] = value} unless(options.nil?)
 self[:command]=cmd
end

Class Method Details

.default_options(hash) ⇒ Object



40
41
42
# File 'lib/cmd.rb', line 40

def self.default_options(hash)
  hash.each { |key, value| @@default_options[key] = value}
end

Instance Method Details

#executeObject

Raises:

  • (TimeoutError)


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
# File 'lib/cmd.rb', line 44

def execute
  if(self[:quiet])
 self[:echo_output] = false
 self[:echo_command] = false
  end
  
  puts self[:command] if(self[:echo_command] || self[:debug])
  system  
    
  if(self[:debug])
 puts "command: #{self[:command]}"
 puts "output: #{self[:output]}"
 puts "error: #{self[:error]}"
 puts "exit_code: #{self[:exit_code]}"
  end
  
  raise TimeoutError.new("Commnad '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])

  if((self[:exit_code] != 0) && !self[:ignore_exit_code])
 exception_text = "Exit code: #{self[:exit_code]}"
 exception_text = "#{exception_text}\nError: '#{self[:error]}'"
 exception_text = "#{exception_text}\nOutput: '#{self[:output]}'"
 raise StandardError.new(exception_text) 
  end
end

#execute_as(username) ⇒ Object



6
7
8
9
# File 'lib/cmd_windows.rb', line 6

def execute_as(username)
  self[:command] = "runas /noprofile /savecred /user:#{username} \"#{self[:command]}\""
  wait_on_spawned_process(self) { self.execute }
end

#get_child_processes(pid) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/cmd.rb', line 131

def get_child_processes(pid)
  processes = []
  Sys::ProcTable.ps do |p| 
 if(p.ppid == pid) 
get_child_processes(p.pid).each { |cp| processes << cp }
processes << p
 end
  end
  return processes
end

#interruptObject



123
124
125
126
127
128
129
130
# File 'lib/cmd.rb', line 123

def interrupt
  process = get_child_processes(self[:pid])
  
  Sys::ProcTable.ps { |p| process << p  if(p.pid == self[:pid]) }

  process.each { |p| Process.kill(self[:timeout_signal],p.pid) unless(Sys::ProcTable.ps(p.pid).nil?) }
  Process.waitpid(self[:pid]) unless(Sys::ProcTable.ps(self[:pid]).nil?) 
end

#systemObject



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
# File 'lib/cmd.rb', line 70

def system
  begin
    output = ''
 error = ''  
 Thread.abort_on_exception = true
 mutex = Mutex.new
 
 Open3.popen3(self[:command]) do |stdin, stdout, stderr, wait_thr|
      self[:pid] = wait_thr.pid 

if(key?(:timeout))
  start_time = Time.now
  Thread.new do
    while wait_thr.alive? do
        sleep(0.1)
    if((Time.now - start_time).to_f > self[:timeout])
    self[:timed_out] = true
    interrupt
    sleep(0.1)
    end
  end
  end
      end

      {:output => stdout,:error => stderr}.each do |key, stream|
        Thread.new do         
    while wait_thr.alive? && !key?(:timed_out) do
      if(!stream.closed? && !(char = stream.getc).nil?)
      case key
        when :output
          output << char
      putc char if(self[:echo_output])
        when :error
          error << char
      end
      else
        sleep(0.1)
    end
       end
  end
end

wait_thr.join

   self[:output] = output unless(output.empty?)         
self[:error] = error unless(error.empty?)
self[:exit_code] = wait_thr.value.to_i    
 end
  rescue Exception => e
 self[:error] = "#{self[:error]}\nException: #{e.to_s}"
 self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
  end
end