Class: Taskr::Actions::Shell

Inherits:
Base
  • Object
show all
Defined in:
lib/taskr/actions.rb

Instance Attribute Summary

Attributes inherited from Base

#parameters, #task, #task_action

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_s, #trigger

Constructor Details

This class inherits a constructor from Taskr::Actions::Base

Instance Method Details

#executeObject



135
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
# File 'lib/taskr/actions.rb', line 135

def execute
  if parameters.kind_of? Hash
    user = parameters['as_user']
    cmd = parameters['command']
  else
    user = nil
  end
  
  unless user.blank?
    cmd = "sudo -u #{user} #{cmd}"
  end
  
  outio = StringIO.new
  errio = StringIO.new
  old_stdout, $stdout = $stdout, outio
  old_stderr, $stderr = $stderr, errio
  
  out = `#{cmd}`
  
  err = errio.string
  out = outio.string
  LogEntry.debug(task_action, out) unless out.blank?
  LogEntry.error(task_action, err) unless err.blank?
  
  $stdout = old_stdout
  $stderr = old_stderr
  
  unless $?.success?
    msg = "Shell command #{cmd.inspect} failed (returned code #{$?}): #{out}"
    LogEntry.error(task_action, msg)
    raise msg
  end
end