Class: Jackal::Commander::Executor
- Inherits:
-
Jackal::Callback
- Object
- Jackal::Callback
- Jackal::Commander::Executor
- Defined in:
- lib/jackal-commander/executor.rb
Overview
Run executions
Constant Summary collapse
- DEFAULT_PROCESS_OUTPUT_ROOT =
default temp storage directory for process outputs
'/tmp/jackal-executor'
Instance Attribute Summary collapse
-
#process_output_root ⇒ String
readonly
Root path for process output files.
Instance Method Summary collapse
-
#execute(message) ⇒ Object
Execute requested action.
-
#setup(*_) ⇒ Object
Setup the callback.
-
#valid?(message) ⇒ Truthy, Falsey
Determine validity of message.
Instance Attribute Details
#process_output_root ⇒ String (readonly)
Returns root path for process output files.
9 10 11 |
# File 'lib/jackal-commander/executor.rb', line 9 def process_output_root @process_output_root end |
Instance Method Details
#execute(message) ⇒ Object
Execute requested action
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 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 |
# File 'lib/jackal-commander/executor.rb', line 38 def execute() failure_wrap() do |payload| actions = [ payload.get(:data, :commander, :action), payload.get(:data, :commander, :actions) ].flatten.compact.uniq cmds = actions.map do |action| if(action.is_a?(Hash)) extra_args = action[:arguments] action = action[:name] else extra_args = '' end if(cmd = config.get(:actions, action)) debug "Action maps to command: #{cmd.inspect}" [action, cmd, extra_args] else error "No command mapping configured for requested action: #{action}" raise KeyError.new("Invalid command mapping. Key not found: `#{action}`") end end results = cmds.map do |action, command, extra_args| if(extra_args.is_a?(String)) command = "#{command} #{extra_args}" end cmd_input = Shellwords.shellsplit(command) if(extra_args.is_a?(Array)) cmd_input += extra_args end process = ChildProcess.build(*cmd_input) stdout = File.open(File.join(process_output_root, [payload[:id], 'stdout'].join('-')), 'w+') stderr = File.open(File.join(process_output_root, [payload[:id], 'stderr'].join('-')), 'w+') process.io.stdout = stdout process.io.stderr = stderr debug "Running requested action: #{action} (#{command})" process.start debug "Process started. Waiting for process completion (#{action})" status = process.wait stdout.rewind stderr.rewind if(status == 0) info "Successfully executed action: #{action}" info "STDOUT (#{action}): #{stdout.read.gsub("\n", '')}" else error "Failed to successfully execute action: #{action}" error "Failed action exit code (#{action}): #{process.exit_code}" error "STDOUT (#{action}): #{stdout.read.gsub("\n", '')}" error "STDERR (#{action}): #{stderr.read.gsub("\n", '')}" raise "Execution of action `#{action}` failed! (#{})" end [action, {:exit_code => process.exit_code}] end payload.set(:data, :commander, :results, results) job_completed(:commander, payload, ) end end |
#setup(*_) ⇒ Object
Setup the callback
15 16 17 18 19 20 21 22 |
# File 'lib/jackal-commander/executor.rb', line 15 def setup(*_) require 'shellwords' require 'tempfile' require 'childprocess' require 'fileutils' @process_output_root = config.fetch(:process_output_root, DEFAULT_PROCESS_OUTPUT_ROOT) FileUtils.mkdir_p(process_output_root) end |
#valid?(message) ⇒ Truthy, Falsey
Determine validity of message
28 29 30 31 32 33 |
# File 'lib/jackal-commander/executor.rb', line 28 def valid?() super do |payload| payload.get(:data, :commander, :action) || payload.get(:data, :commander, :actions) end end |