class Pione::RuleEngine::ActionShellScript

ActionShellScript handles action rule’s shell script.

Attributes

location[R]

Public Class Methods

new(env, working_directory, rule_definition, dry_run) click to toggle source
# File lib/pione/rule-engine/action-handler.rb, line 229
def initialize(env, working_directory, rule_definition, dry_run)
  @env = env
  @working_directory = working_directory
  @rule_definition = rule_definition
  @dry_run = dry_run
  @location = @working_directory.location + "__pione__.sh"
end

Public Instance Methods

call(session_id, request_from, client_ui) click to toggle source

Call the shell script.

@param session_id [String]

session id

@param request_from [String]

address of the client that task requested

@param client_ui [String]

UI type of the client

@return [void]

# File lib/pione/rule-engine/action-handler.rb, line 272
def call(session_id, request_from, client_ui)
  callee_env = {
    "PATH" => (@working_directory.location + "bin").path.to_s + ":" + ENV["PATH"],
    "PIONE_SESSION_ID" => session_id,
    "PIONE_REQUEST_FROM" => request_from.to_s,
    "PIONE_CLIENT_UI" => client_ui.to_s
  }
  command = "./#{@location.basename} > #{stdout.basename} 2> #{stderr.basename}"
  options = {:chdir => @working_directory.location.path.to_s}

  # execute command
  system(callee_env, command, options)

  result = $?.success?
  if result
    # delete .stderr and .stdout files if they are empty
    stdout.delete if stdout.size == 0
    stderr.delete if stderr.size == 0
  end
  return result
#end
end
stderr() click to toggle source

Return the location of stderr file.

@return [Locaiton]

location of stderr file
# File lib/pione/rule-engine/action-handler.rb, line 307
def stderr
  @working_directory.location + ".stderr"
end
stdout() click to toggle source

Return the location of stdout file.

@return [Location]

location of stdout file
# File lib/pione/rule-engine/action-handler.rb, line 299
def stdout
  @working_directory.location + ".stdout"
end
write() click to toggle source

Write the rule action into a shell script.

@return [String]

written shell script
# File lib/pione/rule-engine/action-handler.rb, line 241
def write
  content = @rule_definition.action_context.eval(@env).content
  sh = Util::EmbededExprExpander.expand(@env, content)

  # write the action
  if @dry_run
    rule_condition = @rule_definition.rule_condition_context.eval(@env)
    rule_definition.outputs.flatten.each do |output|
      @location.append("touch %s" % output.eval(@env).pieces.first.pattern)
    end
  else
    @location.create(sh)
  end

  # chmod 700
  if @working_directory.location.scheme == "local"
    FileUtils.chmod(0700, @location.path)
  end

  return sh
end