Class: Bolt::Task

Inherits:
Struct
  • Object
show all
Defined in:
lib/bolt/task.rb,
lib/bolt/task/run.rb,
lib/bolt/task/puppet_server.rb

Overview

Represents a Task.

Direct Known Subclasses

PuppetServer

Defined Under Namespace

Modules: Run Classes: PuppetServer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, remote: false) ⇒ Task

Returns a new instance of Task.



25
26
27
28
29
30
31
# File 'lib/bolt/task.rb', line 25

def initialize(task, remote: false)
  super(nil, nil, [], {})

  @remote = remote

  task.reject { |k, _| k == 'parameters' }.each { |k, v| self[k] = v }
end

Instance Attribute Details

#fileObject

Returns the value of attribute file

Returns:

  • (Object)

    the current value of file



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

def file
  @file
end

#filesObject

Returns the value of attribute files

Returns:

  • (Object)

    the current value of files



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

def files
  @files
end

#metadataObject

Returns the value of attribute metadata

Returns:

  • (Object)

    the current value of metadata



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

def 
  @metadata
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



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

def name
  @name
end

#remoteObject (readonly)

Returns the value of attribute remote.



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

def remote
  @remote
end

Instance Method Details

#descriptionObject



37
38
39
# File 'lib/bolt/task.rb', line 37

def description
  ['description']
end

#file_path(file_name) ⇒ Object

This provides a method we can override in subclasses if the ‘path’ needs to be fetched or computed.



70
71
72
# File 'lib/bolt/task.rb', line 70

def file_path(file_name)
  file_map[file_name]['path']
end

#implementationsObject



74
75
76
# File 'lib/bolt/task.rb', line 74

def implementations
  ['implementations']
end

#module_nameObject



55
56
57
# File 'lib/bolt/task.rb', line 55

def module_name
  name.split('::').first
end

#parameter_defaultsObject



45
46
47
48
49
# File 'lib/bolt/task.rb', line 45

def parameter_defaults
  (parameters || {}).each_with_object({}) do |(name, param_spec), defaults|
    defaults[name] = param_spec['default'] if param_spec.key?('default')
  end
end

#parametersObject



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

def parameters
  ['parameters']
end

#remote_instanceObject



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

def remote_instance
  self.class.new(to_h.each_with_object({}) { |(k, v), h| h[k.to_s] = v }, remote: true)
end

#select_implementation(target, provided_features = []) ⇒ Object

Returns a hash of implementation name, path to executable, input method (if defined), and any additional files (name and path)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bolt/task.rb', line 80

def select_implementation(target, provided_features = [])
  impl = if (impls = implementations)
           available_features = target.feature_set + provided_features
           impl = impls.find do |imp|
             remote_impl = imp['remote']
             remote_impl = ['remote'] if remote_impl.nil?
             Set.new(imp['requirements']).subset?(available_features) && !!remote_impl == @remote
           end
           raise NoImplementationError.new(target, self) unless impl
           impl = impl.dup
           impl['path'] = file_path(impl['name'])
           impl.delete('requirements')
           impl
         else
           raise NoImplementationError.new(target, self) unless !!['remote'] == @remote
           name = files.first['name']
           { 'name' => name, 'path' => file_path(name) }
         end

  inmethod = impl['input_method'] || ['input_method']
  impl['input_method'] = inmethod unless inmethod.nil?

  mfiles = impl.fetch('files', []) + .fetch('files', [])
  dirnames, filenames = mfiles.partition { |file| file.end_with?('/') }
  impl['files'] = filenames.map do |file|
    path = file_path(file)
    raise "No file found for reference #{file}" if path.nil?
    { 'name' => file, 'path' => path }
  end

  unless dirnames.empty?
    files.each do |file|
      name = file['name']
      if dirnames.any? { |dirname| name.start_with?(dirname) }
        impl['files'] << { 'name' => name, 'path' => file_path(name) }
      end
    end
  end

  impl
end

#supports_noopObject



51
52
53
# File 'lib/bolt/task.rb', line 51

def supports_noop
  ['supports_noop']
end

#tasks_dirObject



59
60
61
# File 'lib/bolt/task.rb', line 59

def tasks_dir
  File.join(module_name, 'tasks')
end