Class: Bolt::Plugin::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/plugin/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, **_opts) ⇒ Task

Returns a new instance of Task.



16
17
18
# File 'lib/bolt/plugin/task.rb', line 16

def initialize(context:, **_opts)
  @context = context
end

Instance Attribute Details

#executorObject

Returns the value of attribute executor.



14
15
16
# File 'lib/bolt/plugin/task.rb', line 14

def executor
  @executor
end

#inventoryObject

Returns the value of attribute inventory.



14
15
16
# File 'lib/bolt/plugin/task.rb', line 14

def inventory
  @inventory
end

#palObject

Returns the value of attribute pal.



14
15
16
# File 'lib/bolt/plugin/task.rb', line 14

def pal
  @pal
end

Instance Method Details

#hooksObject



6
7
8
# File 'lib/bolt/plugin/task.rb', line 6

def hooks
  %i[validate_resolve_reference puppet_library resolve_reference]
end

#nameObject



10
11
12
# File 'lib/bolt/plugin/task.rb', line 10

def name
  'task'
end

#puppet_library(opts, target, apply_prep) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bolt/plugin/task.rb', line 49

def puppet_library(opts, target, apply_prep)
  params = opts['parameters'] || {}
  run_opts = {}
  run_opts[:run_as] = opts['_run_as'] if opts['_run_as']
  begin
    task = apply_prep.get_task(opts['task'], params)
  rescue Bolt::Error => e
    raise Bolt::Plugin::PluginError::ExecutionError.new(e.message, name, 'puppet_library')
  end
  proc do
    apply_prep.run_task([target], task, params, run_opts).first
  end
end

#resolve_reference(opts) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/bolt/plugin/task.rb', line 39

def resolve_reference(opts)
  result = run_task(opts)

  unless result.value.include?('value')
    raise Bolt::ValidationError, "Task result did not return 'value': #{result.value}"
  end

  result['value']
end

#run_task(opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bolt/plugin/task.rb', line 20

def run_task(opts)
  params = opts['parameters'] || {}
  options = { catch_errors: true }

  raise Bolt::ValidationError, "Task plugin requires that the 'task' is specified" unless opts['task']
  task = @context.get_validated_task(opts['task'], params)

  result = @context.run_local_task(task, params, options).first

  raise Bolt::Error.new(result.error_hash['msg'], result.error_hash['kind']) if result.error_hash
  result
end

#validate_options(opts) ⇒ Object Also known as: validate_resolve_reference



33
34
35
36
# File 'lib/bolt/plugin/task.rb', line 33

def validate_options(opts)
  raise Bolt::ValidationError, "Task plugin requires that the 'task' is specified" unless opts['task']
  @context.get_validated_task(opts['task'], opts['parameters'] || {})
end