Class: Coral::Util::Shell

Inherits:
Core show all
Defined in:
lib/coral_core/util/shell.rb

Instance Attribute Summary

Attributes inherited from Core

#ui

Class Method Summary collapse

Methods inherited from Core

#initialize, #inspect, #logger, logger, logger=, ui

Methods inherited from Config

#[], #[]=, array, #array, #clear, #defaults, #delete, ensure, #export, #filter, filter, #get, #get_array, #get_hash, #hash, hash, #import, init, #init, init_flat, #initialize, #set, #string, string, string_map, #string_map, #symbol, symbol, symbol_map, #symbol_map, test, #test

Methods included from Mixin::ConfigOptions

#clear_options, #contexts, #get_options, #set_options

Methods included from Mixin::ConfigCollection

#all_properties, #clear_properties, #delete_property, #get_property, #save_properties, #set_property

Methods included from Mixin::Lookup

#hiera, #hiera_config, #initialized?, #lookup, #lookup_array, #lookup_hash, #normalize

Methods included from Mixin::ConfigOps

#parse

Constructor Details

This class inherits a constructor from Coral::Core

Class Method Details

.check_conditions!(line, conditions, match_prefix = '') ⇒ Object




106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/coral_core/util/shell.rb', line 106

def self.check_conditions!(line, conditions, match_prefix = '')
  prefix = ''
  
  unless ! conditions || conditions.empty?
    conditions.each do |key, event|
      if event.check(line)
        prefix = match_prefix
        conditions.delete(key)
      end
    end
  end
  
  result = true
  if block_given?
    result = yield
    
    unless prefix.empty?
      case result
      when Hash
        result[:prefix] = prefix
      else
        result = { :success => result, :prefix => prefix }
      end
    end
  end
  return result
end

.close_exec_pipe(thread, output, original, write, label) ⇒ Object




94
95
96
97
98
99
100
101
102
# File 'lib/coral_core/util/shell.rb', line 94

def self.close_exec_pipe(thread, output, original, write, label)
  output.reopen(original)
   
  write.close
  success = thread.value
  
  original.close
  return success
end

.exec(command, options = {}) ⇒ Object




68
69
70
# File 'lib/coral_core/util/shell.rb', line 68

def self.exec(command, options = {})
  return exec!(command, options)
end

.exec!(command, options = {}) ⇒ Object


Utilities



9
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/coral_core/util/shell.rb', line 9

def self.exec!(command, options = {})
  config = Config.ensure(options)
  
  min   = config.get(:min, 1).to_i
  tries = config.get(:tries, min).to_i
  tries = ( min > tries ? min : tries )
  
  info_prefix  = config.get(:info_prefix, '')
  info_suffix  = config.get(:info_suffix, '')
  error_prefix = config.get(:error_prefix, '')
  error_suffix = config.get(:error_suffix, '')
  
  ui           = config.get(:ui, Coral.ui)
  
  conditions   = Coral::Event.instance(config.get(:exit, {}), true)
  
  $stdout.sync = true
  $stderr.sync = true  
  
  for i in tries.downto(1)
    ui.info(">> running: #{command}")
    
    begin
      t1, output_new, output_orig, output_reader = pipe_exec_stream!($stdout, conditions, { 
        :prefix => info_prefix, 
        :suffix => info_suffix, 
      }, 'output') do |line|
        block_given? ? yield(line) : true
      end
    
      t2, error_new, error_orig, error_reader = pipe_exec_stream!($stderr, conditions, { 
        :prefix => error_prefix, 
        :suffix => error_suffix, 
      }, 'error') do |line|
        block_given? ? yield(line) : true
      end
    
      system_success = system(command)
    
    ensure
      output_success = close_exec_pipe(t1, $stdout, output_orig, output_new, 'output')
      error_success  = close_exec_pipe(t2, $stderr, error_orig, error_new, 'error')
    end
    ui.info('')
    
    success = ( system_success && output_success && error_success )
                
    min -= 1
    break if success && min <= 0 && conditions.empty?
  end
  unless conditions.empty?
    success = false
  end
  
  return success   
end

.pipe_exec_stream!(output, conditions, options, label) ⇒ Object




74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coral_core/util/shell.rb', line 74

def self.pipe_exec_stream!(output, conditions, options, label)
  original     = output.dup
  read, write  = IO.pipe
  
  match_prefix = ( options[:match_prefix] ? options[:match_prefix] : 'EXIT' )
          
  thread = process_stream!(read, original, options, label) do |line|
    check_conditions!(line, conditions, match_prefix) do
      block_given? ? yield(line) : true
    end
  end
  
  thread.abort_on_exception = false
  
  output.reopen(write)    
  return thread, write, original, read
end

.process_stream!(input, output, options, label) ⇒ Object




136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/coral_core/util/shell.rb', line 136

def self.process_stream!(input, output, options, label)
  return Thread.new do
    success        = true      
    default_prefix = ( options[:prefix] ? options[:prefix] : '' )
    default_suffix = ( options[:suffix] ? options[:suffix] : '' )
    
    begin
      while ( data = input.readpartial(1024) )
        message = data.strip
        newline = ( data[-1,1].match(/\n/) ? true : false )
                               
        unless message.empty?
          lines = message.split(/\n/)
          lines.each_with_index do |line, index|
            prefix  = default_prefix
            suffix  = default_suffix
            
            unless line.empty?
              if block_given?
                result = yield(line)
                                        
                if result && result.is_a?(Hash)
                  prefix = result[:prefix]
                  suffix = result[:suffix]
                  result = result[:success]                 
                end
                success = result if success
              end
          
              prefix = ( prefix && ! prefix.empty? ? "#{prefix}: " : '' )
              suffix = ( suffix && ! suffix.empty? ? suffix : '' )            
              eol    = ( index < lines.length - 1 || newline ? "\n" : ' ' )
          
              output.write(prefix.lstrip + line + suffix.rstrip + eol)
            end
          end
        end
      end
    rescue EOFError
    end
    
    input.close()
    success
  end
end