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

Inherits:
Object
  • Object
show all
Includes:
TemplateUtil
Defined in:
lib/mcollective/util/playbook/tasks.rb,
lib/mcollective/util/playbook/tasks/base.rb,
lib/mcollective/util/playbook/tasks/shell_task.rb,
lib/mcollective/util/playbook/tasks/slack_task.rb,
lib/mcollective/util/playbook/tasks/webhook_task.rb,
lib/mcollective/util/playbook/tasks/mcollective_task.rb,
lib/mcollective/util/playbook/tasks/graphite_event_task.rb

Defined Under Namespace

Classes: Base, Graphite_eventTask, McollectiveTask, ShellTask, SlackTask, WebhookTask

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateUtil

#__template_process_string, #__template_resolve, #t

Constructor Details

#initialize(playbook) ⇒ Tasks

Returns a new instance of Tasks.



17
18
19
20
21
# File 'lib/mcollective/util/playbook/tasks.rb', line 17

def initialize(playbook)
  @playbook = playbook

  reset
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



15
16
17
# File 'lib/mcollective/util/playbook/tasks.rb', line 15

def results
  @results
end

#tasksObject (readonly)

Returns the value of attribute tasks.



15
16
17
# File 'lib/mcollective/util/playbook/tasks.rb', line 15

def tasks
  @tasks
end

Instance Method Details

#from_hash(data) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mcollective/util/playbook/tasks.rb', line 181

def from_hash(data)
  case data
  when Array
    load_tasks(data, "tasks")
  when Hash
    data.each do |set, tasks|
      load_tasks(tasks, set)
    end
  end

  self
end

#load_tasks(data, set) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mcollective/util/playbook/tasks.rb', line 151

def load_tasks(data, set)
  data.each_with_index do |task, idx|
    task.each do |type, props|
      Log.debug("Loading task %d of type %s into %s" % [idx, type, set])

      runner = runner_for(type)
      runner.description = props.fetch("description", "%s task" % [type])

      task_data = {
        :description => runner.description,
        :type => type,
        :runner => runner,
        :properties => {
          "tries" => 1,
          "try_sleep" => 10,
          "fail_ok" => false
        }.merge(props)
      }

      runner.fail_ok = task_data[:properties]["fail_ok"]

      task_data[:result] = TaskResult.new(task_data)

      @tasks[set] << task_data
    end
  end

  @tasks[set]
end

#prepareObject



36
# File 'lib/mcollective/util/playbook/tasks.rb', line 36

def prepare; end

#resetObject



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

def reset
  @results = []
  @tasks = {
    "tasks" => [],
    "pre_task" => [],
    "post_task" => [],
    "on_fail" => [],
    "on_success" => [],
    "pre_book" => [],
    "post_book" => []
  }
end

#runObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mcollective/util/playbook/tasks.rb', line 125

def run
  @playbook.in_context("running") do
    unless run_set("pre_book")
      Log.warn("Playbook pre_book hook failed to run, failing entire playbook")
      return false
    end

    success = run_set("tasks")

    Log.info("Finished running main tasks in playbook: success: %s" % success)

    if success
      success = run_set("on_success")
    else
      run_set("on_fail")
    end

    unless run_set("post_book")
      Log.warn("Playbook post_book hook failed to run, failing entire playbookbook")
      return false
    end

    success
  end
end

#run_set(set) ⇒ Boolean

Runs a specific task set

Parameters:

  • set (String)

    one of the known task sets

Returns:

  • (Boolean)

    true if all tasks and all their hooks passed



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mcollective/util/playbook/tasks.rb', line 103

def run_set(set)
  set_tasks = @tasks[set]

  return true if set_tasks.empty?

  @playbook.in_context(set) do
    Log.info("About to run task set %s with %d task(s)" % [set, set_tasks.size])

    # would typically use map here but you cant break out of a map and keep the thus far built up array
    # so it's either this or a inject loop
    passed = set_tasks.take_while do |task|
      @playbook.in_context("%s.%s" % [set, task[:type]]) { run_task(task, set, set == "tasks") }
    end

    set_success = passed.size == set_tasks.size

    Log.info("Done running task set %s with %d task(s): success: %s" % [set, set_tasks.size, set_success])

    set_success
  end
end

#run_task(task, set, hooks = true) ⇒ Boolean

Runs a specific task

Parameters:

  • task (Hash)

    a task entry

  • set (String)

    the set the task is running in

  • hooks (Boolean) (defaults to: true)

    indicates if hooks should be run

Returns:

  • (Boolean)

    indicating task success



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
94
95
96
97
# File 'lib/mcollective/util/playbook/tasks.rb', line 53

def run_task(task, set, hooks=true)
  properties = task[:properties]
  result = task[:result]
  task_runner = task[:runner]

  Log.info("About to run task: %s" % t(task[:runner].description))
  result.description = t(task[:runner].description)

  if hooks && !run_set("pre_task")
    Log.warn("Failing task because a critical pre_task hook failed")
    return false
  end

  if properties["pre_sleep"]
    Log.info("Sleeping %d seconds before check" % [properties["pre_sleep"]])
    sleep(Integer(properties["pre_sleep"]))
  end

  (1..properties["tries"]).each do |try|
    task_runner.from_hash(t(properties))
    task_runner.validate_configuration!

    @results << result.timed_run(set)

    if result.success?
      Log.info(result.msg)
    else
      Log.error(result.msg)
    end

    if try != properties["tries"] && !result.success
      Log.warn("Task failed on try %d/%d, sleeping %ds: %s" % [try, properties["tries"], properties["try_sleep"], result.msg])
      sleep(Integer(properties["try_sleep"]))
    end

    break if result.success
  end

  if hooks && !run_set("post_task")
    Log.warn("Failing task because a critical post_task hook failed")
    return false
  end

  result.success
end

#runner_for(type) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/mcollective/util/playbook/tasks.rb', line 39

def runner_for(type)
  klass_name = "%sTask" % type.capitalize

  Tasks.const_get(klass_name).new(@playbook)
rescue NameError
  raise("Cannot find a handler for Task type %s" % type)
end