Class: Nucleon::Util::Shell

Inherits:
Core show all
Includes:
Parallel
Defined in:
lib/core/util/shell.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

@@supervisors =

{}

Instance Attribute Summary

Attributes inherited from Core

#logger, #ui

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parallel

included

Methods inherited from Core

#initialize, #initialized?, logger, ui, ui_group, #ui_group

Methods included from Mixin::Colors

#black, #blue, #cyan, #green, #grey, #purple, #red, #yellow

Methods inherited from Config

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

Methods included from Mixin::ConfigOptions

#all_options, #clear_options, #contexts, #get_options, #set_options

Methods included from Mixin::ConfigCollection

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

Constructor Details

This class inherits a constructor from Nucleon::Core

Class Method Details

.connection(name = :core) ⇒ Object


Shell interface



51
52
53
# File 'lib/core/util/shell.rb', line 51

def self.connection(name = :core)
  Nucleon.manager(@@supervisors, name, self)
end

Instance Method Details

#exec(command, options = {}, &code) ⇒ Object




63
64
65
66
67
68
69
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
123
124
125
# File 'lib/core/util/shell.rb', line 63

def exec(command, options = {}, &code)
  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, Nucleon.ui)
  
  conditions   = Nucleon.events(config.get(:exit, {}), true)
  
  $stdout.sync = true
  $stderr.sync = true
  
  system_result = Result.new(command)
  
  for i in tries.downto(1)
    logger.info(">> running shell: #{command}")
    
    begin
      t1, output_new, output_orig, output_reader = pipe_exec_stream($stdout, conditions, { 
        :prefix => info_prefix, 
        :suffix => info_suffix,
        :quiet  => config.get(:quiet, false) 
      }, 'output') do |data|
        system_result.append_output(data)
        code ? code.call(:output, command, data) : true
      end
      
      t2, error_new, error_orig, error_reader = pipe_exec_stream($stderr, conditions, { 
        :prefix => error_prefix, 
        :suffix => error_suffix,
        :quiet  => config.get(:quiet, false)  
      }, 'error') do |data|
        system_result.append_errors(data)
        code ? code.call(:error, command, data) : true
      end
      
      system_success       = system(command)
      system_result.status = $?.exitstatus
      
    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
    
    unless config.get(:quiet, false)
      logger.warn("`#{command}` messages: #{system_result.errors}") if system_result.errors.length > 0
    end
    logger.warn("`#{command}` status: #{system_result.status}") unless system_result.status == 0
      
    success = ( system_success && output_success && error_success )
                
    min -= 1
    break if success && min <= 0 && conditions.empty?
  end
  system_result
end

#test_connectionObject




57
58
59
# File 'lib/core/util/shell.rb', line 57

def test_connection
  true
end