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.


24
25
26
# File 'lib/bolt/plugin/task.rb', line 24

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

Instance Attribute Details

#executorObject

Returns the value of attribute executor.


22
23
24
# File 'lib/bolt/plugin/task.rb', line 22

def executor
  @executor
end

#inventoryObject

Returns the value of attribute inventory.


22
23
24
# File 'lib/bolt/plugin/task.rb', line 22

def inventory
  @inventory
end

#palObject

Returns the value of attribute pal.


22
23
24
# File 'lib/bolt/plugin/task.rb', line 22

def pal
  @pal
end

Instance Method Details

#hook_descriptionsObject


10
11
12
13
14
15
16
# File 'lib/bolt/plugin/task.rb', line 10

def hook_descriptions
  {
    puppet_library: 'Run a task to install the Puppet agent package.',
    resolve_reference: 'Run a task as a plugin.',
    validate_resolve_reference: nil
  }
end

#hooksObject


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

def hooks
  hook_descriptions.keys
end

#nameObject


18
19
20
# File 'lib/bolt/plugin/task.rb', line 18

def name
  'task'
end

#puppet_library(opts, target, apply_prep) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bolt/plugin/task.rb', line 57

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 = @context.get_validated_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


47
48
49
50
51
52
53
54
55
# File 'lib/bolt/plugin/task.rb', line 47

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


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bolt/plugin/task.rb', line 28

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


41
42
43
44
# File 'lib/bolt/plugin/task.rb', line 41

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