Class: Maestro::Util::Shell
- Inherits:
-
Object
- Object
- Maestro::Util::Shell
- Defined in:
- lib/util/shell.rb,
lib/util/version.rb
Defined Under Namespace
Classes: ExitCode
Constant Summary collapse
- IS_WINDOWS =
Utility variables
RbConfig::CONFIG['host_os'] =~ /mswin/
- SEPARATOR =
IS_WINDOWS ? "\\" : "/"
- MOVE_COMMAND =
IS_WINDOWS ? 'move' : 'mv'
- ENV_EXPORT_COMMAND =
IS_WINDOWS ? 'set' : 'export'
- COMMAND_SEPARATOR =
IS_WINDOWS ? ‘&&’ : ‘&&’
'&&'- SCRIPT_EXTENSION =
IS_WINDOWS ? '.bat' : '.shell'
- SHELL_EXECUTABLE =
IS_WINDOWS ? '' : 'bash '
- VERSION =
'0.0.3'
Instance Attribute Summary collapse
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
-
#output_file ⇒ Object
readonly
Returns the value of attribute output_file.
-
#script_file ⇒ Object
readonly
Returns the value of attribute script_file.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
Class Method Summary collapse
Instance Method Summary collapse
- #create_script(contents) ⇒ Object
- #run_script ⇒ Object
-
#run_script_with_delegate(delegate, on_output) ⇒ Object
if
delegateprovided, the method named/symbolized byon_outputvalue will be called for each line of output to either stdout or stderr. - #to_s ⇒ Object (also: #output)
Instance Attribute Details
#exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
13 14 15 |
# File 'lib/util/shell.rb', line 13 def exit_code @exit_code end |
#output_file ⇒ Object (readonly)
Returns the value of attribute output_file.
11 12 13 |
# File 'lib/util/shell.rb', line 11 def output_file @output_file end |
#script_file ⇒ Object (readonly)
Returns the value of attribute script_file.
10 11 12 |
# File 'lib/util/shell.rb', line 10 def script_file @script_file end |
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
12 13 14 |
# File 'lib/util/shell.rb', line 12 def shell @shell end |
Class Method Details
.run_command(command) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/util/shell.rb', line 40 def Shell.run_command(command) shell = Shell.new shell.create_script(command) shell.run_script return shell.exit_code, shell.to_s end |
.unset_env_variable(var) ⇒ Object
36 37 38 |
# File 'lib/util/shell.rb', line 36 def Shell.unset_env_variable(var) IS_WINDOWS ? "set #{var}=" : "unset #{var}" end |
Instance Method Details
#create_script(contents) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/util/shell.rb', line 47 def create_script(contents) raise "Script Cannot Be Empty" if contents.nil? or contents.empty? @script_file = Tempfile.new(["script", SCRIPT_EXTENSION]) @output_file = Tempfile.new(['output','log']) # Run any commands in the default system Ruby environment, rather # than the one the agent is currently using (which within the wrapper, # sets clean values for these to avoid RVM or System gems that might # conflict). If the caller needs a specific Ruby environment, it should # establish that itself (as the rake task does through rvm if chosen) # Add clear env variable commands to head of script, since we don't necessarily have access to env here (depending on # version of ruby/bugs) contents = "#{Shell.unset_env_variable('GEM_HOME')}\n#{Shell.unset_env_variable('GEM_PATH')}\n#{contents}" @script_file.write(contents) @script_file.close Maestro.log.debug "Writing Script File To #{@script_file.path}" return get_command(@script_file.path) end |
#run_script ⇒ Object
66 67 68 |
# File 'lib/util/shell.rb', line 66 def run_script run_script_with_delegate(nil, nil) end |
#run_script_with_delegate(delegate, on_output) ⇒ Object
if delegate provided, the method named/symbolized by on_output value will be called for each line of output to either stdout or stderr. two parameters are passed:
+text+ String Output text. This may be any amount of data from 1 character to many lines.
do not assume it always represents a single line.
+err+ Boolean True if line is from stderr
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 |
# File 'lib/util/shell.rb', line 76 def run_script_with_delegate(delegate, on_output) File.open(@output_file.path, 'a') do |out_file| status = IO.popen4(@command_line) do |pid, stdin, stdout, stderr| threads = [] # Read stdout/stderr and push to output [ stdout, stderr ].each do |stream| threads << Thread.new do while !stream.eof? && text = stream.readpartial(1024) out_file.write(text) if delegate && on_output delegate.send(on_output, text, stream == stderr) end end end end # Wait for stream handler threads to exit threads.each { |t| t.join } end end @exit_code = ExitCode.new($?) return @exit_code end |
#to_s ⇒ Object Also known as: output
103 104 105 |
# File 'lib/util/shell.rb', line 103 def to_s @output_file.read if @output_file end |