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(config) ⇒ Task

This creates it’s own PAL so we don’t have to pass a promise around



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

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/bolt/plugin/task.rb', line 20

def config
  @config
end

Instance Method Details

#executorObject



27
28
29
30
# File 'lib/bolt/plugin/task.rb', line 27

def executor
  # Analytics should be handled at a higher level so create a new executor.
  @executor ||= Bolt::Executor.new
end

#hooksObject



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

def hooks
  %w[inventory_targets inventory_config]
end

#inventoryObject



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

def inventory
  @inventory ||= Bolt::Inventory.new({}, config)
end

#inventory_config(opts) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/bolt/plugin/task.rb', line 62

def inventory_config(opts)
  result = run_task(opts)

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

  result['config']
end

#inventory_targets(opts) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bolt/plugin/task.rb', line 72

def inventory_targets(opts)
  raise Bolt::ValidationError, "Task plugin requires that the 'task' is specified" unless opts['task']

  result = run_task(opts)

  targets = result['targets']
  unless targets.is_a?(Array)
    raise Bolt::ValidationError, "Task result did not return a targets array: #{result.value}"
  end

  unless targets.all? { |t| t.is_a?(Hash) }
    msg = "All targets returned by an inventory targets task must be hashes, got: #{targets}"
    raise Bolt::ValidationError, msg
  end

  targets
end

#nameObject



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

def name
  'task'
end

#palObject



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

def pal
  # Hiera config should not be used yet.
  @pal ||= Bolt::PAL.new(config.modulepath, config.hiera_config)
end

#run_task(opts) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
# File 'lib/bolt/plugin/task.rb', line 36

def run_task(opts)
  result = pal.run_task(opts['task'],
                        'localhost',
                        opts['parameters'] || {},
                        executor,
                        inventory).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_inventory_config



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

def validate_options(opts)
  raise Bolt::ValidationError, "Task plugin requires that the 'task' is specified" unless opts['task']

  task = pal.task_signature(opts['task'])

  raise Bolt::ValidationError, "Could not find task #{opts['task']}" unless task

  errors = []
  unless task.runnable_with?(opts['parameters'] || {}) { |msg| errors << msg }
    # This relies on runnable with printing a partial message before the first real error
    raise Bolt::ValidationError, "Invalid parameters for #{errors.join("\n")}"
  end
end