Class: MCollective::Util::Playbook::Tasks::ShellTask

Inherits:
Base
  • Object
show all
Defined in:
lib/mcollective/util/playbook/tasks/shell_task.rb

Instance Attribute Summary

Attributes inherited from Base

#description, #fail_ok

Instance Method Summary collapse

Methods inherited from Base

#initialize, #run_task, #startup_hook, #to_s

Methods included from MCollective::Util::Playbook::TemplateUtil

#__template_process_string, #__template_resolve, #t

Constructor Details

This class inherits a constructor from MCollective::Util::Playbook::Tasks::Base

Instance Method Details

#from_hash(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mcollective/util/playbook/tasks/shell_task.rb', line 11

def from_hash(data)
  @cwd = data["cwd"] || Dir.pwd
  @timeout = data["timeout"]
  @nodes = data["nodes"]
  @environment = data["environment"]

  if @nodes.is_a?(Array)
    @command = "%s --nodes %s" % [data["command"], @nodes.join(",")]
  else
    @command = data["command"]
  end

  self
end

#runObject



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
# File 'lib/mcollective/util/playbook/tasks/shell_task.rb', line 55

def run
  if @nodes
    Log.info("Starting command %s against %d nodes" % [@command, @nodes.size])
  else
    Log.info("Starting command %s" % [@command])
  end

  begin
    options = shell_options

    shell = Shell.new(@command, options)
    shell.runcommand

    options["stdout"].each do |output|
      output.lines.each do |line|
        Log.info(line.chomp)
      end
    end

    if shell.status.exitstatus == 0
      Log.info("Successfully ran command %s" % [@command])
      [true, "Command completed successfully", options["stdout"]]
    else
      Log.warn("Failed to run command %s with exit code %s" % [@command, shell.status.exitstatus])
      [false, "Command failed with code %d" % [shell.status.exitstatus], options["stdout"]]
    end
  rescue
    msg = "Could not run command %s: %s: %s" % [@command, $!.class, $!.to_s]
    Log.debug(msg)
    Log.debug($!.backtrace.join("\t\n"))

    [false, msg, []]
  end
end

#shell_optionsObject



26
27
28
29
30
31
32
33
34
# File 'lib/mcollective/util/playbook/tasks/shell_task.rb', line 26

def shell_options
  options = {}
  options["cwd"] = @cwd if @cwd
  options["timeout"] = Integer(@timeout) if @timeout
  options["environment"] = @environment if @environment
  options["stdout"] = []
  options["stderr"] = options["stdout"]
  options
end

#to_execution_result(results) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mcollective/util/playbook/tasks/shell_task.rb', line 36

def to_execution_result(results)
  result = {
    "value" => results[2].join("\r\n").chomp,
    "type" => "shell",
    "fail_ok" => @fail_ok,
    "error" => {
      "msg" => results[1],
      "kind" => "choria.playbook/taskerror",
      "details" => {
        "command" => @command
      }
    }
  }

  result.delete("error") if results[0]

  {"localhost" => result}
end

#validate_configuration!Object



6
7
8
9
# File 'lib/mcollective/util/playbook/tasks/shell_task.rb', line 6

def validate_configuration!
  raise("A command was not given") unless @command
  raise("Nodes were given but is not an array") if @nodes && !@nodes.is_a?(Array)
end